diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java index 05fdd6b51c..61e68e8aef 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java @@ -31,16 +31,19 @@ public class InMemoryKeyValueStorage implements KeyValueStorage { private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); @Override - public Optional get(final BytesValue key) { - final Lock lock = rwLock.readLock(); + public void clear() { + final Lock lock = rwLock.writeLock(); lock.lock(); try { - return Optional.ofNullable(hashValueStore.get(key)); + hashValueStore.clear(); } finally { lock.unlock(); } } + @Override + public void close() {} + @Override public boolean containsKey(final BytesValue key) throws StorageException { final Lock lock = rwLock.readLock(); @@ -53,8 +56,14 @@ public boolean containsKey(final BytesValue key) throws StorageException { } @Override - public Transaction startTransaction() { - return new InMemoryTransaction(); + public Optional get(final BytesValue key) { + final Lock lock = rwLock.readLock(); + lock.lock(); + try { + return Optional.ofNullable(hashValueStore.get(key)); + } finally { + lock.unlock(); + } } @Override @@ -64,23 +73,14 @@ public long removeUnless(final Predicate inUseCheck) { } @Override - public void clear() { - final Lock lock = rwLock.writeLock(); - lock.lock(); - try { - hashValueStore.clear(); - } finally { - lock.unlock(); - } + public Transaction startTransaction() { + return new InMemoryTransaction(); } public Set keySet() { return Collections.unmodifiableSet(new HashSet<>(hashValueStore.keySet())); } - @Override - public void close() {} - private class InMemoryTransaction extends AbstractTransaction { private Map updatedValues = new HashMap<>(); diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java index d17c9ae072..221682a8bf 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java @@ -17,22 +17,25 @@ import tech.pegasys.pantheon.util.bytes.BytesValue; import java.io.Closeable; -import java.util.Objects; import java.util.Optional; import java.util.function.Predicate; /** Service provided by pantheon to facilitate persistent data storage. */ public interface KeyValueStorage extends Closeable { + void clear(); + + default boolean containsKey(final BytesValue key) throws StorageException { + return get(key).isPresent(); + } + /** * @param key Index into persistent data repository. * @return The value persisted at the key index. */ Optional get(BytesValue key) throws StorageException; - default boolean containsKey(final BytesValue key) throws StorageException { - return get(key).isPresent(); - } + long removeUnless(Predicate inUseCheck); /** * Begins a transaction. Returns a transaction object that can be updated and committed. @@ -41,50 +44,6 @@ default boolean containsKey(final BytesValue key) throws StorageException { */ Transaction startTransaction() throws StorageException; - long removeUnless(Predicate inUseCheck); - - void clear(); - - class Entry { - private final BytesValue key; - private final BytesValue value; - - private Entry(final BytesValue key, final BytesValue value) { - this.key = key; - this.value = value; - } - - public static Entry create(final BytesValue key, final BytesValue value) { - return new Entry(key, value); - } - - public BytesValue getKey() { - return key; - } - - public BytesValue getValue() { - return value; - } - - @Override - public boolean equals(final Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof Entry)) { - return false; - } - final Entry other = (Entry) obj; - return Objects.equals(getKey(), other.getKey()) - && Objects.equals(getValue(), other.getValue()); - } - - @Override - public int hashCode() { - return Objects.hash(key, value); - } - } - class StorageException extends RuntimeException { public StorageException(final Throwable t) { super(t); diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java index e767f45085..1e618e01d4 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java @@ -73,9 +73,31 @@ private RocksDbKeyValueStorage( } } - private BlockBasedTableConfig createBlockBasedTableConfig(final RocksDbConfiguration config) { - final LRUCache cache = new LRUCache(config.getCacheCapacity()); - return new BlockBasedTableConfig().setBlockCache(cache); + @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; + } + db.deleteRange(firstKey, rocksIterator.key()); + } catch (final RocksDBException e) { + throw new StorageException(e); + } + } + + @Override + public void close() { + if (closed.compareAndSet(false, true)) { + txOptions.close(); + options.close(); + db.close(); + } } @Override @@ -90,13 +112,6 @@ public Optional get(final BytesValue key) throws StorageException { } } - @Override - public Transaction startTransaction() throws StorageException { - throwIfClosed(); - final WriteOptions options = new WriteOptions(); - return new RocksDbTransaction(db.beginTransaction(options), options); - } - @Override public long removeUnless(final Predicate inUseCheck) throws StorageException { long removedNodeCounter = 0; @@ -117,30 +132,15 @@ public long removeUnless(final Predicate inUseCheck) throws StorageE } @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; - } - db.deleteRange(firstKey, rocksIterator.key()); - } catch (final RocksDBException e) { - throw new StorageException(e); - } + public Transaction startTransaction() throws StorageException { + throwIfClosed(); + final WriteOptions options = new WriteOptions(); + return new RocksDbTransaction(db.beginTransaction(options), options); } - @Override - public void close() { - if (closed.compareAndSet(false, true)) { - txOptions.close(); - options.close(); - db.close(); - } + private BlockBasedTableConfig createBlockBasedTableConfig(final RocksDbConfiguration config) { + final LRUCache cache = new LRUCache(config.getCacheCapacity()); + return new BlockBasedTableConfig().setBlockCache(cache); } private void throwIfClosed() { @@ -151,6 +151,7 @@ private void throwIfClosed() { } private class RocksDbTransaction extends AbstractTransaction { + private final org.rocksdb.Transaction innerTx; private final WriteOptions options; diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/SegmentedKeyValueStorageAdapter.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/SegmentedKeyValueStorageAdapter.java index 4241551b9e..fa54313ffa 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/SegmentedKeyValueStorageAdapter.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/SegmentedKeyValueStorageAdapter.java @@ -20,6 +20,7 @@ import java.util.function.Predicate; public class SegmentedKeyValueStorageAdapter implements KeyValueStorage { + private final S segmentHandle; private final SegmentedKeyValueStorage storage; @@ -30,8 +31,13 @@ public SegmentedKeyValueStorageAdapter( } @Override - public Optional get(final BytesValue key) throws StorageException { - return storage.get(segmentHandle, key); + public void clear() { + storage.clear(segmentHandle); + } + + @Override + public void close() throws IOException { + storage.close(); } @Override @@ -39,6 +45,16 @@ public boolean containsKey(final BytesValue key) throws StorageException { return storage.containsKey(segmentHandle, key); } + @Override + public Optional get(final BytesValue key) throws StorageException { + return storage.get(segmentHandle, key); + } + + @Override + public long removeUnless(final Predicate inUseCheck) { + return storage.removeUnless(segmentHandle, inUseCheck); + } + @Override public Transaction startTransaction() throws StorageException { final SegmentedKeyValueStorage.Transaction transaction = storage.startTransaction(); @@ -64,19 +80,4 @@ public void rollback() { } }; } - - @Override - public long removeUnless(final Predicate inUseCheck) { - return storage.removeUnless(segmentHandle, inUseCheck); - } - - @Override - public void clear() { - storage.clear(segmentHandle); - } - - @Override - public void close() throws IOException { - storage.close(); - } }