@Override public Collection<Value> values() { return new ValueCollection( new SetView<Map.Entry<?, Value>>( elements, actualSize ) ); }
@Override public Set<Key> keySet() { return new KeySetView<Key>( new SetView<Map.Entry<Key, ?>>( elements, actualSize ) ); }
@Override public CascadingCache create( FlowProcess flowProcess ) { return new DirectMappedCache(); }
private CascadingCache<Object, Object> getDirectMappedCache( int capacity, CacheEvictionCallback cacheEvictionCallback ) { CascadingCache<Object, Object> map = new DirectMappedCache<Object, Object>(); map.setCacheEvictionCallback( cacheEvictionCallback ); map.setCapacity( capacity ); map.initialize(); return map; }
@Override protected boolean removeEldestEntry( Map.Entry<Key, Value> eldest ) { boolean doRemove = size() > capacity; if( doRemove ) { callback.evict( eldest ); if( flushes % getCapacity() == 0 ) // every multiple, write out data { Runtime runtime = Runtime.getRuntime(); long freeMem = runtime.freeMemory() / 1024 / 1024; long maxMem = runtime.maxMemory() / 1024 / 1024; long totalMem = runtime.totalMemory() / 1024 / 1024; LOG.info( "flushed keys num times: {}, with capacity: {}", flushes + 1, capacity ); LOG.info( "mem on flush (mb), free: " + freeMem + ", total: " + totalMem + ", max: " + maxMem ); float percent = (float) totalMem / (float) maxMem; if( percent < 0.80F ) LOG.info( "total mem is {}% of max mem, to better utilize unused memory consider increasing the cache size", (int) ( percent * 100.0F ) ); } flushes++; } return doRemove; } };
@Override protected boolean removeEldestEntry( Map.Entry<Key, Value> eldest ) { boolean doRemove = size() > capacity; if( doRemove ) { callback.evict( eldest ); if( flushes % getCapacity() == 0 ) // every multiple, write out data { Runtime runtime = Runtime.getRuntime(); long freeMem = runtime.freeMemory() / 1024 / 1024; long maxMem = runtime.maxMemory() / 1024 / 1024; long totalMem = runtime.totalMemory() / 1024 / 1024; LOG.info( "flushed keys num times: {}, with capacity: {}", flushes + 1, capacity ); LOG.info( "mem on flush (mb), free: " + freeMem + ", total: " + totalMem + ", max: " + maxMem ); float percent = (float) totalMem / (float) maxMem; if( percent < 0.80F ) LOG.info( "total mem is {}% of max mem, to better utilize unused memory consider increasing the cache size", (int) ( percent * 100.0F ) ); } flushes++; } return doRemove; } };
@Override public Value remove( Object key ) { if( key == null ) throw new IllegalArgumentException( "key cannot be null" ); int index = index( key ); Entry<Key, Value> existing = elements[ index ]; if( existing == null || !existing.getKey().equals( key ) ) return null; elements[ index ] = null; actualSize--; evictionCallBack.evict( existing ); return existing.getValue(); }
@Test(expected = IllegalArgumentException.class) public void testContainsKeyNull() { CascadingCache<Object, Object> cache = getDirectMappedCache( 10, CacheEvictionCallback.NULL ); cache.containsKey( null ); }
public void reset() { currentIndex = -1; nextElement = findNext(); } }
SetView( T[] elements, int size ) { this.elements = elements; this.size = size; this.iter = new Iter<T>( elements ); }
@Override public CascadingCache create( FlowProcess flowProcess ) { return new LRUHashMapCache(); } }
@Override public CascadingCache create( FlowProcess flowProcess ) { return new OrderedHashMapCache(); }
@Override public Set<Map.Entry<Key, Value>> entrySet() { return new SetView<Entry<Key, Value>>( elements, actualSize ); }
@Override public Iterator<Value> iterator() { return new ValueIterator<Value>( parent.iterator() ); }
@Override public Iterator<Key> iterator() { return new KeyIterator<Key>( parent.iterator() ); }
@Override public boolean containsKey( Object key ) { if( key == null ) throw new IllegalArgumentException( "null keys are not permitted" ); return get( key ) != null; }
@Override public Value get( Object key ) { int index = index( key ); Entry<Key, Value> existing = elements[ index ]; if( existing == null || !key.equals( existing.getKey() ) ) return null; return existing.getValue(); }
@Test(expected = IllegalArgumentException.class) public void testContainsValueNull() { CascadingCache<Object, Object> cache = getDirectMappedCache( 10, CacheEvictionCallback.NULL ); cache.containsValue( null ); }
public Iter( T[] elements ) { this.elements = elements; this.nextElement = findNext(); }