Search in sources :

Example 16 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class CacheContinuousQueryHandlerV3 method register.

/**
 * {@inheritDoc}
 */
@Override
public RegisterStatus register(UUID nodeId, UUID routineId, GridKernalContext ctx) throws IgniteCheckedException {
    final IgniteClosure trans = getTransformer();
    if (trans != null)
        ctx.resource().injectGeneric(trans);
    if (locTransLsnr != null) {
        ctx.resource().injectGeneric(locTransLsnr);
        asyncCb = U.hasAnnotation(locTransLsnr, IgniteAsyncCallback.class);
    }
    return super.register(nodeId, routineId, ctx);
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteAsyncCallback(org.apache.ignite.lang.IgniteAsyncCallback)

Example 17 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class CacheContinuousQueryWithTransformerExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache continuous query with transformer example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, Organization> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            // Create new continuous query with transformer.
            ContinuousQueryWithTransformer<Integer, Organization, String> qry = new ContinuousQueryWithTransformer<>();
            // Factory to create transformers.
            Factory<IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>> factory = FactoryBuilder.factoryOf(// Only this field will be sent over to a local node over the network.
            (IgniteClosure<CacheEntryEvent<? extends Integer, ? extends Organization>, String>) event -> event.getValue().name());
            qry.setRemoteTransformerFactory(factory);
            // Listener that will receive transformed data.
            qry.setLocalListener(names -> {
                for (String name : names) System.out.println("New organization name: " + name);
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, Organization>> cur = cache.query(qry)) {
                populateCache(cache);
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Factory(javax.cache.configuration.Factory) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) Address(org.apache.ignite.examples.model.Address) IgniteCache(org.apache.ignite.IgniteCache) OrganizationType(org.apache.ignite.examples.model.OrganizationType) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignition(org.apache.ignite.Ignition) Organization(org.apache.ignite.examples.model.Organization) IgniteClosure(org.apache.ignite.lang.IgniteClosure) Map(java.util.Map) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) Organization(org.apache.ignite.examples.model.Organization) IgniteClosure(org.apache.ignite.lang.IgniteClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite)

Example 18 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalInjection.

/**
 * @throws Exception If failed.
 */
public void testLocalInjection() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Boolean>> lists = grid().compute().broadcast(new IgniteCallable<List<Boolean>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Boolean> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Boolean> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Boolean>() {

                    @IgniteInstanceResource
                    Ignite ignite;

                    @Override
                    public Boolean apply(Cache.Entry<Integer, Value> e) {
                        return ignite != null;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Boolean> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        for (int i = 0; i < 50; i++) assertEquals(Boolean.TRUE, res.get(i));
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 19 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinary.

/**
 * @throws Exception If failed.
 */
public void testLocalKeepBinary() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, BinaryObject>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, BinaryObject> e) {
                        return e.getValue().field("idx");
                    }
                };
                return ignite.cache("test-cache").withKeepBinary().query(new ScanQuery<Integer, BinaryObject>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) BinaryObject(org.apache.ignite.binary.BinaryObject) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 20 with IgniteClosure

use of org.apache.ignite.lang.IgniteClosure in project ignite by apache.

the class GridCacheQueryTransformerSelfTest method testLocal.

/**
 * @throws Exception If failed.
 */
public void testLocal() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

IgniteClosure (org.apache.ignite.lang.IgniteClosure)26 Ignite (org.apache.ignite.Ignite)15 IgniteCache (org.apache.ignite.IgniteCache)8 Cache (javax.cache.Cache)7 ArrayList (java.util.ArrayList)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 List (java.util.List)5 CacheEntryEvent (javax.cache.event.CacheEntryEvent)5 IgniteException (org.apache.ignite.IgniteException)5 ContinuousQueryWithTransformer (org.apache.ignite.cache.query.ContinuousQueryWithTransformer)5 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)4 IOException (java.io.IOException)3 UUID (java.util.UUID)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)3