private <K> void testReadOnMissingValues(K[] keys, FunctionalMap.ReadOnlyMap<K, String> ro, ReadMethod method) throws Exception { tm.begin(); for (K key : keys) { Assert.assertEquals(ro.eval(key, view -> view.find().isPresent()).join(), Boolean.FALSE); } tm.commit(); tm.begin(); for (K key : keys) { expectExceptionNonStrict(CompletionException.class, CacheException.class, NoSuchElementException.class, () -> method.eval(key, ro, EntryView.ReadEntryView::get)); // The first exception should cause the whole transaction to fail assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus()); tm.rollback(); break; } if (tm.getStatus() == Status.STATUS_ACTIVE) { tm.commit(); } }
@Override protected void workerLogic(Cache<Integer, Integer> cache, Set<Integer> threadKeys, int iteration) { FunctionalMap.ReadOnlyMap<Integer, Integer> ro = ReadOnlyMapImpl.create(FunctionalMapImpl.create(cache.getAdvancedCache())); List<CompletableFuture> futures = new ArrayList(threadKeys.size()); for (Integer key : threadKeys) { futures.add(ro.eval(key, (view -> view.get() + 1)).thenAccept(value -> assertEquals(Integer.valueOf(key + 1), value))); } futures.stream().forEach(f -> f.join()); } }
wo.eval(KEY, MarshallableFunctions.removeConsumer()).join(); assertNull(cache.putIfAbsent(KEY, "y")); assertEquals("y", ro.eval(KEY, MarshallableFunctions.returnReadOnlyFindOrNull()).join()); tm.commit();
/** * Write-only allows for non-capturing values to be written along with metadata, * and read-only allows for both values and metadata to be retrieved. */ private <K> void doWriteValueAndReadValueAndMetadata(Supplier<K> keySupplier, ReadOnlyMap<K, String> map1, WriteOnlyMap<K, String> map2) { K key = keySupplier.get(); await( map2.eval(key, "one", SetValueAndConstantLifespan.getInstance()).thenCompose(r -> map1.eval(key, identity()).thenAccept(ro -> { assertNull(r); assertEquals(Optional.of("one"), ro.find()); assertEquals("one", ro.get()); assertEquals(Optional.of(new MetaLifespan(100000)), ro.findMetaParam(MetaLifespan.class)); } ) ) ); }
/** * Write-only allows for constant, non-capturing, values to be written, * and read-only allows for those values to be retrieved. */ private <K> void doWriteConstantAndReadGetsValue(Supplier<K> keySupplier, ReadOnlyMap<K, String> map1, WriteOnlyMap<K, String> map2) { K key = keySupplier.get(); await( map2.eval(key, SetStringConstant.INSTANCE).thenCompose(r -> map1.eval(key, returnReadOnlyFindOrNull()).thenAccept(v -> { assertNull(r); assertEquals("one", v); } ) ) ); }
@Test(dataProvider = "owningModeAndReadWrites") public void testWriteModsInTxContext(boolean isOwner, WriteMethod method) throws Exception { Object KEY = getKey(isOwner, DIST); cache(0, DIST).put(KEY, "a"); tm.begin(); assertEquals("a", cache(0, DIST).put(KEY, "b")); // read-write operation should execute locally instead assertEquals("b", method.eval(KEY, null, rw, EntryView.ReadEntryView::get, (e, prev) -> e.set(prev + "c"), getClass())); // make sure that the operation was executed in context assertEquals("bc", ro.eval(KEY, MarshallableFunctions.returnReadOnlyFindOrNull()).join()); tm.commit(); }
@Override public V get(Object key) { return await(readOnly.eval(toK(key), returnReadOnlyFindOrNull())); }
@Override public boolean containsKey(K key) { return await(readOnly.eval(key, returnReadOnlyFindIsPresent())); }
@Override public boolean containsKey(Object key) { return await(readOnly.eval(toK(key), returnReadOnlyFindIsPresent())); }
@Override public V get(K key) { return await(readOnly.eval(key, returnReadOnlyFindOrNull())); }
private static Map<?, ?> read(Cache<Object, Object> cache, Collection<?> keys) { ReadOnlyMap<Object, Object> ro = ReadOnlyMapImpl.create(FunctionalMapImpl.create(cache.getAdvancedCache())); Map<Object, Object> map = new HashMap<>(); for (Object key : keys) { ro.eval(key, MarshallableFunctions.identity()).join().find().ifPresent(value -> map.put(key, value)); } return map; }
protected <K> void testReadOnMissingValue(K key, FunctionalMap.ReadOnlyMap<K, String> ro, ReadMethod method) { assertEquals(ro.eval(key, view -> view.find().isPresent()).join(), Boolean.FALSE); expectExceptionNonStrict(CompletionException.class, CacheException.class, NoSuchElementException.class, () -> method.eval(key, ro, view -> view.get()) ); }
<K> void doReturnViewFromReadOnlyEval(Supplier<K> keySupplier, ReadOnlyMap<K, String> ro, WriteOnlyMap<K, String> wo) { K k = keySupplier.get(); assertReadOnlyViewEmpty(k, await(ro.eval(k, identity()))); await(wo.eval(k, wv -> wv.set("one"))); assertReadOnlyViewEquals(k, "one", await(ro.eval(k, identity()))); }