Search in sources :

Example 21 with QueryObserverAdapter

use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.

the class LikePredicateJUnitTest method equalityForm_2.

/**
   * Tests simple \% or \ _ terminated string which in effect means equality
   * 
   * @throws Exception
   */
private void equalityForm_2(boolean useBindPrms) throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    String str = "d_";
    String base = "abc";
    for (int i = 1; i < 6; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.status = base + str;
        region.put(new Integer(i), pf);
    }
    base = "abc";
    str = "d%";
    for (int i = 6; i < 11; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.status = base + str;
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q, q1;
    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);
    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);
    predicate = "";
    if (useBindPrms) {
        predicate = "$1";
    } else {
        predicate = " 'abcd\\%'";
    }
    q1 = qs.newQuery("SELECT distinct *  FROM /pos ps WHERE ps.status like " + predicate);
    if (useBindPrms) {
        results = (SelectResults) q1.execute(new Object[] { "abcd\\%" });
    } else {
        results = (SelectResults) q1.execute();
    }
    bag = new ResultsBag(null);
    for (int i = 6; i < 11; ++i) {
        bag.add(region.get(new Integer(i)));
    }
    SelectResults expectedResults1 = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
    SelectResults[][] rs1 = new SelectResults[][] { { results, expectedResults1 } };
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs1, 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);
    if (useBindPrms) {
        results = (SelectResults) q1.execute(new Object[] { "abcd\\%" });
    } else {
        results = (SelectResults) q1.execute();
    }
    rs1[0][0] = results;
    rs1[0][1] = expectedResults1;
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs1, 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 22 with QueryObserverAdapter

use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.

the class LikePredicateJUnitTest method testLikeWithOtherIndexedField2.

/**
   * Query with index on other fields.
   * 
   * @throws Exception
   */
@Test
public void testLikeWithOtherIndexedField2() throws Exception {
    Cache cache = CacheUtils.getCache();
    AttributesFactory attributesFactory = new AttributesFactory();
    RegionAttributes regionAttributes = attributesFactory.create();
    Region region = cache.createRegion("pos", regionAttributes);
    int size = 10;
    String base = "abc";
    for (int i = 1; i <= size; i++) {
        Portfolio pf = new Portfolio(i);
        pf.pkid = "1";
        if ((i % 4) == 0) {
            pf.status = base;
        } else if ((i <= 2)) {
            pf.pkid = "2";
        }
        region.put(new Integer(i), pf);
    }
    QueryService qs = cache.getQueryService();
    Query q;
    SelectResults results;
    SelectResults expectedResults;
    int expectedResultSize = 2;
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%'");
    results = (SelectResults) q.execute();
    if (results.size() != expectedResultSize) {
        fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' or ps.pkid = '2' ");
    results = (SelectResults) q.execute();
    if (results.size() != (expectedResultSize * 2)) {
        fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 0 ");
    results = (SelectResults) q.execute();
    if (results.size() != size) {
        fail("Unexpected result. expected :" + size + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 4 ");
    results = (SelectResults) q.execute();
    if (results.size() != 7) {
        fail("Unexpected result. expected :" + 7 + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' and ps.ID > 3 ");
    results = (SelectResults) q.execute();
    if (results.size() != 2) {
        fail("Unexpected result. expected :" + 2 + " found : " + results.size());
    }
    // Query to be compared with indexed results.
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' and ps.pkid = '1' ");
    expectedResults = (SelectResults) q.execute();
    if (expectedResults.size() != expectedResultSize) {
        fail("Unexpected result. expected :" + expectedResultSize + " found : " + expectedResults.size());
    }
    // Create Index
    qs.createIndex("pkid", IndexType.FUNCTIONAL, "ps.pkid", "/pos ps");
    qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
    qs.createIndex("id", IndexType.FUNCTIONAL, "ps.ID", "/pos ps");
    QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private boolean indexCalled = false;

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

        public void endQuery() {
            assertTrue(indexCalled);
        }
    });
    results = (SelectResults) q.execute();
    SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
    // rs[0][1] = expectedResults;
    if (results.size() != expectedResultSize) {
        fail("Unexpected result. expected :" + expectedResultSize + " found : " + results.size());
    }
    // compare results.
    CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '_b_' or ps.pkid = '2' ");
    results = (SelectResults) q.execute();
    if (results.size() != (expectedResultSize * 2)) {
        fail("Unexpected result. expected :" + (expectedResultSize * 2) + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 0 ");
    results = (SelectResults) q.execute();
    if (results.size() != size) {
        fail("Unexpected result. expected :" + size + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' or ps.ID > 4 ");
    results = (SelectResults) q.execute();
    if (results.size() != 7) {
        fail("Unexpected result. expected :" + 7 + " found : " + results.size());
    }
    q = qs.newQuery(" SELECT  *  FROM /pos ps WHERE ps.status like '%b%' and ps.ID > 3 ");
    results = (SelectResults) q.execute();
    if (results.size() != 2) {
        fail("Unexpected result. expected :" + 2 + " found : " + results.size());
    }
    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) 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) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 23 with QueryObserverAdapter

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

use of org.apache.geode.cache.query.internal.QueryObserverAdapter 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 25 with QueryObserverAdapter

use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.

the class QueryUsingPoolDUnitTest method testBug36969.

/**
   * This the dunit test for the bug no : 36969
   *
   * @throws Exception
   */
@Test
public void testBug36969() throws Exception {
    final String name = this.getName();
    final String rootRegionName = "root";
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numberOfEntries = 100;
    // Start server
    vm0.invoke("Create Bridge Server", () -> {
        createAndStartBridgeServer();
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        final Region region1 = createRegion(name, factory.createRegionAttributes());
        final Region region2 = createRegion(name + "_2", factory.createRegionAttributes());
        QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterQueryEvaluation(Object result) {
                // Destroy the region in the test
                region1.close();
            }
        });
        for (int i = 0; i < numberOfEntries; i++) {
            region1.put("key-" + i, new TestObject(i, "ibm"));
            region2.put("key-" + i, new TestObject(i, "ibm"));
        }
    });
    final int port = vm0.invoke("getCachedServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    final String regionName1 = "/" + rootRegionName + "/" + name;
    final String regionName2 = "/" + rootRegionName + "/" + name + "_2";
    // Create client pool.
    final String poolName = "testBug36969";
    vm1.invoke("createPool", () -> createPool(poolName, host0, port));
    // Execute client queries in VM1
    vm1.invoke("Execute queries", () -> {
        String queryString = "select distinct * from " + regionName1 + ", " + regionName2;
        // SelectResults results = null;
        QueryService qService = null;
        try {
            qService = (PoolManager.find(poolName)).getQueryService();
        } catch (Exception e) {
            Assert.fail("Failed to get QueryService.", e);
        }
        try {
            Query query = qService.newQuery(queryString);
            query.execute();
            fail("The query should have experienced RegionDestroyedException");
        } catch (Exception e) {
        // OK
        }
    });
    // Stop server
    vm0.invoke("Stop CacheServer", () -> {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
        stopBridgeServer(getCache());
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)63 SelectResults (org.apache.geode.cache.query.SelectResults)59 Test (org.junit.Test)55 Query (org.apache.geode.cache.query.Query)54 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)51 Portfolio (org.apache.geode.cache.query.data.Portfolio)49 Region (org.apache.geode.cache.Region)48 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)38 Index (org.apache.geode.cache.query.Index)36 QueryService (org.apache.geode.cache.query.QueryService)35 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)23 Collection (java.util.Collection)18 ObjectType (org.apache.geode.cache.query.types.ObjectType)18 LocalRegion (org.apache.geode.internal.cache.LocalRegion)14 AttributesFactory (org.apache.geode.cache.AttributesFactory)12 Cache (org.apache.geode.cache.Cache)11 List (java.util.List)10 ArrayList (java.util.ArrayList)9 RegionAttributes (org.apache.geode.cache.RegionAttributes)9 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)7