Search in sources :

Example 26 with IgnitePredicate

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

the class IgniteCacheReplicatedQuerySelfTest method testDistributedQuery.

/**
     * @throws Exception If test failed.
     */
public void testDistributedQuery() throws Exception {
    int keyCnt = 4;
    final CountDownLatch latch = new CountDownLatch(keyCnt * 2);
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            latch.countDown();
            return true;
        }
    };
    ignite2.events().localListen(lsnr, EventType.EVT_CACHE_OBJECT_PUT);
    ignite3.events().localListen(lsnr, EventType.EVT_CACHE_OBJECT_PUT);
    Transaction tx = ignite1.transactions().txStart();
    try {
        for (int i = 1; i <= keyCnt; i++) cache1.put(new CacheKey(i), new CacheValue(String.valueOf(i)));
        tx.commit();
        info("Committed transaction: " + tx);
    } catch (IgniteException e) {
        tx.rollback();
        throw e;
    }
    latch.await();
    QueryCursor<Cache.Entry<CacheKey, CacheValue>> qry = cache1.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4"));
    // Distributed query.
    assertEquals(2, qry.getAll().size());
    // Create new query, old query cannot be modified after it has been executed.
    qry = cache3.query(new SqlQuery<CacheKey, CacheValue>(CacheValue.class, "val > 1 and val < 4").setLocal(true));
    // Tests execute on node.
    Iterator<Cache.Entry<CacheKey, CacheValue>> iter = qry.iterator();
    assert iter != null;
    assert iter.hasNext();
    iter.next();
    assert iter.hasNext();
    iter.next();
    assert !iter.hasNext();
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Event(org.apache.ignite.events.Event)

Example 27 with IgnitePredicate

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

the class CacheUtils method foreach.

/**
     * @param cacheName Cache name.
     * @param fun An operation that accepts a cache entry and processes it.
     * @param keyFilter Cache keys filter.
     * @param <K> Cache key object type.
     * @param <V> Cache value object type.
     */
public static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
    bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) fun.accept(new CacheEntry<>(entry, cache));
        }
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 28 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testSingleFileOverwrite.

/**
     * Checks events on CRUD operations with single
     * file overwrite.
     *
     * @throws Exception If failed.
     */
public void testSingleFileOverwrite() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    // NB: In case of create-overwrite FILE_PURGED event will be sent in PRIMARY IGFS mode only.
    final boolean awaitForPurgeEvt = grid(1).configuration().getFileSystemConfiguration()[0].getDefaultMode() == IgfsMode.PRIMARY;
    final int evtsCnt = 1 + 4 + (awaitForPurgeEvt ? 1 : 0);
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    final IgfsPath file = new IgfsPath("/file1");
    // Will generate create, open and close events.
    igfs.create(file, false).close();
    // Will generate PURGE (async), OPEN_WRITE & close events.
    igfs.create(file, true).close();
    try {
        // Won't generate any event.
        igfs.create(file, false).close();
    } catch (Exception ignore) {
    // No-op.
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    final IgfsPath file1 = new IgfsPath("/file1");
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
    assertEquals(file1, evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
    assertEquals(file1, evt.path());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
    assertEquals(file1, evt.path());
    assertEquals(0, evt.dataSize());
    assertOneToOne(evtList.subList(3, evtsCnt), new P1<Event>() {

        @Override
        public boolean apply(Event e) {
            IgfsEvent e0 = (IgfsEvent) e;
            return e0.type() == EVT_IGFS_FILE_PURGED && e0.path().equals(file1);
        }
    }, new P1<Event>() {

        @Override
        public boolean apply(Event e) {
            IgfsEvent e0 = (IgfsEvent) e;
            return e0.type() == EVT_IGFS_FILE_OPENED_WRITE && e0.path().equals(file1);
        }
    }, new P1<Event>() {

        @Override
        public boolean apply(Event e) {
            IgfsEvent e0 = (IgfsEvent) e;
            return e0.type() == EVT_IGFS_FILE_CLOSED_WRITE && e0.path().equals(file1);
        }
    });
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 29 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testDeleteNonRecursive.

/**
     * Checks events on CRUD operations with non-recursive
     * directory deletion.
     *
     * @throws Exception If failed.
     */
public void testDeleteNonRecursive() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 2 + 0 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1/dir2");
    // Will generate 2 EVT_IGFS_DIR_CREATED events.
    igfs.mkdirs(dir);
    try {
        // Will generate no events.
        igfs.delete(dir.parent(), false);
    } catch (IgniteException ignore) {
    // No-op.
    }
    // Will generate 1 EVT_IGFS_DIR_DELETED event.
    assertTrue(igfs.delete(dir, false));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
    IgfsEvent evt3 = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_DIR_DELETED, evt3.type());
    assertEquals(new IgfsPath("/dir1/dir2"), evt3.path());
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 30 with IgnitePredicate

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

the class IgfsEventsAbstractSelfTest method testSingleEmptyDir.

/**
     * Checks events on CRUD operations on a single empty
     * directory.
     *
     * @throws Exception If failed.
     */
public void testSingleEmptyDir() throws Exception {
    final List<Event> evtList = new ArrayList<>();
    final int evtsCnt = 1 + 1 + 0 + 1;
    final CountDownLatch latch = new CountDownLatch(evtsCnt);
    grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            log.info("Received event [evt=" + evt + ']');
            evtList.add(evt);
            latch.countDown();
            return true;
        }
    }, EVTS_IGFS);
    IgfsPath dir = new IgfsPath("/dir1");
    // Will generate EVT_IGFS_DIR_CREATED.
    igfs.mkdirs(dir);
    IgfsPath mvDir = new IgfsPath("/mvDir1");
    // Will generate EVT_IGFS_DIR_RENAMED.
    igfs.rename(dir, mvDir);
    // Will generate no event.
    assertFalse(igfs.delete(dir, true));
    // Will generate EVT_IGFS_DIR_DELETED events.
    assertTrue(igfs.delete(mvDir, true));
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(evtsCnt, evtList.size());
    IgfsEvent evt = (IgfsEvent) evtList.get(0);
    assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(1);
    assertEquals(EVT_IGFS_DIR_RENAMED, evt.type());
    assertEquals(new IgfsPath("/dir1"), evt.path());
    assertEquals(new IgfsPath("/mvDir1"), evt.newPath());
    assertTrue(evt.isDirectory());
    evt = (IgfsEvent) evtList.get(2);
    assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
    assertEquals(new IgfsPath("/mvDir1"), evt.path());
    assertTrue(evt.isDirectory());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)75 Event (org.apache.ignite.events.Event)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 Ignite (org.apache.ignite.Ignite)27 ArrayList (java.util.ArrayList)15 UUID (java.util.UUID)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 HashMap (java.util.HashMap)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 Map (java.util.Map)6 IgniteException (org.apache.ignite.IgniteException)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 CacheQueryReadEvent (org.apache.ignite.events.CacheQueryReadEvent)6 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)6 Ignition (org.apache.ignite.Ignition)5