Search in sources :

Example 11 with ResultsBag

use of org.apache.geode.cache.query.internal.ResultsBag in project geode by apache.

the class LikePredicateJUnitTest method likePercentageTerminated_2.

/**
   * Tests a pattern which just contains a single % indicating all match
   * 
   * @throws Exception
   */
private void likePercentageTerminated_2(boolean useBindParam) throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    char ch = 'd';
    String base = "abc";
    for (int i = 1; i < 6; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.status = base + ch;
        ch += 1;
        region.put(new Integer(i), pf);
    }
    base = "abd";
    ch = 'd';
    for (int i = 6; i < 11; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.status = base + ch;
        ch += 1;
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    SelectResults expectedResults;
    String predicate = "";
    if (useBindParam) {
        predicate = "$1";
    } else {
        predicate = " '%'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindParam) {
        results = (SelectResults) q.execute(new Object[] { "%" });
    } else {
        results = (SelectResults) q.execute();
    }
    ResultsBag bag = new ResultsBag(null);
    for (int i = 1; i < 11; ++i) {
        bag.add(region.get(new Integer(i)));
    }
    expectedResults = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
    SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    // Create Index
    qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    if (useBindParam) {
        results = (SelectResults) q.execute(new Object[] { "%" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs[0][0] = results;
    rs[0][1] = expectedResults;
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    QueryObserverHolder.setInstance(old);
}
Also used : Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) Portfolio(org.apache.geode.cache.query.data.Portfolio) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) ResultsCollectionWrapper(org.apache.geode.cache.query.internal.ResultsCollectionWrapper) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) ResultsBag(org.apache.geode.cache.query.internal.ResultsBag) Cache(org.apache.geode.cache.Cache)

Example 12 with ResultsBag

use of org.apache.geode.cache.query.internal.ResultsBag in project geode by apache.

the class LikePredicateJUnitTest method testLikePredicateOnNullValues.

@Test
public void testLikePredicateOnNullValues() throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    char ch = 'd';
    String base = "abc";
    for (int i = 1; i < 6; i++) {
        Portfolio pf = new Portfolio(i);
        pf.pkid = "abc";
        if (i % 2 == 0) {
            pf.pkid = null;
            pf.status = "like";
        } else if (i == 3) {
            pf.status = null;
        }
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    String[] query = new String[] { "SELECT distinct *  FROM /pos ps WHERE ps.pkid like '%b%'", "SELECT * FROM /pos ps WHERE ps.pkid like '%b%' and ps.status like '%ctiv%'", "SELECT * FROM /pos ps WHERE ps.pkid like '_bc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like 'abc%'", "SELECT pkid FROM /pos ps WHERE ps.pkid = 'abc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.status = 'like'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.status like '%ike'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.pkid like '_bc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like 'ml%' or ps.status = 'like'" };
    // null check
    for (int i = 0; i < query.length; i++) {
        q = qs.newQuery(query[i]);
        // No NPE.
        results = (SelectResults) q.execute();
    }
    // validate results
    q = qs.newQuery(query[0]);
    results = (SelectResults) q.execute();
    assertEquals("Result size is not as expected", 3, results.size());
    q = qs.newQuery(query[1]);
    results = (SelectResults) q.execute();
    assertEquals("Result size is not as expected", 2, results.size());
    // Create index.
    qs.createIndex("pkid", IndexType.FUNCTIONAL, "ps.pkid", "/pos ps");
    qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
    for (int i = 0; i < query.length; i++) {
        q = qs.newQuery(query[i]);
        // No NPE.
        results = (SelectResults) q.execute();
    }
    ResultsBag bag = new ResultsBag(null);
    for (int i = 1; i < 6; ++i) {
        bag.add(region.get(new Integer(i)));
    }
}
Also used : Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) Portfolio(org.apache.geode.cache.query.data.Portfolio) AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) ResultsBag(org.apache.geode.cache.query.internal.ResultsBag) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ResultsBag (org.apache.geode.cache.query.internal.ResultsBag)12 SelectResults (org.apache.geode.cache.query.SelectResults)10 AttributesFactory (org.apache.geode.cache.AttributesFactory)9 Region (org.apache.geode.cache.Region)9 Cache (org.apache.geode.cache.Cache)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 Query (org.apache.geode.cache.query.Query)8 QueryService (org.apache.geode.cache.query.QueryService)8 Portfolio (org.apache.geode.cache.query.data.Portfolio)8 ResultsCollectionWrapper (org.apache.geode.cache.query.internal.ResultsCollectionWrapper)8 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)8 Collection (java.util.Collection)6 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)6 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)6 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1