/** * Returns a {@link RevObject} cache * <p> * Multiple invocations of this method for the same key are guaranteed to return the same * {@link ObjectCache} as long as there's still at least one handle to the same cache, or a new * one otherwise. * * @param uniqueCacheIdentifier a client defined identifier for the objectcache */ public ObjectCache acquire(final String uniqueCacheIdentifier) { checkNotNull(uniqueCacheIdentifier); CacheIdentifier prefix = CACHE_IDS.get(uniqueCacheIdentifier); if (prefix == null) { prefix = new CacheIdentifier(CACHE_ID_SEQ.incrementAndGet()); CacheIdentifier existing = CACHE_IDS.putIfAbsent(uniqueCacheIdentifier, prefix); if (existing != null) { prefix = existing; } } return CACHES.acquire(prefix); }
private void invalidateAll(CacheIdentifier prefix, ConcurrentMap<CacheKey, ?> map) { map.keySet().parallelStream().filter((k) -> { int keyprefix = k.prefix(); int expectedPrefix = prefix.prefix(); return keyprefix == expectedPrefix; }).forEach(map::remove); }
/** * Checks whether a given {@link ObjectId} is already cached on this {@code ObjectCache} */ public boolean contains(ObjectId id) { return sharedCache.get().contains(keyPrefix.create(id)); }
public @Before void before() { mockSharedCache = mock(SharedCache.class); cacheId1 = new CacheIdentifier(0); cacheId2 = new CacheIdentifier(1000); cache1 = new ObjectCache(() -> mockSharedCache, cacheId1); cache2 = new ObjectCache(() -> mockSharedCache, cacheId2); o1 = RevFeatureBuilder.builder().addValue(0L).addValue("zero").build(); o2 = RevFeatureBuilder.builder().addValue(1L).addValue("one").build(); k11 = cacheId1.create(o1.getId()); k12 = cacheId1.create(o2.getId()); k21 = cacheId2.create(o1.getId()); k22 = cacheId2.create(o2.getId()); }
public @Before void before() { mockSharedCache = mock(SharedCache.class); cacheId1 = new CacheIdentifier(0); cacheId2 = new CacheIdentifier(1000); cache1 = new ObjectCache(() -> mockSharedCache, cacheId1); cache2 = new ObjectCache(() -> mockSharedCache, cacheId2); o1 = RevFeature.builder().addValue(0L).addValue("zero").build(); o2 = RevFeature.builder().addValue(1L).addValue("one").build(); k11 = cacheId1.create(o1.getId()); k21 = cacheId2.create(o1.getId()); k22 = cacheId2.create(o2.getId()); }
/** * Checks whether a given {@link ObjectId} is already cached on this {@code ObjectCache} */ public boolean contains(ObjectId id) { return sharedCache.get().contains(keyPrefix.create(id)); }
/** * Returns a {@link RevObject} cache * <p> * Multiple invocations of this method for the same key are guaranteed to return the same * {@link ObjectCache} as long as there's still at least one handle to the same cache, or a new * one otherwise. * * @param uniqueCacheIdentifier a client defined identifier for the objectcache */ public ObjectCache acquire(final String uniqueCacheIdentifier) { checkNotNull(uniqueCacheIdentifier); CacheIdentifier prefix = CACHE_IDS.get(uniqueCacheIdentifier); if (prefix == null) { prefix = new CacheIdentifier(CACHE_ID_SEQ.incrementAndGet()); CacheIdentifier existing = CACHE_IDS.putIfAbsent(uniqueCacheIdentifier, prefix); if (existing != null) { prefix = existing; } } return CACHES.acquire(prefix); }
private void invalidateAll(CacheIdentifier prefix, ConcurrentMap<CacheKey, ?> map) { map.keySet().parallelStream().filter(k -> { int keyprefix = k.prefix(); int expectedPrefix = prefix.prefix(); return keyprefix == expectedPrefix; }).forEach(map::remove); }
/** * Returns the cached object with the given id, if present, or {@code null} otherwise */ public @Nullable RevObject getIfPresent(ObjectId id) { return sharedCache.get().getIfPresent(keyPrefix.create(id)); } }
public void setUp(Supplier<SharedCache> sharedCache) throws Exception { this.sharedCache = sharedCache.get(); cache = new ObjectCache(sharedCache, new CacheIdentifier(1)); Stopwatch sw = Stopwatch.createStarted(); Runtime runtime = Runtime.getRuntime(); long mem = runtime.totalMemory() - Runtime.getRuntime().freeMemory(); leafTrees = createLeafTrees(treeCount); long mem2 = runtime.totalMemory() - Runtime.getRuntime().freeMemory(); System.err.printf("leaf tree mem: %,d\n", mem2 - mem); bucketTrees = createBucketTrees(bucketTreeCount); features = createFeatures(featureCount); sw.stop(); System.err.printf("Created %,d features, %,d trees, %,d buckets in %s\n", featureCount, treeCount, treeCount, sw); }
private void invalidateAll(CacheIdentifier prefix, ConcurrentMap<Key, ?> map) { map.keySet().parallelStream().filter((k) -> { int keyprefix = k.prefix(); int expectedPrefix = prefix.prefix(); return keyprefix == expectedPrefix; }).forEach((k) -> { map.remove(k); }); }
/** * Prunes the given object id from the cache */ public void invalidate(ObjectId id) { sharedCache.get().invalidate(keyPrefix.create(id)); }
public @Before void before() { repo1Id = new CacheIdentifier(1); repo2Id = new CacheIdentifier(1000); store = new HeapObjectStore(); store.open(); obj = RevObjectTestSupport.INSTANCE.createFeaturesTree(store, "f-", 10); }
/** * Prunes the given object id from the cache */ public void invalidate(ObjectId id) { sharedCache.get().invalidate(keyPrefix.create(id)); }
public @Before void before() { cache = spy(SharedCache.build(maxCacheSizeBytes)); repo1Id = new CacheIdentifier(1); repo2Id = new CacheIdentifier(1000); store = new HeapObjectStore(); store.open(); obj = RevObjectTestSupport.createFeaturesTree(store, "f-", 10); }
/** * Returns the cached object with the given id, if present, or {@code null} otherwise */ public @Nullable RevObject getIfPresent(ObjectId id) { return sharedCache.get().getIfPresent(keyPrefix.create(id)); } }
public void put(RevObject obj) { SharedCache cache = sharedCache.get(); CacheKey key = keyPrefix.create(obj.getId()); cache.put(key, obj); }
public void put(RevObject obj) { sharedCache.get().put(keyPrefix.create(obj.getId()), obj); }
public @Test void testInvalidateAll() { final int L1Capacity = 10; SharedCache cache = SharedCache.build(L1Capacity, 16 * 1024 * 1024); List<RevObject> objects = createObjects(500); objects.forEach((o) -> cache.put(repo1Id.create(o.getId()), o)); objects.forEach((o) -> cache.put(repo2Id.create(o.getId()), o)); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo2Id.create(o.getId())))); cache.invalidateAll(); objects.forEach((o) -> assertNull(cache.getIfPresent(repo2Id.create(o.getId())))); objects.forEach((o) -> assertNull(cache.getIfPresent(repo1Id.create(o.getId())))); }
public @Test void testL1WriteBack() { final int L1Capacity = 1000; cache = createCache(L1Capacity, maxCacheSizeBytes); List<RevObject> objects = createObjects(100); objects.forEach((o) -> cache.put(repo1Id.create(o.getId()), o)); objects.forEach((o) -> assertNull(cache.getIfPresent(repo2Id.create(o.getId())))); objects.forEach((o) -> assertNotNull(cache.getIfPresent(repo1Id.create(o.getId())))); }