|
|
@ -25,43 +25,26 @@ public class ColumnFamilyTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void listColumnFamilies() throws RocksDBException { |
|
|
|
public void listColumnFamilies() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
Options options = null; |
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
try { |
|
|
|
dbFolder.getRoot().getAbsolutePath())) { |
|
|
|
options = new Options(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DBOptions dbOptions = new DBOptions(); |
|
|
|
|
|
|
|
dbOptions.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
// Test listColumnFamilies
|
|
|
|
// Test listColumnFamilies
|
|
|
|
List<byte[]> columnFamilyNames; |
|
|
|
final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies(options, |
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(1); |
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(1); |
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void defaultColumnFamily() throws RocksDBException { |
|
|
|
public void defaultColumnFamily() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
Options options = null; |
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
ColumnFamilyHandle cfh; |
|
|
|
dbFolder.getRoot().getAbsolutePath())) { |
|
|
|
|
|
|
|
final ColumnFamilyHandle cfh = db.getDefaultColumnFamily(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
cfh = db.getDefaultColumnFamily(); |
|
|
|
|
|
|
|
assertThat(cfh).isNotNull(); |
|
|
|
assertThat(cfh).isNotNull(); |
|
|
|
|
|
|
|
|
|
|
|
final byte[] key = "key".getBytes(); |
|
|
|
final byte[] key = "key".getBytes(); |
|
|
@ -74,66 +57,52 @@ public class ColumnFamilyTest { |
|
|
|
assertThat(cfh).isNotNull(); |
|
|
|
assertThat(cfh).isNotNull(); |
|
|
|
assertThat(actualValue).isEqualTo(value); |
|
|
|
assertThat(actualValue).isEqualTo(value); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (db != null) { |
|
|
|
cfh.close(); |
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createColumnFamily() throws RocksDBException { |
|
|
|
public void createColumnFamily() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
Options options = null; |
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
ColumnFamilyHandle columnFamilyHandle = null; |
|
|
|
dbFolder.getRoot().getAbsolutePath())) { |
|
|
|
|
|
|
|
final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily( |
|
|
|
|
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes(), |
|
|
|
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new Options(); |
|
|
|
final List<byte[]> columnFamilyNames = RocksDB.listColumnFamilies( |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
columnFamilyHandle = db.createColumnFamily( |
|
|
|
|
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes(), new ColumnFamilyOptions())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<byte[]> columnFamilyNames; |
|
|
|
|
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(2); |
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(2); |
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf"); |
|
|
|
assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf"); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (columnFamilyHandle != null) { |
|
|
|
columnFamilyHandle.close(); |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void openWithColumnFamilies() throws RocksDBException { |
|
|
|
public void openWithColumnFamilies() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfNames = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes()) |
|
|
|
new ArrayList<>(); |
|
|
|
); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
|
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
new ArrayList<>(); |
|
|
|
new ArrayList<>(); |
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
// Test open database with column family names
|
|
|
|
// Test open database with column family names
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfNames, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
try { |
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
assertThat(columnFamilyHandleList.size()).isEqualTo(2); |
|
|
|
assertThat(columnFamilyHandleList.size()).isEqualTo(2); |
|
|
|
db.put("dfkey1".getBytes(), "dfvalue".getBytes()); |
|
|
|
db.put("dfkey1".getBytes(), "dfvalue".getBytes()); |
|
|
|
db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(), |
|
|
|
db.put(columnFamilyHandleList.get(0), "dfkey2".getBytes(), |
|
|
@ -154,43 +123,38 @@ public class ColumnFamilyTest { |
|
|
|
assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), |
|
|
|
assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), |
|
|
|
"dfkey2".getBytes())).isNull(); |
|
|
|
"dfkey2".getBytes())).isNull(); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
} |
|
|
|
columnFamilyHandle.close(); |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getWithOutValueAndCf() throws RocksDBException { |
|
|
|
public void getWithOutValueAndCf() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
// Test open database with column family names
|
|
|
|
// Test open database with column family names
|
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
.setCreateIfMissing(true) |
|
|
|
cfDescriptors, columnFamilyHandleList); |
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
|
|
|
|
try { |
|
|
|
db.put(columnFamilyHandleList.get(0), new WriteOptions(), |
|
|
|
db.put(columnFamilyHandleList.get(0), new WriteOptions(), |
|
|
|
"key1".getBytes(), "value".getBytes()); |
|
|
|
"key1".getBytes(), "value".getBytes()); |
|
|
|
db.put("key2".getBytes(), "12345678".getBytes()); |
|
|
|
db.put("key2".getBytes(), "12345678".getBytes()); |
|
|
|
byte[] outValue = new byte[5]; |
|
|
|
final byte[] outValue = new byte[5]; |
|
|
|
// not found value
|
|
|
|
// not found value
|
|
|
|
int getResult = db.get("keyNotFound".getBytes(), outValue); |
|
|
|
int getResult = db.get("keyNotFound".getBytes(), outValue); |
|
|
|
assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND); |
|
|
|
assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND); |
|
|
|
// found value which fits in outValue
|
|
|
|
// found value which fits in outValue
|
|
|
|
getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(), outValue); |
|
|
|
getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(), |
|
|
|
|
|
|
|
outValue); |
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
assertThat(outValue).isEqualTo("value".getBytes()); |
|
|
|
assertThat(outValue).isEqualTo("value".getBytes()); |
|
|
|
// found value which fits partially
|
|
|
|
// found value which fits partially
|
|
|
@ -199,79 +163,62 @@ public class ColumnFamilyTest { |
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
assertThat(outValue).isEqualTo("12345".getBytes()); |
|
|
|
assertThat(outValue).isEqualTo("12345".getBytes()); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
} |
|
|
|
columnFamilyHandle.close(); |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void createWriteDropColumnFamily() throws RocksDBException { |
|
|
|
public void createWriteDropColumnFamily() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions opt = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
|
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
|
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
|
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
ColumnFamilyHandle tmpColumnFamilyHandle = null; |
|
|
|
ColumnFamilyHandle tmpColumnFamilyHandle = null; |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
opt = new DBOptions(); |
|
|
|
|
|
|
|
opt.setCreateIfMissing(true); |
|
|
|
|
|
|
|
opt.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
tmpColumnFamilyHandle = db.createColumnFamily( |
|
|
|
tmpColumnFamilyHandle = db.createColumnFamily( |
|
|
|
new ColumnFamilyDescriptor("tmpCF".getBytes(), new ColumnFamilyOptions())); |
|
|
|
new ColumnFamilyDescriptor("tmpCF".getBytes(), |
|
|
|
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
db.dropColumnFamily(tmpColumnFamilyHandle); |
|
|
|
db.dropColumnFamily(tmpColumnFamilyHandle); |
|
|
|
tmpColumnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (tmpColumnFamilyHandle != null) { |
|
|
|
if (tmpColumnFamilyHandle != null) { |
|
|
|
tmpColumnFamilyHandle.dispose(); |
|
|
|
tmpColumnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
db.close(); |
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (opt != null) { |
|
|
|
|
|
|
|
opt.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void writeBatch() throws RocksDBException { |
|
|
|
public void writeBatch() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
try (final ColumnFamilyOptions defaultCfOptions = new ColumnFamilyOptions() |
|
|
|
DBOptions opt = null; |
|
|
|
.setMergeOperator(new StringAppendOperator())) { |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
new ArrayList<>(); |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
defaultCfOptions), |
|
|
|
new ArrayList<>(); |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
|
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
|
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList); |
|
|
|
|
|
|
|
final WriteBatch writeBatch = new WriteBatch(); |
|
|
|
|
|
|
|
final WriteOptions writeOpt = new WriteOptions()) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
opt = new DBOptions(); |
|
|
|
|
|
|
|
opt.setCreateIfMissing(true); |
|
|
|
|
|
|
|
opt.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
|
|
|
new ColumnFamilyOptions().setMergeOperator(new StringAppendOperator()))); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WriteBatch writeBatch = new WriteBatch(); |
|
|
|
|
|
|
|
WriteOptions writeOpt = new WriteOptions(); |
|
|
|
|
|
|
|
writeBatch.put("key".getBytes(), "value".getBytes()); |
|
|
|
writeBatch.put("key".getBytes(), "value".getBytes()); |
|
|
|
writeBatch.put(db.getDefaultColumnFamily(), |
|
|
|
writeBatch.put(db.getDefaultColumnFamily(), |
|
|
|
"mergeKey".getBytes(), "merge".getBytes()); |
|
|
|
"mergeKey".getBytes(), "merge".getBytes()); |
|
|
@ -284,7 +231,7 @@ public class ColumnFamilyTest { |
|
|
|
writeBatch.remove("xyz".getBytes()); |
|
|
|
writeBatch.remove("xyz".getBytes()); |
|
|
|
writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes()); |
|
|
|
writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes()); |
|
|
|
db.write(writeOpt, writeBatch); |
|
|
|
db.write(writeOpt, writeBatch); |
|
|
|
writeBatch.dispose(); |
|
|
|
|
|
|
|
assertThat(db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat(db.get(columnFamilyHandleList.get(1), |
|
|
|
"xyz".getBytes()) == null); |
|
|
|
"xyz".getBytes()) == null); |
|
|
|
assertThat(new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat(new String(db.get(columnFamilyHandleList.get(1), |
|
|
@ -296,43 +243,35 @@ public class ColumnFamilyTest { |
|
|
|
assertThat(new String(db.get(db.getDefaultColumnFamily(), |
|
|
|
assertThat(new String(db.get(db.getDefaultColumnFamily(), |
|
|
|
"mergeKey".getBytes()))).isEqualTo("merge,merge"); |
|
|
|
"mergeKey".getBytes()))).isEqualTo("merge,merge"); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (opt != null) { |
|
|
|
|
|
|
|
opt.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void iteratorOnColumnFamily() throws RocksDBException { |
|
|
|
public void iteratorOnColumnFamily() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
RocksIterator rocksIterator = null; |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
new ArrayList<>(); |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
.setCreateIfMissing(true) |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), |
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), |
|
|
|
"value".getBytes()); |
|
|
|
"value".getBytes()); |
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), |
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), |
|
|
|
"value2".getBytes()); |
|
|
|
"value2".getBytes()); |
|
|
|
rocksIterator = db.newIterator( |
|
|
|
try (final RocksIterator rocksIterator = |
|
|
|
columnFamilyHandleList.get(1)); |
|
|
|
db.newIterator(columnFamilyHandleList.get(1))) { |
|
|
|
rocksIterator.seekToFirst(); |
|
|
|
rocksIterator.seekToFirst(); |
|
|
|
Map<String, String> refMap = new HashMap<>(); |
|
|
|
Map<String, String> refMap = new HashMap<>(); |
|
|
|
refMap.put("newcfkey", "value"); |
|
|
|
refMap.put("newcfkey", "value"); |
|
|
@ -345,88 +284,73 @@ public class ColumnFamilyTest { |
|
|
|
rocksIterator.next(); |
|
|
|
rocksIterator.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
assertThat(i).isEqualTo(2); |
|
|
|
assertThat(i).isEqualTo(2); |
|
|
|
rocksIterator.dispose(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (rocksIterator != null) { |
|
|
|
|
|
|
|
rocksIterator.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
} finally { |
|
|
|
db.close(); |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
|
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void multiGet() throws RocksDBException { |
|
|
|
public void multiGet() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
db.put(columnFamilyHandleList.get(0), "key".getBytes(), |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
"value".getBytes()); |
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), |
|
|
|
|
|
|
|
"value".getBytes()); |
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<byte[]> keys = Arrays.asList(new byte[][]{"key".getBytes(), "newcfkey".getBytes()}); |
|
|
|
final List<byte[]> keys = Arrays.asList(new byte[][]{ |
|
|
|
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList, keys); |
|
|
|
"key".getBytes(), "newcfkey".getBytes() |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList, |
|
|
|
|
|
|
|
keys); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
.isEqualTo("value"); |
|
|
|
.isEqualTo("value"); |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
.isEqualTo("value"); |
|
|
|
.isEqualTo("value"); |
|
|
|
retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList, keys); |
|
|
|
retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList, |
|
|
|
|
|
|
|
keys); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
.isEqualTo("value"); |
|
|
|
.isEqualTo("value"); |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
.isEqualTo("value"); |
|
|
|
.isEqualTo("value"); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void properties() throws RocksDBException { |
|
|
|
public void properties() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
assertThat(db.getProperty("rocksdb.estimate-num-keys")). |
|
|
|
assertThat(db.getProperty("rocksdb.estimate-num-keys")). |
|
|
|
isNotNull(); |
|
|
|
isNotNull(); |
|
|
|
assertThat(db.getLongProperty(columnFamilyHandleList.get(0), |
|
|
|
assertThat(db.getLongProperty(columnFamilyHandleList.get(0), |
|
|
@ -441,14 +365,10 @@ public class ColumnFamilyTest { |
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
"rocksdb.sstables")).isNotNull(); |
|
|
|
"rocksdb.sstables")).isNotNull(); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -456,28 +376,23 @@ public class ColumnFamilyTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void iterators() throws RocksDBException { |
|
|
|
public void iterators() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true) |
|
|
|
|
|
|
|
.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
List<RocksIterator> iterators = null; |
|
|
|
List<RocksIterator> iterators = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
iterators = db.newIterators(columnFamilyHandleList); |
|
|
|
iterators = db.newIterators(columnFamilyHandleList); |
|
|
|
assertThat(iterators.size()).isEqualTo(2); |
|
|
|
assertThat(iterators.size()).isEqualTo(2); |
|
|
|
RocksIterator iter = iterators.get(0); |
|
|
|
RocksIterator iter = iterators.get(0); |
|
|
|
iter.seekToFirst(); |
|
|
|
iter.seekToFirst(); |
|
|
|
Map<String, String> defRefMap = new HashMap<>(); |
|
|
|
final Map<String, String> defRefMap = new HashMap<>(); |
|
|
|
defRefMap.put("dfkey1", "dfvalue"); |
|
|
|
defRefMap.put("dfkey1", "dfvalue"); |
|
|
|
defRefMap.put("key", "value"); |
|
|
|
defRefMap.put("key", "value"); |
|
|
|
while (iter.isValid()) { |
|
|
|
while (iter.isValid()) { |
|
|
@ -486,7 +401,7 @@ public class ColumnFamilyTest { |
|
|
|
iter.next(); |
|
|
|
iter.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
// iterate over new_cf key/value pairs
|
|
|
|
// iterate over new_cf key/value pairs
|
|
|
|
Map<String, String> cfRefMap = new HashMap<>(); |
|
|
|
final Map<String, String> cfRefMap = new HashMap<>(); |
|
|
|
cfRefMap.put("newcfkey", "value"); |
|
|
|
cfRefMap.put("newcfkey", "value"); |
|
|
|
cfRefMap.put("newcfkey2", "value2"); |
|
|
|
cfRefMap.put("newcfkey2", "value2"); |
|
|
|
iter = iterators.get(1); |
|
|
|
iter = iterators.get(1); |
|
|
@ -498,247 +413,193 @@ public class ColumnFamilyTest { |
|
|
|
} |
|
|
|
} |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (iterators != null) { |
|
|
|
if (iterators != null) { |
|
|
|
for (RocksIterator rocksIterator : iterators) { |
|
|
|
for (final RocksIterator rocksIterator : iterators) { |
|
|
|
rocksIterator.dispose(); |
|
|
|
rocksIterator.close(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
db.close(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
public void failPutDisposedCF() throws RocksDBException { |
|
|
|
public void failPutDisposedCF() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.put(columnFamilyHandleList.get(1), "key".getBytes(), "value".getBytes()); |
|
|
|
db.put(columnFamilyHandleList.get(1), "key".getBytes(), |
|
|
|
|
|
|
|
"value".getBytes()); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
public void failRemoveDisposedCF() throws RocksDBException { |
|
|
|
public void failRemoveDisposedCF() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.remove(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
db.remove(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
} |
|
|
|
columnFamilyHandle.close(); |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
public void failGetDisposedCF() throws RocksDBException { |
|
|
|
public void failGetDisposedCF() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
db.get(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
db.get(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
} |
|
|
|
columnFamilyHandle.close(); |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException { |
|
|
|
public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( |
|
|
|
DBOptions options = null; |
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY), |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
try (final DBOptions options = new DBOptions() |
|
|
|
new ArrayList<>(); |
|
|
|
.setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
|
|
|
columnFamilyHandleList)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new DBOptions(); |
|
|
|
final List<byte[]> keys = new ArrayList<>(); |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
List<byte[]> keys = new ArrayList<>(); |
|
|
|
|
|
|
|
keys.add("key".getBytes()); |
|
|
|
keys.add("key".getBytes()); |
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
List<ColumnFamilyHandle> cfCustomList = new ArrayList<>(); |
|
|
|
final List<ColumnFamilyHandle> cfCustomList = new ArrayList<>(); |
|
|
|
db.multiGet(cfCustomList, keys); |
|
|
|
db.multiGet(cfCustomList, keys); |
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
for (final ColumnFamilyHandle columnFamilyHandle : |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandleList) { |
|
|
|
|
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testByteCreateFolumnFamily() throws RocksDBException { |
|
|
|
public void testByteCreateFolumnFamily() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
Options options = null; |
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath()) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
final byte[] b0 = new byte[]{(byte) 0x00}; |
|
|
|
|
|
|
|
final byte[] b1 = new byte[]{(byte) 0x01}; |
|
|
|
|
|
|
|
final byte[] b2 = new byte[]{(byte) 0x02}; |
|
|
|
ColumnFamilyHandle cf1 = null, cf2 = null, cf3 = null; |
|
|
|
ColumnFamilyHandle cf1 = null, cf2 = null, cf3 = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] b0 = new byte[] { (byte)0x00 }; |
|
|
|
|
|
|
|
byte[] b1 = new byte[] { (byte)0x01 }; |
|
|
|
|
|
|
|
byte[] b2 = new byte[] { (byte)0x02 }; |
|
|
|
|
|
|
|
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); |
|
|
|
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); |
|
|
|
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); |
|
|
|
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); |
|
|
|
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
final List<byte[]> families = RocksDB.listColumnFamilies(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
assertThat(families).contains("default".getBytes(), b0, b1); |
|
|
|
assertThat(families).contains("default".getBytes(), b0, b1); |
|
|
|
cf3 = db.createColumnFamily(new ColumnFamilyDescriptor(b2)); |
|
|
|
cf3 = db.createColumnFamily(new ColumnFamilyDescriptor(b2)); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (cf1 != null) { |
|
|
|
if (cf1 != null) { |
|
|
|
cf1.dispose(); |
|
|
|
cf1.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (cf2 != null) { |
|
|
|
if (cf2 != null) { |
|
|
|
cf2.dispose(); |
|
|
|
cf2.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (cf3 != null) { |
|
|
|
if (cf3 != null) { |
|
|
|
cf3.dispose(); |
|
|
|
cf3.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCFNamesWithZeroBytes() throws RocksDBException { |
|
|
|
public void testCFNamesWithZeroBytes() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
Options options = null; |
|
|
|
|
|
|
|
ColumnFamilyHandle cf1 = null, cf2 = null; |
|
|
|
ColumnFamilyHandle cf1 = null, cf2 = null; |
|
|
|
|
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new Options().setCreateIfMissing(true); |
|
|
|
final byte[] b0 = new byte[]{0, 0}; |
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
final byte[] b1 = new byte[]{0, 1}; |
|
|
|
|
|
|
|
|
|
|
|
byte[] b0 = new byte[] { 0, 0 }; |
|
|
|
|
|
|
|
byte[] b1 = new byte[] { 0, 1 }; |
|
|
|
|
|
|
|
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); |
|
|
|
cf1 = db.createColumnFamily(new ColumnFamilyDescriptor(b0)); |
|
|
|
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); |
|
|
|
cf2 = db.createColumnFamily(new ColumnFamilyDescriptor(b1)); |
|
|
|
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
final List<byte[]> families = RocksDB.listColumnFamilies(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
assertThat(families).contains("default".getBytes(), b0, b1); |
|
|
|
assertThat(families).contains("default".getBytes(), b0, b1); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (cf1 != null) { |
|
|
|
if (cf1 != null) { |
|
|
|
cf1.dispose(); |
|
|
|
cf1.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (cf2 != null) { |
|
|
|
if (cf2 != null) { |
|
|
|
cf2.dispose(); |
|
|
|
cf2.close(); |
|
|
|
} |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testCFNameSimplifiedChinese() throws RocksDBException { |
|
|
|
public void testCFNameSimplifiedChinese() throws RocksDBException { |
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
Options options = null; |
|
|
|
|
|
|
|
ColumnFamilyHandle columnFamilyHandle = null; |
|
|
|
ColumnFamilyHandle columnFamilyHandle = null; |
|
|
|
|
|
|
|
try (final Options options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
final RocksDB db = RocksDB.open(options, |
|
|
|
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
options = new Options().setCreateIfMissing(true); |
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
final String simplifiedChinese = "\u7b80\u4f53\u5b57"; |
|
|
|
final String simplifiedChinese = "\u7b80\u4f53\u5b57"; |
|
|
|
columnFamilyHandle = db.createColumnFamily( |
|
|
|
columnFamilyHandle = db.createColumnFamily( |
|
|
|
new ColumnFamilyDescriptor(simplifiedChinese.getBytes())); |
|
|
|
new ColumnFamilyDescriptor(simplifiedChinese.getBytes())); |
|
|
|
|
|
|
|
|
|
|
|
List<byte[]> families = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
final List<byte[]> families = RocksDB.listColumnFamilies(options, |
|
|
|
assertThat(families).contains("default".getBytes(), simplifiedChinese.getBytes()); |
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
assertThat(families).contains("default".getBytes(), |
|
|
|
|
|
|
|
simplifiedChinese.getBytes()); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (columnFamilyHandle != null) { |
|
|
|
if (columnFamilyHandle != null) { |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
columnFamilyHandle.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|