Search in sources :

Example 11 with IgniteBiPredicate

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

the class GridCacheAdapter method localLoadCache.

/**
 * {@inheritDoc}
 */
@Override
public void localLoadCache(final IgniteBiPredicate<K, V> p, Object[] args) throws IgniteCheckedException {
    final boolean replicate = ctx.isDrEnabled();
    final AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    ExpiryPolicy plc0 = opCtx != null ? opCtx.expiry() : null;
    final ExpiryPolicy plc = plc0 != null ? plc0 : ctx.expiry();
    final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
    if (p != null)
        ctx.kernalContext().resource().injectGeneric(p);
    try {
        if (ctx.store().isLocal()) {
            DataStreamerImpl ldr = ctx.kernalContext().dataStream().dataStreamer(ctx.name());
            try {
                ldr.skipStore(true);
                ldr.receiver(new IgniteDrDataStreamerCacheUpdater());
                ldr.keepBinary(keepBinary);
                LocalStoreLoadClosure c = new LocalStoreLoadClosure(p, ldr, plc);
                ctx.store().loadCache(c, args);
                c.onDone();
            } finally {
                ldr.closeEx(false);
            }
        } else {
            // Version for all loaded entries.
            final GridCacheVersion ver0 = ctx.versions().nextForLoad();
            ctx.store().loadCache(new CIX3<KeyCacheObject, Object, GridCacheVersion>() {

                @Override
                public void applyx(KeyCacheObject key, Object val, @Nullable GridCacheVersion ver) throws IgniteException {
                    assert ver == null;
                    long ttl = CU.ttlForLoad(plc);
                    if (ttl == CU.TTL_ZERO)
                        return;
                    loadEntry(key, val, ver0, (IgniteBiPredicate<Object, Object>) p, topVer, replicate, ttl);
                }
            }, args);
        }
    } finally {
        if (p instanceof PlatformCacheEntryFilter)
            ((PlatformCacheEntryFilter) p).onClose();
    }
}
Also used : IgniteDrDataStreamerCacheUpdater(org.apache.ignite.internal.processors.dr.IgniteDrDataStreamerCacheUpdater) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) DataStreamerImpl(org.apache.ignite.internal.processors.datastreamer.DataStreamerImpl) PlatformCacheEntryFilter(org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteException(org.apache.ignite.IgniteException) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) IgniteExternalizableExpiryPolicy(org.apache.ignite.internal.processors.cache.distributed.IgniteExternalizableExpiryPolicy)

Example 12 with IgniteBiPredicate

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

the class IgniteCamelStreamerTest method subscribeToPutEvents.

/**
 * Subscribe to cache put events.
 */
private CountDownLatch subscribeToPutEvents(int expect) {
    Ignite ignite = grid();
    // Listen to cache PUT events and expect as many as messages as test data items
    final CountDownLatch latch = new CountDownLatch(expect);
    @SuppressWarnings("serial") IgniteBiPredicate<UUID, CacheEvent> callback = new IgniteBiPredicate<UUID, CacheEvent>() {

        @Override
        public boolean apply(UUID uuid, CacheEvent evt) {
            latch.countDown();
            return true;
        }
    };
    remoteLsnr = ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(callback, null, EVT_CACHE_OBJECT_PUT);
    return latch;
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 13 with IgniteBiPredicate

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

the class CacheContinuousAsyncQueryExample 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 example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            int keyCnt = 20;
            // These entries will be queried by initial predicate.
            for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
            // Create new continuous query.
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {

                @Override
                public boolean apply(Integer key, String val) {
                    return key > 10;
                }
            }));
            // Callback that is called locally when update notifications are received.
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
                    for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                }
            });
            // This filter will be evaluated remotely on all nodes.
            // Entry that pass this filter will be sent to the caller.
            qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

                @Override
                public CacheEntryEventFilter<Integer, String> create() {
                    return new CacheEntryFilter();
                }
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                // Iterate through existing data.
                for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                // Add a few more keys and watch more query notifications.
                for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
            // Iterate through entries which was updated from filter.
            for (int i = 0; i < 10; i++) System.out.println("Entry updated from filter [key=" + i + ", val=" + cache.get(i) + ']');
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 14 with IgniteBiPredicate

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

the class CacheEventsExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException, InterruptedException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache events example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            // This optional local callback is called for each event notification
            // that passed remote predicate listener.
            IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {

                @Override
                public boolean apply(UUID uuid, CacheEvent evt) {
                    System.out.println("Received event [evt=" + evt.name() + ", key=" + evt.key() + ", oldVal=" + evt.oldValue() + ", newVal=" + evt.newValue());
                    // Continue listening.
                    return true;
                }
            };
            // Remote listener which only accepts events for keys that are
            // greater or equal than 10 and if event node is primary for this key.
            IgnitePredicate<CacheEvent> rmtLsnr = new IgnitePredicate<CacheEvent>() {

                @Override
                public boolean apply(CacheEvent evt) {
                    System.out.println("Cache event [name=" + evt.name() + ", key=" + evt.key() + ']');
                    int key = evt.key();
                    return key >= 10 && ignite.affinity(CACHE_NAME).isPrimary(ignite.cluster().localNode(), key);
                }
            };
            // Subscribe to specified cache events on all nodes that have cache running.
            // Cache events are explicitly enabled in examples/config/example-ignite.xml file.
            ignite.events(ignite.cluster().forCacheNodes(CACHE_NAME)).remoteListen(locLsnr, rmtLsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
            // Generate cache events.
            for (int i = 0; i < 20; i++) cache.put(i, Integer.toString(i));
            // 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 : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 15 with IgniteBiPredicate

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

the class GridCacheQueryTransformerSelfTest method testLocalKeepBinaryFiltered.

/**
 * @throws Exception If failed.
 */
public void testLocalKeepBinaryFiltered() 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 {
                IgniteBiPredicate<Integer, BinaryObject> filter = new IgniteBiPredicate<Integer, BinaryObject>() {

                    @Override
                    public boolean apply(Integer k, BinaryObject v) {
                        return v.<Integer>field("idx") % 1000 == 0;
                    }
                };
                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<>(filter).setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(5, res.size());
        Collections.sort(res);
        for (int i = 0; i < 5; i++) assertEquals(i * 1000, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) 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)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)23 Ignite (org.apache.ignite.Ignite)18 UUID (java.util.UUID)12 CountDownLatch (java.util.concurrent.CountDownLatch)9 CacheEvent (org.apache.ignite.events.CacheEvent)8 IgniteCache (org.apache.ignite.IgniteCache)6 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)5 Cache (javax.cache.Cache)4 IgniteException (org.apache.ignite.IgniteException)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ScanQuery (org.apache.ignite.cache.query.ScanQuery)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 CacheException (javax.cache.CacheException)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 Ignition (org.apache.ignite.Ignition)2