Search in sources :

Example 1 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class Cache method put.

/**
 * Put the node in the cache if RLP encoded value is longer than 32 bytes
 *
 * @param o
 *         the Node which could be a pair-, multi-item Node or single
 *         Value
 * @return keccak hash of RLP encoded node if length > 32 otherwise
 * return node itself
 */
public synchronized Object put(Object o) {
    Value value = new Value(o);
    byte[] enc = value.encode();
    if (enc.length >= 32) {
        byte[] sha = HashUtil.h256(value.encode());
        ByteArrayWrapper key = wrap(sha);
        this.nodes.put(key, new Node(value, true));
        this.removedNodes.remove(key);
        this.isDirty = true;
        return sha;
    }
    return value;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) Value(org.aion.rlp.Value)

Example 2 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class Cache method markRemoved.

public synchronized void markRemoved(byte[] key) {
    ByteArrayWrapper keyW = new ByteArrayWrapper(key);
    removedNodes.add(keyW);
    nodes.remove(keyW);
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 3 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class Cache method get.

public synchronized Value get(byte[] key) {
    ByteArrayWrapper wrappedKey = wrap(key);
    Node node = nodes.get(wrappedKey);
    if (node != null) {
        // cachehits++;
        return node.getValue();
    }
    if (this.dataSource != null) {
        Optional<byte[]> data = (this.dataSource == null) ? Optional.empty() : this.dataSource.get(key);
        if (data.isPresent()) {
            // dbhits++;
            Value val = fromRlpEncoded(data.get());
            nodes.put(wrappedKey, new Node(val, false));
            return val;
        }
    }
    return null;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) Value(org.aion.rlp.Value)

Example 4 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class Cache method commit.

public synchronized void commit(boolean flushCache) {
    // Don't try to commit if it isn't dirty
    if ((dataSource == null) || !this.isDirty) {
        // clear cache when flush requested
        if (flushCache) {
            this.nodes.clear();
        }
        return;
    }
    // long start = System.nanoTime();
    // int batchMemorySize = 0;
    Map<byte[], byte[]> batch = new HashMap<>();
    for (ByteArrayWrapper nodeKey : this.nodes.keySet()) {
        Node node = this.nodes.get(nodeKey);
        if (node == null || node.isDirty()) {
            byte[] value;
            if (node != null) {
                node.setDirty(false);
                value = node.getValue().encode();
            } else {
                value = null;
            }
            byte[] key = nodeKey.getData();
            batch.put(key, value);
        // batchMemorySize += length(key, value);
        }
    }
    /* for (ByteArrayWrapper removedNode : removedNodes) {
            batch.put(removedNode.getData(), null);
        } */
    this.dataSource.putBatch(batch);
    this.isDirty = false;
    if (flushCache) {
        this.nodes.clear();
    }
    this.removedNodes.clear();
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class JournalPruneDataSource method put.

/**
 * ***** updates ******
 */
public synchronized void put(byte[] key, byte[] value) {
    ByteArrayWrapper keyW = new ByteArrayWrapper(key);
    // Check to see the value exists.
    if (value != null) {
        // If it exists and pruning is enabled.
        if (enabled) {
            currentUpdates.insertedKeys.add(keyW);
            incRef(keyW);
        }
        // Insert into the database.
        src.put(key, value);
    } else {
        // Value does not exist, so we delete from current updates
        if (enabled) {
            currentUpdates.deletedKeys.add(keyW);
        }
    // TODO: Do we delete the key?
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)45 BigInteger (java.math.BigInteger)10 Address (org.aion.base.type.Address)5 ITransaction (org.aion.base.type.ITransaction)4 Entry (java.util.Map.Entry)3 DataWord (org.aion.mcf.vm.types.DataWord)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 Test (org.junit.Test)3 java.util (java.util)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ImportResult (org.aion.mcf.core.ImportResult)2 Value (org.aion.rlp.Value)2 AccountState (org.aion.txpool.common.AccountState)2 TxDependList (org.aion.txpool.common.TxDependList)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2