Search in sources :

Example 1 with Batch

use of org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch in project bookkeeper by apache.

the class EntryLocationIndex method updateLocations.

public void updateLocations(Iterable<EntryLocation> newLocations) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("Update locations -- {}", Iterables.size(newLocations));
    }
    Batch batch = newBatch();
    // Update all the ledger index pages with the new locations
    for (EntryLocation e : newLocations) {
        if (log.isDebugEnabled()) {
            log.debug("Update location - ledger: {} -- entry: {}", e.ledger, e.entry);
        }
        addLocation(batch, e.ledger, e.entry, e.location);
    }
    batch.flush();
    batch.close();
}
Also used : Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch) EntryLocation(org.apache.bookkeeper.bookie.EntryLocation)

Example 2 with Batch

use of org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch in project bookkeeper by apache.

the class EntryLocationIndex method removeOffsetFromDeletedLedgers.

public void removeOffsetFromDeletedLedgers() throws IOException {
    LongPairWrapper firstKeyWrapper = LongPairWrapper.get(-1, -1);
    LongPairWrapper lastKeyWrapper = LongPairWrapper.get(-1, -1);
    LongPairWrapper keyToDelete = LongPairWrapper.get(-1, -1);
    Set<Long> ledgersToDelete = deletedLedgers.items();
    if (ledgersToDelete.isEmpty()) {
        return;
    }
    log.info("Deleting indexes for ledgers: {}", ledgersToDelete);
    Batch batch = locationsDb.newBatch();
    try {
        for (long ledgerId : ledgersToDelete) {
            if (log.isDebugEnabled()) {
                log.debug("Deleting indexes from ledger {}", ledgerId);
            }
            firstKeyWrapper.set(ledgerId, 0);
            lastKeyWrapper.set(ledgerId, Long.MAX_VALUE);
            batch.deleteRange(firstKeyWrapper.array, lastKeyWrapper.array);
        }
        batch.flush();
        // Removed from pending set
        for (long ledgerId : ledgersToDelete) {
            deletedLedgers.remove(ledgerId);
        }
    } finally {
        firstKeyWrapper.recycle();
        lastKeyWrapper.recycle();
        keyToDelete.recycle();
        batch.close();
    }
}
Also used : Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch)

Example 3 with Batch

use of org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch in project bookkeeper by apache.

the class EntryLocationIndex method addLocation.

public void addLocation(long ledgerId, long entryId, long location) throws IOException {
    Batch batch = locationsDb.newBatch();
    addLocation(batch, ledgerId, entryId, location);
    batch.flush();
    batch.close();
}
Also used : Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch)

Example 4 with Batch

use of org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch in project bookkeeper by apache.

the class KeyValueStorageTest method simple.

@Test
public void simple() throws Exception {
    File tmpDir = File.createTempFile("bookie", "test");
    tmpDir.delete();
    KeyValueStorage db = storageFactory.newKeyValueStorage(tmpDir.getAbsolutePath(), DbConfigType.Small, configuration);
    assertEquals(null, db.getFloor(toArray(3)));
    assertEquals(0, db.count());
    db.put(toArray(5), toArray(5));
    assertEquals(null, db.getFloor(toArray(3)));
    assertEquals(1, db.count());
    assertEquals(null, db.getFloor(toArray(5)));
    assertEquals(5, fromArray(db.getFloor(toArray(6)).getKey()));
    db.put(toArray(3), toArray(3));
    assertEquals(null, db.getFloor(toArray(3)));
    assertEquals(2, db.count());
    // //
    db.put(toArray(5), toArray(5));
    // Count can be imprecise
    assertTrue(db.count() > 0);
    assertEquals(null, db.getFloor(toArray(1)));
    assertEquals(null, db.getFloor(toArray(3)));
    assertEquals(3, fromArray(db.getFloor(toArray(5)).getKey()));
    assertEquals(5, fromArray(db.getFloor(toArray(6)).getKey()));
    assertEquals(5, fromArray(db.getFloor(toArray(10)).getKey()));
    // Iterate
    List<Long> foundKeys = Lists.newArrayList();
    try (CloseableIterator<Entry<byte[], byte[]>> iter = db.iterator()) {
        while (iter.hasNext()) {
            foundKeys.add(fromArray(iter.next().getKey()));
        }
    }
    assertEquals(Lists.newArrayList(3L, 5L), foundKeys);
    // Iterate over keys
    foundKeys = Lists.newArrayList();
    CloseableIterator<byte[]> iter2 = db.keys();
    try {
        while (iter2.hasNext()) {
            foundKeys.add(fromArray(iter2.next()));
        }
    } finally {
        iter2.close();
    }
    assertEquals(Lists.newArrayList(3L, 5L), foundKeys);
    // Scan with limits
    foundKeys = Lists.newArrayList();
    iter2 = db.keys(toArray(1), toArray(4));
    try {
        while (iter2.hasNext()) {
            foundKeys.add(fromArray(iter2.next()));
        }
    } finally {
        iter2.close();
    }
    assertEquals(Lists.newArrayList(3L), foundKeys);
    // Test deletion
    db.put(toArray(10), toArray(10));
    db.put(toArray(11), toArray(11));
    db.put(toArray(12), toArray(12));
    db.put(toArray(14), toArray(14));
    // Count can be imprecise
    assertTrue(db.count() > 0);
    assertEquals(10L, fromArray(db.get(toArray(10))));
    db.delete(toArray(10));
    assertEquals(null, db.get(toArray(10)));
    assertTrue(db.count() > 0);
    Batch batch = db.newBatch();
    batch.remove(toArray(11));
    batch.remove(toArray(12));
    batch.remove(toArray(13));
    batch.flush();
    assertEquals(null, db.get(toArray(11)));
    assertEquals(null, db.get(toArray(12)));
    assertEquals(null, db.get(toArray(13)));
    assertEquals(14L, fromArray(db.get(toArray(14))));
    batch.close();
    db.close();
    tmpDir.delete();
}
Also used : Entry(java.util.Map.Entry) Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch) File(java.io.File) Test(org.junit.Test)

Example 5 with Batch

use of org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch in project bookkeeper by apache.

the class DbLedgerStorage method addLedgerToIndex.

/**
 * Add an already existing ledger to the index.
 *
 * <p>This method is only used as a tool to help the migration from InterleaveLedgerStorage to DbLedgerStorage
 *
 * @param ledgerId
 *            the ledger id
 * @param entries
 *            a map of entryId -> location
 * @return the number of
 */
public long addLedgerToIndex(long ledgerId, boolean isFenced, byte[] masterKey, Iterable<SortedMap<Long, Long>> entries) throws Exception {
    LedgerData ledgerData = LedgerData.newBuilder().setExists(true).setFenced(isFenced).setMasterKey(ByteString.copyFrom(masterKey)).build();
    ledgerIndex.set(ledgerId, ledgerData);
    AtomicLong numberOfEntries = new AtomicLong();
    // Iterate over all the entries pages
    Batch batch = entryLocationIndex.newBatch();
    entries.forEach(map -> {
        map.forEach((entryId, location) -> {
            try {
                entryLocationIndex.addLocation(batch, ledgerId, entryId, location);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            numberOfEntries.incrementAndGet();
        });
    });
    batch.flush();
    batch.close();
    return numberOfEntries.get();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Batch(org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch) LedgerData(org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData) IOException(java.io.IOException)

Aggregations

Batch (org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorage.Batch)6 IOException (java.io.IOException)2 File (java.io.File)1 Entry (java.util.Map.Entry)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)1 EntryLocation (org.apache.bookkeeper.bookie.EntryLocation)1 LedgerData (org.apache.bookkeeper.bookie.storage.ldb.DbLedgerStorageDataFormats.LedgerData)1 Test (org.junit.Test)1