Search in sources :

Example 6 with ByteArrayWrapper

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;
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 7 with ByteArrayWrapper

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());
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper)

Example 8 with ByteArrayWrapper

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));
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper) Test(org.junit.Test)

Example 9 with ByteArrayWrapper

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;
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Example 10 with ByteArrayWrapper

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()));
    }
}
Also used : ByteArrayWrapper(org.ethereum.db.ByteArrayWrapper) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.ethereum.db.ByteArrayWrapper)23 Test (org.junit.Test)5 Metric (co.rsk.metrics.profilers.Metric)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 DataWord (org.ethereum.vm.DataWord)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 Path (java.nio.file.Path)1 DigestOutputStream (java.security.DigestOutputStream)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MapSnapshot (org.ethereum.util.MapSnapshot)1