/** * The assumption is that this method will be invoked only by cache.putAll and cache.removeAll methods. */ @Override public Map<K, ValueHolder<V>> bulkCompute(final Set<? extends K> keys, final Function<Iterable<? extends Map.Entry<? extends K, ? extends V>>, Iterable<? extends Map.Entry<? extends K, ? extends V>>> remappingFunction) throws StoreAccessException { Map<K, ValueHolder<V>> valueHolderMap = new HashMap<>(); if(remappingFunction instanceof Ehcache.PutAllFunction) { Ehcache.PutAllFunction<K, V> putAllFunction = (Ehcache.PutAllFunction<K, V>)remappingFunction; Map<K, V> entriesToRemap = putAllFunction.getEntriesToRemap(); for(Map.Entry<K, V> entry: entriesToRemap.entrySet()) { PutStatus putStatus = silentPut(entry.getKey(), entry.getValue()); if(putStatus == PutStatus.PUT) { putAllFunction.getActualPutCount().incrementAndGet(); valueHolderMap.put(entry.getKey(), new ClusteredValueHolder<>(entry.getValue())); } } } else if(remappingFunction instanceof Ehcache.RemoveAllFunction) { Ehcache.RemoveAllFunction<K, V> removeAllFunction = (Ehcache.RemoveAllFunction<K, V>)remappingFunction; for (K key : keys) { boolean removed = silentRemove(key); if(removed) { removeAllFunction.getActualRemoveCount().incrementAndGet(); } } } else { throw new UnsupportedOperationException("This bulkCompute method is not yet capable of handling generic computation functions"); } return valueHolderMap; }
@Override public void doPutAll(final Map<? extends K, ? extends V> entries) throws StoreAccessException { // Copy all entries to write into a Map Map<K, V> entriesToRemap = CollectionUtil.copyMapButFailOnNull(entries); PutAllFunction<K, V> putAllFunction = new PutAllFunction<>(logger, entriesToRemap, runtimeConfiguration.getExpiryPolicy()); store.bulkCompute(entries.keySet(), putAllFunction); addBulkMethodEntriesCount(BulkOps.PUT_ALL, putAllFunction.getActualPutCount().get()); addBulkMethodEntriesCount(BulkOps.UPDATE_ALL, putAllFunction.getActualUpdateCount().get()); }
@Override public Iterable<? extends Map.Entry<? extends K, ? extends V>> apply(final Iterable<? extends Map.Entry<? extends K, ? extends V>> entries) { Map<K, V> mutations = new LinkedHashMap<>(); // then record we handled these mappings for (Map.Entry<? extends K, ? extends V> entry: entries) { K key = entry.getKey(); V existingValue = entry.getValue(); V newValue = entriesToRemap.remove(key); if (newValueAlreadyExpired(key, existingValue, newValue)) { mutations.put(key, null); } else { actualPutCount.incrementAndGet(); if(existingValue != null) { actualUpdateCount.incrementAndGet(); } mutations.put(key, newValue); } } // Finally return the values to be installed in the Cache's Store return mutations.entrySet(); }
/** * The assumption is that this method will be invoked only by cache.putAll and cache.removeAll methods. */ @Override public Map<K, ValueHolder<V>> bulkCompute(final Set<? extends K> keys, final Function<Iterable<? extends Map.Entry<? extends K, ? extends V>>, Iterable<? extends Map.Entry<? extends K, ? extends V>>> remappingFunction) throws StoreAccessException { Map<K, ValueHolder<V>> valueHolderMap = new HashMap<>(); if(remappingFunction instanceof Ehcache.PutAllFunction) { Ehcache.PutAllFunction<K, V> putAllFunction = (Ehcache.PutAllFunction<K, V>)remappingFunction; Map<K, V> entriesToRemap = putAllFunction.getEntriesToRemap(); for(Map.Entry<K, V> entry: entriesToRemap.entrySet()) { PutStatus putStatus = silentPut(entry.getKey(), entry.getValue()); if(putStatus == PutStatus.PUT) { putAllFunction.getActualPutCount().incrementAndGet(); valueHolderMap.put(entry.getKey(), new ClusteredValueHolder<>(entry.getValue())); } } } else if(remappingFunction instanceof Ehcache.RemoveAllFunction) { Ehcache.RemoveAllFunction<K, V> removeAllFunction = (Ehcache.RemoveAllFunction<K, V>)remappingFunction; for (K key : keys) { boolean removed = silentRemove(key); if(removed) { removeAllFunction.getActualRemoveCount().incrementAndGet(); } } } else { throw new UnsupportedOperationException("This compute method is not yet capable of handling generic computation functions"); } return valueHolderMap; }
@Override public Iterable<? extends Map.Entry<? extends K, ? extends V>> apply(final Iterable<? extends Map.Entry<? extends K, ? extends V>> entries) { Map<K, V> mutations = new LinkedHashMap<>(); // then record we handled these mappings for (Map.Entry<? extends K, ? extends V> entry: entries) { K key = entry.getKey(); V existingValue = entry.getValue(); V newValue = entriesToRemap.remove(key); if (newValueAlreadyExpired(key, existingValue, newValue)) { mutations.put(key, null); } else { actualPutCount.incrementAndGet(); if(existingValue != null) { actualUpdateCount.incrementAndGet(); } mutations.put(key, newValue); } } // Finally return the values to be installed in the Cache's Store return mutations.entrySet(); }
@Override public void doPutAll(final Map<? extends K, ? extends V> entries) throws StoreAccessException { // Copy all entries to write into a Map Map<K, V> entriesToRemap = CollectionUtil.copyMapButFailOnNull(entries); PutAllFunction<K, V> putAllFunction = new PutAllFunction<>(logger, entriesToRemap, runtimeConfiguration.getExpiryPolicy()); store.bulkCompute(entries.keySet(), putAllFunction); addBulkMethodEntriesCount(BulkOps.PUT_ALL, putAllFunction.getActualPutCount().get()); addBulkMethodEntriesCount(BulkOps.UPDATE_ALL, putAllFunction.getActualUpdateCount().get()); }
private Map<K, ValueHolder<V>> getkValueHolderMap(Ehcache.PutAllFunction<K, V> remappingFunction, Set<K> successes, Map<K, Exception> failures) throws StoreAccessException { Map<K, V> entriesToRemap = CollectionUtil.copyMapButFailOnNull(putAllFunction.getEntriesToRemap()); }; Map<K, ValueHolder<V>> computedMap = delegate.bulkCompute(putAllFunction.getEntriesToRemap().keySet(), computeFunction); if (!failures.isEmpty()) { throw new BulkCacheWritingException(failures, successes);
@Test public void testBulkComputePutAll() throws Exception { store.put(1L, "another one"); Map<Long, String> map = new HashMap<>(); map.put(1L, "one"); map.put(2L, "two"); Ehcache.PutAllFunction<Long, String> putAllFunction = new Ehcache.PutAllFunction<>(null, map, null); Map<Long, Store.ValueHolder<String>> valueHolderMap = store.bulkCompute(new HashSet<>(Arrays.asList(1L, 2L)), putAllFunction); assertThat(valueHolderMap.get(1L).get(), is(map.get(1L))); assertThat(store.get(1L).get(), is(map.get(1L))); assertThat(valueHolderMap.get(2L).get(), is(map.get(2L))); assertThat(store.get(2L).get(), is(map.get(2L))); assertThat(putAllFunction.getActualPutCount().get(), is(2)); validateStats(store, EnumSet.of(StoreOperationOutcomes.PutOutcome.PUT)); //outcome of the initial store put }