Search in sources :

Example 1 with Matcher

use of org.infinispan.objectfilter.Matcher in project infinispan by infinispan.

the class AbstractMatcherTest method testProjectionOnRepeatedAttribute.

@Test
public void testProjectionOnRepeatedAttribute() throws Exception {
    String queryString = "select p.address.postCode, p.phoneNumbers.number from org.infinispan.objectfilter.test.model.Person p where p.name = 'John'";
    Matcher matcher = createMatcher();
    Object person = createPerson1();
    List<Object[]> result = new ArrayList<>();
    FilterSubscription filterSubscription = matcher.registerFilter(queryString, (userContext, eventType, instance, projection, sortProjection) -> result.add(projection));
    assertNotNull(filterSubscription.getProjection());
    assertEquals(2, filterSubscription.getProjection().length);
    assertEquals("address.postCode", filterSubscription.getProjection()[0]);
    assertEquals("phoneNumbers.number", filterSubscription.getProjection()[1]);
    matcher.match(null, null, person);
    matcher.unregisterFilter(filterSubscription);
    assertEquals(1, result.size());
    assertEquals(2, result.get(0).length);
    assertEquals("SW12345", result.get(0)[0]);
    // todo [anistor] it is unclear what whe should expect here...
    // expect the first phone number
    assertEquals("0040888888", result.get(0)[1]);
}
Also used : Matcher(org.infinispan.objectfilter.Matcher) FilterSubscription(org.infinispan.objectfilter.FilterSubscription) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with Matcher

use of org.infinispan.objectfilter.Matcher in project infinispan by infinispan.

the class AbstractMatcherTest method testTautologyAndProjections.

/**
 * Test that projections are properly computed even if the query is a tautology so no predicates will ever be
 * computed.
 */
@Test
public void testTautologyAndProjections() throws Exception {
    String queryString = "select name from org.infinispan.objectfilter.test.model.Person where age < 30 or age >= 30";
    Matcher matcher = createMatcher();
    Object person = createPerson1();
    List<Object[]> result = new ArrayList<>();
    FilterSubscription filterSubscription = matcher.registerFilter(queryString, (userContext, eventType, instance, projection, sortProjection) -> result.add(projection));
    assertNotNull(filterSubscription.getProjection());
    assertEquals(1, filterSubscription.getProjection().length);
    assertEquals("name", filterSubscription.getProjection()[0]);
    matcher.match(null, null, person);
    matcher.unregisterFilter(filterSubscription);
    assertEquals(1, result.size());
    assertEquals(1, result.get(0).length);
    assertEquals("John", result.get(0)[0]);
}
Also used : Matcher(org.infinispan.objectfilter.Matcher) FilterSubscription(org.infinispan.objectfilter.FilterSubscription) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with Matcher

use of org.infinispan.objectfilter.Matcher in project infinispan by infinispan.

the class AbstractMatcherTest method testObjectFilterWithDSLSamePredicate2.

@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<Person> q = qf.from(Person.class).having("name").like("Jo%").and(qf.not().having("name").like("Jo%").or().having("id").lt(1000)).build();
    ObjectFilter objectFilter = matcher.getObjectFilter(q);
    ObjectFilter.FilterResult result = objectFilter.filter(person);
    assertNotNull(result);
    assertSame(person, result.getInstance());
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) Matcher(org.infinispan.objectfilter.Matcher) ObjectFilter(org.infinispan.objectfilter.ObjectFilter) Person(org.infinispan.objectfilter.test.model.Person) Test(org.junit.Test)

Example 4 with Matcher

use of org.infinispan.objectfilter.Matcher in project infinispan by infinispan.

the class RowMatcherTest method match.

private boolean match(Query<?> query, Object obj) {
    Matcher matcher = createMatcher();
    int[] matchCount = { 0 };
    matcher.registerFilter(query, (userContext, eventType, instance, projection, sortProjection) -> matchCount[0]++);
    matcher.match(null, null, obj);
    return matchCount[0] == 1;
}
Also used : Matcher(org.infinispan.objectfilter.Matcher) RowMatcher(org.infinispan.objectfilter.impl.RowMatcher)

Example 5 with Matcher

use of org.infinispan.objectfilter.Matcher in project infinispan by infinispan.

the class RowMatcherTest method testDuplicateProjections.

@Test
public void testDuplicateProjections() {
    String queryString = "select p.name, p.name, p.age from Row p where p.name = 'John'";
    Matcher matcher = createMatcher();
    Object person = createPerson1();
    List<Object[]> result = new ArrayList<>();
    FilterSubscription filterSubscription = matcher.registerFilter(queryString, (userContext, eventType, instance, projection, sortProjection) -> result.add(projection));
    assertNotNull(filterSubscription.getProjection());
    assertEquals(3, filterSubscription.getProjection().length);
    assertEquals("name", filterSubscription.getProjection()[0]);
    assertEquals("name", filterSubscription.getProjection()[1]);
    assertEquals("age", filterSubscription.getProjection()[2]);
    matcher.match(null, null, person);
    matcher.unregisterFilter(filterSubscription);
    assertEquals(1, result.size());
    assertEquals(3, result.get(0).length);
    assertEquals("John", result.get(0)[0]);
    assertEquals("John", result.get(0)[1]);
    assertEquals(40, result.get(0)[2]);
}
Also used : Matcher(org.infinispan.objectfilter.Matcher) RowMatcher(org.infinispan.objectfilter.impl.RowMatcher) FilterSubscription(org.infinispan.objectfilter.FilterSubscription) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Matcher (org.infinispan.objectfilter.Matcher)34 Test (org.junit.Test)27 FilterSubscription (org.infinispan.objectfilter.FilterSubscription)14 RowMatcher (org.infinispan.objectfilter.impl.RowMatcher)14 ObjectFilter (org.infinispan.objectfilter.ObjectFilter)13 ArrayList (java.util.ArrayList)8 Person (org.infinispan.objectfilter.test.model.Person)8 QueryFactory (org.infinispan.query.dsl.QueryFactory)4 ReflectionMatcher (org.infinispan.objectfilter.impl.ReflectionMatcher)2 FilterCallback (org.infinispan.objectfilter.FilterCallback)1