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