Search in sources :

Example 51 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitQueryForStructBagWithRangeIndex.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults
   * 
   * Tests StructBag behaviour
   */
@Ignore
@Test
public void testLimitQueryForStructBagWithRangeIndex() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT * FROM /portfolios pf, pf.positions.values pos WHERE pf.ID > 1  AND pos.secId = 'GOOG' limit 1";
        query = qs.newQuery(queryString);
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios pf");
        Index posindex = qs.createIndex("posIndex", "pos.secId", "/portfolios pf, pf.positions.values pos");
        assertNotNull(index);
        assertNotNull(posindex);
        result = (SelectResults) query.execute();
        assertEquals(1, result.size());
        assertFalse(observer.limitAppliedAtIndex);
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Index(org.apache.geode.cache.query.Index) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 52 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitJunctionOnRangeIndexedFieldWithAndClauseOnNonIndexedField.

@Test
public void testLimitJunctionOnRangeIndexedFieldWithAndClauseOnNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(i);
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(i);
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values POS");
    String queryString = "SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 9 AND P.ID < 21 AND POS.secId = 'VMW' LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 53 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForStructBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryForStructBag() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT DISTINCT * FROM /portfolios pf, pf.positions.values WHERE pf.ID > 0 limit 5";
        query = qs.newQuery(queryString);
        final int[] num = new int[1];
        num[0] = 0;
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }
        });
        result = (SelectResults) query.execute();
        assertEquals(5, num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 54 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttribute.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults . This test contains
   * projection attributes. Since the attribute is unique every time, the limit will be satisfied
   * with first 5 iterations
   * 
   * Tests ResultBag behaviour
   * 
   */
@Test
public void testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttribute() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios pf WHERE pf.ID > 0 limit 5";
        query = qs.newQuery(queryString);
        final int[] num = new int[1];
        num[0] = 0;
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }
        });
        result = (SelectResults) query.execute();
        assertEquals(5, num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 55 with QueryObserver

use of org.apache.geode.cache.query.internal.QueryObserver 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)

Aggregations

QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)71 SelectResults (org.apache.geode.cache.query.SelectResults)55 Test (org.junit.Test)48 Region (org.apache.geode.cache.Region)47 Query (org.apache.geode.cache.query.Query)42 Portfolio (org.apache.geode.cache.query.data.Portfolio)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)41 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)38 Index (org.apache.geode.cache.query.Index)31 QueryService (org.apache.geode.cache.query.QueryService)25 LocalRegion (org.apache.geode.internal.cache.LocalRegion)23 Collection (java.util.Collection)17 Cache (org.apache.geode.cache.Cache)13 AttributesFactory (org.apache.geode.cache.AttributesFactory)12 Position (org.apache.geode.cache.query.data.Position)9 Iterator (java.util.Iterator)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 Set (java.util.Set)7 Host (org.apache.geode.test.dunit.Host)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7