Search in sources :

Example 26 with QueryObserver

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

the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreationDifferentTypes.

@Test
public void testBasicMultiIndexCreationDifferentTypes() throws Exception {
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("statusIndex", "status", r.getFullPath());
    qs.defineHashIndex("IDIndex", "ID", r.getFullPath());
    qs.defineKeyIndex("keyIDIndex", "ID", r.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(r, "statusIndex");
    assertTrue(ind instanceof CompactRangeIndex);
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "IDIndex");
    assertTrue(ind instanceof HashIndex);
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "keyIDIndex");
    assertTrue(ind instanceof PrimaryKeyIndex);
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

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

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        assertEquals(5, sr.size());
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 27 with QueryObserver

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

the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegions.

@Test
public void testIndexCreationOnMultipleRegions() throws Exception {
    Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
    for (int i = 0; i < 10; i++) {
        pr.put("" + i, new Portfolio(i));
    }
    Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
    for (int i = 0; i < 10; i++) {
        overflow.put("" + i, new Portfolio(i));
    }
    Region r = CacheUtils.getRegion(regionName);
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("IDIndex", "ID", pr.getFullPath());
    qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
    qs.defineIndex("statusIndex", "status", overflow.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(overflow, "statusIndex");
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(pr, "IDIndex");
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "secIDIndex");
    assertEquals(12, ind.getStatistics().getNumberOfKeys());
    assertEquals(20, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

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

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        if (i == 2) {
            assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
        } else {
            assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
        }
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 28 with QueryObserver

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

the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegionsBeforePuts.

@Test
public void testIndexCreationOnMultipleRegionsBeforePuts() throws Exception {
    Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
    Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
    Region r = CacheUtils.getRegion(regionName);
    QueryService qs = CacheUtils.getQueryService();
    qs.defineIndex("IDIndex", "ID", pr.getFullPath());
    qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
    qs.defineIndex("statusIndex", "status", overflow.getFullPath());
    List<Index> indexes = qs.createDefinedIndexes();
    for (int i = 0; i < 10; i++) {
        r.put("" + i, new Portfolio(i));
    }
    for (int i = 0; i < 10; i++) {
        pr.put("" + i, new Portfolio(i));
    }
    for (int i = 0; i < 10; i++) {
        overflow.put("" + i, new Portfolio(i));
    }
    assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
    Index ind = qs.getIndex(overflow, "statusIndex");
    assertEquals(2, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(pr, "IDIndex");
    assertEquals(10, ind.getStatistics().getNumberOfKeys());
    assertEquals(10, ind.getStatistics().getNumberOfValues());
    ind = qs.getIndex(r, "secIDIndex");
    assertEquals(12, ind.getStatistics().getNumberOfKeys());
    assertEquals(20, ind.getStatistics().getNumberOfValues());
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

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

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
    for (int i = 0; i < queries.length; i++) {
        SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
        if (i == 2) {
            assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
        } else {
            assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
        }
    }
    QueryObserverHolder.setInstance(old);
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) HashIndex(org.apache.geode.cache.query.internal.index.HashIndex) PrimaryKeyIndex(org.apache.geode.cache.query.internal.index.PrimaryKeyIndex) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex.

/**
   * 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 testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios pf WHERE pf.ID > 0 limit 5";
        query = qs.newQuery(queryString);
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios pf");
        assertNotNull(index);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
        assertTrue(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) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 30 with QueryObserver

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBagWithIndex.

/**
   * 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 as distinct will eliminate them.
   * This test validates the above behaviour if projection sttribute is present and the projection
   * attribute may be duplicate
   * 
   * Tests ResultBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBagWithIndex() {
    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;
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios1 pf");
        assertNotNull(index);
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
        assertTrue(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) 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