Simplify a test case in Java ReadOnlyTest (#7608)

Summary:
The original test nests a lot of `try` blocks. This PR flattens these blocks into independent blocks, so that each `try` block closes the DB before opening the next DB instance.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7608

Test Plan: watch the existing java tests to pass

Reviewed By: zhichao-cao

Differential Revision: D24611621

Pulled By: cheng-chang

fbshipit-source-id: d486c5d37ac25d4b860d739ef2cdd58e6064d42d
This commit is contained in:
cheng-chang 2020-11-04 16:47:42 -08:00 committed by Facebook GitHub Bot
parent c9c9709a1a
commit 1f627210ca

View File

@ -31,115 +31,60 @@ public class ReadOnlyTest {
final RocksDB db = RocksDB.open(options, final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) { dbFolder.getRoot().getAbsolutePath())) {
db.put("key".getBytes(), "value".getBytes()); db.put("key".getBytes(), "value".getBytes());
try (final RocksDB db2 = RocksDB.openReadOnly( }
dbFolder.getRoot().getAbsolutePath())) { try (final RocksDB db = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath())) {
assertThat("value"). assertThat("value").isEqualTo(new String(db.get("key".getBytes())));
isEqualTo(new String(db2.get("key".getBytes())));
}
} }
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); final List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor( cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>(); final List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<>();
try (final RocksDB db = RocksDB.open(dbFolder.getRoot().getAbsolutePath(), try (final RocksDB db = RocksDB.open(
cfDescriptors, columnFamilyHandleList)) { dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) {
try (final ColumnFamilyOptions newCfOpts = new ColumnFamilyOptions(); columnFamilyHandleList.add(
final ColumnFamilyOptions newCf2Opts = new ColumnFamilyOptions() db.createColumnFamily(new ColumnFamilyDescriptor("new_cf".getBytes(), cfOpts)));
) { columnFamilyHandleList.add(
columnFamilyHandleList.add(db.createColumnFamily( db.createColumnFamily(new ColumnFamilyDescriptor("new_cf2".getBytes(), cfOpts)));
new ColumnFamilyDescriptor("new_cf".getBytes(), newCfOpts))); db.put(columnFamilyHandleList.get(2), "key2".getBytes(), "value2".getBytes());
columnFamilyHandleList.add(db.createColumnFamily( }
new ColumnFamilyDescriptor("new_cf2".getBytes(), newCf2Opts)));
db.put(columnFamilyHandleList.get(2), "key2".getBytes(),
"value2".getBytes());
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = columnFamilyHandleList.clear();
new ArrayList<>(); try (final RocksDB db = RocksDB.openReadOnly(
try (final RocksDB db2 = RocksDB.openReadOnly( dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) {
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, assertThat(db.get("key2".getBytes())).isNull();
readOnlyColumnFamilyHandleList)) { assertThat(db.get(columnFamilyHandleList.get(0), "key2".getBytes())).isNull();
try (final ColumnFamilyOptions newCfOpts2 = }
new ColumnFamilyOptions();
final ColumnFamilyOptions newCf2Opts2 =
new ColumnFamilyOptions()
) {
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,
newCfOpts2));
cfDescriptors.add(new ColumnFamilyDescriptor("new_cf2".getBytes(),
newCf2Opts2));
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList2 cfDescriptors.clear();
= new ArrayList<>(); cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
try (final RocksDB db3 = RocksDB.openReadOnly( cfDescriptors.add(new ColumnFamilyDescriptor("new_cf2".getBytes(), cfOpts));
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList.clear();
readOnlyColumnFamilyHandleList2)) { try (final RocksDB db = RocksDB.openReadOnly(
try { dbFolder.getRoot().getAbsolutePath(), cfDescriptors, columnFamilyHandleList)) {
assertThat(new String(db3.get( assertThat(new String(db.get(columnFamilyHandleList.get(1), "key2".getBytes())))
readOnlyColumnFamilyHandleList2.get(1), .isEqualTo("value2");
"key2".getBytes()))).isEqualTo("value2");
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList2) {
columnFamilyHandle.close();
}
}
}
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
}
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
columnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@Test(expected = RocksDBException.class) @Test(expected = RocksDBException.class)
public void failToWriteInReadOnly() throws RocksDBException { public void failToWriteInReadOnly() throws RocksDBException {
try (final Options options = new Options() try (final Options options = new Options().setCreateIfMissing(true)) {
.setCreateIfMissing(true)) { try (final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
// no-op
try (final RocksDB db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath())) {
//no-op
} }
} }
try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) { try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()) {
final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( final List<ColumnFamilyDescriptor> cfDescriptors =
new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts) Arrays.asList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
);
final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = final List<ColumnFamilyHandle> readOnlyColumnFamilyHandleList = new ArrayList<>();
new ArrayList<>(); try (final RocksDB rDb = RocksDB.openReadOnly(dbFolder.getRoot().getAbsolutePath(),
try (final RocksDB rDb = RocksDB.openReadOnly( cfDescriptors, readOnlyColumnFamilyHandleList)) {
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, // test that put fails in readonly mode
readOnlyColumnFamilyHandleList)) { rDb.put("key".getBytes(), "value".getBytes());
try {
// test that put fails in readonly mode
rDb.put("key".getBytes(), "value".getBytes());
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -161,15 +106,7 @@ public class ReadOnlyTest {
try (final RocksDB rDb = RocksDB.openReadOnly( try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList)) { readOnlyColumnFamilyHandleList)) {
try { rDb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(), "value".getBytes());
rDb.put(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes(), "value".getBytes());
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -193,14 +130,7 @@ public class ReadOnlyTest {
try (final RocksDB rDb = RocksDB.openReadOnly( try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList)) { readOnlyColumnFamilyHandleList)) {
try { rDb.delete("key".getBytes());
rDb.delete("key".getBytes());
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -223,15 +153,8 @@ public class ReadOnlyTest {
try (final RocksDB rDb = RocksDB.openReadOnly( try (final RocksDB rDb = RocksDB.openReadOnly(
dbFolder.getRoot().getAbsolutePath(), cfDescriptors, dbFolder.getRoot().getAbsolutePath(), cfDescriptors,
readOnlyColumnFamilyHandleList)) { readOnlyColumnFamilyHandleList)) {
try {
rDb.delete(readOnlyColumnFamilyHandleList.get(0), rDb.delete(readOnlyColumnFamilyHandleList.get(0),
"key".getBytes()); "key".getBytes());
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -256,15 +179,8 @@ public class ReadOnlyTest {
readOnlyColumnFamilyHandleList); readOnlyColumnFamilyHandleList);
final WriteBatch wb = new WriteBatch(); final WriteBatch wb = new WriteBatch();
final WriteOptions wOpts = new WriteOptions()) { final WriteOptions wOpts = new WriteOptions()) {
try {
wb.put("key".getBytes(), "value".getBytes()); wb.put("key".getBytes(), "value".getBytes());
rDb.write(wOpts, wb); rDb.write(wOpts, wb);
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -289,16 +205,9 @@ public class ReadOnlyTest {
readOnlyColumnFamilyHandleList); readOnlyColumnFamilyHandleList);
final WriteBatch wb = new WriteBatch(); final WriteBatch wb = new WriteBatch();
final WriteOptions wOpts = new WriteOptions()) { final WriteOptions wOpts = new WriteOptions()) {
try {
wb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(), wb.put(readOnlyColumnFamilyHandleList.get(0), "key".getBytes(),
"value".getBytes()); "value".getBytes());
rDb.write(wOpts, wb); rDb.write(wOpts, wb);
} finally {
for (final ColumnFamilyHandle columnFamilyHandle :
readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }
@ -318,14 +227,7 @@ public class ReadOnlyTest {
try (final DBOptions options = new DBOptions(); try (final DBOptions options = new DBOptions();
final RocksDB rDb = RocksDB.openReadOnly(options, dbFolder.getRoot().getAbsolutePath(), final RocksDB rDb = RocksDB.openReadOnly(options, dbFolder.getRoot().getAbsolutePath(),
cfDescriptors, readOnlyColumnFamilyHandleList, true);) { cfDescriptors, readOnlyColumnFamilyHandleList, true);) {
try { // no-op... should have raised an error as errorIfWalFileExists=true
// no-op... should have raised an error as errorIfWalFileExists=true
} finally {
for (final ColumnFamilyHandle columnFamilyHandle : readOnlyColumnFamilyHandleList) {
columnFamilyHandle.close();
}
}
} }
} }
} }