Search in sources :

Example 31 with ColumnFamilyHandle

use of org.rocksdb.ColumnFamilyHandle in project jstorm by alibaba.

the class RocksDBTest method ttlDbOpenWithColumnFamilies.

public void ttlDbOpenWithColumnFamilies() throws RocksDBException, InterruptedException {
    DBOptions dbOptions = null;
    TtlDB ttlDB = null;
    List<ColumnFamilyDescriptor> cfNames = new ArrayList<ColumnFamilyDescriptor>();
    List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<ColumnFamilyHandle>();
    cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
    cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
    List<Integer> ttlValues = new ArrayList<Integer>();
    // Default column family with infinite lifetime
    ttlValues.add(0);
    // new column family with 1 second ttl
    ttlValues.add(1);
    try {
        System.out.println("Begin to open db");
        dbOptions = new DBOptions().setCreateMissingColumnFamilies(true).setCreateIfMissing(true);
        ttlDB = TtlDB.open(dbOptions, rootDir, cfNames, columnFamilyHandleList, ttlValues, false);
        System.out.println("Successfully open db " + rootDir);
        ttlDB.put("key".getBytes(), "value".getBytes());
        assertThat(ttlDB.get("key".getBytes())).isEqualTo("value".getBytes());
        ttlDB.put(columnFamilyHandleList.get(1), "key".getBytes(), "value".getBytes());
        assertThat(ttlDB.get(columnFamilyHandleList.get(1), "key".getBytes())).isEqualTo("value".getBytes());
        TimeUnit.SECONDS.sleep(2);
        ttlDB.compactRange();
        ttlDB.compactRange(columnFamilyHandleList.get(1));
        assertThat(ttlDB.get("key".getBytes())).isNotNull();
        assertThat(ttlDB.get(columnFamilyHandleList.get(1), "key".getBytes())).isNull();
    } finally {
        for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
            columnFamilyHandle.dispose();
        }
        if (ttlDB != null) {
            ttlDB.close();
        }
        if (dbOptions != null) {
            dbOptions.dispose();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DBOptions(org.rocksdb.DBOptions) TtlDB(org.rocksdb.TtlDB) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 32 with ColumnFamilyHandle

use of org.rocksdb.ColumnFamilyHandle in project jstorm by alibaba.

the class RocksTTLDBCache method putBatch.

protected void putBatch(Map<String, Object> map, Entry<Integer, ColumnFamilyHandle> putEntry) {
    WriteOptions writeOpts = null;
    WriteBatch writeBatch = null;
    Set<byte[]> putKeys = new HashSet<>();
    try {
        writeOpts = new WriteOptions();
        writeBatch = new WriteBatch();
        for (Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            byte[] data = Utils.javaSerialize(value);
            if (StringUtils.isBlank(key) || data == null || data.length == 0) {
                continue;
            }
            byte[] keyByte = key.getBytes();
            writeBatch.put(putEntry.getValue(), keyByte, data);
            putKeys.add(keyByte);
        }
        ttlDB.write(writeOpts, writeBatch);
    } catch (Exception e) {
        LOG.error("Failed to putBatch into DB, " + map.keySet(), e);
    } finally {
        if (writeOpts != null) {
            writeOpts.dispose();
        }
        if (writeBatch != null) {
            writeBatch.dispose();
        }
    }
    for (Entry<Integer, ColumnFamilyHandle> entry : windowHandlers.entrySet()) {
        if (entry.getKey().equals(putEntry.getKey())) {
            continue;
        }
        for (byte[] keyByte : putKeys) {
            try {
                ttlDB.remove(entry.getValue(), keyByte);
            } catch (Exception e) {
                LOG.error("Failed to remove other's " + new String(keyByte));
            }
        }
    }
}
Also used : WriteOptions(org.rocksdb.WriteOptions) WriteBatch(org.rocksdb.WriteBatch) IOException(java.io.IOException) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) HashSet(java.util.HashSet)

Example 33 with ColumnFamilyHandle

use of org.rocksdb.ColumnFamilyHandle in project jstorm by alibaba.

the class WindowedRocksDbHdfsState method get.

@Override
public V get(TimeWindow window, K key) {
    try {
        ColumnFamilyHandle handler = getColumnFamilyHandle(window);
        V ret = null;
        if (key != null) {
            byte[] rawKey = serializer.serialize(key);
            byte[] rawData = rocksDb.get(handler, rawKey);
            ret = rawData != null ? (V) serializer.deserialize(rawData) : null;
        }
        return ret;
    } catch (RocksDBException e) {
        LOG.error("Failed to get value by key-{} for timeWindow={}", key, window);
        throw new RuntimeException(e.getMessage());
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 34 with ColumnFamilyHandle

use of org.rocksdb.ColumnFamilyHandle in project jstorm by alibaba.

the class WindowedRocksDbHdfsState method remove.

@Override
public void remove(TimeWindow window, Object key) {
    try {
        ColumnFamilyHandle handler = getColumnFamilyHandle(window);
        rocksDb.remove(handler, serializer.serialize(key));
    } catch (RocksDBException e) {
        LOG.warn("Failed to remove " + key + "for timeWindow=" + window, e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 35 with ColumnFamilyHandle

use of org.rocksdb.ColumnFamilyHandle in project jstorm by alibaba.

the class WindowedRocksDbHdfsState method getColumnFamilyHandle.

private ColumnFamilyHandle getColumnFamilyHandle(TimeWindow window) throws RocksDBException {
    ColumnFamilyHandle handler = null;
    if (window == null) {
        handler = rocksDb.getDefaultColumnFamily();
    } else {
        handler = windowToCFHandler.get(window);
        if (handler == null) {
            handler = rocksDb.createColumnFamily(new ColumnFamilyDescriptor(serializer.serialize(window)));
            windowToCFHandler.put(window, handler);
        }
    }
    return handler;
}
Also used : ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Aggregations

ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)55 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)27 ArrayList (java.util.ArrayList)21 RocksDBException (org.rocksdb.RocksDBException)19 RocksDB (org.rocksdb.RocksDB)16 Test (org.junit.Test)10 DBOptions (org.rocksdb.DBOptions)9 ColumnFamilyOptions (org.rocksdb.ColumnFamilyOptions)8 File (java.io.File)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 WriteOptions (org.rocksdb.WriteOptions)6 IOException (java.io.IOException)5 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)5 ReadOptions (org.rocksdb.ReadOptions)5 List (java.util.List)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 KeyGroup (org.apache.flink.runtime.state.restore.KeyGroup)4 LinkedHashMap (java.util.LinkedHashMap)3 SortedMap (java.util.SortedMap)3