Fix rocksDB storage so that clear removes all values (#1894)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Jason Frame 5 years ago committed by GitHub
parent 1848945879
commit a047286a23
  1. 4
      services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/ColumnarRocksDbKeyValueStorage.java
  2. 17
      services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java
  3. 16
      services/kvstore/src/test/java/tech/pegasys/pantheon/services/kvstore/AbstractKeyValueStorageTest.java

@ -184,7 +184,9 @@ public class ColumnarRocksDbKeyValueStorage
final byte[] firstKey = rocksIterator.key();
rocksIterator.seekToLast();
if (rocksIterator.isValid()) {
db.deleteRange(segmentHandle, firstKey, rocksIterator.key());
final byte[] lastKey = rocksIterator.key();
db.deleteRange(segmentHandle, firstKey, lastKey);
db.delete(segmentHandle, lastKey);
}
}
} catch (final RocksDBException e) {

@ -14,6 +14,7 @@ package tech.pegasys.pantheon.services.kvstore;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.services.kvstore.SegmentedKeyValueStorage.StorageException;
import tech.pegasys.pantheon.services.util.RocksDbUtil;
import tech.pegasys.pantheon.util.bytes.BytesValue;
@ -76,16 +77,16 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable {
@Override
public void clear() {
try (final RocksIterator rocksIterator = db.newIterator()) {
if (!rocksIterator.isValid()) {
return;
}
rocksIterator.seekToFirst();
final byte[] firstKey = rocksIterator.key();
rocksIterator.seekToLast();
if (!rocksIterator.isValid()) {
return;
if (rocksIterator.isValid()) {
final byte[] firstKey = rocksIterator.key();
rocksIterator.seekToLast();
if (rocksIterator.isValid()) {
final byte[] lastKey = rocksIterator.key();
db.deleteRange(firstKey, lastKey);
db.delete(lastKey);
}
}
db.deleteRange(firstKey, rocksIterator.key());
} catch (final RocksDBException e) {
throw new StorageException(e);
}

@ -76,6 +76,22 @@ public abstract class AbstractKeyValueStorageTest {
assertThat(store.containsKey(BytesValue.fromHexString("12"))).isTrue();
}
@Test
public void clearRemovesAll() throws Exception {
final KeyValueStorage store = createStore();
Transaction tx = store.startTransaction();
tx.put(BytesValue.fromHexString("0F"), BytesValue.fromHexString("0ABC"));
tx.put(BytesValue.fromHexString("10"), BytesValue.fromHexString("0ABC"));
tx.put(BytesValue.fromHexString("11"), BytesValue.fromHexString("0ABC"));
tx.put(BytesValue.fromHexString("12"), BytesValue.fromHexString("0ABC"));
tx.commit();
store.clear();
assertThat(store.containsKey(BytesValue.fromHexString("0F"))).isFalse();
assertThat(store.containsKey(BytesValue.fromHexString("10"))).isFalse();
assertThat(store.containsKey(BytesValue.fromHexString("11"))).isFalse();
assertThat(store.containsKey(BytesValue.fromHexString("12"))).isFalse();
}
@Test
public void containsKey() throws Exception {
final KeyValueStorage store = createStore();

Loading…
Cancel
Save