public void testAttributeQuery() throws Exception {
    RemoteCache<Integer, Memo> remoteCache = remoteCacheManager.getCache();

    remoteCache.put(1, createMemo1());
    remoteCache.put(2, createMemo2());

    // get memo1 back from remote cache and check its attributes
    Memo fromCache = remoteCache.get(1);
    assertMemo1(fromCache);

    // get memo1 back from remote cache via query and check its attributes
    QueryFactory qf = Search.getQueryFactory(remoteCache);
    Query query = qf.from(Memo.class).having("text").like("%ipsum%").toBuilder().build();
    List<Memo> list = query.list();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(Memo.class, list.get(0).getClass());
    assertMemo1(list.get(0));

    // get memo2 back from remote cache via query and check its attributes
    query = qf.from(Memo.class).having("author.name").eq("Adrian").toBuilder().build();
    list = query.list();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(Memo.class, list.get(0).getClass());
    assertMemo2(list.get(0));
  }
  public void testProjections() throws Exception {
    final RemoteCache<Integer, User> remoteCache0 = client(0).getCache();
    final RemoteCache<Integer, User> remoteCache1 = client(1).getCache();

    remoteCache0.put(1, createUser1());
    remoteCache1.put(2, createUser2());

    // get user back from remote cache and check its attributes
    User fromCache = remoteCache0.get(1);
    assertUser(fromCache);

    // get user back from remote cache via query and check its attributes
    QueryFactory qf = Search.getQueryFactory(remoteCache1);
    Query query =
        qf.from(User.class)
            .setProjection("name", "surname")
            .having("name")
            .eq("Tom")
            .toBuilder()
            .build();
    List<Object[]> list = query.list();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(Object[].class, list.get(0).getClass());
    assertEquals("Tom", list.get(0)[0]);
    assertEquals("Cat", list.get(0)[1]);
  }
  @Test
  public void testMatcherAndObjectFilterWithDSL() throws Exception {
    Matcher matcher = createMatcher();
    Object person = createPerson1();

    QueryFactory qf = createQueryFactory();
    Query q = qf.from(Person.class).having("name").eq("John").toBuilder().build();

    final boolean b[] = new boolean[1];

    FilterSubscription filterSubscription =
        matcher.registerFilter(
            q,
            new FilterCallback() {
              @Override
              public void onFilterResult(
                  Object userContext,
                  Object instance,
                  Object eventType,
                  Object[] projection,
                  Comparable[] sortProjection) {
                b[0] = true;
              }
            });

    ObjectFilter objectFilter = matcher.getObjectFilter(filterSubscription);

    ObjectFilter.FilterResult result = objectFilter.filter(person);
    assertNotNull(result);
    assertTrue(result.getInstance() == person);

    matcher.match(null, person, null);
    assertTrue(b[0]);
  }
  @Test
  public void testObjectFilterWithDSLSamePredicate1() throws Exception {
    Matcher matcher = createMatcher();
    Object person = createPerson1();

    QueryFactory qf = createQueryFactory();

    // use the same '< 1000' predicate on two different attributes to demonstrate they do not
    // interfere (see ISPN-4654)
    Query q =
        qf.from(Person.class)
            .having("id")
            .lt(1000)
            .and()
            .having("age")
            .lt(1000)
            .toBuilder()
            .build();

    ObjectFilter objectFilter = matcher.getObjectFilter(q);

    ObjectFilter.FilterResult result = objectFilter.filter(person);
    assertNotNull(result);
    assertTrue(result.getInstance() == person);
  }
 @Test
 public void testDSL() throws Exception {
   QueryFactory qf = createQueryFactory();
   Query q =
       qf.from(Person.class).having("phoneNumbers.number").eq("004012345").toBuilder().build();
   assertTrue(match(q, createPerson1()));
 }
  public void testEventFilterAndConverter() {
    QueryFactory qf = Search.getQueryFactory(cache());

    Query query =
        qf.from(Person.class).having("age").lte(31).toBuilder().select("name", "age").build();

    EntryListener listener = new EntryListener();

    // we want our cluster listener to be notified only if the entity matches our query
    cache().addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      cache.put(i, value);
    }

    assertEquals(10, cache.size());
    assertEquals(7, listener.createEvents.size());
    assertTrue(listener.modifyEvents.isEmpty());

    for (ObjectFilter.FilterResult r : listener.createEvents) {
      assertNotNull(r.getProjection());
      assertEquals(2, r.getProjection().length);
      assertTrue((Integer) r.getProjection()[1] <= 31);
    }

    cache().removeListener(listener);
  }
  public void testEventFilter() {
    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(99);

      cache().put(i, value);
    }
    assertEquals(10, cache.size());

    QueryFactory qf = Search.getQueryFactory(cache());

    Query query =
        qf.from(Person.class)
            .having("age")
            .lte(param("ageParam"))
            .toBuilder()
            .build()
            .setParameter("ageParam", 31);

    EntryListener listener = new EntryListener();

    // we want our cluster listener to be notified only if the entity matches our query
    cache().addListener(listener, Search.makeFilter(query), null);

    assertTrue(listener.createEvents.isEmpty());
    assertTrue(listener.modifyEvents.isEmpty());

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      cache().put(i, value);
    }

    assertEquals(10, cache.size());
    assertTrue(listener.createEvents.isEmpty());
    assertEquals(7, listener.modifyEvents.size());

    for (ObjectFilter.FilterResult r : listener.modifyEvents) {
      Person p = (Person) r.getInstance();
      assertTrue(p.getAge() <= 31);
    }

    cache().removeListener(listener);

    // ensure no more invocations after the listener was removed
    listener.createEvents.clear();
    listener.modifyEvents.clear();
    Person value = new Person();
    value.setName("George");
    value.setAge(30);

    cache().put(-1, value);
    assertTrue(listener.createEvents.isEmpty());
    assertTrue(listener.modifyEvents.isEmpty());
  }
  protected CallCountingCQResultListener<Object, Object> createContinuousQuery() {
    QueryFactory<?> qf = Search.getQueryFactory(cache(0));

    Query query = qf.from(Person.class).having("age").lte(30).toBuilder().build();

    CallCountingCQResultListener<Object, Object> listener = new CallCountingCQResultListener<>();
    ContinuousQuery<Object, Object> cq = new ContinuousQuery<Object, Object>(cache(0));
    cq.addContinuousQueryListener(query, listener);
    return listener;
  }
Beispiel #9
0
  @SuppressWarnings("rawtypes")
  private static QueryBuilder getQueryBuilder(String cacheName, InfinispanConnection conn)
      throws TranslatorException {

    Class<?> type = conn.getCacheClassType();

    QueryFactory qf = conn.getQueryFactory();

    QueryBuilder qb = qf.from(type);

    return qb;
  }
  @Test
  public void testObjectFilterWithDSL() throws Exception {
    Matcher matcher = createMatcher();
    Object person = createPerson1();

    QueryFactory qf = createQueryFactory();
    Query q = qf.from(Person.class).having("name").eq("John").toBuilder().build();

    ObjectFilter objectFilter = matcher.getObjectFilter(q);

    ObjectFilter.FilterResult result = objectFilter.filter(person);
    assertNotNull(result);
    assertTrue(result.getInstance() == person);
  }
  public void testEventFilter() {
    QueryFactory qf = Search.getQueryFactory(cache(0));

    Query query = qf.from(org.infinispan.query.test.Person.class).having("age").lte(31).build();

    EntryListener listener = new EntryListener();

    for (int i = 0; i < 5; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 30);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    // we want our cluster listener to be notified only if the entity matches our query
    cache(0).addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    assertEquals(9, listener.results.size());
    for (ObjectFilter.FilterResult r : listener.results) {
      Person p = (Person) r.getInstance();
      assertTrue(p.getAge() <= 31);
    }

    cache(0).removeListener(listener);

    // ensure no more invocations after the listener was removed
    listener.results.clear();
    Person value = new Person();
    value.setName("George");
    value.setAge(30);

    Object key = new MagicKey(cache(0));
    cache(0).put(key, value);
    assertEquals(0, listener.results.size());
  }
  public void testEmbeddedAttributeQuery() throws Exception {
    final RemoteCache<Integer, User> remoteCache0 = client(0).getCache();
    final RemoteCache<Integer, User> remoteCache1 = client(1).getCache();

    remoteCache0.put(1, createUser1());
    remoteCache1.put(2, createUser2());

    // get user back from remote cache via query and check its attributes
    QueryFactory qf = Search.getQueryFactory(remoteCache1);
    Query query = qf.from(User.class).having("addresses.postCode").eq("1234").toBuilder().build();
    List<User> list = query.list();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals(User.class, list.get(0).getClass());
    assertUser(list.get(0));
  }
  public void testEventFilterAndConverter() {
    QueryFactory qf = Search.getQueryFactory(cache(0));

    Query query =
        qf.from(org.infinispan.query.test.Person.class)
            .having("age")
            .lte(31)
            .select("name", "age")
            .build();

    EntryListener listener = new EntryListener();

    for (int i = 0; i < 5; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 30);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    // we want our cluster listener to be notified only if the entity matches our query
    cache(0).addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    assertEquals(9, listener.results.size());
    for (ObjectFilter.FilterResult r : listener.results) {
      assertTrue((Integer) r.getProjection()[1] <= 31);
    }

    cache(0).removeListener(listener);
  }
  @Test
  public void testObjectFilterWithDSLSamePredicate2() throws Exception {
    Matcher matcher = createMatcher();
    Object person = createPerson1();

    QueryFactory qf = createQueryFactory();

    // use the same "like 'Jo%'" predicate (in positive and negative form) on the same attribute to
    // demonstrate they do not interfere (see ISPN-4654)
    Query q =
        qf.from(Person.class)
            .having("name")
            .like("Jo%")
            .and(qf.not().having("name").like("Jo%").or().having("id").lt(1000))
            .toBuilder()
            .build();

    ObjectFilter objectFilter = matcher.getObjectFilter(q);

    ObjectFilter.FilterResult result = objectFilter.filter(person);
    assertNotNull(result);
    assertTrue(result.getInstance() == person);
  }
 private Query makeQuery(Cache c) {
   QueryFactory qf = Search.getQueryFactory(c);
   return qf.from(Person.class).having("age").gte(18).toBuilder().build();
 }