Removing unused methods on KeyValueStorage (#1661)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
This commit is contained in:
CJ Hare
2019-07-11 11:04:42 +10:00
committed by GitHub
parent d6987fa06b
commit e43cee30a9
4 changed files with 85 additions and 124 deletions

View File

@@ -31,16 +31,19 @@ public class InMemoryKeyValueStorage implements KeyValueStorage {
private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
@Override
public Optional<BytesValue> 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 class InMemoryKeyValueStorage implements KeyValueStorage {
}
@Override
public Transaction startTransaction() {
return new InMemoryTransaction();
public Optional<BytesValue> 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 class InMemoryKeyValueStorage implements KeyValueStorage {
}
@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<BytesValue> keySet() {
return Collections.unmodifiableSet(new HashSet<>(hashValueStore.keySet()));
}
@Override
public void close() {}
private class InMemoryTransaction extends AbstractTransaction {
private Map<BytesValue, BytesValue> updatedValues = new HashMap<>();

View File

@@ -17,22 +17,25 @@ import static com.google.common.base.Preconditions.checkState;
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<BytesValue> get(BytesValue key) throws StorageException;
default boolean containsKey(final BytesValue key) throws StorageException {
return get(key).isPresent();
}
long removeUnless(Predicate<BytesValue> inUseCheck);
/**
* Begins a transaction. Returns a transaction object that can be updated and committed.
@@ -41,50 +44,6 @@ public interface KeyValueStorage extends Closeable {
*/
Transaction startTransaction() throws StorageException;
long removeUnless(Predicate<BytesValue> 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);

View File

@@ -73,49 +73,6 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable {
}
}
private BlockBasedTableConfig createBlockBasedTableConfig(final RocksDbConfiguration config) {
final LRUCache cache = new LRUCache(config.getCacheCapacity());
return new BlockBasedTableConfig().setBlockCache(cache);
}
@Override
public Optional<BytesValue> get(final BytesValue key) throws StorageException {
throwIfClosed();
try (final OperationTimer.TimingContext ignored =
rocksDBMetricsHelper.getReadLatency().startTimer()) {
return Optional.ofNullable(db.get(key.getArrayUnsafe())).map(BytesValue::wrap);
} catch (final RocksDBException e) {
throw new StorageException(e);
}
}
@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<BytesValue> inUseCheck) throws StorageException {
long removedNodeCounter = 0;
try (final RocksIterator rocksIterator = db.newIterator()) {
rocksIterator.seekToFirst();
while (rocksIterator.isValid()) {
final byte[] key = rocksIterator.key();
if (!inUseCheck.test(BytesValue.wrap(key))) {
removedNodeCounter++;
db.delete(key);
}
rocksIterator.next();
}
} catch (final RocksDBException e) {
throw new StorageException(e);
}
return removedNodeCounter;
}
@Override
public void clear() {
try (final RocksIterator rocksIterator = db.newIterator()) {
@@ -143,6 +100,49 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable {
}
}
@Override
public Optional<BytesValue> get(final BytesValue key) throws StorageException {
throwIfClosed();
try (final OperationTimer.TimingContext ignored =
rocksDBMetricsHelper.getReadLatency().startTimer()) {
return Optional.ofNullable(db.get(key.getArrayUnsafe())).map(BytesValue::wrap);
} catch (final RocksDBException e) {
throw new StorageException(e);
}
}
@Override
public long removeUnless(final Predicate<BytesValue> inUseCheck) throws StorageException {
long removedNodeCounter = 0;
try (final RocksIterator rocksIterator = db.newIterator()) {
rocksIterator.seekToFirst();
while (rocksIterator.isValid()) {
final byte[] key = rocksIterator.key();
if (!inUseCheck.test(BytesValue.wrap(key))) {
removedNodeCounter++;
db.delete(key);
}
rocksIterator.next();
}
} catch (final RocksDBException e) {
throw new StorageException(e);
}
return removedNodeCounter;
}
@Override
public Transaction startTransaction() throws StorageException {
throwIfClosed();
final WriteOptions options = new WriteOptions();
return new RocksDbTransaction(db.beginTransaction(options), options);
}
private BlockBasedTableConfig createBlockBasedTableConfig(final RocksDbConfiguration config) {
final LRUCache cache = new LRUCache(config.getCacheCapacity());
return new BlockBasedTableConfig().setBlockCache(cache);
}
private void throwIfClosed() {
if (closed.get()) {
LOG.error("Attempting to use a closed RocksDbKeyValueStorage");
@@ -151,6 +151,7 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable {
}
private class RocksDbTransaction extends AbstractTransaction {
private final org.rocksdb.Transaction innerTx;
private final WriteOptions options;

View File

@@ -20,6 +20,7 @@ import java.util.Optional;
import java.util.function.Predicate;
public class SegmentedKeyValueStorageAdapter<S> implements KeyValueStorage {
private final S segmentHandle;
private final SegmentedKeyValueStorage<S> storage;
@@ -30,8 +31,13 @@ public class SegmentedKeyValueStorageAdapter<S> implements KeyValueStorage {
}
@Override
public Optional<BytesValue> 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 class SegmentedKeyValueStorageAdapter<S> implements KeyValueStorage {
return storage.containsKey(segmentHandle, key);
}
@Override
public Optional<BytesValue> get(final BytesValue key) throws StorageException {
return storage.get(segmentHandle, key);
}
@Override
public long removeUnless(final Predicate<BytesValue> inUseCheck) {
return storage.removeUnless(segmentHandle, inUseCheck);
}
@Override
public Transaction startTransaction() throws StorageException {
final SegmentedKeyValueStorage.Transaction<S> transaction = storage.startTransaction();
@@ -64,19 +80,4 @@ public class SegmentedKeyValueStorageAdapter<S> implements KeyValueStorage {
}
};
}
@Override
public long removeUnless(final Predicate<BytesValue> inUseCheck) {
return storage.removeUnless(segmentHandle, inUseCheck);
}
@Override
public void clear() {
storage.clear(segmentHandle);
}
@Override
public void close() throws IOException {
storage.close();
}
}