Search in sources :

Example 1 with Portfolio

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

the class LikePredicateJUnitTest method testUndefined.

@Test
public void testUndefined() throws Exception {
    String[] queries = { "select * from /pos where status.toUpperCase() like '%ACT%'", "select * from /pos where status.toUpperCase() like 'ACT%'", "select * from /pos where status.toUpperCase() like '%IVE'", "select * from /pos where status.toUpperCase() like 'ACT_VE'", "select * from /pos where status like '%CT%'", "select * from /pos where status like 'ACT%'", "select * from /pos where status like 'ACT_VE'", "select * from /pos where position1.secId like '%B%'", "select * from /pos where position1.secId like 'IB%'", "select * from /pos where position1.secId like 'I_M'", "select * from /pos p, p.positions.values pos where pos.secId like '%B%'", "select * from /pos p, p.positions.values pos where pos.secId like 'IB%'", "select * from /pos p, p.positions.values pos where pos.secId like 'I_M'" };
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    for (int i = 0; i < 10; i++) {
        Portfolio p = new Portfolio(i);
        if (i % 2 == 0) {
            p.status = null;
            p.position1 = null;
            p.positions = null;
        } else {
            p.position1 = new Position("IBM", 0);
            p.status = "ACTIVE";
        }
        region.put("key-" + i, p);
    }
    SelectResults[][] res = new SelectResults[queries.length][2];
    QueryService qs = CacheUtils.getCache().getQueryService();
    SelectResults results = null;
    for (int i = 0; i < queries.length; i++) {
        Query q = qs.newQuery(queries[i]);
        try {
            results = (SelectResults) q.execute();
            res[i][0] = results;
        } catch (Exception e) {
            fail("Query execution failed for query " + queries[i] + " with exception: " + e);
        }
        if (i < 10) {
            assertEquals("Query " + queries[i] + " should return 5 results and not " + results.size(), 5, results.size());
        } else {
            assertEquals("Query " + queries[i] + " should return 1 results and not " + results.size(), 1, results.size());
        }
    }
    try {
        qs.createIndex("status", "status", region.getFullPath());
        qs.createIndex("position1secId", "pos1.secId", region.getFullPath());
        qs.createIndex("secId", "pos.secId", region.getFullPath() + " pos, pos.secId");
    } catch (Exception e) {
        fail("Index creation failed");
    }
    for (int i = 0; i < queries.length; i++) {
        Query q = qs.newQuery(queries[i]);
        try {
            results = (SelectResults) q.execute();
            res[i][1] = results;
        } catch (Exception e) {
            fail("Query execution failed for query " + queries[i] + " with exception: " + e);
        }
        if (i < 10) {
            assertEquals("Query " + queries[i] + " should return 5 results and not " + results.size(), 5, results.size());
        } else {
            assertEquals("Query " + queries[i] + " should return 1 results and not " + results.size(), 1, results.size());
        }
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(res);
}
Also used : Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with Portfolio

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

the class LikePredicateJUnitTest method executeInvalidQueryWithLike.

private void executeInvalidQueryWithLike(boolean useBindParam) {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    for (int i = 0; i < 10; i++) {
        region.put("key-" + i, new Portfolio(i));
    }
    region.put("key-" + 11, new PortfolioModifiedStatus(11));
    QueryService qs = cache.getQueryService();
    Query[] q = new Query[4];
    SelectResults[][] results = new SelectResults[2][2];
    String predicate = "";
    String predicate2 = "";
    if (useBindParam) {
        predicate = "$1";
        predicate2 = "$1";
    } else {
        predicate = " '%nactive'";
        predicate2 = " 'inactive'";
    }
    boolean exceptionThrown = false;
    q[0] = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    q[1] = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status = " + predicate2);
    q[2] = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE NOT (ps.status like " + predicate + ")");
    q[3] = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE NOT (ps.status = " + predicate2 + ")");
    try {
        if (useBindParam) {
            results[0][0] = (SelectResults) q[0].execute(new Object[] { "%nactive" });
            results[0][1] = (SelectResults) q[1].execute(new Object[] { "inactive" });
            results[1][0] = (SelectResults) q[2].execute(new Object[] { "%nactive" });
            results[1][1] = (SelectResults) q[3].execute(new Object[] { "inactive" });
        } else {
            results[0][0] = (SelectResults) q[0].execute();
            results[0][1] = (SelectResults) q[1].execute();
            results[1][0] = (SelectResults) q[2].execute();
            results[1][1] = (SelectResults) q[3].execute();
        }
    } catch (Exception e) {
        exceptionThrown = true;
        fail("Query execution failed " + e);
    }
    assertTrue(results[0][0].size() > 0);
    assertTrue(CacheUtils.compareResultsOfWithAndWithoutIndex(results));
    assertFalse("Query exception should not have been thrown", exceptionThrown);
    try {
        qs.createIndex("IDindex", "ps.ID", "/pos ps");
    } catch (Exception e) {
        fail("Index creation failed");
    }
    exceptionThrown = false;
    try {
        if (useBindParam) {
            results[0][0] = (SelectResults) q[0].execute(new Object[] { "%nactive" });
            results[0][1] = (SelectResults) q[1].execute(new Object[] { "inactive" });
            results[1][0] = (SelectResults) q[2].execute(new Object[] { "%nactive" });
            results[1][1] = (SelectResults) q[3].execute(new Object[] { "inactive" });
        } else {
            results[0][0] = (SelectResults) q[0].execute();
            results[0][1] = (SelectResults) q[1].execute();
            results[1][0] = (SelectResults) q[2].execute();
            results[1][1] = (SelectResults) q[3].execute();
        }
    } catch (Exception e) {
        exceptionThrown = true;
        fail("Query execution failed " + e);
    }
    assertTrue(results[0][0].size() > 0);
    assertTrue(CacheUtils.compareResultsOfWithAndWithoutIndex(results));
    assertFalse("Query exception should not have been thrown", exceptionThrown);
}
Also used : Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) Portfolio(org.apache.geode.cache.query.data.Portfolio) AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 3 with Portfolio

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

the class LikePredicateJUnitTest method testQueryExecutionMultipleTimesWithBindParams.

@Test
public void testQueryExecutionMultipleTimesWithBindParams() throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    Region region2 = cache.createRegion("pos2", regionAttributes);
    // Create Index
    Index i1 = cache.getQueryService().createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
    Index i2 = cache.getQueryService().createIndex("description", IndexType.FUNCTIONAL, "ps.description", "/pos ps");
    Index i3 = cache.getQueryService().createIndex("description2", IndexType.FUNCTIONAL, "ps2.description", "/pos2 ps2");
    for (int i = 0; i < 10; i++) {
        Portfolio p = new Portfolio(i);
        region.put("key-" + i, p);
        region2.put("key-" + i, p);
    }
    executeQueryMultipleTimes("SELECT distinct *  FROM /pos ps WHERE ps.status like $1", true);
    assertEquals(2, i1.getStatistics().getTotalUses());
    assertEquals(0, i2.getStatistics().getTotalUses());
    assertEquals(0, i3.getStatistics().getTotalUses());
    executeQueryMultipleTimes("SELECT distinct *  FROM /pos ps WHERE ps.status like $1 or ps.description like $2", true);
    assertEquals(4, i1.getStatistics().getTotalUses());
    assertEquals(2, i2.getStatistics().getTotalUses());
    assertEquals(0, i3.getStatistics().getTotalUses());
    executeQueryMultipleTimes("SELECT distinct *  FROM /pos ps WHERE ps.ID >= 0 and ps.status like $1", true);
    assertEquals(6, i1.getStatistics().getTotalUses());
    assertEquals(2, i2.getStatistics().getTotalUses());
    assertEquals(0, i3.getStatistics().getTotalUses());
    executeQueryMultipleTimes("SELECT distinct *  FROM /pos ps1, /pos2 ps2 WHERE ps1.status like $1 or ps2.description like $2", true);
    assertEquals(8, i1.getStatistics().getTotalUses());
    assertEquals(2, i2.getStatistics().getTotalUses());
    assertEquals(2, i3.getStatistics().getTotalUses());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with Portfolio

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

the class LikePredicateJUnitTest method likePercentageTerminated_1.

/**
   * Tests simple % terminated pattern with atleast one preceding character
   * 
   * @throws Exception
   */
private void likePercentageTerminated_1(boolean useBindParam) 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);
    }
    base = "abd";
    ch = 'd';
    for (int i = 6; i < 11; ++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 (useBindParam) {
        predicate = "$1";
    } else {
        predicate = " 'abc%'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindParam) {
        results = (SelectResults) q.execute(new Object[] { "abc%" });
    } 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");
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

        public void afterIndexLookup(Collection results) {
            indexCalled = true;
        }

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    if (useBindParam) {
        results = (SelectResults) q.execute(new Object[] { "abc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs[0][0] = results;
    rs[0][1] = expectedResults;
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    QueryObserverHolder.setInstance(old);
}
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) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) 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) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) ResultsBag(org.apache.geode.cache.query.internal.ResultsBag) Cache(org.apache.geode.cache.Cache)

Example 5 with Portfolio

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

the class LikePredicateJUnitTest method likePercentageTerminated_4.

/**
   * Tests a simple % terminated like predicate with an AND condition
   * 
   * @throws Exception
   */
private void likePercentageTerminated_4(boolean useBindPrm) throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    String base = "abc";
    String pattern = base;
    // so we will get string like abcdcdcdcdcdc
    for (int i = 1; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pattern += "dc";
        pf.status = pattern;
        region.put(new Integer(i), pf);
    }
    base = "abd";
    pattern = base;
    // so we will get string like abddcdcdcd
    for (int i = 201; i < 400; ++i) {
        Portfolio pf = new Portfolio(i);
        pattern += "dc";
        pf.status = pattern;
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    SelectResults expectedResults;
    String predicate = "";
    if (useBindPrm) {
        predicate = "$1";
    } else {
        predicate = " 'abc%'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate + " AND ps.ID > 2 AND ps.ID < 150");
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "abc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    ResultsBag bag = new ResultsBag(null);
    for (int i = 3; i < 150; ++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");
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean[] indexCalled = new boolean[] { false, false };

        private int i = 0;

        public void afterIndexLookup(Collection results) {
            indexCalled[i++] = true;
        }

        public void endQuery() {
            assertTrue(indexCalled[0]);
            assertFalse(indexCalled[1]);
        }
    });
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "abc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs[0][0] = results;
    rs[0][1] = expectedResults;
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    qs.createIndex("id", IndexType.FUNCTIONAL, "ps.ID", "/pos ps");
    QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean[] indexCalled = new boolean[] { false, false };

        private int i = 0;

        public void afterIndexLookup(Collection results) {
            indexCalled[i++] = true;
        }

        public void endQuery() {
            // Only one indexed condition should be called
            boolean indexInvoked = false;
            for (int i = 0; i < indexCalled.length; ++i) {
                indexInvoked = indexInvoked || indexCalled[i];
            }
            assertTrue(indexInvoked);
        }
    });
    if (useBindPrm) {
        results = (SelectResults) q.execute(new Object[] { "abc%" });
    } else {
        results = (SelectResults) q.execute();
    }
    rs[0][0] = results;
    rs[0][1] = expectedResults;
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    QueryObserverHolder.setInstance(old);
}
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) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) 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) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) ResultsBag(org.apache.geode.cache.query.internal.ResultsBag) Cache(org.apache.geode.cache.Cache)

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