Search in sources :

Example 31 with QueryObserverAdapter

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

the class CustomerOptimizationsJUnitTest method testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_1.

@Ignore
@Test
public void testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_1() throws QueryException {
    QueryService qs = CacheUtils.getQueryService();
    Region rgn = CacheUtils.getRegion("/pos");
    for (int i = 100; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.setCreateTime(10l);
        rgn.put("" + i, pf);
    }
    String[] queries = new String[] { "select  p.status as sts, p as pos from /pos p  where   (p.ID IN  SET( 0,1,2,3,4,5,101,102,103,104,105) AND p.createTime > 9l) OR (p.ID IN  SET( 20,30,110,120) AND p.createTime > 7l)" };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][0] = (SelectResults) q.execute();
    }
    qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
    // qs.createIndex("CreateTime", IndexType.FUNCTIONAL,"createTime", "/pos");
    final boolean[] expectedIndexUsed = new boolean[] { true };
    final boolean[] actualIndexUsed = new boolean[] { false };
    final boolean[] expectedProjectionCallabck = new boolean[] { false };
    final boolean[] actualProjectionCallback = new boolean[] { false };
    final boolean[] expectedUnionCallback = { true };
    final boolean[] actualUnionCallback = new boolean[queries.length];
    final boolean[] expectedIntersectionCallback = { false };
    final boolean[] actualIntersectionCallback = new boolean[queries.length];
    ObjectType[] expectedTypes = new ObjectType[] { new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
    QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private int i = 0;

        public void invokedQueryUtilsUnion(SelectResults r1, SelectResults r2) {
            actualUnionCallback[i] = true;
        }

        public void invokedQueryUtilsIntersection(SelectResults r1, SelectResults r2) {
            actualIntersectionCallback[i] = true;
        }

        public void beforeIndexLookup(Index index, int oper, Object key) {
            actualIndexUsed[i] = true;
        }

        public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
            actualProjectionCallback[i] = true;
        }

        public void afterQueryEvaluation(Object result) {
            ++i;
        }
    });
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][1] = (SelectResults) q.execute();
        assertEquals(expectedUnionCallback[i], actualUnionCallback[i]);
        assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
        assertEquals(expectedIndexUsed[i], actualIndexUsed[i]);
        assertEquals(expectedIntersectionCallback[i], actualIntersectionCallback[i]);
        assertEquals(expectedProjectionCallabck[i], actualProjectionCallback[i]);
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) Index(org.apache.geode.cache.query.Index) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Region(org.apache.geode.cache.Region) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 32 with QueryObserverAdapter

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

the class QueryUsingPoolDUnitTest method testUnSupportedOps.

@Test
public void testUnSupportedOps() 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());
        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("getCacheServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    final String regionName1 = "/" + rootRegionName + "/" + name;
    // Create client pool.
    final String poolName = "testUnSupportedOps";
    vm1.invoke("createPool", () -> createPool(poolName, host0, port));
    // Execute client queries in VM1
    vm1.invoke("Execute queries", () -> {
        final Region region1 = ((ClientCache) getCache()).createClientRegionFactory(ClientRegionShortcut.LOCAL).create(name);
        String queryString = "select distinct * from " + regionName1 + " where ticker = $1";
        Object[] params = new Object[] { new String("ibm") };
        // SelectResults results = null;
        QueryService qService = null;
        try {
            qService = (PoolManager.find(poolName)).getQueryService();
        } catch (Exception e) {
            Assert.fail("Failed to get QueryService.", e);
        }
        // Testing Remote Query with params.
        try {
            Query query = qService.newQuery(queryString);
            query.execute(params);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        // Test with Index.
        try {
            qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        try {
            String importString = "import org.apache.geode.admin.QueryUsingPoolDUnitTest.TestObject;";
            qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1, importString);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        } catch (Exception e) {
            Assert.fail("Failed with UnExpected Exception.", e);
        }
        try {
            qService.getIndex(region1, "test");
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.getIndexes(region1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.getIndexes(region1, IndexType.FUNCTIONAL);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.removeIndexes(region1);
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
        try {
            qService.removeIndexes();
        } catch (UnsupportedOperationException e) {
        // Expected behavior.
        }
    });
    // Stop server
    vm0.invoke("Stop CacheServer", () -> {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
        stopBridgeServer(getCache());
    });
}
Also used : Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AttributesFactory(org.apache.geode.cache.AttributesFactory) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 33 with QueryObserverAdapter

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

the class RemoteQueryDUnitTest method testBug36969.

/**
   * This the dunit test for the bug no : 36969
   */
@Test
public void testBug36969() throws Exception {
    final String name = this.getName();
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numberOfEntries = 100;
    // Start server
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Properties config = new Properties();
            config.setProperty(LOCATORS, "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
            getSystem(config);
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            final Region region = createRegion(name, factory.createRegionAttributes());
            QueryObserverHolder.setInstance(new QueryObserverAdapter() {

                public void afterQueryEvaluation(Object result) {
                    // Destroy the region in the test
                    region.close();
                }
            });
            try {
                startBridgeServer(0, false);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
        }
    });
    // Initialize server region
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "ibm"));
            }
        }
    });
    final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client region in VM1
    vm1.invoke(new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            Properties config = new Properties();
            config.setProperty(MCAST_PORT, "0");
            getSystem(config);
            PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
            getCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setPoolName("clientPool");
            createRegion(name, factory.createRegionAttributes());
        }
    });
    // Execute client queries in VM1
    vm1.invoke(new CacheSerializableRunnable("Execute queries") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            String queryStrings = "id<9";
            // SelectResults results = null;
            try {
                region.query(queryStrings);
                fail("The query should have experienced RegionDestroyedException");
            } catch (QueryInvocationTargetException qte) {
            // Ok test passed
            } catch (Exception e) {
                Assert.fail("Failed executing query " + queryStrings + " due  to unexpected Excecption", e);
            }
        }
    });
    // Start server
    vm0.invoke(new CacheSerializableRunnable("Create two regions") {

        public void run2() throws CacheException {
            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"));
            }
        }
    });
    // Execute client queries in VM1
    vm1.invoke(new CacheSerializableRunnable("Execute queries") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            String queryString = "select distinct * from /" + name;
            // SelectResults results = null;
            try {
                region.query(queryString);
                fail("The query should have experienced RegionDestroyedException");
            } catch (QueryInvocationTargetException qte) {
            // Ok test passed
            } catch (Exception e) {
                Assert.fail("Failed executing query " + queryString + " due  to unexpected Excecption", e);
            }
        }
    });
    // Close client VM1
    vm1.invoke(new CacheSerializableRunnable("Close client") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.close();
            PoolManager.find("clientPool").destroy();
        }
    });
    // Stop server
    vm0.invoke(new SerializableRunnable("Stop CacheServer") {

        public void run() {
            QueryObserverHolder.setInstance(new QueryObserverAdapter());
            stopBridgeServer(getCache());
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 34 with QueryObserverAdapter

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

the class IndexMaintenanceJUnitTest method testQueryOnHeterogenousObjects.

/**
   * Tests query on region containing heterogenous objects
   */
@Test
public void testQueryOnHeterogenousObjects() {
    try {
        DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = true;
        Cache cache = CacheUtils.getCache();
        region = CacheUtils.createRegion("portfolio1", null);
        for (int i = 0; i < 5; ++i) {
            Portfolio p = new Portfolio(i + 1);
            region.put(new Integer(i + 1), p);
        }
        for (int i = 5; i < 10; ++i) {
            region.put(new Integer(i + 1), new Integer(i + 1));
        }
        String queryStr = "Select distinct * from /portfolio1 pf1 where pf1.getID() > 3";
        Query q = qs.newQuery(queryStr);
        SelectResults rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        Iterator itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID()", "/portfolio1 pf");
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            private boolean indexUsed = false;

            public void beforeIndexLookup(Index index, int oper, Object key) {
                indexUsed = true;
            }

            public void endQuery() {
                assertTrue(indexUsed);
            }
        });
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        qs.removeIndex(i1);
        queryStr = "Select distinct * from /portfolio1 pf1 where pf1.pkid > '3'";
        q = qs.newQuery(queryStr);
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
        i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.pkid", "/portfolio1 pf");
        QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            private boolean indexUsed = false;

            public void beforeIndexLookup(Index index, int oper, Object key) {
                indexUsed = true;
            }

            public void endQuery() {
                assertTrue(indexUsed);
            }
        });
        rs = (SelectResults) q.execute();
        assertEquals(2, rs.size());
        itr = rs.iterator();
        while (itr.hasNext()) {
            Portfolio p = (Portfolio) itr.next();
            assertTrue(p == region.get(new Integer(4)) || p == region.get(new Integer(5)));
        }
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        DefaultQueryService.TEST_QUERY_HETEROGENEOUS_OBJECTS = false;
    }
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 35 with QueryObserverAdapter

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

the class IndexMaintainceJUnitTest method test006ConcurrentMapClearAndRegionPutOperation.

// Asif : Tests for Region clear operations on Index in a Local VM for cases
// when a clear
// operation & region put operation occur concurrentlty
@Test
public void test006ConcurrentMapClearAndRegionPutOperation() {
    try {
        CacheUtils.restartCache();
        IndexMaintainceJUnitTest.isInitDone = false;
        init();
        Query q = qs.newQuery("SELECT DISTINCT * FROM /portfolio where status = 'active'");
        QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIndexLookup(Collection coll) {
                IndexMaintainceJUnitTest.this.indexUsed = true;
            }

            public void beforeRerunningIndexCreationQuery() {
                // Spawn a separate thread here which does a put opertion on region
                Thread th = new Thread(new Runnable() {

                    public void run() {
                        // Assert that the size of region is now 0
                        assertTrue(IndexMaintainceJUnitTest.region.size() == 0);
                        IndexMaintainceJUnitTest.region.put("" + 8, new Portfolio(8));
                    }
                });
                th.start();
                ThreadUtils.join(th, 30 * 1000);
                assertTrue(IndexMaintainceJUnitTest.region.size() == 1);
            }
        });
        SelectResults set = (SelectResults) q.execute();
        if (set.size() == 0 || !this.indexUsed) {
            fail("Either Size of the result set is zero or Index is not used ");
        }
        this.indexUsed = false;
        region.clear();
        set = (SelectResults) q.execute();
        if (set.size() != 1 || !this.indexUsed) {
            fail("Either Size of the result set is not one or Index is not used ");
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        IndexMaintainceJUnitTest.isInitDone = false;
        CacheUtils.restartCache();
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Portfolio(org.apache.geode.cache.query.data.Portfolio) Collection(java.util.Collection) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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