Search in sources :

Example 26 with Portfolio

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

the class IndexUseMultFrmSnglCondJUnitTest method testMultiFromWithSingleConditionUsingIndex.

@Test
public void testMultiFromWithSingleConditionUsingIndex() throws Exception {
    // create region 1 and 2
    Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
    Region region2 = CacheUtils.createRegion("portfolios2", Portfolio.class);
    for (int i = 0; i < 100; i++) {
        Portfolio p = null;
        if (i != 0 && i < 5) {
            p = new Portfolio(5);
        } else {
            p = new Portfolio(i);
        }
        region1.put(i, p);
        region2.put(i, p);
    }
    QueryService qs = CacheUtils.getQueryService();
    // create and execute query
    String queryString = "SELECT * from /portfolios1 P1, /portfolios2 P2 WHERE P1.ID = 5";
    Query query = qs.newQuery(queryString);
    SelectResults sr1 = (SelectResults) query.execute();
    // create index
    Index index = qs.createIndex("P1IDIndex", IndexType.FUNCTIONAL, "P1.ID", "/portfolios1 P1");
    // execute query
    SelectResults sr2 = (SelectResults) query.execute();
    assertEquals("Index result set does not match unindexed result set size", sr1.size(), sr2.size());
    // size will be number of matching in region 1 x region 2 size
    assertEquals("Query result set size does not match expected size", 5 * region2.size(), sr2.size());
}
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 27 with Portfolio

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

the class LikePredicateJUnitTest method likePercentageTerminated_5.

private void likePercentageTerminated_5(boolean useBindPrm) throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    char ch = 'd';
    String base = "abc";
    for (int i = 1; i < 6; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.status = base + ch;
        ch += 1;
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    SelectResults expectedResults;
    String predicate = "";
    if (useBindPrm) {
        predicate = "$1";
    } else {
        predicate = " 'a%c%'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "a%bc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    ResultsBag bag = new ResultsBag(null);
    for (int i = 1; i < 6; ++i) {
        bag.add(region.get(new Integer(i)));
    }
    expectedResults = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
    SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    // Create Index
    qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "a%bc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs = new SelectResults[][] { { results, expectedResults } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    if (useBindPrm) {
        predicate = "$1";
    } else {
        predicate = "'abc_'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "abc_" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs = new SelectResults[][] { { results, expectedResults } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    if (useBindPrm) {
        predicate = "$1";
    } else {
        predicate = "'_bc_'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "_bc_" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs = new SelectResults[][] { { results, expectedResults } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
}
Also used : Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) Portfolio(org.apache.geode.cache.query.data.Portfolio) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) ResultsCollectionWrapper(org.apache.geode.cache.query.internal.ResultsCollectionWrapper) Region(org.apache.geode.cache.Region) ResultsBag(org.apache.geode.cache.query.internal.ResultsBag) Cache(org.apache.geode.cache.Cache)

Example 28 with Portfolio

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

the class LimitClauseJUnitTest method testNotLimitAtIndexLevelForMultiSingleIndexAndClauseUsage.

/*
   * This test shows an error with order by. Limit does not affect this and will enable this test
   * after the bug for order by is logged and fixed
   * 
   * @Test public void testNotApplyingLimitAtIndexLevelForMultiIndexAndClauseUsageWithOrderBy()
   * throws Exception { //try { Query query; SelectResults result; int limit = 25; Region region =
   * CacheUtils.createRegion("portfolios1", Portfolio.class); for (int i = 30; i > 0; i--) {
   * Portfolio p = new Portfolio(i); p.positions.clear(); p.positions.put("IBM", new Position("IBM",
   * i)); region.put("KEY" + i, p); }
   * 
   * String queryString =
   * "<trace>SELECT distinct P FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 5 AND POS.secId = 'IBM' ORDER BY P.ID"
   * ; query = qs.newQuery(queryString); SelectResults resultsNoIndex = (SelectResults)
   * query.execute();
   * 
   * //Create Index on ID and secId //Index secIndex = qs.createIndex("secIdIndex", "pos.secId",
   * "/portfolios1 p, p.positions.values pos"); Index idIndex = qs.createIndex("idIndex",
   * IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P");
   * 
   * //assertNotNull(secIndex); assertNotNull(idIndex); assertTrue(idIndex instanceof
   * CompactRangeIndex); SelectResults resultsWithIndex = (SelectResults) query.execute();
   * 
   * assertIndexDetailsEquals(resultsNoIndex.size(), resultsWithIndex.size());
   * assertIndexDetailsEquals(limit, resultsWithIndex.size());
   * 
   * int expectedId = 6; Iterator iterator = resultsNoIndex.iterator(); while (iterator.hasNext()) {
   * Portfolio p = (Portfolio) iterator.next(); assertIndexDetailsEquals(expectedId++, p.ID); }
   * 
   * //check order by results expectedId = 6; iterator = resultsWithIndex.iterator(); while
   * (iterator.hasNext()) { Portfolio p = (Portfolio) iterator.next();
   * assertIndexDetailsEquals(expectedId++, p.ID); } }
   */
@Test
public void testNotLimitAtIndexLevelForMultiSingleIndexAndClauseUsage() throws Exception {
    // try {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 15; i > 0; i--) {
        Portfolio p = new Portfolio(i);
        // CacheUtils.log(p);
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    String queryString = "<trace>SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 4 and P.ID < 11 AND P.ID != 8 LIMIT 5";
    query = qs.newQuery(queryString);
    SelectResults resultsNoIndex = (SelectResults) query.execute();
    // Create Index on ID and secId
    Index secIndex = qs.createIndex("secIdIndex", "pos.secId", "/portfolios1 p, p.positions.values pos");
    Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values pos");
    // assertNotNull(secIndex);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertEquals(resultsNoIndex.size(), resultsWithIndex.size());
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) 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)

Example 29 with Portfolio

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag.

/**
   * 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 testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag() {
    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 {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, 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 30 with Portfolio

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

the class LimitClauseJUnitTest method testLimitJunctionOnRangeIndexedFieldWithAndClauseRangeIndexedField.

@Test
public void testLimitJunctionOnRangeIndexedFieldWithAndClauseRangeIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(i);
        p.shortID = 1;
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 21; i < 100; i++) {
        Portfolio p = new Portfolio(i);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(i);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values POS");
    Index shortIdIndex = qs.createIndex("shortIdIndex", IndexType.FUNCTIONAL, "P.shortID", "/portfolios1 P, P.positions.values POS");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 21 AND P.shortID = 2 LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    // assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) 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

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