Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

AT- Removing unused methods on KeyValueStorage #1661

Merged
merged 5 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -53,8 +56,14 @@ public boolean containsKey(final BytesValue key) throws StorageException {
}

@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
Expand All @@ -64,23 +73,14 @@ public long removeUnless(final Predicate<BytesValue> 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<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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
Expand All @@ -41,50 +44,6 @@ default boolean containsKey(final BytesValue key) throws StorageException {
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,13 +112,6 @@ public Optional<BytesValue> 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<BytesValue> inUseCheck) throws StorageException {
long removedNodeCounter = 0;
Expand All @@ -117,30 +132,15 @@ public long removeUnless(final Predicate<BytesValue> 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() {
Expand All @@ -151,6 +151,7 @@ private void throwIfClosed() {
}

private class RocksDbTransaction extends AbstractTransaction {

private final org.rocksdb.Transaction innerTx;
private final WriteOptions options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Predicate;

public class SegmentedKeyValueStorageAdapter<S> implements KeyValueStorage {

private final S segmentHandle;
private final SegmentedKeyValueStorage<S> storage;

Expand All @@ -30,15 +31,30 @@ public SegmentedKeyValueStorageAdapter(
}

@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
public boolean containsKey(final BytesValue key) throws StorageException {
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();
Expand All @@ -64,19 +80,4 @@ public void rollback() {
}
};
}

@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();
}
}