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();
}
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));
}
});
}
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);
}
});
}
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());
}
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());
}
Aggregations