Search in sources :

Example 21 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testCorrectOrCondResultWithMultiIndexes.

/**
   * Test verifies the result of query with 'OR' conditions when separate indexes are used for two
   * comparisons of OR clause. Like, "select * from /portfolio where ID > 0 OR status = 'active'"
   * and we have indexes on ID and status both. Both indexes must be used.
   */
@Test
public void testCorrectOrCondResultWithMultiIndexes() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 10; i++) {
        region.put("" + i, new Portfolio(i));
    }
    for (int i = 10; i < 20; i++) {
        Portfolio p = new Portfolio(i);
        if (i % 2 == 0) {
            p.status = null;
        }
        region.put("" + i, p);
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT * FROM /pos pf,  positions.values pos where pf.ID > 0 OR pf.status='active' OR  pos.secId != 'IBM'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status='active'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status LIKE 'act%'", "SELECT DISTINCT * FROM /pos pf where pf.ID > 0 OR pf.status='active' ORDER BY pf.status desc LIMIT 10", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status IN SET('active', 'inactive')" };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i][0] = (SelectResults) q.execute();
            if (observer.isIndexesUsed) {
                fail("How could index be present when not created!?");
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
    Index index2 = qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "pos.secId", "/pos pf, pf.positions.values pos");
    Index index3 = qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            sr[i][1] = (SelectResults) q.execute();
            if (!observer2.isIndexesUsed && !queries[i].contains("LIKE")) {
                fail("FAILED: Index NOT Used for query : " + q.getQueryString());
            } else if (!queries[i].contains("LIKE")) {
                assertTrue(observer2.indexesUsed.size() >= 2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : 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) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 22 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method executeQuery_1.

private void executeQuery_1(Region region, boolean checkReferentialIntegrity) throws Exception {
    // Region region = CacheUtils.createRegion("pos", Portfolio.class);
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /pos pf where pf.description = 'XXXX'  and pf.status='active' " };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    ObjectType resType1 = null, resType2 = null;
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i][0] = (SelectResults) q.execute();
            if (!observer.isIndexesUsed) {
                CacheUtils.log("NO INDEX USED");
            } else {
                fail("How did Index get created?");
            }
            // CacheUtils.log(Utils.printResult(r));
            resType1 = ((SelectResults) sr[i][0]).getCollectionType().getElementType();
            resSize1 = (((SelectResults) sr[i][0]).size());
            CacheUtils.log(resType1);
            // strg1=resType1.getFieldNames();
            set1 = (((SelectResults) sr[i][0]).asSet());
            Iterator iter = set1.iterator();
            while (iter.hasNext()) {
                valPf1 = iter.next();
                // valPf1 = stc1.get(strg1[0]);
                // valPos1 = stc1.get(strg1[1]);
                isActive1 = ((Portfolio) valPf1).isActive();
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
    Index index2 = (Index) qs.createIndex("IdIndex", IndexType.FUNCTIONAL, "pf.iD", "/pos pf");
    Index index3 = qs.createIndex("descriptionIndex", IndexType.FUNCTIONAL, "pf.description", "/pos pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            sr[i][1] = (SelectResults) q.execute();
            if (observer2.isIndexesUsed) {
                assertEquals(1, observer2.indexesUsed.size());
            } else {
                fail("FAILED: Index NOT Used");
            }
            resType2 = ((SelectResults) sr[i][1]).getCollectionType().getElementType();
            resSize2 = (((SelectResults) sr[i][1]).size());
            CacheUtils.log(resType2);
            // strg2=resType2.getFieldNames();
            set2 = (((SelectResults) sr[i][1]).asSet());
            Iterator iter = set2.iterator();
            while (iter.hasNext()) {
                valPf2 = iter.next();
                // valPf2=stc2.get(strg2[0]);
                // valPos2=stc2.get(strg2[1]);
                isActive2 = ((Portfolio) valPf2).isActive();
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    if ((resType1).equals(resType2)) {
        CacheUtils.log("Both Search Results are of the same Type i.e.--> " + resType1);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    if (resSize1 == resSize2 || resSize1 != 0) {
        CacheUtils.log("Search Results size is Non Zero and equal in both cases i.e.  Size= " + resSize1);
    } else {
        fail("FAILED:Search result size is different in both the cases");
    }
    itert2 = set2.iterator();
    itert1 = set1.iterator();
    while (itert1.hasNext()) {
        Object pos2 = itert2.next();
        Object pos1 = itert1.next();
        Object posFromRegion = region.get(((Portfolio) pos1).getPk());
        if (!pos1.equals(pos2))
            fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
        if (checkReferentialIntegrity) {
            assertTrue(pos2 == pos1);
            assertTrue(pos2 == posFromRegion);
        }
        /*
       * if(stc2.get(strg2[1]) != stc1.get(strg1[1]))
       * fail("FAILED: In both the cases Positions are different");
       * if(((Position)stc2.get(strg2[1])).secId != ((Position)stc1.get(strg1[1])).secId)
       * fail("FAILED: In both the cases Positions secIds are different");
       */
        if (((Portfolio) pos2).isActive() != ((Portfolio) pos1).isActive())
            fail("FAILED: Status of the Portfolios found are different");
        if (((Portfolio) pos2).getID() != ((Portfolio) pos1).getID())
            fail("FAILED: IDs of the Portfolios found are different");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Index(org.apache.geode.cache.query.Index) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator)

Example 23 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method executeQuery_3.

private void executeQuery_3(Region region, boolean checkReferentialIntegrity) throws Exception {
    // Region region = CacheUtils.createRegion("pos", Portfolio.class);
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /pos pf where pf.description = 'XXXX'  and pf.status='active' and pf.createTime = 5 " };
    ObjectType resType1 = null, resType2 = null;
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i][0] = (SelectResults) q.execute();
            if (!observer.isIndexesUsed) {
                CacheUtils.log("NO INDEX USED");
            }
            // CacheUtils.log(Utils.printResult(r));
            resType1 = ((SelectResults) sr[i][0]).getCollectionType().getElementType();
            resSize1 = (((SelectResults) sr[i][0]).size());
            CacheUtils.log(resType1);
            assertEquals(1, resSize1);
            set1 = (((SelectResults) sr[i][0]).asSet());
            Iterator iter = set1.iterator();
            while (iter.hasNext()) {
                valPf1 = iter.next();
                isActive1 = ((Portfolio) valPf1).isActive();
                assertTrue(isActive1);
                assertEquals("XXXX", ((Portfolio) valPf1).description);
                assertEquals(5, ((Portfolio) valPf1).getCreateTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    Index index1 = qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf");
    Index index2 = (Index) qs.createIndex("IdIndex", IndexType.FUNCTIONAL, "pf.iD", "/pos pf");
    Index index3 = qs.createIndex("descriptionIndex", IndexType.FUNCTIONAL, "pf.description", "/pos pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            sr[i][1] = (SelectResults) q.execute();
            if (observer2.isIndexesUsed) {
                assertEquals(1, observer2.indexesUsed.size());
            } else {
                fail("FAILED: Index NOT Used");
            }
            resType2 = ((SelectResults) sr[i][1]).getCollectionType().getElementType();
            resSize2 = (((SelectResults) sr[i][1]).size());
            // strg2=resType2.getFieldNames();
            set2 = (((SelectResults) sr[i][1]).asSet());
            Iterator iter = set2.iterator();
            while (iter.hasNext()) {
                valPf2 = iter.next();
                // valPf2=stc2.get(strg2[0]);
                // valPos2=stc2.get(strg2[1]);
                isActive2 = ((Portfolio) valPf2).isActive();
                assertTrue(isActive2);
                assertEquals("XXXX", ((Portfolio) valPf2).description);
                assertEquals(5, ((Portfolio) valPf2).getCreateTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    if ((resType1).equals(resType2)) {
        CacheUtils.log("Both Search Results are of the same Type i.e.--> " + resType1);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    if (resSize1 == resSize2 || resSize1 != 0) {
        CacheUtils.log("Search Results size is Non Zero and equal in both cases i.e.  Size= " + resSize1);
    } else {
        fail("FAILED:Search result size is different in both the cases");
    }
    itert2 = set2.iterator();
    itert1 = set1.iterator();
    while (itert1.hasNext()) {
        Object pos2 = itert2.next();
        Object pos1 = itert1.next();
        Object posFromRegion = region.get(((Portfolio) pos1).getPk());
        if (!pos1.equals(pos2))
            fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
        if (checkReferentialIntegrity) {
            assertTrue(pos2 == pos1);
            assertTrue(pos2 == posFromRegion);
        }
        if (((Portfolio) pos2).isActive() != ((Portfolio) pos1).isActive())
            fail("FAILED: Status of the Portfolios found are different");
        if (((Portfolio) pos2).getID() != ((Portfolio) pos1).getID())
            fail("FAILED: IDs of the Portfolios found are different");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Index(org.apache.geode.cache.query.Index) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator)

Example 24 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class IndexCreationJUnitTest method testIndexInitializationForOverFlowRegions.

@Test
public void testIndexInitializationForOverFlowRegions() throws Exception {
    InternalDistributedSystem.getAnyInstance().disconnect();
    File file = new File("persistData0");
    file.mkdir();
    {
        Properties props = new Properties();
        props.setProperty(NAME, "test");
        props.setProperty(MCAST_PORT, "0");
        props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
        props.setProperty(ENABLE_TIME_STATISTICS, "true");
        props.setProperty(CACHE_XML_FILE, getClass().getResource("index-recovery-overflow.xml").toURI().getPath());
        DistributedSystem ds = DistributedSystem.connect(props);
        // Create the cache which causes the cache-xml-file to be parsed
        Cache cache = CacheFactory.create(ds);
        QueryService qs = cache.getQueryService();
        Region region = cache.getRegion("mainReportRegion");
        for (int i = 0; i < 100; i++) {
            Portfolio pf = new Portfolio(i);
            pf.setCreateTime(i);
            region.put("" + i, pf);
        }
        IndexStatistics is1 = qs.getIndex(region, "status").getStatistics();
        assertEquals(2, is1.getNumberOfKeys());
        assertEquals(100, is1.getNumberOfValues());
        IndexStatistics is2 = qs.getIndex(region, "ID").getStatistics();
        assertEquals(100, is2.getNumberOfKeys());
        assertEquals(100, is2.getNumberOfValues());
        // verify that a query on the creation time works as expected
        SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
        assertEquals("OQL index results did not match", 1, results.size());
        cache.close();
        ds.disconnect();
    }
    {
        Properties props = new Properties();
        props.setProperty(NAME, "test");
        props.setProperty(MCAST_PORT, "0");
        props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
        props.setProperty(ENABLE_TIME_STATISTICS, "true");
        props.setProperty(CACHE_XML_FILE, getClass().getResource("index-recovery-overflow.xml").toURI().getPath());
        DistributedSystem ds = DistributedSystem.connect(props);
        Cache cache = CacheFactory.create(ds);
        QueryService qs = cache.getQueryService();
        Region region = cache.getRegion("mainReportRegion");
        assertTrue("Index initialization time should not be 0.", ((LocalRegion) region).getCachePerfStats().getIndexInitializationTime() > 0);
        IndexStatistics is1 = qs.getIndex(region, "status").getStatistics();
        assertEquals(2, is1.getNumberOfKeys());
        assertEquals(100, is1.getNumberOfValues());
        IndexStatistics is2 = qs.getIndex(region, "ID").getStatistics();
        assertEquals(100, is2.getNumberOfKeys());
        assertEquals(100, is2.getNumberOfValues());
        // verify that a query on the creation time works as expected
        SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
        assertEquals("OQL index results did not match", 1, results.size());
        ds.disconnect();
        FileUtils.deleteDirectory(file);
    }
}
Also used : IndexStatistics(org.apache.geode.cache.query.IndexStatistics) SelectResults(org.apache.geode.cache.query.SelectResults) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Properties(java.util.Properties) File(java.io.File) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 25 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class IndexCreationJUnitTest method testIndexCreationWithIndexOperatorUsage.

@Test
public void testIndexCreationWithIndexOperatorUsage() throws Exception {
    // Task ID : ICM 18
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "select distinct * from /portfolios pf where pf.collectionHolderMap[(pf.ID).toString()].arr[pf.ID] != -1" };
    Object[][] r = new Object[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        q = qs.newQuery(queries[i]);
        CacheUtils.getLogger().info("Executing query: " + queries[i]);
        r[i][0] = q.execute();
        CacheUtils.log("Executed query:" + queries[i]);
    }
    Index i1 = qs.createIndex("fIndex", IndexType.FUNCTIONAL, "sIter", "/portfolios pf, pf.collectionHolderMap[(pf.ID).toString()].arr sIter");
    Index i2 = qs.createIndex("cIndex", IndexType.FUNCTIONAL, "pf.collectionHolderMap[(pf.ID).toString()].arr[pf.ID]", "/portfolios pf");
    // pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        q = qs.newQuery(queries[i]);
        CacheUtils.getLogger().info("Executing query: " + queries[i]);
        QueryObserverImpl observer = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer);
        r[i][1] = q.execute();
        SelectResults results = (SelectResults) r[i][1];
        assertTrue(results.size() > 0);
        CacheUtils.log("Executing query: " + queries[i] + " with index created");
        if (!observer.isIndexesUsed) {
            fail("Index is NOT uesd");
        }
        Iterator itr = observer.indexesUsed.iterator();
        assertTrue(itr.hasNext());
        String temp = itr.next().toString();
        assertEquals(temp, "cIndex");
    }
    CacheUtils.log(((RangeIndex) i1).dump());
    CacheUtils.log(((CompactRangeIndex) i2).dump());
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
// CacheUtils.log(((RangeIndex)i3).dump());
// Index i3 =
// qs.createIndex("Task6Index",IndexType.FUNCTIONAL,"pos.secId","/portfolios
// pf, pf.positions.values pos");
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) CompactMapRangeIndex(org.apache.geode.cache.query.internal.index.CompactMapRangeIndex) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

QueryService (org.apache.geode.cache.query.QueryService)532 Test (org.junit.Test)383 SelectResults (org.apache.geode.cache.query.SelectResults)331 Region (org.apache.geode.cache.Region)316 Query (org.apache.geode.cache.query.Query)316 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)215 Portfolio (org.apache.geode.cache.query.data.Portfolio)199 CacheException (org.apache.geode.cache.CacheException)139 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)118 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 Index (org.apache.geode.cache.query.Index)109 Host (org.apache.geode.test.dunit.Host)104 VM (org.apache.geode.test.dunit.VM)104 Iterator (java.util.Iterator)102 Cache (org.apache.geode.cache.Cache)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)86 Struct (org.apache.geode.cache.query.Struct)70 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)68 IOException (java.io.IOException)67 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)65