Search in sources :

Example 41 with QueryObserver

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

the class IndexMaintenanceJUnitTest method testQueryOnHeterogenousObjects.

/**
   * Tests query on region containing heterogenous objects
   */
@Test
public void testQueryOnHeterogenousObjects() {
    try {
        DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = true;
        Cache cache = CacheUtils.getCache();
        region = CacheUtils.createRegion("portfolio1", null);
        for (int i = 0; i < 5; ++i) {
            Portfolio p = new Portfolio(i + 1);
            region.put(new Integer(i + 1), p);
        }
        for (int i = 5; i < 10; ++i) {
            region.put(new Integer(i + 1), new Integer(i + 1));
        }
        String queryStr = "Select distinct * from /portfolio1 pf1 where pf1.getID() > 3";
        Query q = qs.newQuery(queryStr);
        SelectResults rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        Iterator itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID()", "/portfolio1 pf");
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            private boolean indexUsed = false;

            public void beforeIndexLookup(Index index, int oper, Object key) {
                indexUsed = true;
            }

            public void endQuery() {
                assertTrue(indexUsed);
            }
        });
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        qs.removeIndex(i1);
        queryStr = "Select distinct * from /portfolio1 pf1 where pf1.pkid > '3'";
        q = qs.newQuery(queryStr);
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.pkid", "/portfolio1 pf");
        QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            private boolean indexUsed = false;

            public void beforeIndexLookup(Index index, int oper, Object key) {
                indexUsed = true;
            }

            public void endQuery() {
                assertTrue(indexUsed);
            }
        });
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = false;
    }
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 42 with QueryObserver

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

the class IndexStatisticsJUnitTest method verifyReadLockCountStatsForRangeIndex.

public void verifyReadLockCountStatsForRangeIndex(boolean isPr) throws Exception {
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    String regionName = "exampleRegion";
    String name = "/" + regionName;
    final Cache cache = CacheUtils.getCache();
    Region r1 = cache.getRegion(regionName);
    QueryService qs = cache.getQueryService();
    Index secIdIndex = qs.createIndex("secId", "pos.secId", name + " p, p.positions.values pos");
    assertEquals(cache.getQueryService().getIndexes().size(), 1);
    for (int i = 0; i < 10; i++) {
        r1.put("key-" + i, new Portfolio(i));
    }
    String query = "select distinct * from " + name + " p, p.positions.values pos where pos.secId = 'IBM' ";
    final Query q = cache.getQueryService().newQuery(query);
    SelectResults sr = (SelectResults) q.execute();
    assertEquals("Read locks should have been taken by the query ", 1, observer.readLockCount);
    assertEquals("Read lock count should have been released by the query ", 0, secIdIndex.getStatistics().getReadLockCount());
    QueryObserverHolder.setInstance(old);
    qs.removeIndexes();
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Cache(org.apache.geode.cache.Cache)

Example 43 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part as distinct will eliminate them.
   * This test validates the above behaviour if projection attribute is present and the projection
   * attribute may be duplicate
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        num[0] = 0;
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT pf.ID , pf.createTime FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), 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 : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part of the resultset as distinct will
   * eliminate them
   * 
   * Tests ResultBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        num[0] = 0;
        // In the worst possible case all the unique values come in
        // consecutive order & hence only 5 iterations will yield the
        // result
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT * FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), 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 : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField.

@Test
public void testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 1;
        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(10);
        p.shortID = 2;
        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", "P.ID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 20 AND P.shortID > 1 AND P.shortID < 3 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)

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