Search in sources :

Example 11 with MutableEntry

use of javax.cache.processor.MutableEntry in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method cacheUpdate.

/**
 * @param grid Grid index.
 * @param rmv If {@code true} then executes remove.
 * @param op Operation type.
 * @param key Key.
 * @param val Value.
 * @param expOld Expected expOld value.
 * @param expRmvRet Expected remove result.
 * @throws Exception If failed.
 */
private void cacheUpdate(int grid, boolean rmv, Operation op, String key, final Integer val, @Nullable final Integer expOld, @Nullable final Integer expRmvRet) throws Exception {
    IgniteCache<String, Integer> cache = jcache(grid);
    if (rmv) {
        assertNull(val);
        switch(op) {
            case UPDATE:
                {
                    assertEquals(expRmvRet, cache.getAndRemove(key));
                    break;
                }
            case UPDATEX:
                {
                    cache.remove(key);
                    break;
                }
            case TRANSFORM:
                {
                    cache.invoke(key, new EntryProcessor<String, Integer, Void>() {

                        @Override
                        public Void process(MutableEntry<String, Integer> e, Object... args) {
                            Integer old = e.getValue();
                            assertEquals(expOld, old);
                            e.remove();
                            return null;
                        }
                    });
                    break;
                }
            default:
                fail();
        }
    } else {
        switch(op) {
            case UPDATE:
                {
                    assertEquals(expOld, cache.getAndPut(key, val));
                    break;
                }
            case UPDATEX:
                {
                    cache.put(key, val);
                    break;
                }
            case TRANSFORM:
                {
                    cache.invoke(key, new EntryProcessor<String, Integer, Void>() {

                        @Override
                        public Void process(MutableEntry<String, Integer> e, Object... args) {
                            Integer old = e.getValue();
                            assertEquals(expOld, old);
                            e.setValue(val);
                            return null;
                        }
                    });
                    break;
                }
            default:
                fail();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessor(javax.cache.processor.EntryProcessor) MutableEntry(javax.cache.processor.MutableEntry)

Example 12 with MutableEntry

use of javax.cache.processor.MutableEntry in project ignite by apache.

the class IgniteCacheManyAsyncOperationsTest method testInvokeAsyncWithKeepBinary.

/**
 * @throws Exception If failed.
 */
@Test
public void testInvokeAsyncWithKeepBinary() throws Exception {
    try (Ignite client = startClientGrid(gridCount())) {
        assertTrue(client.configuration().isClientMode());
        IgniteCache<Integer, BinaryObject> cache = client.cache(DEFAULT_CACHE_NAME).withKeepBinary();
        BinaryObject value = client.binary().builder("TEST").build();
        cache.put(1, value);
        // Start parallel operations to initiate operation retry.
        List<IgniteFuture<Void>> futs = IntStream.range(0, 1000).parallel().mapToObj(i -> cache.invokeAsync(1, new EntryProcessor<Integer, BinaryObject, Void>() {

            @Override
            public Void process(MutableEntry<Integer, BinaryObject> e, Object... args) {
                BinaryObject val = e.getValue();
                assertNotNull(val);
                return null;
            }
        })).collect(Collectors.toList());
        futs.forEach(f -> f.get());
    }
}
Also used : IntStream(java.util.stream.IntStream) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteFuture(org.apache.ignite.lang.IgniteFuture) HashMap(java.util.HashMap) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) EntryProcessor(javax.cache.processor.EntryProcessor) IgniteCache(org.apache.ignite.IgniteCache) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) ArrayList(java.util.ArrayList) MutableEntry(javax.cache.processor.MutableEntry) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Map(java.util.Map) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) CacheMode(org.apache.ignite.cache.CacheMode) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) Test(org.junit.Test)

Example 13 with MutableEntry

use of javax.cache.processor.MutableEntry in project ignite by apache.

the class GridCacheCommandHandler method appendOrPrepend.

/**
 * Handles append and prepend commands.
 *
 * @param ctx Kernal context.
 * @param cache Cache.
 * @param key Key.
 * @param req Request.
 * @param prepend Whether to prepend.
 * @return Future of operation result.
 * @throws IgniteCheckedException In case of any exception.
 */
private static IgniteInternalFuture<?> appendOrPrepend(final GridKernalContext ctx, final IgniteInternalCache<Object, Object> cache, final Object key, GridRestCacheRequest req, final boolean prepend) throws IgniteCheckedException {
    assert cache != null;
    assert key != null;
    assert req != null;
    final Object val = req.value();
    if (val == null)
        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
    return ctx.closure().callLocalSafe(new GridPlainCallable<Object>() {

        @Override
        public Object call() throws Exception {
            EntryProcessorResult<Boolean> res = cache.invoke(key, new EntryProcessor<Object, Object, Boolean>() {

                @Override
                public Boolean process(MutableEntry<Object, Object> entry, Object... objects) throws EntryProcessorException {
                    try {
                        Object curVal = entry.getValue();
                        if (curVal == null)
                            return false;
                        // Modify current value with appendix one.
                        Object newVal = appendOrPrepend(curVal, val, !prepend);
                        // Put new value asynchronously.
                        entry.setValue(newVal);
                        return true;
                    } catch (IgniteCheckedException e) {
                        throw new EntryProcessorException(e);
                    }
                }
            });
            try {
                return res.get();
            } catch (EntryProcessorException e) {
                throw new IgniteCheckedException(e.getCause());
            }
        }
    }, false);
}
Also used : EntryProcessorResult(javax.cache.processor.EntryProcessorResult) EntryProcessor(javax.cache.processor.EntryProcessor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) MutableEntry(javax.cache.processor.MutableEntry) CacheConfigurationOverride(org.apache.ignite.internal.processors.cache.CacheConfigurationOverride) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Example 14 with MutableEntry

use of javax.cache.processor.MutableEntry in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method cacheOperationsTest.

/**
 * @throws Exception If failed.
 */
private void cacheOperationsTest() throws Exception {
    final Ignite client = startClientGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check put and get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.getAndPut(9999, 9999);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.getAndPut(9999, 9999);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(9999, dfltCache.get(9999));
            return true;
        }
    }), // Check put operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.put(10000, 10000);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            dfltCache.put(10000, 10000);
            return true;
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertTrue((Boolean) o);
            assertEquals(10000, dfltCache.get(10000));
            return true;
        }
    }), // Check get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.get(10001);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.get(10001);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            return true;
        }
    }), // Check put and invoke operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.put(CACHE_PUT_INVOKE_KEY, 10000);
                dfltCache.invoke(CACHE_PUT_INVOKE_KEY, new CacheEntryProcessor<Object, Object, Object>() {

                    @Override
                    public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                        assertTrue(entry.exists());
                        return (int) entry.getValue() * 2;
                    }
                });
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            dfltCache.put(CACHE_PUT_INVOKE_KEY, 10000);
            return dfltCache.invoke(CACHE_PUT_INVOKE_KEY, new CacheEntryProcessor<Object, Object, Object>() {

                @Override
                public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                    assertTrue(entry.exists());
                    return (int) entry.getValue() * 2;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            assertEquals(20000, (int) o);
            return true;
        }
    }), // Check put async operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.putAsync(10002, 10002).get();
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.putAsync(10002, 10002).get();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(10002, dfltCache.get(10002));
            return true;
        }
    }), // Check transaction.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.transactions();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.transactions();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteTransactions txs = (IgniteTransactions) o;
            assertNotNull(txs);
            return true;
        }
    }), // Check get cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cache(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cache(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache0 = (IgniteCache<Object, Object>) o;
            assertNotNull(cache0);
            cache0.put(1, 1);
            assertEquals(1, cache0.get(1));
            return true;
        }
    }), // Check streamer.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.dataStreamer(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.dataStreamer(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteDataStreamer<Object, Object> streamer = (IgniteDataStreamer<Object, Object>) o;
            streamer.addData(2, 2);
            streamer.close();
            assertEquals(2, client.cache(DEFAULT_CACHE_NAME).get(2));
            return true;
        }
    }), // Check create cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.createCache("test_cache");
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.createCache("test_cache");
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache = (IgniteCache<Object, Object>) o;
            assertNotNull(cache);
            cache.put(1, 1);
            assertEquals(1, cache.get(1));
            return true;
        }
    })));
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) T2(org.apache.ignite.internal.util.typedef.T2)

Example 15 with MutableEntry

use of javax.cache.processor.MutableEntry in project ignite by apache.

the class GridCacheAbstractMetricsSelfTest method testGetMetricsDisable.

/**
 * @throws Exception If failed.
 */
@Test
public void testGetMetricsDisable() throws Exception {
    // Disable statistics.
    for (int i = 0; i < gridCount(); i++) {
        Ignite g = grid(i);
        IgniteCache cache = g.cache(DEFAULT_CACHE_NAME);
        cache.enableStatistics(false);
    }
    IgniteCache<Object, Object> jcache = grid(0).cache(DEFAULT_CACHE_NAME);
    // Write to cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.put(i, i);
    // Invoke update on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            Object key = entry.getKey();
            entry.setValue(key);
            return null;
        }
    });
    // Read-only invoke on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            entry.getKey();
            return null;
        }
    });
    // Remove invoke on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            entry.remove();
            return null;
        }
    });
    // Get from cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.get(i);
    // Remove from cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.remove(i);
    // Assert that statistics is clear.
    for (int i = 0; i < gridCount(); i++) {
        CacheMetrics m = grid(i).cache(DEFAULT_CACHE_NAME).localMetrics();
        assertEquals(m.getCacheGets(), 0);
        assertEquals(m.getCachePuts(), 0);
        assertEquals(m.getCacheRemovals(), 0);
        assertEquals(m.getCacheHits(), 0);
        assertEquals(m.getCacheMisses(), 0);
        assertEquals(m.getAverageGetTime(), 0f);
        assertEquals(m.getAverageRemoveTime(), 0f);
        assertEquals(m.getAveragePutTime(), 0f);
        assertEquals(m.getAverageTxCommitTime(), 0f);
        assertEquals(m.getAverageTxRollbackTime(), 0f);
        assertEquals(m.getEntryProcessorPuts(), 0);
        assertEquals(m.getEntryProcessorRemovals(), 0);
        assertEquals(m.getEntryProcessorReadOnlyInvocations(), 0);
        assertEquals(m.getEntryProcessorMinInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorMaxInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorAverageInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorInvocations(), 0);
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) Test(org.junit.Test)

Aggregations

MutableEntry (javax.cache.processor.MutableEntry)17 Ignite (org.apache.ignite.Ignite)9 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 EntryProcessorException (javax.cache.processor.EntryProcessorException)7 IgniteCache (org.apache.ignite.IgniteCache)7 EntryProcessor (javax.cache.processor.EntryProcessor)6 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 Transaction (org.apache.ignite.transactions.Transaction)4 Map (java.util.Map)3 CacheEntryEvent (javax.cache.event.CacheEntryEvent)3 IgniteException (org.apache.ignite.IgniteException)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2