Search in sources :

Example 21 with DB

use of org.iq80.leveldb.DB in project qi4j-sdk by Qi4j.

the class LevelDBEntityStoreMixin method activateService.

@Override
public void activateService() throws Exception {
    charset = Charset.forName("UTF-8");
    configuration.refresh();
    LevelDBEntityStoreConfiguration config = configuration.get();
    // Choose flavour
    String flavour = config.flavour().get();
    DBFactory factory;
    if ("jni".equalsIgnoreCase(flavour)) {
        factory = newJniDBFactory();
    } else if ("java".equalsIgnoreCase(flavour)) {
        factory = newJavaDBFactory();
    } else {
        factory = newDBFactory();
    }
    // Apply configuration
    Options options = new Options();
    options.createIfMissing(true);
    if (config.blockRestartInterval().get() != null) {
        options.blockRestartInterval(config.blockRestartInterval().get());
    }
    if (config.blockSize().get() != null) {
        options.blockSize(config.blockSize().get());
    }
    if (config.cacheSize().get() != null) {
        options.cacheSize(config.cacheSize().get());
    }
    if (config.compression().get() != null) {
        options.compressionType(config.compression().get() ? CompressionType.SNAPPY : CompressionType.NONE);
    }
    if (config.maxOpenFiles().get() != null) {
        options.maxOpenFiles(config.maxOpenFiles().get());
    }
    if (config.paranoidChecks().get() != null) {
        options.paranoidChecks(config.paranoidChecks().get());
    }
    if (config.verifyChecksums().get() != null) {
        options.verifyChecksums(config.verifyChecksums().get());
    }
    if (config.writeBufferSize().get() != null) {
        options.writeBufferSize(config.writeBufferSize().get());
    }
    // Open/Create the database
    File dbFile = new File(fileConfig.dataDirectory(), descriptor.identity());
    db = factory.open(dbFile, options);
}
Also used : Options(org.iq80.leveldb.Options) DBFactory(org.iq80.leveldb.DBFactory) File(java.io.File)

Example 22 with DB

use of org.iq80.leveldb.DB in project EventHub by Codecademy.

the class MigrateIdMapToUseLevelDB method main.

public static void main(String[] args) throws Exception {
    String userStorageDirectory = args[0];
    String filename = userStorageDirectory + "/id_map.ser";
    File file = new File(filename);
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
        //noinspection unchecked
        Map<String, Integer> idMap = (Map<String, Integer>) ois.readObject();
        int currentId = ois.readInt();
        Options options = new Options();
        options.createIfMissing(true);
        try (DB idMapDb = JniDBFactory.factory.open(new File(userStorageDirectory + "/id_map.db"), options)) {
            try (WriteBatch batch = idMapDb.createWriteBatch()) {
                for (Map.Entry<String, Integer> entry : idMap.entrySet()) {
                    batch.put(bytes(entry.getKey()), bytes("" + entry.getValue()));
                }
                batch.put(bytes("__eventtracker__id"), bytes("" + currentId));
                idMapDb.write(batch);
            }
        }
    }
}
Also used : Options(org.iq80.leveldb.Options) File(java.io.File) Map(java.util.Map) WriteBatch(org.iq80.leveldb.WriteBatch) FileInputStream(java.io.FileInputStream) DB(org.iq80.leveldb.DB) ObjectInputStream(java.io.ObjectInputStream)

Example 23 with DB

use of org.iq80.leveldb.DB in project EventHub by Codecademy.

the class DatedEventIndexModule method getDatedEventIndex.

@Provides
public DatedEventIndex getDatedEventIndex(@Named("eventhub.directory") String eventIndexDirectory) throws IOException {
    Options options = new Options();
    options.createIfMissing(true);
    DB db = new DB(JniDBFactory.factory.open(new File(eventIndexDirectory + "/dated_event_index.db"), options));
    return DatedEventIndex.create(db);
}
Also used : Options(org.iq80.leveldb.Options) File(java.io.File) DB(com.codecademy.eventhub.base.DB) Provides(com.google.inject.Provides)

Example 24 with DB

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

the class LevelDBTableCore method deleteColumn.

public void deleteColumn(byte[] row, byte[] column) throws IOException {
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    try (DBIterator iterator = db.iterator()) {
        addToDeleteBatch(batch, iterator, row, column);
        db.write(batch);
    }
}
Also used : DBIterator(org.iq80.leveldb.DBIterator) WriteBatch(org.iq80.leveldb.WriteBatch) DB(org.iq80.leveldb.DB)

Example 25 with DB

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

the class LevelDBTableCore method deleteRows.

public void deleteRows(byte[] prefix) throws IOException {
    Preconditions.checkNotNull(prefix, "prefix must not be null");
    DB db = getDB();
    WriteBatch batch = db.createWriteBatch();
    try (DBIterator iterator = db.iterator()) {
        iterator.seek(createStartKey(prefix));
        while (iterator.hasNext()) {
            Map.Entry<byte[], byte[]> entry = iterator.next();
            if (!Bytes.startsWith(KeyValue.fromKey(entry.getKey()).getRow(), prefix)) {
                // iterator is past prefix
                break;
            }
            batch.delete(entry.getKey());
        }
        db.write(batch);
    }
}
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)

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