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();
}
}
}
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));
}
}
}
}
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());
}
}
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);
}
}
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;
}
Aggregations