|
|
@ -23,121 +23,66 @@ public class RocksDBColumnFamilySample { |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("RocksDBColumnFamilySample"); |
|
|
|
System.out.println("RocksDBColumnFamilySample"); |
|
|
|
RocksDB db = null; |
|
|
|
RocksDB db = null; |
|
|
|
DBOptions dbOptions = null; |
|
|
|
Options options = null; |
|
|
|
List<RocksIterator> iterators = new ArrayList<>(); |
|
|
|
ColumnFamilyHandle columnFamilyHandle = null; |
|
|
|
RocksIterator iterator = null; |
|
|
|
|
|
|
|
ColumnFamilyHandle cfHandle = null; |
|
|
|
|
|
|
|
WriteBatch wb = null; |
|
|
|
WriteBatch wb = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
// Setup DBOptions
|
|
|
|
options = new Options().setCreateIfMissing(true); |
|
|
|
dbOptions = new DBOptions(). |
|
|
|
db = RocksDB.open(options, db_path); |
|
|
|
setCreateIfMissing(true). |
|
|
|
assert(db != null); |
|
|
|
setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
// Setup ColumnFamily descriptors
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
// Default column family
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("default")); |
|
|
|
|
|
|
|
// New column families
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("cf_green", |
|
|
|
|
|
|
|
new ColumnFamilyOptions().setComparator( |
|
|
|
|
|
|
|
BuiltinComparator.BYTEWISE_COMPARATOR))); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("cf_blue", |
|
|
|
|
|
|
|
new ColumnFamilyOptions().setComparator( |
|
|
|
|
|
|
|
BuiltinComparator.REVERSE_BYTEWISE_COMPARATOR))); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("cf_red", |
|
|
|
|
|
|
|
new ColumnFamilyOptions(). |
|
|
|
|
|
|
|
setMergeOperator(new StringAppendOperator()))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ColumnFamilyHandle> cfHandles = |
|
|
|
// create column family
|
|
|
|
new ArrayList<>(); |
|
|
|
columnFamilyHandle = db.createColumnFamily( |
|
|
|
db = RocksDB.open(dbOptions, |
|
|
|
new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions())); |
|
|
|
db_path, cfNames, cfHandles); |
|
|
|
assert(columnFamilyHandle != null); |
|
|
|
// List column families in database
|
|
|
|
|
|
|
|
System.out.println("List existent column families:"); |
|
|
|
} finally { |
|
|
|
List<byte[]> cfListing = RocksDB.listColumnFamilies( |
|
|
|
if (columnFamilyHandle != null) { |
|
|
|
new Options(), db_path); |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
for (byte[] cf : cfListing) { |
|
|
|
|
|
|
|
System.out.format(" - %s\n", new String(cf)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Bootstrapping values
|
|
|
|
|
|
|
|
System.out.println("Writing values to database."); |
|
|
|
|
|
|
|
for (int i=0; i < cfNames.size(); i++) { |
|
|
|
|
|
|
|
for (int j=0; j < 10; j++) { |
|
|
|
|
|
|
|
db.put(cfHandles.get(i), |
|
|
|
|
|
|
|
String.valueOf(j).getBytes(), |
|
|
|
|
|
|
|
String.valueOf(j).getBytes()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// Retrieve values using get
|
|
|
|
if (db != null) { |
|
|
|
System.out.println("Retrieve values with get."); |
|
|
|
db.close(); |
|
|
|
for (int i=0; i < cfNames.size(); i++) { |
|
|
|
db = null; |
|
|
|
for (int j=0; j < 10; j++) { |
|
|
|
|
|
|
|
System.out.format(" %s", new String( |
|
|
|
|
|
|
|
db.get(cfHandles.get(i), |
|
|
|
|
|
|
|
String.valueOf(j).getBytes()))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
System.out.println(""); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// Add a new column family to existing database
|
|
|
|
} |
|
|
|
System.out.println("Add new column family"); |
|
|
|
|
|
|
|
cfHandle = db.createColumnFamily(new ColumnFamilyDescriptor( |
|
|
|
// open DB with two column families
|
|
|
|
"cf_temp", new ColumnFamilyOptions(). |
|
|
|
List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>(); |
|
|
|
setMergeOperator(new StringAppendOperator()))); |
|
|
|
// have to open default column family
|
|
|
|
System.out.println("Write key/value into new column family."); |
|
|
|
columnFamilyDescriptors.add(new ColumnFamilyDescriptor( |
|
|
|
db.put(cfHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions())); |
|
|
|
System.out.format("Lookup 'key' retrieved value: %s\n", new String( |
|
|
|
// open the new one, too
|
|
|
|
db.get(cfHandle, "key".getBytes()))); |
|
|
|
columnFamilyDescriptors.add(new ColumnFamilyDescriptor( |
|
|
|
// Delete key
|
|
|
|
"new_cf", new ColumnFamilyOptions())); |
|
|
|
System.out.println("Delete key/value in new column family."); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>(); |
|
|
|
db.remove(cfHandle, "key".getBytes()); |
|
|
|
try { |
|
|
|
// WriteBatch with column family
|
|
|
|
db = RocksDB.open(new DBOptions(), db_path, |
|
|
|
|
|
|
|
columnFamilyDescriptors, columnFamilyHandles); |
|
|
|
|
|
|
|
assert(db != null); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// put and get from non-default column family
|
|
|
|
|
|
|
|
db.put(columnFamilyHandles.get(0), new WriteOptions(), |
|
|
|
|
|
|
|
"key".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
String value = new String(db.get(columnFamilyHandles.get(0), |
|
|
|
|
|
|
|
"key".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// atomic write
|
|
|
|
wb = new WriteBatch(); |
|
|
|
wb = new WriteBatch(); |
|
|
|
wb.put(cfHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
wb.put(columnFamilyHandles.get(0), "key2".getBytes(), "value2".getBytes()); |
|
|
|
wb.put(cfHandle, "key2".getBytes(), "value2".getBytes()); |
|
|
|
wb.put(columnFamilyHandles.get(1), "key3".getBytes(), "value3".getBytes()); |
|
|
|
wb.remove(cfHandle, "key2".getBytes()); |
|
|
|
wb.remove(columnFamilyHandles.get(0), "key".getBytes()); |
|
|
|
wb.merge(cfHandle, "key".getBytes(), "morevalues".getBytes()); |
|
|
|
|
|
|
|
db.write(new WriteOptions(), wb); |
|
|
|
db.write(new WriteOptions(), wb); |
|
|
|
// Retrieve a single iterator with a cf handle
|
|
|
|
|
|
|
|
System.out.println("Retrieve values using a iterator on" + |
|
|
|
// drop column family
|
|
|
|
" a column family."); |
|
|
|
db.dropColumnFamily(columnFamilyHandles.get(1)); |
|
|
|
iterator = db.newIterator(cfHandle); |
|
|
|
|
|
|
|
iterator.seekToFirst(); |
|
|
|
|
|
|
|
while(iterator.isValid()) { |
|
|
|
|
|
|
|
System.out.format(" %s", new String( |
|
|
|
|
|
|
|
iterator.value())); |
|
|
|
|
|
|
|
iterator.next(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
System.out.println(""); |
|
|
|
|
|
|
|
// Delete column family
|
|
|
|
|
|
|
|
System.out.println("Delete column family."); |
|
|
|
|
|
|
|
db.dropColumnFamily(cfHandle); |
|
|
|
|
|
|
|
// Retrieve values from cf using iterator
|
|
|
|
|
|
|
|
System.out.println("Retrieve values with iterators"); |
|
|
|
|
|
|
|
iterators = db.newIterators(cfHandles); |
|
|
|
|
|
|
|
assert(iterators.size() == 4); |
|
|
|
|
|
|
|
for (RocksIterator iter : iterators) { |
|
|
|
|
|
|
|
iter.seekToFirst(); |
|
|
|
|
|
|
|
while(iter.isValid()) { |
|
|
|
|
|
|
|
System.out.format(" %s", new String( |
|
|
|
|
|
|
|
iter.value())); |
|
|
|
|
|
|
|
iter.next(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
System.out.println(""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
|
|
|
|
for (ColumnFamilyHandle handle : columnFamilyHandles){ |
|
|
|
|
|
|
|
handle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
if (db != null) { |
|
|
|
db.close(); |
|
|
|
db.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (dbOptions != null) { |
|
|
|
|
|
|
|
dbOptions.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (iterator != null) { |
|
|
|
|
|
|
|
iterator.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (RocksIterator iter : iterators) { |
|
|
|
|
|
|
|
iter.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (wb != null) { |
|
|
|
if (wb != null) { |
|
|
|
wb.dispose(); |
|
|
|
wb.dispose(); |
|
|
|
} |
|
|
|
} |
|
|
|