RAMFreenetStore.java

Index Score
freenet.store
Freenet

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
package freenet.store; import java.io.IOException; import java.util.Arrays; import com.sleepycat.je.DatabaseException; import freenet.keys.KeyVerifyException; import freenet.support.ByteArrayWrapper; import freenet.support.LRUHashtable; public class RAMFreenetStore implements FreenetStore { private final static class Block { byte[] header; byte[] data; byte[] fullKey; } private final LRUHashtable<ByteArrayWrapper, Block> blocksByRoutingKey; private final StoreCallback callback; private int maxKeys; private long hits; private long misses; private long writes; public RAMFreenetStore(StoreCallback callback, int maxKeys) { this.callback = callback; this.blocksByRoutingKey = new LRUHashtable<ByteArrayWrapper, Block>(); this.maxKeys = maxKeys; callback.setStore(this); } public synchronized StorableBlock fetch(byte[] routingKey, byte[] fullKey, boolean dontPromote) throws IOException { ByteArrayWrapper key = new ByteArrayWrapper(routingKey); Block block = blocksByRoutingKey.get(key); if(block == null) { misses++; return null; } try { StorableBlock ret = callback.construct(block.data, block.header, routingKey, block.fullKey); hits++; if(!dontPromote) blocksByRoutingKey.push(key, block); return ret; } catch (KeyVerifyException e) { blocksByRoutingKey.removeKey(key); misses++; return null; } } public synchronized long getMaxKeys() { return (long) maxKeys; } public synchronized long hits() { return hits; } public synchronized long keyCount() { return blocksByRoutingKey.size(); } public synchronized long misses() { return misses; } public synchronized void put(StorableBlock block, byte[] routingkey, byte[] fullKey, byte[] data, byte[] header, boolean overwrite) throws IOException, KeyCollisionException { writes++; ByteArrayWrapper key = new ByteArrayWrapper(routingkey); Block oldBlock = blocksByRoutingKey.get(key); boolean storeFullKeys = callback.storeFullKeys(); if(oldBlock != null) { if(callback.collisionPossible()) { boolean equals = Arrays.equals(oldBlock.data, data) && Arrays.equals(oldBlock.header, header) && (storeFullKeys ? Arrays.equals(oldBlock.fullKey, fullKey) : true); if(equals) return; if(overwrite) { oldBlock.data = data; oldBlock.header = header; if(storeFullKeys) oldBlock.fullKey = fullKey; } else { throw new KeyCollisionException(); } return; } else { return; } } Block storeBlock = new Block(); storeBlock.data = data; storeBlock.header = header; if(storeFullKeys) storeBlock.fullKey = fullKey; blocksByRoutingKey.push(key, storeBlock); while(blocksByRoutingKey.size() > maxKeys) { blocksByRoutingKey.popKey(); } } public synchronized void setMaxKeys(long maxStoreKeys, boolean shrinkNow) throws DatabaseException, IOException { this.maxKeys = (int)Math.min(Integer.MAX_VALUE, maxStoreKeys); // Always shrink now regardless of parameter as we will shrink on the next put() anyway. while(blocksByRoutingKey.size() > maxKeys) { blocksByRoutingKey.popKey(); } } public long writes() { return writes; } }

The table below shows all metrics for RAMFreenetStore.java.

MetricValueDescription