Search in sources :

Example 1 with ContinuousQueryListener

use of org.infinispan.query.api.continuous.ContinuousQueryListener in project quarkus by quarkusio.

the class TestServlet method onStart.

void onStart(@Observes StartupEvent ev) {
    log.info("Servlet has started");
    cache.addClientListener(new EventPrintListener());
    log.info("Added client listener");
    ContinuousQuery<String, Book> continuousQuery = Search.getContinuousQuery(cache);
    QueryFactory queryFactory = Search.getQueryFactory(cache);
    Query query = queryFactory.create("from book_sample.Book where publicationYear > 2011");
    ContinuousQueryListener<String, Book> listener = new ContinuousQueryListener<String, Book>() {

        @Override
        public void resultJoining(String key, Book value) {
            log.warn("Adding key: " + key + " for book: " + value);
            matches.put(key, value);
        }

        @Override
        public void resultLeaving(String key) {
            log.warn("Removing key: " + key);
            matches.remove(key);
        }

        @Override
        public void resultUpdated(String key, Book value) {
            log.warn("Entry updated: " + key);
        }
    };
    continuousQuery.addContinuousQueryListener(query, listener);
    log.info("Added continuous query listener");
    cache.put("book1", new Book("Game of Thrones", "Lots of people perish", 2010, Collections.singleton(new Author("George", "Martin")), Type.FANTASY, new BigDecimal("23.99")));
    cache.put("book2", new Book("Game of Thrones Path 2", "They win?", 2023, Collections.singleton(new Author("Son", "Martin")), Type.FANTASY, new BigDecimal("54.99")));
    magazineCache.put("first-mad", new Magazine("MAD", YearMonth.of(1952, 10), Collections.singletonList("Blob named Melvin")));
    magazineCache.put("first-time", new Magazine("TIME", YearMonth.of(1923, 3), Arrays.asList("First helicopter", "Change in divorce law", "Adam's Rib movie released", "German Reparation Payments")));
    magazineCache.put("popular-time", new Magazine("TIME", YearMonth.of(1997, 4), Arrays.asList("Yep, I'm gay", "Backlash against HMOS", "False Hope on Breast Cancer?")));
    log.info("Inserted values");
    waitUntilStarted.countDown();
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) ContinuousQuery(org.infinispan.query.api.continuous.ContinuousQuery) Query(org.infinispan.query.dsl.Query) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) BigDecimal(java.math.BigDecimal)

Example 2 with ContinuousQueryListener

use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest 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<Object[]> 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);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) Test(org.testng.annotations.Test) MultiHotRodServersTest(org.infinispan.client.hotrod.test.MultiHotRodServersTest)

Example 3 with ContinuousQueryListener

use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest method testContinuousQuery.

public void testContinuousQuery() {
    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<User> query = qf.<User>create("FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
    final BlockingQueue<KeyValuePair<String, User>> joined = new LinkedBlockingQueue<>();
    final BlockingQueue<String> left = new LinkedBlockingQueue<>();
    ContinuousQueryListener<String, User> listener = new ContinuousQueryListener<String, User>() {

        @Override
        public void resultJoining(String key, User 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().getAge(), 32, 22);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    user3.setAge(30);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 1, (kv) -> kv.getValue().getAge(), 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);
    expectNoMoreElementsInQueues(joined, left);
    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);
    expectNoMoreElementsInQueues(joined, left);
    timeService.advance(6);
    assertNull(remoteCache.get("expiredUser1"));
    assertNull(remoteCache.get("expiredUser2"));
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 2);
    expectNoMoreElementsInQueues(joined, left);
    continuousQuery.removeContinuousQueryListener(listener);
    user2.setAge(22);
    remoteCache.put("user" + user2.getId(), user2);
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 4 with ContinuousQueryListener

use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest method testContinuousQueryChangingParameter.

public void testContinuousQueryChangingParameter() 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> cq = Search.getContinuousQuery(remoteCache);
    cq.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    joined.clear();
    left.clear();
    cq.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 resultLeaving(String key) {
            left.add(key);
        }
    };
    cq.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 3);
    expectElementsInQueue(left, 0);
    cq.removeContinuousQueryListener(listener);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 5 with ContinuousQueryListener

use of org.infinispan.query.api.continuous.ContinuousQueryListener in project infinispan by infinispan.

the class RemoteContinuousQueryTest method testContinuousQuery.

public void testContinuousQuery() {
    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<User> query = qf.<User>create("FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
    final BlockingQueue<KeyValuePair<String, User>> joined = new LinkedBlockingQueue<>();
    final BlockingQueue<KeyValuePair<String, User>> updated = new LinkedBlockingQueue<>();
    final BlockingQueue<String> left = new LinkedBlockingQueue<>();
    ContinuousQueryListener<String, User> listener = new ContinuousQueryListener<String, User>() {

        @Override
        public void resultJoining(String key, User value) {
            joined.add(new KeyValuePair<>(key, value));
        }

        @Override
        public void resultUpdated(String key, User 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().getAge(), 32, 22);
    expectElementsInQueue(updated, 0);
    expectElementsInQueue(left, 0);
    user3.setAge(30);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 1, (kv) -> kv.getValue().getAge(), 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().getAge(), 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);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Aggregations

ContinuousQueryListener (org.infinispan.query.api.continuous.ContinuousQueryListener)10 QueryFactory (org.infinispan.query.dsl.QueryFactory)10 User (org.infinispan.query.dsl.embedded.testdomain.User)8 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)7 UserPB (org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)6 KeyValuePair (org.infinispan.util.KeyValuePair)6 MultiHotRodServersTest (org.infinispan.client.hotrod.test.MultiHotRodServersTest)2 Test (org.testng.annotations.Test)2 BigDecimal (java.math.BigDecimal)1 User (org.infinispan.protostream.sampledomain.User)1 ContinuousQuery (org.infinispan.query.api.continuous.ContinuousQuery)1 Query (org.infinispan.query.dsl.Query)1 Test (org.junit.Test)1