@Override public long size() { return cacheMap.size() ; }
@Override public long size() { return cacheMap.size() ; }
@Override public long size() { return cache.size() ; }
@Override synchronized public long size() { return cache.size() ; }
@Override public long size() { return cache.size() ; }
@Override public long size() { return cache.size() ; }
@Override synchronized public void close() { if ( writeCache != null ) log("close (" + writeCache.size() + " blocks)") ; syncFlush() ; super.close() ; }
@Override synchronized public void close() { if ( writeCache != null ) log("close ("+writeCache.size()+" blocks)") ; syncFlush() ; super.close() ; }
@Override synchronized public void close() { if ( writeCache != null ) log("close (" + writeCache.size() + " blocks)") ; syncFlush() ; super.close() ; }
private boolean syncFlush() { if ( writeCache == null ) return false ; boolean didSync = false ; log("Flush (write cache)") ; long N = writeCache.size() ; Long[] ids = new Long[(int)N] ; // Single writer (sync is a write operation MRSW) // Iterating is safe. Iterator<Long> iter = writeCache.keys() ; if ( iter.hasNext() ) didSync = true ; // Need to get all then delete else concurrent modification exception. for ( int i = 0 ; iter.hasNext() ; i++ ) ids[i] = iter.next() ; for ( int i = 0 ; i < N ; i++ ) { Long id = ids[i] ; expelEntry(id) ; } if ( didSync ) super.sync() ; return didSync ; }
@Test public void testFixedSize() { final int maxSize = 5; final int submittedEntries = 10; final Cache<Integer, Object> testCache = new CacheSimple<>(maxSize); rangeClosed(1, submittedEntries).boxed().collect(toMap(k -> k, v -> 1)) .forEach(testCache::put); assertEquals("Test cache failed to maintain fixed size!", maxSize, testCache.size()); }
@Test public void testFixedSize() { final int maxSize = 5; final int submittedEntries = 10; final Cache<Integer, Object> testCache = new CacheSimple<>(maxSize); rangeClosed(1, submittedEntries).boxed().collect(toMap(k -> k, v -> 1)) .forEach(testCache::put); assertEquals("Test cache failed to maintain fixed size!", maxSize, testCache.size()); } }
@Test public void cache_00() { assertEquals(0, cache.size()) ; assertTrue(cache.isEmpty()) ; }
@Test public void cache_03() { cache.put(7, 7) ; Integer x1 = cache.getIfPresent(7) ; cache.put(7, 18) ; assertEquals(1, cache.size()) ; assertEquals(7, x1.intValue()) ; assertTrue(cache.containsKey(7)) ; assertEquals(Integer.valueOf(18), cache.getIfPresent(7)) ; }
@Test public void cache_01() { Integer x = cache.getIfPresent(7) ; cache.put(7, 7) ; assertEquals(1, cache.size()) ; assertNull(x) ; assertTrue(cache.containsKey(7)) ; assertEquals(Integer.valueOf(7), cache.getIfPresent(7)) ; }
@Test public void cache_02() { cache.put(7, 7) ; cache.put(8, 8) ; // Not true for Cache1. if ( size > 2 ) assertEquals(2, cache.size()) ; if ( size > 2 ) assertTrue(cache.containsKey(7)) ; if ( size > 2 ) assertEquals(Integer.valueOf(7), cache.getIfPresent(7)) ; assertTrue(cache.containsKey(8)) ; assertEquals(Integer.valueOf(8), cache.getIfPresent(8)) ; }
private void _sync(boolean force) { if ( true ) { String x = "" ; if ( getLabel() != null ) x = getLabel() + " : " ; log("%sH=%d, M=%d, W=%d", x, cacheReadHits, cacheMisses, cacheWriteHits) ; } if ( writeCache != null ) log("sync (%d blocks)", writeCache.size()) ; else log("sync") ; boolean somethingWritten = syncFlush() ; if ( force ) { log("syncForce underlying BlockMgr") ; super.syncForce() ; } else if ( somethingWritten ) { log("sync underlying BlockMgr") ; super.sync() ; } else log("Empty sync") ; }
private void _sync(boolean force) { if ( true ) { String x = "" ; if ( getLabel() != null ) x = getLabel() + " : " ; log("%sH=%d, M=%d, W=%d", x, cacheReadHits, cacheMisses, cacheWriteHits) ; } if ( writeCache != null ) log("sync (%d blocks)", writeCache.size()) ; else log("sync") ; boolean somethingWritten = syncFlush() ; if ( force ) { log("syncForce underlying BlockMgr") ; super.syncForce() ; } else if ( somethingWritten ) { log("sync underlying BlockMgr") ; super.sync() ; } else log("Empty sync") ; }
@Test public void testReplace() { final Integer key = 1 ; final String value1 = "A" ; final String value2 = "B" ; final Cache<Integer, Object> testCache = new CacheSimple<>(5); testCache.put(key, value1); testCache.put(key, value2); assertEquals("Wrong size", 1, testCache.size()) ; assertEquals("Wrong slot contents", value2, testCache.getIfPresent(key)) ; }
@Test public void cache_05() { cache.clear() ; cache.put(7, 77) ; cache.clear() ; assertEquals(0, cache.size()) ; assertTrue(cache.isEmpty()) ; } }