@Override @Nullable public <T> T get(Object key, Callable<T> valueLoader) { try { return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader); } catch (EntryProcessorException ex) { throw new ValueRetrievalException(key, valueLoader, ex.getCause()); } }
@Override @Nullable public <T> T get(Object key, Callable<T> valueLoader) { try { return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader); } catch (EntryProcessorException ex) { throw new ValueRetrievalException(key, valueLoader, ex.getCause()); } }
/** * @throws Exception If failed. */ @Test public void testInvoke() throws Exception { Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME); assertNotNull(cache.invoke(100, new EntryProcessor<Integer, String, Object>() { @Override public Object process(MutableEntry<Integer, String> entry, Object... args) { CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class); checkVersionedEntry(verEntry); return verEntry.version(); } })); }
@Test public void invoke() { expect(cache.invoke(1, new GetEntryProcessor())).isNull(); // miss changesOf(0, 1, 0, 0); expect(cache.invoke(1, new GetKeyEntryProcessor())).isEqualTo(1); // miss changesOf(0, 1, 0, 0); expect(cache.invoke(1, new ExistEntryProcessor())).isEqualTo(false); // miss changesOf(0, 1, 0, 0); expect(cache.invoke(1, new SetEntryProcessor("a"))).isEqualTo("a"); // put changesOf(0, 1, 1, 0); // FIXME Why is there a miss? expect(cache.invoke(1, new SetEntryProcessor("b"))).isEqualTo("b"); // update changesOf(1, 0, 1, 0); expect(cache.invoke(1, new GetEntryProcessor())).isEqualTo("b"); // hit changesOf(1, 0, 0, 0); expect(cache.invoke(1, new GetKeyEntryProcessor())).isEqualTo(1); // hit changesOf(1, 0, 0, 0); expect(cache.invoke(1, new ExistEntryProcessor())).isEqualTo(true); // hit changesOf(1, 0, 0, 0); expect(cache.invoke(1, new RemoveEntryProcessor())).isNull(); // hit changesOf(1, 0, 0, 1); // FIXME Why is there a hit? expect(cache.invoke(1, new RemoveEntryProcessor())).isNull(); // miss changesOf(0, 1, 0, 1); // FIXME Why is there a remove? }
@Override public void createInitialState(K key, BucketConfiguration configuration) { JCacheEntryProcessor<K, Nothing> entryProcessor = JCacheEntryProcessor.initStateProcessor(configuration); cache.invoke(key, entryProcessor); }
@Override public <T extends Serializable> CommandResult<T> execute(K key, GridCommand<T> command) { JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command); return cache.invoke(key, entryProcessor); }
@Override public <T extends Serializable> CommandResult<T> execute(K key, GridCommand<T> command) { JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.executeProcessor(command); return cache.invoke(key, entryProcessor); }
@Override public <T extends Serializable> T createInitialStateAndExecute(K key, BucketConfiguration configuration, GridCommand<T> command) { JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration); CommandResult<T> result = cache.invoke(key, entryProcessor); return result.getData(); }
@Override public <T> T invoke( K key, final EntryProcessor<K, V, T> entryProcessor, Object... arguments) throws EntryProcessorException { EntryProcessor<K0, V0, T> processor = wrapEntryProcessor(entryProcessor); return cache.invoke(keyTransformer.compact(key), processor, arguments); }
@Test public void nullGetValue() { String result = cache.invoke(123, new GetEntryProcessor<Integer, String>()); assertNull(result); }
@Override public void execute(Context context) throws Exception { int userId = context.readUserId(); context.write("New username: "); String username = context.readLine(); Cache<Integer, User> userCache = context.getUserCache(); User result = userCache.invoke(userId, new UserUpdateEntryProcessor(), username); context.writeln("User updated: " + result); }
@Test public void close() { cache.close(); try { cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class)); fail("null key"); } catch (IllegalStateException e) { // } }
@Test public void testProcessorEmptyExceptionIsWrapped() { try { cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class)); fail(); } catch (EntryProcessorException e) { assertTrue(e.getCause() instanceof RuntimeException); //expected } }
@Test public void noValueSetValue() { final Integer key = 123; final Integer ret = 456; final String value = "abc"; assertEquals(ret, cache.invoke(key, new SetValueCreateEntryReturnDifferentTypeEntryProcessor<Integer, String, Integer>(ret, value))); assertEquals(value, cache.get(key)); }
@Test public void noValueNoMutation() { final Integer key = 123; final Integer ret = 456; assertEquals(ret, cache.invoke(key, new AssertNotPresentEntryProcessor<Integer, String, Integer>(ret))); assertFalse(cache.containsKey(key)); }
@Test public void testProcessorExceptionIsWrapped() { try { cache.invoke(123, new ThrowExceptionEntryProcessor<Integer, String, Void>(UnsupportedOperationException.class)); fail(); } catch (EntryProcessorException e) { assertTrue(e.getCause() instanceof RuntimeException); //expected } }
@Test public void existingReplace() { final Integer key = 123; final String oldValue = "abc"; final String newValue = "def"; cache.put(key, oldValue); assertEquals(oldValue, cache.invoke(key, new ReplaceEntryProcessor<Integer, String, String>(oldValue, newValue))); assertEquals(newValue, cache.get(key)); }
@Test public void shouldWriteThroughUsingInvoke_remove_nonExistingEntry() { assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); cache.invoke(1, new RemoveEntryProcessor<Integer, String, Object>()); assertEquals(0, cacheWriter.getWriteCount()); assertEquals(1, cacheWriter.getDeleteCount()); assertFalse(cacheWriter.hasWritten(1)); }
@Test public void shouldWriteThroughUsingInvoke_remove() { assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); cache.put(1, "Gudday World"); cache.invoke(1, new RemoveEntryProcessor<Integer, String, Object>(true)); assertEquals(1, cacheWriter.getWriteCount()); assertEquals(1, cacheWriter.getDeleteCount()); assertFalse(cacheWriter.hasWritten(1)); }
@Test public void shouldWriteThroughUsingInvoke_setValue_UpdateEntry() { assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); cache.put(1, "Gudday World"); cache.invoke(1, new SetEntryProcessor("Hello World")); assertEquals(2, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); assertTrue(cacheWriter.hasWritten(1)); assertEquals("Hello World", cacheWriter.get(1)); }