Search in sources :

Example 26 with QueryService

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

the class IndexCreationJUnitTest method testIndexCreationWithoutLoadingData.

@Test
public void testIndexCreationWithoutLoadingData() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    Index i1 = ((DefaultQueryService) qs).createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios", null, false);
    Index i2 = ((DefaultQueryService) qs).createIndex("secIndex", IndexType.FUNCTIONAL, "pos.secId", "/portfolios p, p.positions.values pos", null, false);
    Index i3 = ((DefaultQueryService) qs).createIndex("statusHashIndex", IndexType.HASH, "status", "/portfolios", null, false);
    assertEquals("Index should have been empty ", 0, i1.getStatistics().getNumberOfKeys());
    assertEquals("Index should have been empty ", 0, i1.getStatistics().getNumberOfValues());
    assertEquals("Index should have been empty ", 0, i2.getStatistics().getNumberOfKeys());
    assertEquals("Index should have been empty ", 0, i2.getStatistics().getNumberOfValues());
    assertEquals("Index should have been empty ", 0, i3.getStatistics().getNumberOfKeys());
    assertEquals("Index should have been empty ", 0, i3.getStatistics().getNumberOfValues());
    qs.removeIndexes();
    i1 = ((DefaultQueryService) qs).createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios", null, true);
    i2 = ((DefaultQueryService) qs).createIndex("secIndex", IndexType.FUNCTIONAL, "pos.secId", "/portfolios p, p.positions.values pos", null, true);
    i3 = ((DefaultQueryService) qs).createIndex("statusHashIndex", IndexType.HASH, "status", "/portfolios", null, true);
    assertEquals("Index should not have been empty ", 2, i1.getStatistics().getNumberOfKeys());
    assertEquals("Index should not have been empty ", 4, i1.getStatistics().getNumberOfValues());
    assertEquals("Index should not have been empty ", 8, i2.getStatistics().getNumberOfKeys());
    assertEquals("Index should not have been empty ", 8, i2.getStatistics().getNumberOfValues());
    // hash
    assertEquals("Index should not have been empty ", 0, i3.getStatistics().getNumberOfKeys());
    // index
    // does
    // not
    // have
    // keys
    assertEquals("Index should not have been empty ", 4, i3.getStatistics().getNumberOfValues());
}
Also used : DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) 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) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 27 with QueryService

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

the class IndexPrimaryKeyUsageJUnitTest method testPrimaryKeyIndexUsageNegativeTestA.

@Test
public void testPrimaryKeyIndexUsageNegativeTestA() throws Exception {
    // Task ID: PKI 2
    Object[] r = new Object[5];
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "select distinct * from /portfolios x, x.positions.values where x.pk = '1'", "select distinct * from /portfolios.entries x, x.value.positions.values where x.value.pkid = '1'", "select distinct * from /portfolios.entries x, x.value.positions.values where x.key = '1'" };
    qs = CacheUtils.getQueryService();
    qs.createIndex("pkidIndex", IndexType.PRIMARY_KEY, "pkid", "/portfolios");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            r[i] = q.execute();
            if (!observer.isIndexesUsed == false) {
                fail("FAILED: INDEX IS USED!");
            }
            if (((SelectResults) r[i]).size() == 0) {
                fail("FAILED:Search result Size is zero");
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
}
Also used : Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 28 with QueryService

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

the class IndexUsageInNestedQueryJUnitTest method testNestedQueriesResultsasStructSet.

@Test
public void testNestedQueriesResultsasStructSet() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "select distinct * from /portfolios p, (select distinct pos  as poos from /portfolios x, x.positions.values pos" + " where pos.secId = 'YHOO') as k", "select distinct * from /portfolios p, (select distinct pos as poos from /portfolios p, p.positions.values pos" + " where pos.secId = 'YHOO') as k", "select distinct * from /portfolios p, (select distinct x.ID as ID  from /portfolios x" + // Currently Index Not Getting Used
    " where x.ID = p.ID) as k ", "select distinct * from /portfolios p, (select distinct pos as poos from /portfolios x, p.positions.values pos" + // Currently Index Not Getting Used
    " where x.ID = p.ID) as k", "select distinct * from /portfolios p, (select distinct x as pf , myPos as poos from /portfolios x, x.positions.values as myPos) as k " + "  where k.poos.secId = 'YHOO'", "select distinct * from /portfolios p, (select distinct x as pf , myPos as poos from /portfolios x, x.positions.values as myPos) as K" + "  where K.poos.secId = 'YHOO'", "select distinct * from /portfolios p, (select distinct val from positions.values as val where val.secId = 'YHOO') as k ", "select distinct * from /portfolios x, x.positions.values where " + "secId = element(select distinct vals.secId from /portfolios p, p.positions.values vals where vals.secId = 'YHOO')" };
    SelectResults[][] r = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            // DebuggerSupport.waitForJavaDebugger(CacheUtils.getLogger());
            r[i][0] = (SelectResults) q.execute();
            if (!observer.isIndexesUsed) {
                CacheUtils.log("NO INDEX USED");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    qs.createIndex("idIndex", IndexType.FUNCTIONAL, "p.ID", "/portfolios p");
    qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "b.secId", "/portfolios pf, pf.positions.values b");
    qs.createIndex("cIndex", IndexType.FUNCTIONAL, "pf.collectionHolderMap[(pf.ID).toString()].arr[pf.ID]", "/portfolios pf");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            r[i][1] = (SelectResults) q.execute();
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(r, this);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with QueryService

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

the class IndexUsageWithAliasAsProjAtrbtJUnitTest method testComparisonBetnWithAndWithoutIndexCreation.

@Test
public void testComparisonBetnWithAndWithoutIndexCreation() throws Exception {
    // TASK IUM 7
    Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { // IUM 7
    "Select distinct security from /portfolios, secIds security where length > 1", // IUM 8
    "Select distinct security from /portfolios , secIds security where length > 2 AND (intern <> 'SUN' OR intern <> 'DELL' )", // IUM 9
    "Select distinct  security from /portfolios  pos , secIds security where length > 2 and pos.ID > 0" };
    SelectResults[][] r = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        q = CacheUtils.getQueryService().newQuery(queries[i]);
        QueryObserverImpl observer = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer);
        r[i][0] = (SelectResults) q.execute();
        if (!observer.isIndexesUsed) {
            CacheUtils.log("NO INDEX USED");
        } else {
            fail("If index were not there how did they get used ???? ");
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    qs.createIndex("lengthIndex", IndexType.FUNCTIONAL, "length", "/portfolios,secIds, positions.values");
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        q = CacheUtils.getQueryService().newQuery(queries[i]);
        QueryObserverImpl observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        r[i][1] = (SelectResults) q.execute();
        if (observer2.isIndexesUsed) {
            CacheUtils.log("YES INDEX IS USED!");
        } else {
            fail("Index should have been used!!! ");
        }
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(r, 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) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 30 with QueryService

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

the class IndexUseMultFrmSnglCondJUnitTest method testIndexUsageComaprison.

@Test
public void testIndexUsageComaprison() throws Exception {
    Region region = CacheUtils.createRegion("portfolios", Portfolio.class);
    StructType resArType1 = null;
    StructType resArType2 = null;
    String[] strAr1 = null;
    String[] strAr2 = null;
    int resArSize1 = 0;
    int resArSize2 = 0;
    Object valPf1 = null;
    Object valPos1 = null;
    Object valPf2 = null;
    Object valPos2 = null;
    String SECID1 = null;
    String SECID2 = null;
    Iterator iter1 = null;
    Iterator iter2 = null;
    Set set1 = null;
    Set set2 = null;
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    QueryService qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * from /portfolios pf, pf.positions.values pos where pos.secId = 'IBM'" };
    SelectResults[][] r = 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);
            r[i][0] = (SelectResults) q.execute();
            if (observer.isIndexesUsed) {
                fail("If index were not there how did they get used ???? ");
            }
            resArType1 = (StructType) (r[i][0]).getCollectionType().getElementType();
            resArSize1 = ((r[i][0]).size());
            CacheUtils.log(resArType1);
            strAr1 = resArType1.getFieldNames();
            set1 = ((r[i][0]).asSet());
            Iterator iter = set1.iterator();
            while (iter.hasNext()) {
                Struct stc1 = (Struct) iter.next();
                valPf1 = stc1.get(strAr1[0]);
                valPos1 = stc1.get(strAr1[1]);
                SECID1 = (((Position) valPos1).getSecId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index and Run the Same Query as above.
    qs.createIndex("secIdIndex", IndexType.FUNCTIONAL, "b.secId", "/portfolios pf, pf.positions.values b");
    for (int j = 0; j < queries.length; j++) {
        Query q2 = null;
        try {
            q2 = CacheUtils.getQueryService().newQuery(queries[j]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            r[j][1] = (SelectResults) q2.execute();
            if (observer2.isIndexesUsed != true) {
                fail("FAILED: Index NOT Used");
            }
            resArType2 = (StructType) (r[j][1]).getCollectionType().getElementType();
            CacheUtils.log(resArType2);
            resArSize2 = (r[j][1]).size();
            strAr2 = resArType2.getFieldNames();
            set2 = ((r[j][1]).asSet());
            Iterator iter = set2.iterator();
            while (iter.hasNext()) {
                Struct stc2 = (Struct) iter.next();
                valPf2 = stc2.get(strAr2[0]);
                valPos2 = stc2.get(strAr2[1]);
                SECID2 = (((Position) valPos2).getSecId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q2.getQueryString());
        }
    }
    if ((resArType1).equals(resArType2)) {
        CacheUtils.log("Both Search Results are of the same Type i.e.--> " + resArType2);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    if ((resArSize1 == resArSize2) || resArSize1 != 0) {
        CacheUtils.log("Search Results Size is Non Zero and is of Same Size i.e.  Size= " + resArSize1);
    } else {
        fail("FAILED:Search result size is different in both the cases");
    }
    iter2 = set2.iterator();
    iter1 = set1.iterator();
    while (iter1.hasNext()) {
        Struct stc2 = (Struct) iter2.next();
        Struct stc1 = (Struct) iter1.next();
        if (stc2.get(strAr2[0]) != stc1.get(strAr1[0]))
            fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
        if (stc2.get(strAr2[1]) != stc1.get(strAr1[1]) || !((Position) stc1.get(strAr1[1])).secId.equals("IBM"))
            fail("FAILED: In both the cases either Positions Or secIds obtained are different");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(r, this);
}
Also used : Set(java.util.Set) StructType(org.apache.geode.cache.query.types.StructType) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) 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