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]);
  }
Ejemplo n.º 2
0
  private QueryResponse makeResponse(Query q) {
    List<?> list = q.list();
    int numResults = list.size();
    String[] projection = ((BaseQuery) q).getProjection();
    int projSize = projection != null ? projection.length : 0;
    List<WrappedMessage> results =
        new ArrayList<WrappedMessage>(projSize == 0 ? numResults : numResults * projSize);

    for (Object o : list) {
      if (projSize == 0) {
        results.add(new WrappedMessage(o));
      } else {
        Object[] row = (Object[]) o;
        for (int j = 0; j < projSize; j++) {
          results.add(new WrappedMessage(row[j]));
        }
      }
    }

    QueryResponse response = new QueryResponse();
    response.setTotalResults(q.getResultSize());
    response.setNumResults(numResults);
    response.setProjectionSize(projSize);
    response.setResults(results);
    return response;
  }
  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));
  }
Ejemplo n.º 4
0
  @SuppressWarnings("rawtypes")
  public static List<Object> performSearch(
      Condition where, OrderBy orderby, String cacheName, InfinispanConnection conn)
      throws TranslatorException {

    QueryBuilder qb = getQueryBuilder(cacheName, conn);

    if (orderby != null) {
      List<SortSpecification> sss = orderby.getSortSpecifications();
      for (SortSpecification spec : sss) {
        Expression exp = spec.getExpression();
        Column mdIDElement = ((ColumnReference) exp).getMetadataObject();
        SortOrder so = SortOrder.ASC;
        if (spec.getOrdering().name().equalsIgnoreCase(SortOrder.DESC.name())) {
          so = SortOrder.DESC;
        }
        qb = qb.orderBy(mdIDElement.getSourceName(), so);
      }
    }

    FilterConditionContext fcc = buildQueryFromWhereClause(where, qb, null);

    List<Object> results = null;

    Query query = null;
    if (fcc != null) {
      query = fcc.toBuilder().build();
      results = query.list();

      if (results == null) {
        return Collections.emptyList();
      }

    } else if (orderby != null) {
      query = qb.build();
      results = query.list();
      if (results == null) {
        return Collections.emptyList();
      }

    } else {
      results = new ArrayList<Object>();
      RemoteCache<?, Object> c = (RemoteCache<?, Object>) conn.getCache();
      for (Object id : c.keySet()) {
        results.add(c.get(id));
      }
    }

    return results;
  }
  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));
  }
Ejemplo n.º 6
0
 protected Iterator<?> getBaseIterator() {
   return baseQuery.list().iterator();
 }
  public void testEventFilterChangingParameter() {
    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);

    query.setParameter("ageParam", 30);

    listener = new EntryListener();

    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(6, listener.modifyEvents.size());

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