Search in sources :

Example 66 with UncheckedIOException

use of java.io.UncheckedIOException in project herddb by diennea.

the class BLink method delete.

// function delete(v: value): boolean;
// var
// n: nodeptr;
// descent: stack;
// begin
// n := locate-leaf(v, writelock, descent); {v € coverset(n) , n write-locked}
// delete := remove-key(v, n); {decisive}
// nornialize(n, descent, 1); unlock(n, writelock)
// end;
public V delete(K v) {
    Node<K, V> n;
    Deque<ResultCouple<K, V>> descent = new LinkedList<>();
    Queue<CriticJob> maintenance = new LinkedList<>();
    try {
        // v € coverset(n), n write-locked
        n = locate_leaf(v, WRITE_LOCK, descent);
    } catch (IOException ex) {
        throw new UncheckedIOException("failed to delete " + v, ex);
    }
    V delete;
    try {
        // decisive
        delete = n.remove_key(v);
        normalize(n, descent, 1, maintenance);
    } catch (IOException ex) {
        throw new UncheckedIOException("failed to delete " + v, ex);
    } finally {
        unlock(n, WRITE_LOCK);
    }
    if (delete != null) {
        size.decrement();
    }
    handleMainenance(maintenance);
    return delete;
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LinkedList(java.util.LinkedList)

Example 67 with UncheckedIOException

use of java.io.UncheckedIOException in project herddb by diennea.

the class BLink method scan.

public Stream<Entry<K, V>> scan(K from, K to, boolean toInclusive) {
    Node<K, V> n;
    Deque<ResultCouple<K, V>> descent = new LinkedList<>();
    if (from == null) {
        lock_anchor(READ_LOCK);
        n = anchor.first;
        unlock_anchor(READ_LOCK);
        /*
             * We have to lock the first node, scan iterator require a read locked node (as produced from
             * locate_leaf too)
             */
        lock(n, READ_LOCK);
    } else {
        try {
            // v € coverset(n), n read-locked
            n = locate_leaf(from, READ_LOCK, descent);
        } catch (IOException ex) {
            throw new UncheckedIOException("failed to scan from " + from + " to " + to, ex);
        }
    }
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new ScanIterator(n, from, from != null, to, toInclusive), /* No characteristics */
    0), /* No parallel */
    false);
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LinkedList(java.util.LinkedList)

Example 68 with UncheckedIOException

use of java.io.UncheckedIOException in project herddb by diennea.

the class BLink method scan.

/**
 * Supports both from and to empty.
 *
 * @param from inclusive (if not empty)
 * @param to   exclusive
 * @return
 */
public Stream<Entry<K, V>> scan(K from, K to) {
    Node<K, V> n;
    Deque<ResultCouple<K, V>> descent = new LinkedList<>();
    if (from == null) {
        lock_anchor(READ_LOCK);
        n = anchor.first;
        unlock_anchor(READ_LOCK);
        /*
             * We have to lock the first node, scan iterator require a read locked node (as produced from
             * locate_leaf too)
             */
        lock(n, READ_LOCK);
    } else {
        try {
            // v € coverset(n), n read-locked
            n = locate_leaf(from, READ_LOCK, descent);
        } catch (IOException ex) {
            throw new UncheckedIOException("failed to scan from " + from + " to " + to, ex);
        }
    }
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new ScanIterator(n, from, from != null, to, false), /* No characteristics */
    0), /* No parallel */
    false);
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LinkedList(java.util.LinkedList)

Example 69 with UncheckedIOException

use of java.io.UncheckedIOException in project Bytecoder by mirkosertic.

the class ICUBinary method getRequiredData.

// public methods --------------------------------------------------------
/**
 * Loads an ICU binary data file and returns it as a ByteBuffer.
 * The buffer contents is normally read-only, but its position etc. can be modified.
 *
 * @param itemPath Relative ICU data item path, for example "root.res" or "coll/ucadata.icu".
 * @return The data as a read-only ByteBuffer.
 */
public static ByteBuffer getRequiredData(String itemPath) {
    final Class<ICUBinary> root = ICUBinary.class;
    try (InputStream is = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {

        public InputStream run() {
            return root.getResourceAsStream(itemPath);
        }
    })) {
        BufferedInputStream b = new BufferedInputStream(is, 4096);
        DataInputStream inputStream = new DataInputStream(b);
        byte[] bb = new byte[120000];
        int n = inputStream.read(bb);
        ByteBuffer bytes = ByteBuffer.wrap(bb, 0, n);
        return bytes;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 70 with UncheckedIOException

use of java.io.UncheckedIOException in project jackrabbit-oak by apache.

the class SegmentWriteQueueTest method testRecoveryMode.

@Test
public void testRecoveryMode() throws IOException, InterruptedException {
    Set<UUID> added = Collections.synchronizedSet(new HashSet<>());
    Semaphore semaphore = new Semaphore(0);
    AtomicBoolean doBreak = new AtomicBoolean(true);
    List<Long> writeAttempts = Collections.synchronizedList(new ArrayList<>());
    queue = new SegmentWriteQueue((tarEntry, data, offset, size) -> {
        writeAttempts.add(System.currentTimeMillis());
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
        }
        if (doBreak.get()) {
            throw new IOException();
        }
        added.add(new UUID(tarEntry.getMsb(), tarEntry.getLsb()));
    });
    for (int i = 0; i < 10; i++) {
        queue.addToQueue(tarEntry(i), EMPTY_DATA, 0, 0);
    }
    semaphore.release(Integer.MAX_VALUE);
    Thread.sleep(100);
    assertTrue(queue.isBroken());
    // the 10th segment is handled by the recovery thread
    assertEquals(9, queue.getSize());
    writeAttempts.clear();
    while (writeAttempts.size() < 5) {
        Thread.sleep(100);
    }
    long lastAttempt = writeAttempts.get(0);
    for (int i = 1; i < 5; i++) {
        long delay = writeAttempts.get(i) - lastAttempt;
        assertTrue("The delay between attempts to persist segment should be larger than 1s. Actual: " + delay, delay >= 1000);
        lastAttempt = writeAttempts.get(i);
    }
    AtomicBoolean addFinished = new AtomicBoolean(false);
    new Thread(() -> {
        try {
            queue.addToQueue(tarEntry(10), EMPTY_DATA, 0, 0);
            addFinished.set(true);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }).start();
    Thread.sleep(100);
    assertFalse("Adding segments should be blocked until the recovery mode is finished", addFinished.get());
    doBreak.set(false);
    while (queue.isBroken()) {
        Thread.sleep(10);
    }
    assertFalse("Queue shouldn't be broken anymore", queue.isBroken());
    while (added.size() < 11) {
        Thread.sleep(10);
    }
    assertEquals("All segments should be consumed", 11, added.size());
    for (int i = 0; i < 11; i++) {
        assertTrue("All segments should be consumed", added.contains(uuid(i)));
    }
    int i = writeAttempts.size() - 10;
    lastAttempt = writeAttempts.get(i);
    for (; i < writeAttempts.size(); i++) {
        long delay = writeAttempts.get(i) - lastAttempt;
        assertTrue("Segments should be persisted immediately", delay < 1000);
        lastAttempt = writeAttempts.get(i);
    }
}
Also used : Semaphore(java.util.concurrent.Semaphore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) AzureSegmentArchiveEntry(org.apache.jackrabbit.oak.segment.azure.AzureSegmentArchiveEntry) UUID(java.util.UUID) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) After(org.junit.After) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) UncheckedIOException(java.io.UncheckedIOException) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)826 IOException (java.io.IOException)786 File (java.io.File)109 Path (java.nio.file.Path)106 ArrayList (java.util.ArrayList)70 InputStream (java.io.InputStream)58 Map (java.util.Map)58 List (java.util.List)55 HashMap (java.util.HashMap)44 Test (org.junit.Test)38 Files (java.nio.file.Files)37 Collectors (java.util.stream.Collectors)37 Stream (java.util.stream.Stream)31 URL (java.net.URL)29 StringWriter (java.io.StringWriter)27 CursorContext (org.neo4j.io.pagecache.context.CursorContext)25 FileInputStream (java.io.FileInputStream)23 HashSet (java.util.HashSet)23 PageCursor (org.neo4j.io.pagecache.PageCursor)22 Optional (java.util.Optional)21