/** Implementation of getOrFill based on Cache.get and Cache.put */ public static <K,V> V getOrFill(Cache<K,V> cache, K key, Callable<V> callable) { V value = cache.getIfPresent(key) ; if ( value == null ) { try { value = callable.call() ; } catch (Exception e) { throw new AtlasException("Exception on cache fill", e) ; } if ( value != null ) cache.put(key, value) ; } return value ; }
@Override public IRI resolveSilent(String uriStr) { if (resolvedIRIs != null && resolvedIRIs.containsKey(uriStr)) return resolvedIRIs.get(uriStr) ; IRI iri = iriFactory.create(uriStr) ; if (resolvedIRIs != null) resolvedIRIs.put(uriStr, iri) ; return iri ; }
public Iterator<T> iterator() { return cacheMap.keys() ; }
@Override public Value get(Key key) { if ( cache.containsKey(key) ) cacheMisses ++ ; else cacheHits++ ; return cache.get(key) ; }
@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)) ; }
private void writeCache(Block block) { Long id = block.getId() ; log("WriteCache : %d", id) ; // Should not be in the read cache due to a getWrite earlier. if ( readCache.containsKey(id) ) log.warn("write: Block in the read cache") ; if ( writeCache != null ) { writeCache.put(id, block) ; return ; } }
private void testForConsistency() { Iterator<Node> iter1 = Iter.toList(node2id_Cache.keys()).iterator() ; for ( ; iter1.hasNext() ; ) { Node n = iter1.next() ; NodeId nId = node2id_Cache.getIfPresent(n) ; if ( !id2node_Cache.containsKey(nId) ) throw new TDBException("Inconsistent: " + n + " => " + nId) ; if ( notPresent.contains(n) ) throw new TDBException("Inconsistent: " + n + " in notPresent cache (1)") ; } Iterator<NodeId> iter2 = Iter.toList(id2node_Cache.keys()).iterator() ; for ( ; iter2.hasNext() ; ) { NodeId nId = iter2.next() ; Node n = id2node_Cache.getIfPresent(nId) ; if ( !node2id_Cache.containsKey(n) ) throw new TDBException("Inconsistent: " + nId + " => " + n) ; if ( notPresent.contains(n) ) throw new TDBException("Inconsistent: " + n + " in notPresent cache (2)") ; } }
@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 cacheGetter_2() { Cache<Integer, String> cache = CacheFactory.createCache(2) ; String str1 = cache.getOrFill(1, getter(1)) ; String str2 = cache.getOrFill(2, getter(2)) ; String str3 = cache.getOrFill(3, getter(3)) ; assertEquals("1", str1) ; assertEquals("2", str2) ; assertEquals("3", str3) ; cache.put(1, "10") ; str1 = cache.getIfPresent(1) ; assertEquals("10", str1) ; }
@Override synchronized public void free(Block block) { Long id = block.getId() ; log("Free : %d", id) ; if ( readCache.containsKey(id) ) { log.warn("Freeing block from read cache") ; readCache.remove(id) ; } if ( writeCache != null ) writeCache.remove(id) ; super.free(block) ; }
/** * Check caches to see if we can map a NodeId to a Node. Returns null on no * cache entry. */ private Node cacheLookup(NodeId id) { if ( id2node_Cache == null ) return null ; return id2node_Cache.getIfPresent(id) ; }
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 ; }
@Override public void setDropHandler(BiConsumer<Key, T> dropHandler) { cache.setDropHandler(dropHandler) ; }
protected void removeFromCache(Graph graph) { // Assume MRSW - no synchronized needed. if ( graph == null ) return ; cache.remove(graph) ; }
@Override public Node alloc(final String label) { Callable<Node> getter = new Callable<Node>() { @Override public Node call() { return alloc(Bytes.string2bytes(label)); } }; Node n = cache.getOrFill(label, getter); return n; }
@Override public long size() { return cacheMap.size() ; }
public void addCacheModel(String uri, Model m) { if ( isCachingModels() ) modelCache.put(uri, m) ; }