@Override public void writeBatch(Iterable<MarshalledEntry<? extends K, ? extends V>> marshalledEntries) { Map<byte[], byte[]> batch = new HashMap<>(); for (MarshalledEntry<? extends K, ? extends V> entry : marshalledEntries) { batch.put(this.marshall(entry.getKey()), this.marshall(entry)); } if (!batch.isEmpty()) { this.remoteCache.putAll(batch); } }
@Override public void putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) { delegate.putAll(map, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit); }
@Override public void putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit unit) { delegate.putAll(map, lifespan, unit); }
@Override public void putAll(Map<? extends K, ? extends V> m) { delegate.putAll(m); }
public void flush(){ LOG.debug("flush()"); if (!toPut.isEmpty()) cache.putAll(toPut); toPut.clear(); }
@Override public void writeBatch(Iterable<MarshalledEntry<? extends K, ? extends V>> marshalledEntries) { Map<byte[], byte[]> batch = new HashMap<>(); for (MarshalledEntry<? extends K, ? extends V> entry : marshalledEntries) { batch.put(this.marshall(entry.getKey()), this.marshall(entry)); } if (!batch.isEmpty()) { this.remoteCache.putAll(batch); } }
@Test public void testBulkOperations() { Map<String, String> mapIn; Map<String, String> mapOut = new HashMap<String, String>(); mapOut.put("aKey", "aValue"); mapOut.put("bKey", "bValue"); mapOut.put("cKey", "cValue"); remoteCache.putAll(mapOut); mapIn = remoteCache.getBulk(); // check that the maps are equal assertEquals(mapIn, mapOut); }
@Test public void testGetBulkWithLimit() { Map<String, String> mapIn; Map<String, String> mapOut = new HashMap<String, String>(); mapOut.put("aKey", "aValue"); mapOut.put("bKey", "bValue"); mapOut.put("cKey", "cValue"); remoteCache.putAll(mapOut); mapIn = remoteCache.getBulk(2); // we don't know which 2 entries will be retrieved assertEquals(mapIn.size(), 2); }
public static void testPutAll(RemoteCache<String, String> remoteCache) { remoteCache.clear(); Map<String, String> entries = new HashMap<String, String>(2); entries.put(KEY1, VALUE1); entries.put(KEY2, VALUE2); remoteCache.putAll(entries); assertEquals(2, remoteCache.size()); }
@Test public void testBulkOperationsWithLifespan() { long lifespanInSecs = 1; Map<String, String> mapIn = new HashMap<String, String>(); Map<String, String> mapOut = new HashMap<String, String>(); mapOut.put("aKey", "aValue"); mapOut.put("bKey", "bValue"); mapOut.put("cKey", "cValue"); remoteCache.putAll(mapOut, lifespanInSecs, TimeUnit.SECONDS); // give the elements time to be evicted sleepForSecs(lifespanInSecs + 1); mapIn = remoteCache.getBulk(); assertEquals(mapIn.size(), 0); }
@Test public void testPutAllEviction() { RemoteCache<String, String> rc = remoteCacheManager.getCache("object"); rc.clear(); Map<String, String> entries = new HashMap<>(); entries.put("keyA", "A"); entries.put("keyB", "B"); entries.put("keyC", "C"); entries.put("keyD", "D"); rc.putAll(entries); assertEquals(3, rc.size()); }
@Test public void testPutAndGetBulk() { RemoteCache<String, Integer> rc = remoteCacheManager.getCache("off-heap-default"); rc.clear(); Map<String, Integer> values = new HashMap<>(); IntStream.range(0, 100).forEach(i -> values.put("key" + i, i)); rc.putAll(values); assertEquals(100, rc.size()); Set<String> keys = rc.keySet(); assertEquals(100, keys.size()); assertTrue(keys.contains("key0")); assertTrue(keys.contains("key50")); assertTrue(keys.contains("key99")); CloseableIterator<Map.Entry<Object, Object>> iter = rc.retrieveEntries(null, 100); //we cannot assert keys/values direcly as iterator returns items in random order Map<String, Integer> keyVal = new HashMap<>(100); while (iter.hasNext()) { Map.Entry<Object, Object> entry = iter.next(); keyVal.put((String) entry.getKey(), (Integer) entry.getValue()); } IntStream.range(0, 100).forEach(i -> { assertTrue(keyVal.containsKey("key" + i)); assertEquals(new Integer(i), keyVal.get("key" + i)); }); }
public void testLoadingAndStoringEventsHotRod2() { Cache<String, String> embedded = cacheFactory.getEmbeddedCache(); RemoteCache<String, String> remote = cacheFactory.getHotRodCache(); TestCacheListener l = new TestCacheListener(); embedded.addListener(l); Map<String, String> tmp = new HashMap<String, String>(); tmp.put("key", "value"); tmp.put("key2", "value2"); remote.putAll(tmp); assertEquals(2, l.createdCounter); //one event for every put assertEquals("value", l.created.get("key")); assertEquals("value2", l.created.get("key2")); assertTrue(l.modified.isEmpty()); assertTrue(l.removed.isEmpty()); assertTrue(l.visited.isEmpty()); l.reset(); remote.putIfAbsent("newKey", "newValue"); assertEquals(1, l.createdCounter); assertEquals("newValue", l.created.get("newKey")); assertTrue(l.modified.isEmpty()); assertTrue(l.removed.isEmpty()); assertTrue(l.visited.isEmpty()); l.reset(); remote.putIfAbsent("newKey", "shouldNotBeAdded"); assertTrue(l.created.isEmpty()); assertTrue(l.modified.isEmpty()); assertTrue(l.removed.isEmpty()); assertEquals(1, l.visitedCounter); l.reset(); }