use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.
the class RemoteContinuousQueryTest method testContinuousQueryWithProjections.
public void testContinuousQueryWithProjections() {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<KeyValuePair<String, Object[]>> updated = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultUpdated(String key, Object[] value) {
updated.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue()[0], 30);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
user1.setAge(23);
remoteCache.put("user" + user1.getId(), user1);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 1, (kv) -> kv.getValue()[0], 23);
expectElementsInQueue(left, 0);
user1.setAge(40);
user2.setAge(40);
user3.setAge(40);
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 3);
remoteCache.clear();
user1.setAge(21);
user2.setAge(22);
remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
expectElementsInQueue(joined, 2);
expectElementsInQueue(left, 0);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
}
use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.
the class RemoteContinuousQueryTest method testContinuousQueryChangingParameter.
public void testContinuousQueryChangingParameter() {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<KeyValuePair<String, Object[]>> updated = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultUpdated(String key, Object[] value) {
updated.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
continuousQuery.removeContinuousQueryListener(listener);
query.setParameter("ageParam", 40);
listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultUpdated(String key, Object[] value) {
updated.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 3);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
continuousQuery.removeContinuousQueryListener(listener);
}
use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.
the class RemoteContinuousQueryTest method testDisallowGroupingAndAggregation.
/**
* Using grouping and aggregation with continuous query is not allowed.
*/
@Test(expectedExceptions = HotRodClientException.class, expectedExceptionsMessageRegExp = ".*ISPN028509:.*")
public void testDisallowGroupingAndAggregation() {
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<User> query = qf.create("SELECT MAX(age) FROM sample_bank_account.User WHERE age >= 20");
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
};
continuousQuery.addContinuousQueryListener(query, listener);
}
use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.
the class ContinuousQueryObjectStorageTest method testContinuousQueryWithProjections.
public void testContinuousQueryWithProjections() throws InterruptedException {
User user1 = new UserPB();
user1.setId(1);
user1.setName("John");
user1.setSurname("Doe");
user1.setGender(User.Gender.MALE);
user1.setAge(22);
user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
user1.setNotes("Lorem ipsum dolor sit amet");
User user2 = new UserPB();
user2.setId(2);
user2.setName("Spider");
user2.setSurname("Man");
user2.setGender(User.Gender.MALE);
user2.setAge(32);
user2.setAccountIds(Collections.singleton(3));
User user3 = new UserPB();
user3.setId(3);
user3.setName("Spider");
user3.setSurname("Woman");
user3.setGender(User.Gender.FEMALE);
user3.setAge(40);
remoteCache.clear();
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
final BlockingQueue<String> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {
@Override
public void resultJoining(String key, Object[] value) {
joined.add(new KeyValuePair<>(key, value));
}
@Override
public void resultLeaving(String key) {
left.add(key);
}
};
ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
user3.setAge(30);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 1, (kv) -> kv.getValue()[0], 30);
expectElementsInQueue(left, 0);
expectNoMoreElementsInQueues(joined, left);
user1.setAge(40);
user2.setAge(40);
user3.setAge(40);
remoteCache.put("user" + user1.getId(), user1);
remoteCache.put("user" + user2.getId(), user2);
remoteCache.put("user" + user3.getId(), user3);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 3);
remoteCache.clear();
user1.setAge(21);
user2.setAge(22);
remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
expectElementsInQueue(joined, 2);
expectElementsInQueue(left, 0);
timeService.advance(6);
assertNull(remoteCache.get("expiredUser1"));
assertNull(remoteCache.get("expiredUser2"));
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 2);
continuousQuery.removeContinuousQueryListener(listener);
user2.setAge(22);
remoteCache.put("user" + user2.getId(), user2);
expectElementsInQueue(joined, 0);
expectElementsInQueue(left, 0);
}
use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.
the class HotRodCacheContinuousQueries method testQueries.
@Test
public void testQueries() {
RemoteCache<Integer, User> remoteCache = ClusteredIT.createQueryableCache(SERVER_TEST, indexed);
remoteCache.put(1, createUser(1, 25));
remoteCache.put(2, createUser(2, 25));
remoteCache.put(3, createUser(3, 20));
assertEquals(3, remoteCache.size());
QueryFactory qf = Search.getQueryFactory(remoteCache);
Query<User> query = qf.create("FROM sample_bank_account.User WHERE name = 'user1' AND age > 20");
final BlockingQueue<Integer> joined = new LinkedBlockingQueue<>();
final BlockingQueue<Integer> updated = new LinkedBlockingQueue<>();
final BlockingQueue<Integer> left = new LinkedBlockingQueue<>();
ContinuousQueryListener<Integer, User> listener = new ContinuousQueryListener<Integer, User>() {
@Override
public void resultJoining(Integer key, User value) {
joined.add(key);
}
@Override
public void resultUpdated(Integer key, User value) {
updated.add(key);
}
@Override
public void resultLeaving(Integer key) {
left.add(key);
}
};
ContinuousQuery<Integer, User> continuousQuery = Search.getContinuousQuery(remoteCache);
continuousQuery.addContinuousQueryListener(query, listener);
expectElementsInQueue(joined, 1);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
User user4 = createUser(4, 30);
user4.setName("user1");
remoteCache.put(4, user4);
expectElementsInQueue(joined, 1);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
User user1 = remoteCache.get(1);
user1.setAge(19);
remoteCache.put(1, user1);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 1);
user4 = remoteCache.get(4);
user4.setAge(32);
remoteCache.put(4, user4);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 1);
expectElementsInQueue(left, 0);
remoteCache.clear();
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 1);
continuousQuery.removeContinuousQueryListener(listener);
user1.setAge(25);
remoteCache.put(1, user1);
expectElementsInQueue(joined, 0);
expectElementsInQueue(updated, 0);
expectElementsInQueue(left, 0);
}
Aggregations