Search in sources :

Example 11 with RegionAttributes

use of org.apache.geode.cache.RegionAttributes 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 12 with RegionAttributes

use of org.apache.geode.cache.RegionAttributes 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 13 with RegionAttributes

use of org.apache.geode.cache.RegionAttributes 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)

Example 14 with RegionAttributes

use of org.apache.geode.cache.RegionAttributes in project geode by apache.

the class LikePredicateJUnitTest method regexMetaChar.

/**
   * Tests for regular expression meta chars. This has no special meaning with Like.
   * 
   * @throws Exception
   */
private void regexMetaChar(boolean useBindPrms) throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    String[] values = new String[] { "active", "act**ve", "ac+t+ve", "?act?ve", "act)ve^", "|+act(ve", "act*+|ve", "^+act.ve+^", "act[]ve", "act][ve", "act^[a-z]ve", "act/ve", "inactive", "acxtxve", "ac(tiv)e", "act()ive", "act{}ive", "act{ive" };
    // Add values to region.
    for (int i = 0; i < values.length; i++) {
        region.put(new Integer(i), values[i]);
    }
    // Add % and _ with escape char.
    region.put(new Integer(values.length + 1), "act%+ive");
    region.put(new Integer(values.length + 2), "act_+ive");
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    for (int i = 0; i < values.length; i++) {
        if (!useBindPrms) {
            q = qs.newQuery("select p from /pos.values p where p like '" + values[i] + "'");
            results = (SelectResults) q.execute();
        } else {
            q = qs.newQuery("select p from /pos.values p where p like $1");
            results = (SelectResults) q.execute(new Object[] { values[i] });
        }
        List r = results.asList();
        if (r.size() != 1 || !r.get(0).equals(values[i])) {
            fail("Unexpected result. expected :" + values[i] + " for the like predicate: " + values[i] + " found : " + (r.size() == 1 ? r.get(0) : "Result size not equal to 1"));
        }
    }
    // Create Index
    qs.createIndex("p", IndexType.FUNCTIONAL, "p", "/pos.values p");
    for (int i = 0; i < values.length; i++) {
        if (!useBindPrms) {
            q = qs.newQuery("select p from /pos.values p where p like '" + values[i] + "'");
            results = (SelectResults) q.execute();
        } else {
            q = qs.newQuery("select p from /pos.values p where p like $1");
            results = (SelectResults) q.execute(new Object[] { values[i] });
        }
        List r = results.asList();
        if (r.size() != 1 || !r.get(0).equals(values[i])) {
            fail("Unexpected result. expected :" + values[i] + " for the like predicate: " + values[i] + " found : " + (r.size() == 1 ? r.get(0) : "Result size not equal to 1"));
        }
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) RegionAttributes(org.apache.geode.cache.RegionAttributes) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Example 15 with RegionAttributes

use of org.apache.geode.cache.RegionAttributes in project geode by apache.

the class LikePredicateJUnitTest method equalityForm_1.

/**
   * Tests simple non % or non _ terminated string which in effect means equality
   * 
   * @throws Exception
   */
private void equalityForm_1(boolean useBindPrms) 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 (useBindPrms) {
        predicate = "$1";
    } else {
        predicate = " 'abcd'";
    }
    q = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrms) {
        results = (SelectResults) q.execute(new Object[] { "abcd" });
    } else {
        results = (SelectResults) q.execute();
    }
    ResultsBag bag = new ResultsBag(null);
    bag.add(region.get(new Integer(1)));
    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 (useBindPrms) {
        results = (SelectResults) q.execute(new Object[] { "abcd" });
    } 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

RegionAttributes (org.apache.geode.cache.RegionAttributes)590 AttributesFactory (org.apache.geode.cache.AttributesFactory)471 Region (org.apache.geode.cache.Region)256 Test (org.junit.Test)251 Properties (java.util.Properties)158 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)128 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)126 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)118 LocalRegion (org.apache.geode.internal.cache.LocalRegion)112 Cache (org.apache.geode.cache.Cache)99 VM (org.apache.geode.test.dunit.VM)93 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)93 Host (org.apache.geode.test.dunit.Host)89 HashSet (java.util.HashSet)80 CacheException (org.apache.geode.cache.CacheException)65 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)62 CacheServer (org.apache.geode.cache.server.CacheServer)60 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)59 ArrayList (java.util.ArrayList)57 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)56