Search in sources :

Example 26 with WriteBatch

use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.

the class LevelDBTableCore method undo.

public void undo(Map<byte[], ? extends Map<byte[], ?>> persisted, long version) throws IOException {
    if (persisted.isEmpty()) {
        return;
    }
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    for (Map.Entry<byte[], ? extends Map<byte[], ?>> row : persisted.entrySet()) {
        for (Map.Entry<byte[], ?> column : row.getValue().entrySet()) {
            byte[] key = createPutKey(row.getKey(), column.getKey(), version);
            batch.delete(key);
        }
    }
    db.write(batch, service.getWriteOptions());
}
Also used : WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap) DB(org.iq80.leveldb.DB)

Example 27 with WriteBatch

use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.

the class LevelDBTableCore method deleteRows.

/**
   * Delete a list of rows from the table entirely, disregarding transactions.
   * @param toDelete the row keys to delete
   */
public void deleteRows(Collection<byte[]> toDelete) throws IOException {
    if (toDelete.isEmpty()) {
        return;
    }
    // find first row to delete and first entry in the DB to examine
    Iterator<byte[]> rows = toDelete.iterator();
    byte[] currentRow = rows.next();
    byte[] startKey = createStartKey(currentRow);
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    try (DBIterator iterator = db.iterator()) {
        iterator.seek(startKey);
        if (!iterator.hasNext()) {
            // nothing in the db to delete
            return;
        }
        Map.Entry<byte[], byte[]> entry = iterator.next();
        // iterate over the database and the rows to delete, collecting (raw) keys to delete
        while (entry != null && currentRow != null) {
            KeyValue kv = KeyValue.fromKey(entry.getKey());
            int comp = Bytes.compareTo(kv.getRow(), currentRow);
            if (comp == 0) {
                // same row -> delete
                batch.delete(entry.getKey());
                entry = iterator.hasNext() ? iterator.next() : null;
            } else if (comp > 0) {
                // read past current row -> move to next row
                currentRow = rows.hasNext() ? rows.next() : null;
            } else if (comp < 0) {
                // iterator must seek to current row
                iterator.seek(createStartKey(currentRow));
                entry = iterator.hasNext() ? iterator.next() : null;
            }
        }
    }
    // delete all the entries that were found
    db.write(batch, getWriteOptions());
}
Also used : DBIterator(org.iq80.leveldb.DBIterator) WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap) DB(org.iq80.leveldb.DB)

Example 28 with WriteBatch

use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.

the class LevelDBTableCore method persist.

public void persist(Map<byte[], ? extends Map<byte[], byte[]>> changes, long version) throws IOException {
    DB db = getDB();
    // todo support writing null when no transaction
    WriteBatch batch = db.createWriteBatch();
    for (Map.Entry<byte[], ? extends Map<byte[], byte[]>> row : changes.entrySet()) {
        for (Map.Entry<byte[], byte[]> column : row.getValue().entrySet()) {
            byte[] key = createPutKey(row.getKey(), column.getKey(), version);
            batch.put(key, column.getValue() == null ? DELETE_MARKER : column.getValue());
        }
    }
    db.write(batch, service.getWriteOptions());
}
Also used : WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap) DB(org.iq80.leveldb.DB)

Example 29 with WriteBatch

use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.

the class LevelDBMessageTable method pruneMessages.

/**
   * Delete messages of a {@link TopicId} that has exceeded the TTL or if it belongs to an older generation
   *
   * @param topicMetadata {@link TopicMetadata}
   * @param currentTime current timestamp
   * @throws IOException error occurred while trying to delete a row in LevelDB
   */
public void pruneMessages(TopicMetadata topicMetadata, long currentTime) throws IOException {
    WriteBatch writeBatch = levelDB.createWriteBatch();
    long ttlInMs = TimeUnit.SECONDS.toMillis(topicMetadata.getTTL());
    byte[] startRow = MessagingUtils.toDataKeyPrefix(topicMetadata.getTopicId(), Integer.parseInt(MessagingUtils.Constants.DEFAULT_GENERATION));
    byte[] stopRow = Bytes.stopKeyForPrefix(startRow);
    try (CloseableIterator<Map.Entry<byte[], byte[]>> rowIterator = new DBScanIterator(levelDB, startRow, stopRow)) {
        while (rowIterator.hasNext()) {
            Map.Entry<byte[], byte[]> entry = rowIterator.next();
            MessageTable.Entry messageTableEntry = new ImmutableMessageTableEntry(entry.getKey(), null, null);
            int dataGeneration = messageTableEntry.getGeneration();
            int currGeneration = topicMetadata.getGeneration();
            if (MessagingUtils.isOlderGeneration(dataGeneration, currGeneration)) {
                writeBatch.delete(entry.getKey());
                continue;
            }
            if ((dataGeneration == Math.abs(currGeneration)) && ((currentTime - messageTableEntry.getPublishTimestamp()) > ttlInMs)) {
                writeBatch.delete(entry.getKey());
            } else {
                // since the entries are sorted by time.
                break;
            }
        }
    }
    try {
        levelDB.write(writeBatch, WRITE_OPTIONS);
    } catch (DBException ex) {
        throw new IOException(ex);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) IOException(java.io.IOException) ImmutableMessageTableEntry(co.cask.cdap.messaging.store.ImmutableMessageTableEntry) RawMessageTableEntry(co.cask.cdap.messaging.store.RawMessageTableEntry) AbstractMessageTable(co.cask.cdap.messaging.store.AbstractMessageTable) MessageTable(co.cask.cdap.messaging.store.MessageTable) WriteBatch(org.iq80.leveldb.WriteBatch) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMessageTableEntry(co.cask.cdap.messaging.store.ImmutableMessageTableEntry)

Example 30 with WriteBatch

use of org.iq80.leveldb.WriteBatch in project cdap by caskdata.

the class LevelDBPayloadTable method pruneMessages.

/**
   * Delete messages of a {@link TopicId} that has exceeded the TTL or if it belongs to an older generation
   *
   * @param topicMetadata {@link TopicMetadata}
   * @param currentTime current timestamp
   * @throws IOException error occurred while trying to delete a row in LevelDB
   */
public void pruneMessages(TopicMetadata topicMetadata, long currentTime) throws IOException {
    WriteBatch writeBatch = levelDB.createWriteBatch();
    long ttlInMs = TimeUnit.SECONDS.toMillis(topicMetadata.getTTL());
    byte[] startRow = MessagingUtils.toDataKeyPrefix(topicMetadata.getTopicId(), Integer.parseInt(MessagingUtils.Constants.DEFAULT_GENERATION));
    byte[] stopRow = Bytes.stopKeyForPrefix(startRow);
    try (CloseableIterator<Map.Entry<byte[], byte[]>> rowIterator = new DBScanIterator(levelDB, startRow, stopRow)) {
        while (rowIterator.hasNext()) {
            Map.Entry<byte[], byte[]> entry = rowIterator.next();
            PayloadTable.Entry payloadTableEntry = new ImmutablePayloadTableEntry(entry.getKey(), entry.getValue());
            int dataGeneration = payloadTableEntry.getGeneration();
            int currGeneration = topicMetadata.getGeneration();
            if (MessagingUtils.isOlderGeneration(dataGeneration, currGeneration)) {
                writeBatch.delete(entry.getKey());
                continue;
            }
            if ((dataGeneration == Math.abs(currGeneration)) && ((currentTime - payloadTableEntry.getPayloadWriteTimestamp()) > ttlInMs)) {
                writeBatch.delete(entry.getKey());
            } else {
                // since the entries are sorted by time.
                break;
            }
        }
    }
    try {
        levelDB.write(writeBatch, WRITE_OPTIONS);
    } catch (DBException ex) {
        throw new IOException(ex);
    }
}
Also used : DBException(org.iq80.leveldb.DBException) ImmutablePayloadTableEntry(co.cask.cdap.messaging.store.ImmutablePayloadTableEntry) IOException(java.io.IOException) PayloadTable(co.cask.cdap.messaging.store.PayloadTable) AbstractPayloadTable(co.cask.cdap.messaging.store.AbstractPayloadTable) RawPayloadTableEntry(co.cask.cdap.messaging.store.RawPayloadTableEntry) ImmutablePayloadTableEntry(co.cask.cdap.messaging.store.ImmutablePayloadTableEntry) WriteBatch(org.iq80.leveldb.WriteBatch) Map(java.util.Map)

Aggregations

WriteBatch (org.iq80.leveldb.WriteBatch)30 IOException (java.io.IOException)21 DBException (org.iq80.leveldb.DBException)17 JniDBFactory.asString (org.fusesource.leveldbjni.JniDBFactory.asString)12 Map (java.util.Map)9 DB (org.iq80.leveldb.DB)8 DBIterator (org.iq80.leveldb.DBIterator)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 NavigableMap (java.util.NavigableMap)4 RawMessageTableEntry (co.cask.cdap.messaging.store.RawMessageTableEntry)3 RollingWriteBatch (org.apache.hadoop.yarn.server.timeline.RollingLevelDB.RollingWriteBatch)3 ImmutableMessageTableEntry (co.cask.cdap.messaging.store.ImmutableMessageTableEntry)2 RawPayloadTableEntry (co.cask.cdap.messaging.store.RawPayloadTableEntry)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 HashMap (java.util.HashMap)2 Row (co.cask.cdap.api.dataset.table.Row)1 Scanner (co.cask.cdap.api.dataset.table.Scanner)1 AbstractMessageTable (co.cask.cdap.messaging.store.AbstractMessageTable)1