|
|
@ -16,6 +16,8 @@ import org.junit.Test; |
|
|
|
import org.junit.rules.TemporaryFolder; |
|
|
|
import org.junit.rules.TemporaryFolder; |
|
|
|
import org.rocksdb.*; |
|
|
|
import org.rocksdb.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
|
|
public class ColumnFamilyTest { |
|
|
|
public class ColumnFamilyTest { |
|
|
|
|
|
|
|
|
|
|
|
@ClassRule |
|
|
|
@ClassRule |
|
|
@ -26,71 +28,85 @@ public class ColumnFamilyTest { |
|
|
|
public TemporaryFolder dbFolder = new TemporaryFolder(); |
|
|
|
public TemporaryFolder dbFolder = new TemporaryFolder(); |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void columnFamilies() { |
|
|
|
public void listColumnFamilies() throws RocksDBException { |
|
|
|
String db_path = dbFolder.getRoot().getAbsolutePath(); |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
RocksDB db = null; |
|
|
|
Options options = new Options(); |
|
|
|
Options options = null; |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
try { |
|
|
|
|
|
|
|
options = new Options(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
DBOptions dbOptions = new DBOptions(); |
|
|
|
DBOptions dbOptions = new DBOptions(); |
|
|
|
dbOptions.setCreateIfMissing(true); |
|
|
|
dbOptions.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
db = RocksDB.open(options, db_path); |
|
|
|
// Test listColumnFamilies
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
List<byte[]> columnFamilyNames; |
|
|
|
assert(false); |
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
} |
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
// Test listColumnFamilies
|
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
List<byte[]> columnFamilyNames; |
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(1); |
|
|
|
try { |
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, db_path); |
|
|
|
} finally { |
|
|
|
if (columnFamilyNames != null && columnFamilyNames.size() > 0) { |
|
|
|
if (db != null) { |
|
|
|
assert(columnFamilyNames.size() == 1); |
|
|
|
db.close(); |
|
|
|
assert(new String(columnFamilyNames.get(0)).equals("default")); |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (options != null) { |
|
|
|
assert(false); |
|
|
|
options.dispose(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test createColumnFamily
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void createColumnFamily() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
Options options = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
options = new Options(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DBOptions dbOptions = new DBOptions(); |
|
|
|
|
|
|
|
dbOptions.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
db.createColumnFamily(new ColumnFamilyDescriptor("new_cf", |
|
|
|
db.createColumnFamily(new ColumnFamilyDescriptor("new_cf", |
|
|
|
new ColumnFamilyOptions())); |
|
|
|
new ColumnFamilyOptions())); |
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
db.close(); |
|
|
|
|
|
|
|
List<byte[]> columnFamilyNames; |
|
|
|
|
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
|
|
|
assertThat(columnFamilyNames).isNotNull(); |
|
|
|
|
|
|
|
assertThat(columnFamilyNames.size()).isGreaterThan(0); |
|
|
|
|
|
|
|
assertThat(columnFamilyNames.size()).isEqualTo(2); |
|
|
|
|
|
|
|
assertThat(new String(columnFamilyNames.get(0))).isEqualTo("default"); |
|
|
|
|
|
|
|
assertThat(new String(columnFamilyNames.get(1))).isEqualTo("new_cf"); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test listColumnFamilies after create "new_cf"
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void openWithColumnFamilies() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
columnFamilyNames = RocksDB.listColumnFamilies(options, db_path); |
|
|
|
options = new DBOptions(); |
|
|
|
if (columnFamilyNames != null && columnFamilyNames.size() > 0) { |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
assert(columnFamilyNames.size() == 2); |
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
assert(new String(columnFamilyNames.get(0)).equals("default")); |
|
|
|
// Test open database with column family names
|
|
|
|
assert(new String(columnFamilyNames.get(1)).equals("new_cf")); |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
} else { |
|
|
|
new ArrayList<>(); |
|
|
|
assert(false); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
} |
|
|
|
new ArrayList<>(); |
|
|
|
} catch (RocksDBException e) { |
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
assert(false); |
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test open database with column family names
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("default")); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
db = RocksDB.open(dbOptions, db_path, cfNames, columnFamilyHandleList); |
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
assert(columnFamilyHandleList.size() == 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(), |
|
|
|
"dfvalue".getBytes()); |
|
|
|
"dfvalue".getBytes()); |
|
|
@ -99,57 +115,118 @@ public class ColumnFamilyTest { |
|
|
|
|
|
|
|
|
|
|
|
String retVal = new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
String retVal = new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
"newcfkey1".getBytes())); |
|
|
|
"newcfkey1".getBytes())); |
|
|
|
assert(retVal.equals("newcfvalue")); |
|
|
|
assertThat(retVal).isEqualTo("newcfvalue"); |
|
|
|
assert( (db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat((db.get(columnFamilyHandleList.get(1), |
|
|
|
"dfkey1".getBytes())) == null); |
|
|
|
"dfkey1".getBytes()))).isNull(); |
|
|
|
db.remove(columnFamilyHandleList.get(1), "newcfkey1".getBytes()); |
|
|
|
db.remove(columnFamilyHandleList.get(1), "newcfkey1".getBytes()); |
|
|
|
assert( (db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat((db.get(columnFamilyHandleList.get(1), |
|
|
|
"newcfkey1".getBytes())) == null); |
|
|
|
"newcfkey1".getBytes()))).isNull(); |
|
|
|
db.remove("dfkey2".getBytes()); |
|
|
|
db.remove(columnFamilyHandleList.get(0), new WriteOptions(), |
|
|
|
assert( (db.get(columnFamilyHandleList.get(0), |
|
|
|
"dfkey2".getBytes()); |
|
|
|
"dfkey2".getBytes())) == null); |
|
|
|
assertThat(db.get(columnFamilyHandleList.get(0), new ReadOptions(), |
|
|
|
} catch (RocksDBException e) { |
|
|
|
"dfkey2".getBytes())).isNull(); |
|
|
|
assert(false); |
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void getWithOutValueAndCf() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
// Test open database with column family names
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfDescriptors, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(0), new WriteOptions(), "key1".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
db.put("key2".getBytes(), "12345678".getBytes()); |
|
|
|
|
|
|
|
byte[] outValue = new byte[5]; |
|
|
|
|
|
|
|
// not found value
|
|
|
|
|
|
|
|
int getResult = db.get("keyNotFound".getBytes(), outValue); |
|
|
|
|
|
|
|
assertThat(getResult).isEqualTo(RocksDB.NOT_FOUND); |
|
|
|
|
|
|
|
// found value which fits in outValue
|
|
|
|
|
|
|
|
getResult = db.get(columnFamilyHandleList.get(0), "key1".getBytes(), outValue); |
|
|
|
|
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
|
|
|
|
assertThat(outValue).isEqualTo("value".getBytes()); |
|
|
|
|
|
|
|
// found value which fits partially
|
|
|
|
|
|
|
|
getResult = db.get(columnFamilyHandleList.get(0), new ReadOptions(), |
|
|
|
|
|
|
|
"key2".getBytes(), outValue); |
|
|
|
|
|
|
|
assertThat(getResult).isNotEqualTo(RocksDB.NOT_FOUND); |
|
|
|
|
|
|
|
assertThat(outValue).isEqualTo("12345".getBytes()); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test create write to and drop ColumnFamily
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void createWriteDropColumnFamily() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions opt = null; |
|
|
|
ColumnFamilyHandle tmpColumnFamilyHandle = null; |
|
|
|
ColumnFamilyHandle tmpColumnFamilyHandle = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
opt = new DBOptions(); |
|
|
|
|
|
|
|
opt.setCreateIfMissing(true); |
|
|
|
|
|
|
|
opt.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
tmpColumnFamilyHandle = db.createColumnFamily( |
|
|
|
tmpColumnFamilyHandle = db.createColumnFamily( |
|
|
|
new ColumnFamilyDescriptor("tmpCF", new ColumnFamilyOptions())); |
|
|
|
new ColumnFamilyDescriptor("tmpCF", new ColumnFamilyOptions())); |
|
|
|
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
db.dropColumnFamily(tmpColumnFamilyHandle); |
|
|
|
db.dropColumnFamily(tmpColumnFamilyHandle); |
|
|
|
tmpColumnFamilyHandle.dispose(); |
|
|
|
tmpColumnFamilyHandle.dispose(); |
|
|
|
} catch (Exception e) { |
|
|
|
} finally { |
|
|
|
assert(false); |
|
|
|
if (tmpColumnFamilyHandle != null) { |
|
|
|
} |
|
|
|
tmpColumnFamilyHandle.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
// Put to disposed column family tmpColumnFamilyHandle must fail
|
|
|
|
if (db != null) { |
|
|
|
try { |
|
|
|
db.close(); |
|
|
|
db.put(tmpColumnFamilyHandle, "key".getBytes(), "value".getBytes()); |
|
|
|
} |
|
|
|
assert(false); |
|
|
|
if (opt != null) { |
|
|
|
} catch (RocksDBException e) { |
|
|
|
opt.dispose(); |
|
|
|
assert(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Remove to disposed column family tmpColumnFamilyHandle must fail
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void writeBatch() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions opt = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
db.remove(tmpColumnFamilyHandle, "key".getBytes()); |
|
|
|
opt = new DBOptions(); |
|
|
|
assert(false); |
|
|
|
opt.setCreateIfMissing(true); |
|
|
|
} catch (RocksDBException e) { |
|
|
|
opt.setCreateMissingColumnFamilies(true); |
|
|
|
assert(true); |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
} |
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
// Get on a disposed column family tmpColumnFamilyHandle must fail
|
|
|
|
db = RocksDB.open(opt, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
try { |
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
db.get(tmpColumnFamilyHandle, "key".getBytes()); |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
|
|
|
assert(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test WriteBatch
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
WriteBatch writeBatch = new WriteBatch(); |
|
|
|
WriteBatch writeBatch = new WriteBatch(); |
|
|
|
WriteOptions writeOpt = new WriteOptions(); |
|
|
|
WriteOptions writeOpt = new WriteOptions(); |
|
|
|
writeBatch.put("key".getBytes(), "value".getBytes()); |
|
|
|
writeBatch.put("key".getBytes(), "value".getBytes()); |
|
|
@ -161,135 +238,324 @@ public class ColumnFamilyTest { |
|
|
|
writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes()); |
|
|
|
writeBatch.remove(columnFamilyHandleList.get(1), "xyz".getBytes()); |
|
|
|
db.write(writeOpt, writeBatch); |
|
|
|
db.write(writeOpt, writeBatch); |
|
|
|
writeBatch.dispose(); |
|
|
|
writeBatch.dispose(); |
|
|
|
assert(db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat(db.get(columnFamilyHandleList.get(1), |
|
|
|
"xyz".getBytes()) == null); |
|
|
|
"xyz".getBytes()) == null); |
|
|
|
assert(new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat(new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
"newcfkey".getBytes())).equals("value")); |
|
|
|
"newcfkey".getBytes()))).isEqualTo("value"); |
|
|
|
assert(new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
assertThat(new String(db.get(columnFamilyHandleList.get(1), |
|
|
|
"newcfkey2".getBytes())).equals("value2")); |
|
|
|
"newcfkey2".getBytes()))).isEqualTo("value2"); |
|
|
|
assert(new String(db.get("key".getBytes())).equals("value")); |
|
|
|
assertThat(new String(db.get("key".getBytes()))).isEqualTo("value"); |
|
|
|
} catch (Exception e) { |
|
|
|
} finally { |
|
|
|
e.printStackTrace(); |
|
|
|
if (db != null) { |
|
|
|
assert(false); |
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (opt != null) { |
|
|
|
|
|
|
|
opt.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test iterator on column family
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void iteratorOnColumnFamily() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
RocksIterator rocksIterator = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
RocksIterator rocksIterator = db.newIterator( |
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), |
|
|
|
|
|
|
|
"value".getBytes()); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey2".getBytes(), |
|
|
|
|
|
|
|
"value2".getBytes()); |
|
|
|
|
|
|
|
rocksIterator = db.newIterator( |
|
|
|
columnFamilyHandleList.get(1)); |
|
|
|
columnFamilyHandleList.get(1)); |
|
|
|
rocksIterator.seekToFirst(); |
|
|
|
rocksIterator.seekToFirst(); |
|
|
|
Map<String, String> refMap = new HashMap<String, String>(); |
|
|
|
Map<String, String> refMap = new HashMap<>(); |
|
|
|
refMap.put("newcfkey", "value"); |
|
|
|
refMap.put("newcfkey", "value"); |
|
|
|
refMap.put("newcfkey2", "value2"); |
|
|
|
refMap.put("newcfkey2", "value2"); |
|
|
|
int i = 0; |
|
|
|
int i = 0; |
|
|
|
while(rocksIterator.isValid()) { |
|
|
|
while (rocksIterator.isValid()) { |
|
|
|
i++; |
|
|
|
i++; |
|
|
|
refMap.get(new String(rocksIterator.key())).equals( |
|
|
|
assertThat(refMap.get(new String(rocksIterator.key()))). |
|
|
|
new String(rocksIterator.value())); |
|
|
|
isEqualTo(new String(rocksIterator.value())); |
|
|
|
rocksIterator.next(); |
|
|
|
rocksIterator.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
assert(i == 2); |
|
|
|
assertThat(i).isEqualTo(2); |
|
|
|
rocksIterator.dispose(); |
|
|
|
rocksIterator.dispose(); |
|
|
|
} catch(Exception e) { |
|
|
|
} finally { |
|
|
|
assert(false); |
|
|
|
if (rocksIterator != null) { |
|
|
|
|
|
|
|
rocksIterator.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Test property handling on column families
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void multiGet() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
assert(db.getProperty("rocksdb.estimate-num-keys") != null); |
|
|
|
options = new DBOptions(); |
|
|
|
assert(db.getProperty("rocksdb.stats") != null); |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
assert(db.getProperty(columnFamilyHandleList.get(0), |
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
"rocksdb.sstables") != null); |
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = |
|
|
|
assert(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
new ArrayList<>(); |
|
|
|
"rocksdb.estimate-num-keys") != null); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
assert(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
new ArrayList<>(); |
|
|
|
"rocksdb.stats") != null); |
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
assert(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
"rocksdb.sstables") != null); |
|
|
|
|
|
|
|
} catch(Exception e) { |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MultiGet test
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
List<ColumnFamilyHandle> cfCustomList = new ArrayList<ColumnFamilyHandle>(); |
|
|
|
cfDescriptors, columnFamilyHandleList); |
|
|
|
try { |
|
|
|
db.put(columnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes()); |
|
|
|
List<byte[]> keys = new ArrayList<byte[]>(); |
|
|
|
db.put(columnFamilyHandleList.get(1), "newcfkey".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<byte[]> keys = new ArrayList<>(); |
|
|
|
keys.add("key".getBytes()); |
|
|
|
keys.add("key".getBytes()); |
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList,keys); |
|
|
|
Map<byte[], byte[]> retValues = db.multiGet(columnFamilyHandleList, keys); |
|
|
|
assert(retValues.size() == 2); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
assert(new String(retValues.get(keys.get(0))) |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
.equals("value")); |
|
|
|
.isEqualTo("value"); |
|
|
|
assert(new String(retValues.get(keys.get(1))) |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
.equals("value")); |
|
|
|
.isEqualTo("value"); |
|
|
|
|
|
|
|
retValues = db.multiGet(new ReadOptions(), columnFamilyHandleList, keys); |
|
|
|
cfCustomList.add(columnFamilyHandleList.get(0)); |
|
|
|
assertThat(retValues.size()).isEqualTo(2); |
|
|
|
cfCustomList.add(columnFamilyHandleList.get(0)); |
|
|
|
assertThat(new String(retValues.get(keys.get(0)))) |
|
|
|
retValues = db.multiGet(cfCustomList, keys); |
|
|
|
.isEqualTo("value"); |
|
|
|
assert(retValues.size() == 1); |
|
|
|
assertThat(new String(retValues.get(keys.get(1)))) |
|
|
|
assert(new String(retValues.get(keys.get(0))) |
|
|
|
.isEqualTo("value"); |
|
|
|
.equals("value")); |
|
|
|
} finally { |
|
|
|
} catch (RocksDBException e) { |
|
|
|
if (db != null) { |
|
|
|
assert(false); |
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test multiget without correct number of column
|
|
|
|
@Test |
|
|
|
// families
|
|
|
|
public void properties() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
List<byte[]> keys = new ArrayList<byte[]>(); |
|
|
|
options = new DBOptions(); |
|
|
|
keys.add("key".getBytes()); |
|
|
|
options.setCreateIfMissing(true); |
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
cfCustomList.remove(1); |
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
db.multiGet(cfCustomList, keys); |
|
|
|
new ArrayList<>(); |
|
|
|
assert(false); |
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
} catch (RocksDBException e) { |
|
|
|
new ArrayList<>(); |
|
|
|
assert(false); |
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
} catch (IllegalArgumentException e) { |
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
assert(true); |
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
assertThat(db.getProperty("rocksdb.estimate-num-keys")). |
|
|
|
|
|
|
|
isNotNull(); |
|
|
|
|
|
|
|
assertThat(db.getProperty("rocksdb.stats")).isNotNull(); |
|
|
|
|
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(0), |
|
|
|
|
|
|
|
"rocksdb.sstables")).isNotNull(); |
|
|
|
|
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
|
|
|
|
"rocksdb.estimate-num-keys")).isNotNull(); |
|
|
|
|
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
|
|
|
|
"rocksdb.stats")).isNotNull(); |
|
|
|
|
|
|
|
assertThat(db.getProperty(columnFamilyHandleList.get(1), |
|
|
|
|
|
|
|
"rocksdb.sstables")).isNotNull(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void iterators() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
// iterate over default key/value pairs
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
options.setCreateMissingColumnFamilies(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
List<RocksIterator> iterators = |
|
|
|
List<RocksIterator> iterators = |
|
|
|
db.newIterators(columnFamilyHandleList); |
|
|
|
db.newIterators(columnFamilyHandleList); |
|
|
|
assert(iterators.size() == 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<String, String>(); |
|
|
|
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()) { |
|
|
|
defRefMap.get(new String(iter.key())).equals( |
|
|
|
assertThat(defRefMap.get(new String(iter.key()))). |
|
|
|
new String(iter.value())); |
|
|
|
isEqualTo(new String(iter.value())); |
|
|
|
iter.next(); |
|
|
|
iter.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
// iterate over new_cf key/value pairs
|
|
|
|
// iterate over new_cf key/value pairs
|
|
|
|
Map<String,String> cfRefMap = new HashMap<String, String>(); |
|
|
|
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); |
|
|
|
iter.seekToFirst(); |
|
|
|
iter.seekToFirst(); |
|
|
|
while (iter.isValid()) { |
|
|
|
while (iter.isValid()) { |
|
|
|
cfRefMap.get(new String(iter.key())).equals( |
|
|
|
assertThat(cfRefMap.get(new String(iter.key()))). |
|
|
|
new String(iter.value())); |
|
|
|
isEqualTo(new String(iter.value())); |
|
|
|
iter.next(); |
|
|
|
iter.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
// free iterators
|
|
|
|
} finally { |
|
|
|
for (RocksIterator iterator : iterators) { |
|
|
|
if (db != null) { |
|
|
|
iterator.dispose(); |
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
|
|
|
public void failPutDisposedCF() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
|
|
|
|
db.put(columnFamilyHandleList.get(1), "key".getBytes(), "value".getBytes()); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
|
|
|
public void failRemoveDisposedCF() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
|
|
|
|
db.remove(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
|
|
|
public void failGetDisposedCF() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
|
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
|
|
|
|
db.dropColumnFamily(columnFamilyHandleList.get(1)); |
|
|
|
|
|
|
|
db.get(columnFamilyHandleList.get(1), "key".getBytes()); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
} |
|
|
|
} |
|
|
|
assert(true); |
|
|
|
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
|
|
|
public void failMultiGetWithoutCorrectNumberOfCF() throws RocksDBException { |
|
|
|
|
|
|
|
RocksDB db = null; |
|
|
|
|
|
|
|
DBOptions options = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
options = new DBOptions(); |
|
|
|
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY)); |
|
|
|
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("new_cf")); |
|
|
|
|
|
|
|
|
|
|
|
// free cf handles before database close
|
|
|
|
db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath(), |
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) { |
|
|
|
cfNames, columnFamilyHandleList); |
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
List<byte[]> keys = new ArrayList<>(); |
|
|
|
|
|
|
|
keys.add("key".getBytes()); |
|
|
|
|
|
|
|
keys.add("newcfkey".getBytes()); |
|
|
|
|
|
|
|
List<ColumnFamilyHandle> cfCustomList = new ArrayList<>(); |
|
|
|
|
|
|
|
db.multiGet(cfCustomList, keys); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (db != null) { |
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (options != null) { |
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// close database
|
|
|
|
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
// be sure to dispose c++ pointers
|
|
|
|
|
|
|
|
options.dispose(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|