Search in sources :

Example 46 with DB

use of org.iq80.leveldb.DB 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 47 with DB

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

the class LevelDBTableService method dropTable.

public void dropTable(String name) throws IOException {
    DB db = tables.remove(name);
    if (db != null) {
        db.close();
    }
    String dbPath = getDBPath(basePath, name);
    factory.destroy(new File(dbPath), new Options());
}
Also used : WriteOptions(org.iq80.leveldb.WriteOptions) Options(org.iq80.leveldb.Options) File(java.io.File) DB(org.iq80.leveldb.DB)

Example 48 with DB

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

the class LevelDBTableService method openTable.

private DB openTable(String tableName) throws IOException {
    String dbPath = getDBPath(basePath, tableName);
    Options options = new Options();
    options.createIfMissing(false);
    options.errorIfExists(false);
    options.comparator(new KeyValueDBComparator());
    options.blockSize(blockSize);
    options.cacheSize(cacheSize);
    // unfortunately, with the java version of leveldb, with createIfMissing set to false, factory.open will
    // see that there is no table and throw an exception, but it wont clean up after itself and will leave a
    // directory there with a lock.  So we want to avoid calling open if the path doesn't already exist and
    // throw the exception ourselves.
    File dbDir = new File(dbPath);
    if (!dbDir.exists()) {
        throw new IOException("Database " + dbPath + " does not exist and the create if missing option is disabled");
    }
    DB db = factory.open(dbDir, options);
    tables.put(tableName, db);
    return db;
}
Also used : WriteOptions(org.iq80.leveldb.WriteOptions) Options(org.iq80.leveldb.Options) IOException(java.io.IOException) File(java.io.File) DB(org.iq80.leveldb.DB)

Example 49 with DB

use of org.iq80.leveldb.DB 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 50 with DB

use of org.iq80.leveldb.DB 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)

Aggregations

IOException (java.io.IOException)25 DB (org.iq80.leveldb.DB)25 DBException (org.iq80.leveldb.DBException)20 LeveldbIterator (org.apache.hadoop.yarn.server.utils.LeveldbIterator)16 Options (org.iq80.leveldb.Options)16 File (java.io.File)15 JniDBFactory.asString (org.fusesource.leveldbjni.JniDBFactory.asString)14 WriteBatch (org.iq80.leveldb.WriteBatch)9 DBIterator (org.iq80.leveldb.DBIterator)7 Map (java.util.Map)5 Path (org.apache.hadoop.fs.Path)5 Test (org.junit.Test)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 NavigableMap (java.util.NavigableMap)4 NativeDB (org.fusesource.leveldbjni.internal.NativeDB)4 WriteOptions (org.iq80.leveldb.WriteOptions)4 DB (com.codecademy.eventhub.base.DB)3 Provides (com.google.inject.Provides)3 ArrayList (java.util.ArrayList)3