use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.
the class DataSourceWithCache method get.
@Override
public byte[] get(byte[] key) {
Objects.requireNonNull(key);
boolean traceEnabled = logger.isTraceEnabled();
ByteArrayWrapper wrappedKey = ByteUtil.wrap(key);
byte[] value;
this.lock.readLock().lock();
try {
if (committedCache.containsKey(wrappedKey)) {
return committedCache.get(wrappedKey);
}
if (uncommittedCache.containsKey(wrappedKey)) {
return uncommittedCache.get(wrappedKey);
}
value = base.get(key);
if (traceEnabled) {
numOfGetsFromStore.incrementAndGet();
}
// null value, as expected, is allowed here to be stored in committedCache
committedCache.put(wrappedKey, value);
} finally {
if (traceEnabled) {
numOfGets.incrementAndGet();
}
this.lock.readLock().unlock();
}
return value;
}
use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.
the class KeyValueDataSourceTest method updateBatchWithNulls.
@Test(expected = IllegalArgumentException.class)
public void updateBatchWithNulls() {
Map<ByteArrayWrapper, byte[]> updatedValues = generateRandomValuesToUpdate(CACHE_SIZE);
ByteArrayWrapper keyToNull = updatedValues.keySet().iterator().next();
updatedValues.put(keyToNull, null);
keyValueDataSource.updateBatch(updatedValues, Collections.emptySet());
}
use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.
the class DataSourceWithCacheTest method getWithFullCache.
/**
* Note: we cannot exhaustively verify baseDataSource#get access b/c on flush all the uncommittedCache is dumped
* into the underlying layer and it's impossible to establish which entries stayed in the committedCache due to the
* {@link java.util.LinkedHashMap#putAll(Map)} eviction semantic
*/
@Test
public void getWithFullCache() {
int expectedMisses = 1;
Map<ByteArrayWrapper, byte[]> initialEntries = generateRandomValuesToUpdate(CACHE_SIZE + expectedMisses);
dataSourceWithCache.updateBatch(initialEntries, Collections.emptySet());
dataSourceWithCache.flush();
for (ByteArrayWrapper key : initialEntries.keySet()) {
assertThat(dataSourceWithCache.get(key.getData()), is(initialEntries.get(key)));
}
verify(baseDataSource, atLeast(expectedMisses)).get(any(byte[].class));
}
use of org.ethereum.db.ByteArrayWrapper in project nuls by nuls-io.
the class ProgramExecutorImpl method getAccount.
public ProgramAccount getAccount(byte[] address) {
ByteArrayWrapper addressWrapper = new ByteArrayWrapper(address);
ProgramAccount account = accounts.get(addressWrapper);
if (account == null) {
BigInteger balance = getBalance(address, blockNumber);
account = new ProgramAccount(address, balance);
accounts.put(addressWrapper, account);
}
return account;
}
use of org.ethereum.db.ByteArrayWrapper in project rskj by rsksmart.
the class DataSourceWithCacheTest method updateBatch.
@Test
public void updateBatch() {
Map<ByteArrayWrapper, byte[]> initialEntries = generateRandomValuesToUpdate(CACHE_SIZE);
baseDataSource.updateBatch(initialEntries, Collections.emptySet());
Set<ByteArrayWrapper> keysToBatchRemove = initialEntries.keySet().stream().limit(CACHE_SIZE / 2).collect(Collectors.toSet());
dataSourceWithCache.updateBatch(Collections.emptyMap(), keysToBatchRemove);
dataSourceWithCache.flush();
for (ByteArrayWrapper removedKey : keysToBatchRemove) {
assertThat(baseDataSource.get(removedKey.getData()), is(nullValue()));
}
}
Aggregations