Search in sources :

Example 21 with Portfolio

use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.

the class IndexMaintenanceAsynchJUnitTest method init.

private static void init() {
    try {
        String queryString;
        Query query;
        Object result;
        Cache cache = CacheUtils.getCache();
        region = CacheUtils.createRegion("portfolios", Portfolio.class, false);
        for (int i = 0; i < 4; i++) {
            region.put("" + i, new Portfolio(i));
        }
        qs = cache.getQueryService();
        index = (IndexProtocol) qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
        IndexStatistics stats = index.getStatistics();
        assertEquals(4, stats.getNumUpdates());
        // queryString= "SELECT DISTINCT * FROM /portfolios p, p.positions.values pos where
        // pos.secId='IBM'";
        queryString = "SELECT DISTINCT * FROM /portfolios";
        query = CacheUtils.getQueryService().newQuery(queryString);
        result = query.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
    isInitDone = true;
}
Also used : IndexStatistics(org.apache.geode.cache.query.IndexStatistics) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) Cache(org.apache.geode.cache.Cache)

Example 22 with Portfolio

use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.

the class IndexUsageInNestedQueryJUnitTest method setUp.

@Before
public void setUp() throws java.lang.Exception {
    CacheUtils.startCache();
    Region r = CacheUtils.createRegion("portfolios", Portfolio.class);
    for (int i = 0; i < 4; i++) r.put(i + "", new Portfolio(i));
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Before(org.junit.Before)

Example 23 with Portfolio

use of org.apache.geode.cache.query.data.Portfolio 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 24 with Portfolio

use of org.apache.geode.cache.query.data.Portfolio in project geode by apache.

the class IndexUsageWithAliasAsProjAtrbtJUnitTest method testQueryResultComposition.

@Test
public void testQueryResultComposition() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    CacheUtils.getQueryService();
    String[] queries = { // "select distinct intern from /pos,names where length >= 3",
    "select distinct nm from /pos prt,names nm where ID>0", "select distinct prt from /pos prt, names where names[3]='ddd'" };
    for (int i = 0; i < queries.length; i++) {
        Query q = CacheUtils.getQueryService().newQuery(queries[i]);
        q.execute();
    }
}
Also used : Query(org.apache.geode.cache.query.Query) 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 25 with Portfolio

use of org.apache.geode.cache.query.data.Portfolio 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

Portfolio (org.apache.geode.cache.query.data.Portfolio)520 Test (org.junit.Test)415 Region (org.apache.geode.cache.Region)302 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)298 SelectResults (org.apache.geode.cache.query.SelectResults)247 Query (org.apache.geode.cache.query.Query)235 QueryService (org.apache.geode.cache.query.QueryService)195 Index (org.apache.geode.cache.query.Index)121 AttributesFactory (org.apache.geode.cache.AttributesFactory)85 Iterator (java.util.Iterator)81 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)72 LocalRegion (org.apache.geode.internal.cache.LocalRegion)69 VM (org.apache.geode.test.dunit.VM)65 Host (org.apache.geode.test.dunit.Host)64 Cache (org.apache.geode.cache.Cache)62 CacheException (org.apache.geode.cache.CacheException)49 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)47 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)47 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)47 IndexExistsException (org.apache.geode.cache.query.IndexExistsException)46