Search in sources :

Example 31 with Portfolio

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag.

/**
   * 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 of the resultset as distinct will
   * eliminate them
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag() {
    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 if (currObject instanceof Portfolio) {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT * FROM /portfolios1  pf, pf.collectionHolderMap.keySet  WHERE pf.ID > 10 limit 20";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((20 + 4 * numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(20, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(20, 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 32 with Portfolio

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

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testComparisonBetnWithAndWithoutIndexCreation.

@Test
public void testComparisonBetnWithAndWithoutIndexCreation() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /pos pf,  positions.values pos where pf.status='active' and pos.secId= 'IBM' and ID = 0" };
    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");
            } else {
                fail("How could index be present when not created!?");
            }
            // CacheUtils.log(Utils.printResult(r));
            resType1 = (StructType) ((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()) {
                Struct stc1 = (Struct) iter.next();
                valPf1 = stc1.get(strg1[0]);
                valPos1 = stc1.get(strg1[1]);
                isActive1 = ((Portfolio) stc1.get(strg1[0])).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("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) {
                fail("FAILED: Index NOT Used");
            }
            resType2 = (StructType) ((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()) {
                Struct stc2 = (Struct) iter.next();
                valPf2 = stc2.get(strg2[0]);
                valPos2 = stc2.get(strg2[1]);
                isActive2 = ((Portfolio) stc2.get(strg2[0])).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()) {
        Struct stc2 = (Struct) itert2.next();
        Struct stc1 = (Struct) itert1.next();
        if (stc2.get(strg2[0]) != stc1.get(strg1[0]))
            fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
        if (stc2.get(strg2[1]) != stc1.get(strg1[1]))
            fail("FAILED: In both the cases Positions are different");
        if (!StringUtils.equals(((Position) stc2.get(strg2[1])).secId, ((Position) stc1.get(strg1[1])).secId))
            fail("FAILED: In both the cases Positions secIds are different");
        if (((Portfolio) stc2.get(strg2[0])).isActive() != ((Portfolio) stc1.get(strg1[0])).isActive())
            fail("FAILED: Status of the Portfolios found are different");
        if (((Portfolio) stc2.get(strg2[0])).getID() != ((Portfolio) stc1.get(strg1[0])).getID())
            fail("FAILED: IDs of the Portfolios found are different");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) 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)

Example 33 with Portfolio

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

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testIndexUsageIfTwoFieldsIndexedAndOneUnindexed.

/**
   * Test index usage on PR region & Local region if the where clause contains three conditions with
   * two conditions indexed & the total number of from clause iterators is 1.
   */
@Test
public void testIndexUsageIfTwoFieldsIndexedAndOneUnindexed() throws Exception {
    AttributesFactory af = new AttributesFactory();
    af.setValueConstraint(Portfolio.class);
    RegionAttributes ra = af.createRegionAttributes();
    Region region = CacheUtils.getCache().createRegion("pos", ra);
    for (int i = 0; i < 5; i++) {
        region.put("" + i, new Portfolio(i));
    }
    // As default generation of Portfolio objects
    // The status is active for key =0 & key =2 & key =4
    // The description is XXXX for key =1, key =3
    // Let us make explitly
    // Set createTime as 5 for three Portfolio objects of key =0 & key =1 & key=2;
    // Description as XXXX for key =2 only.
    // Thus out of 4 objects created only 1 object ( key =2 ) will have
    // description = XXXX , status = active & time = 5
    ((Portfolio) region.get("0")).setCreateTime(5);
    ((Portfolio) region.get("1")).setCreateTime(5);
    ((Portfolio) region.get("2")).setCreateTime(5);
    ((Portfolio) region.get("2")).description = "XXXX";
    int numSatisfyingOurCond = 0;
    for (int i = 0; i < 5; i++) {
        Portfolio pf = (Portfolio) region.get("" + i);
        if (pf.description != null && pf.description.equals("XXXX") && pf.getCreateTime() == 5 && pf.isActive()) {
            ++numSatisfyingOurCond;
        }
    }
    assertEquals(1, numSatisfyingOurCond);
    executeQuery_3(region, true);
    region.destroyRegion();
    CacheUtils.closeCache();
    CacheUtils.restartCache();
    af = new AttributesFactory();
    af.setValueConstraint(Portfolio.class);
    PartitionAttributesFactory pfa = new PartitionAttributesFactory();
    pfa.setRedundantCopies(0);
    pfa.setTotalNumBuckets(1);
    af.setPartitionAttributes(pfa.create());
    ra = af.createRegionAttributes();
    region = CacheUtils.getCache().createRegion("pos", ra);
    for (int i = 0; i < 5; i++) {
        region.put("" + i, new Portfolio(i));
    }
    // As default generation of Portfolio objects
    // The status is active for key =0 & key =2 & key =4
    // The description is XXXX for key =1, key =3
    // Let us make explitly
    // Set createTime as 5 for three Portfolio objects of key =0 & key =1 & key=2;
    // Description as XXXX for key =2 only.
    // Thus out of 4 objects created only 1 object ( key =2 ) will have
    // description = XXXX , status = active & time = 5
    Portfolio x = (Portfolio) region.get("0");
    x.setCreateTime(5);
    region.put("0", x);
    x = (Portfolio) region.get("1");
    x.setCreateTime(5);
    region.put("1", x);
    x = (Portfolio) region.get("2");
    x.setCreateTime(5);
    x.description = "XXXX";
    region.put("2", x);
    numSatisfyingOurCond = 0;
    for (int i = 0; i < 5; i++) {
        Portfolio pf = (Portfolio) region.get("" + i);
        if (pf.description != null && pf.description.equals("XXXX") && pf.getCreateTime() == 5 && pf.isActive()) {
            ++numSatisfyingOurCond;
        }
    }
    assertEquals(1, numSatisfyingOurCond);
    executeQuery_3(region, false);
    region.destroyRegion();
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) 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 34 with Portfolio

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

the class IndexOperatorJUnitTest method testWithRegion.

@Test
public void testWithRegion() throws Exception {
    Region region = CacheUtils.createRegion("Portfolio", Portfolio.class);
    for (int i = 0; i < 5; i++) {
        region.put("" + i, new Portfolio(i));
    }
    Object result = null;
    Object index = "2";
    result = runQuery(region, index);
    if (result == null || !region.get(index).equals(result))
        fail("failed for Region");
}
Also used : 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 35 with Portfolio

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

the class IteratorTypeDefJUnitTest method testNOConstraintOnRegion.

@Test
public void testNOConstraintOnRegion() throws Exception {
    Region region = CacheUtils.createRegion("portfl", null);
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    CacheUtils.log(region);
    String[] queries = { "IMPORT org.apache.geode.cache.\"query\".data.Position;" + "IMPORT org.apache.geode.cache.\"query\".data.Portfolio;" + "SELECT DISTINCT secId FROM (set<Portfolio>)/portfl, (set<Position>)positions.values WHERE iD > 0" };
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            Object r = q.execute();
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    CacheUtils.log("TestCase: testNOConstraintOnRegion PASS");
}
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)

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