Search in sources :

Example 1 with IgniteBiPredicate

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

the class GridCacheQueryManager method backupsFilter.

/**
     * @param <K> Key type.
     * @param <V> Value type.
     * @param includeBackups Include backups.
     * @return Predicate.
     */
@SuppressWarnings("unchecked")
@Nullable
public <K, V> IndexingQueryFilter backupsFilter(boolean includeBackups) {
    if (includeBackups)
        return null;
    return new IndexingQueryFilter() {

        @Nullable
        @Override
        public IgniteBiPredicate<K, V> forCache(final String cacheName) {
            final GridKernalContext ctx = cctx.kernalContext();
            final GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
            if (cache.context().isReplicated() || cache.configuration().getBackups() == 0)
                return null;
            return new IgniteBiPredicate<K, V>() {

                @Override
                public boolean apply(K k, V v) {
                    return cache.context().affinity().primaryByKey(ctx.discovery().localNode(), k, NONE);
                }
            };
        }

        @Override
        public boolean isValueRequired() {
            return false;
        }
    };
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IgniteBiPredicate

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

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> {
        // Remote filter only accepts tasks whose name being with "good-task" prefix.
        assert evt.taskName().startsWith("good-task");
        System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
        // Return true to continue listening.
        return true;
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
Also used : EVTS_TASK_EXECUTION(org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION) Ignition(org.apache.ignite.Ignition) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteException(org.apache.ignite.IgniteException) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 3 with IgniteBiPredicate

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

the class IgniteMqttStreamerTest method subscribeToPutEvents.

/**
 * @param expect Expected count.
 * @return Latch to be counted down in listener.
 */
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);
    IgniteBiPredicate<UUID, CacheEvent> cb = 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(cb, 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 4 with IgniteBiPredicate

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

the class GridCacheQueryManager method scanIterator.

/**
 * @param qry Query.
 * @param transformer Transformer.
 * @param locNode Local node.
 * @return Full-scan row iterator.
 * @throws IgniteCheckedException If failed to get iterator.
 */
@SuppressWarnings({ "unchecked" })
private GridCloseableIterator scanIterator(final GridCacheQueryAdapter<?> qry, IgniteClosure transformer, boolean locNode) throws IgniteCheckedException {
    assert !cctx.mvccEnabled() || qry.mvccSnapshot() != null;
    final IgniteBiPredicate<K, V> keyValFilter = qry.scanFilter();
    final InternalScanFilter<K, V> intFilter = keyValFilter != null ? new InternalScanFilter<>(keyValFilter) : null;
    try {
        if (keyValFilter instanceof PlatformCacheEntryFilter)
            ((PlatformCacheEntryFilter) keyValFilter).cacheContext(cctx);
        else
            injectResources(keyValFilter);
        Integer part = cctx.isLocal() ? null : qry.partition();
        if (part != null && (part < 0 || part >= cctx.affinity().partitions()))
            return new GridEmptyCloseableIterator() {

                @Override
                public void close() throws IgniteCheckedException {
                    if (intFilter != null)
                        intFilter.close();
                    super.close();
                }
            };
        AffinityTopologyVersion topVer = GridQueryProcessor.getRequestAffinityTopologyVersion();
        if (topVer == null)
            topVer = cctx.affinity().affinityTopologyVersion();
        final boolean backups = qry.includeBackups() || cctx.isReplicated();
        final GridDhtLocalPartition locPart;
        final GridIterator<CacheDataRow> it;
        if (part != null) {
            final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
            GridDhtLocalPartition locPart0 = dht.topology().localPartition(part, topVer, false);
            if (locPart0 == null || locPart0.state() != OWNING || !locPart0.reserve()) {
                throw locPart0 != null && locPart0.state() == LOST ? new CacheInvalidStateException("Failed to execute scan query because cache partition has been " + "lost [cacheName=" + cctx.name() + ", part=" + part + "]") : new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
            }
            locPart = locPart0;
            it = cctx.offheap().cachePartitionIterator(cctx.cacheId(), part, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
        } else {
            locPart = null;
            if (!cctx.isLocal()) {
                final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
                Set<Integer> lostParts = dht.topology().lostPartitions();
                if (!lostParts.isEmpty()) {
                    throw new CacheInvalidStateException("Failed to execute scan query because cache partition " + "has been lost [cacheName=" + cctx.name() + ", part=" + lostParts.iterator().next() + "]");
                }
            }
            it = cctx.offheap().cacheIterator(cctx.cacheId(), true, backups, topVer, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
        }
        ScanQueryIterator iter = new ScanQueryIterator(it, qry, topVer, locPart, SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteBiPredicate.class, keyValFilter), SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteClosure.class, transformer), locNode, locNode ? locIters : null, cctx, log);
        if (locNode) {
            ScanQueryIterator old = locIters.addx(iter);
            assert old == null;
        }
        return iter;
    } catch (IgniteCheckedException | RuntimeException e) {
        if (intFilter != null)
            intFilter.close();
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridDhtUnreservedPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) PlatformCacheEntryFilter(org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException)

Example 5 with IgniteBiPredicate

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

the class CacheContinuousQueryExample 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 CacheEntryEventFilter<Integer, String>() {

                        @Override
                        public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
                            return e.getKey() > 10;
                        }
                    };
                }
            });
            // 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 = keyCnt; i < keyCnt + 10; 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) 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)

Aggregations

IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)57 Ignite (org.apache.ignite.Ignite)36 UUID (java.util.UUID)28 IgniteCache (org.apache.ignite.IgniteCache)22 Test (org.junit.Test)22 List (java.util.List)21 Collection (java.util.Collection)18 Map (java.util.Map)18 HashMap (java.util.HashMap)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 ClusterNode (org.apache.ignite.cluster.ClusterNode)17 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)17 ArrayList (java.util.ArrayList)16 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)16 ScanQuery (org.apache.ignite.cache.query.ScanQuery)15 IgniteEx (org.apache.ignite.internal.IgniteEx)15 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 IgniteException (org.apache.ignite.IgniteException)13 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)13