|
|
|
@ -13,6 +13,8 @@ import org.rocksdb.*; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
public class ReadOnlyTest { |
|
|
|
|
|
|
|
|
|
@ClassRule |
|
|
|
@ -23,119 +25,204 @@ public class ReadOnlyTest { |
|
|
|
|
public TemporaryFolder dbFolder = new TemporaryFolder(); |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void shouldTestReadOnlyOpen() { |
|
|
|
|
RocksDB db = null, db2 = null, db3 = null; |
|
|
|
|
public void readOnlyOpen() throws RocksDBException { |
|
|
|
|
RocksDB db, db2, db3; |
|
|
|
|
List<ColumnFamilyHandle> columnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
List<ColumnFamilyHandle> db2ColumnFamilyHandleList = |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2 = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
List<ColumnFamilyHandle> db3ColumnFamilyHandleList = |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.put("key".getBytes(), "value".getBytes()); |
|
|
|
|
db2 = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
assertThat("value"). |
|
|
|
|
isEqualTo(new String(db2.get("key".getBytes()))); |
|
|
|
|
db.close(); |
|
|
|
|
db2.close(); |
|
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
|
|
|
|
|
db = RocksDB.open( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList); |
|
|
|
|
columnFamilyHandleList.add(db.createColumnFamily( |
|
|
|
|
new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions()))); |
|
|
|
|
columnFamilyHandleList.add(db.createColumnFamily( |
|
|
|
|
new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions()))); |
|
|
|
|
db.put(columnFamilyHandleList.get(2), "key2".getBytes(), |
|
|
|
|
"value2".getBytes()); |
|
|
|
|
|
|
|
|
|
db2 = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
assertThat(db2.get("key2".getBytes())).isNull(); |
|
|
|
|
assertThat(db2.get(readOnlyColumnFamilyHandleList.get(0), "key2".getBytes())). |
|
|
|
|
isNull(); |
|
|
|
|
|
|
|
|
|
cfDescriptors.clear(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions())); |
|
|
|
|
db3 = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList2); |
|
|
|
|
assertThat(new String(db3.get(readOnlyColumnFamilyHandleList2.get(1), |
|
|
|
|
"key2".getBytes()))).isEqualTo("value2"); |
|
|
|
|
db.close(); |
|
|
|
|
db2.close(); |
|
|
|
|
db3.close(); |
|
|
|
|
options.dispose(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToWriteInReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
try { |
|
|
|
|
db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.put("key".getBytes(), "value".getBytes()); |
|
|
|
|
db2 = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
assert("value".equals(new String(db2.get("key".getBytes())))); |
|
|
|
|
db.close(); |
|
|
|
|
db2.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNames = |
|
|
|
|
new ArrayList<ColumnFamilyDescriptor>(); |
|
|
|
|
cfNames.add(new ColumnFamilyDescriptor("default")); |
|
|
|
|
|
|
|
|
|
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath(), cfNames, columnFamilyHandleList); |
|
|
|
|
columnFamilyHandleList.add(db.createColumnFamily( |
|
|
|
|
new ColumnFamilyDescriptor("new_cf", new ColumnFamilyOptions()))); |
|
|
|
|
columnFamilyHandleList.add(db.createColumnFamily( |
|
|
|
|
new ColumnFamilyDescriptor("new_cf2", new ColumnFamilyOptions()))); |
|
|
|
|
db.put(columnFamilyHandleList.get(2), "key2".getBytes(), |
|
|
|
|
"value2".getBytes()); |
|
|
|
|
|
|
|
|
|
db2 = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfNames, db2ColumnFamilyHandleList); |
|
|
|
|
assert(db2.get("key2".getBytes())==null); |
|
|
|
|
assert(db2.get(columnFamilyHandleList.get(0), "key2".getBytes())==null); |
|
|
|
|
|
|
|
|
|
List<ColumnFamilyDescriptor> cfNewName = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
cfNewName.add(new ColumnFamilyDescriptor("default")); |
|
|
|
|
cfNewName.add(new ColumnFamilyDescriptor("new_cf2")); |
|
|
|
|
db3 = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath(), cfNewName, db3ColumnFamilyHandleList); |
|
|
|
|
assert(new String(db3.get(db3ColumnFamilyHandleList.get(1), |
|
|
|
|
"key2".getBytes())).equals("value2")); |
|
|
|
|
}catch (RocksDBException e){ |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
assert(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
// test that put fails in readonly mode
|
|
|
|
|
try { |
|
|
|
|
db2.put("key".getBytes(), "value".getBytes()); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
db3.put(db3ColumnFamilyHandleList.get(1), |
|
|
|
|
"key".getBytes(), "value".getBytes()); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
// test that remove fails in readonly mode
|
|
|
|
|
try { |
|
|
|
|
db2.remove("key".getBytes()); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
db3.remove(db3ColumnFamilyHandleList.get(1), |
|
|
|
|
"key".getBytes()); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
// test that write fails in readonly mode
|
|
|
|
|
rDb.put("key".getBytes(), "value".getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToCFWriteInReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
rDb.put(readOnlyColumnFamilyHandleList.get(0), |
|
|
|
|
"key".getBytes(), "value".getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToRemoveInReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
rDb.remove("key".getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToCFRemoveInReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
rDb.remove(readOnlyColumnFamilyHandleList.get(0), |
|
|
|
|
"key".getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToWriteBatchReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
|
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
WriteBatch wb = new WriteBatch(); |
|
|
|
|
wb.put("key".getBytes(), "value".getBytes()); |
|
|
|
|
try { |
|
|
|
|
db2.write(new WriteOptions(), wb); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
wb.dispose(); |
|
|
|
|
wb = new WriteBatch(); |
|
|
|
|
wb.put(db3ColumnFamilyHandleList.get(1), |
|
|
|
|
"key".getBytes(), "value".getBytes()); |
|
|
|
|
try { |
|
|
|
|
db3.write(new WriteOptions(), wb); |
|
|
|
|
assert(false); |
|
|
|
|
} catch (RocksDBException e) { |
|
|
|
|
assert(true); |
|
|
|
|
} |
|
|
|
|
wb.dispose(); |
|
|
|
|
// cleanup c++ pointers
|
|
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : |
|
|
|
|
columnFamilyHandleList) { |
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
} |
|
|
|
|
rDb.write(new WriteOptions(), wb); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test(expected = RocksDBException.class) |
|
|
|
|
public void failToCFWriteBatchReadOnly() throws RocksDBException { |
|
|
|
|
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); |
|
|
|
|
cfDescriptors.add( |
|
|
|
|
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, |
|
|
|
|
new ColumnFamilyOptions())); |
|
|
|
|
List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = |
|
|
|
|
new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
Options options = new Options(); |
|
|
|
|
options.setCreateIfMissing(true); |
|
|
|
|
|
|
|
|
|
RocksDB db = RocksDB.open(options, |
|
|
|
|
dbFolder.getRoot().getAbsolutePath()); |
|
|
|
|
db.close(); |
|
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : |
|
|
|
|
db2ColumnFamilyHandleList) { |
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
} |
|
|
|
|
db2.close(); |
|
|
|
|
for (ColumnFamilyHandle columnFamilyHandle : |
|
|
|
|
db3ColumnFamilyHandleList) { |
|
|
|
|
columnFamilyHandle.dispose(); |
|
|
|
|
} |
|
|
|
|
db3.close(); |
|
|
|
|
System.out.println("Passed ReadOnlyTest."); |
|
|
|
|
|
|
|
|
|
RocksDB rDb = RocksDB.openReadOnly( |
|
|
|
|
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, |
|
|
|
|
readOnlyColumnFamilyHandleList); |
|
|
|
|
|
|
|
|
|
WriteBatch wb = new WriteBatch(); |
|
|
|
|
wb.put(readOnlyColumnFamilyHandleList.get(0), |
|
|
|
|
"key".getBytes(), "value".getBytes()); |
|
|
|
|
rDb.write(new WriteOptions(), wb); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|