Search in sources :

Example 11 with StructSetOrResultsSet

use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.

the class IndexHintJUnitTest method testMultiIndexWithSingleIndexHint.

// given a choice between two indexes, we hint on each one in different queries and verify
// that both indexes are used
@Test
public void testMultiIndexWithSingleIndexHint() throws Exception {
    createRegion();
    populateData(1000);
    // create index
    createIndex("IDIndex", "p.ID", "/Portfolios p");
    createIndex("SecIndex", "p.status", "/Portfolios p");
    // set observer
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    // execute query
    SelectResults[][] results = new SelectResults[1][2];
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.status = 'inactive'");
    results[0][0] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("IDIndex"));
    observer.reset();
    query = qs.newQuery("<hint 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.status = 'inactive'");
    results[0][1] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("SecIndex"));
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    assertEquals(495, results[0][1].size());
    // Not really with and without index but we can use this method to verify they are the same
    // results
    // regardless of which index used
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with StructSetOrResultsSet

use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.

the class IndexHintJUnitTest method testMultiIndexWithMultiIndexHint.

// Using junction, we will hint and make sure multi index hints are functioning
@Test
public void testMultiIndexWithMultiIndexHint() throws Exception {
    createRegion();
    populateData(1000);
    // create index
    createIndex("IDIndex", "p.ID", "/Portfolios p");
    createIndex("SecIndex", "p.status", "/Portfolios p");
    // set observer
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    // execute query
    SelectResults[][] results = new SelectResults[1][2];
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery("<hint 'IDIndex', 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
    results[0][0] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("IDIndex"));
    assertTrue(observer.wasIndexUsed("SecIndex"));
    observer.reset();
    query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
    results[0][1] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("IDIndex"));
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    assertEquals(95, results[0][1].size());
    // Not really with and without index but we can use this method to verify they are the same
    // results
    // regardless of which index used
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with StructSetOrResultsSet

use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.

the class IndexHintJUnitTest method testMultiIndexWithSingleIndexHintWithRangeJunction.

// Using junction, we will hint and make sure single index hints are functioning
@Test
public void testMultiIndexWithSingleIndexHintWithRangeJunction() throws Exception {
    createRegion();
    populateData(1000);
    // create index
    createIndex("IDIndex", "p.ID", "/Portfolios p");
    createIndex("SecIndex", "p.status", "/Portfolios p");
    // set observer
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    // execute query
    SelectResults[][] results = new SelectResults[1][2];
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery("<hint 'IDIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
    results[0][0] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("IDIndex"));
    observer.reset();
    query = qs.newQuery("<hint 'SecIndex'>select * from /Portfolios p where p.ID > 10 and p.ID < 200 and p.status = 'inactive'");
    results[0][1] = (SelectResults) query.execute();
    // verify index usage
    assertTrue(observer.wasIndexUsed("SecIndex"));
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    assertEquals(95, results[0][1].size());
    // Not really with and without index but we can use this method to verify they are the same
    // results
    // regardless of which index used
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(results, 1, new String[] { "<query with hints>" });
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with StructSetOrResultsSet

use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.

the class PdxLocalQueryDUnitTest method testLocalPdxQueriesOnPR.

@Test
public void testLocalPdxQueriesOnPR() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client = host.getVM(2);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    final String[] queries = { "select * from " + name + " where position1 = $1", "select * from " + name + " where aDay = $1", // numberOfEntries
    "select distinct * from " + name + " p where p.status = 'inactive'", // 1
    "select distinct p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
    "select p from " + name + " p where p.status = 'inactive'", // 4
    "select * from " + name + " p, p.positions.values v where v.secId = 'IBM'", // 4
    "select v from " + name + " p, p.positions.values v where v.secId = 'IBM'", // numberOfEntries
    "select p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
    "select distinct * from " + name + " p where p.status = 'inactive' order by p.ID", // 19
    "select * from " + name + " p where p.status = 'inactive' or p.ID > 0", // numberOfEntries
    "select * from " + name + " p where p.status = 'inactive' and p.ID >= 0", // numberOfEntries*2
    "select * from " + name + " p where p.status in set ('inactive', 'active')", // 9
    "select * from " + name + " p where p.ID > 0 and p.ID < 10", // numberOfEntries*2
    "select v from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries*2
    "select v.secId from " + name + " p, p.positions.values v where p.status = 'inactive'", "select distinct p from " + name + // numberOfEntries
    " p, p.positions.values v where p.status = 'inactive' and v.pid >= 0", "select distinct p from " + name + // numberOfEntries*2
    " p, p.positions.values v where p.status = 'inactive' or v.pid > 0", // numberOfEntries*2
    "select distinct * from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries
    "select * from " + name + ".values v where v.status = 'inactive'", // 19
    "select v from " + name + " v where v in (select p from " + name + " p where p.ID > 0)", "select v from " + name + " v where v.status in (select distinct p.status from " + name + // numberOfEntries
    " p where p.status = 'inactive')", "select * from " + name + " v where v.status = ELEMENT (select distinct p.status from " + name + // numberOfEntries
    " p where p.status = 'inactive')" };
    final int[] results = { 2, 3, numberOfEntries, 1, numberOfEntries, 4, 4, numberOfEntries, numberOfEntries, 19, numberOfEntries, numberOfEntries * 2, 9, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, 19, numberOfEntries, numberOfEntries };
    // Start server1
    final int port1 = (Integer) server1.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                r1.put("key-" + i, new PortfolioPdx(i));
            }
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server2
    final int port2 = (Integer) server2.invoke(new SerializableCallable("Create Server2") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.PARTITION).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // client loads pdx objects on server
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
            for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
                region.put("key-" + i, new PortfolioPdx(i));
            }
            QueryService qs = null;
            SelectResults sr = null;
            // Execute query remotely
            try {
                qs = cache.getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            PositionPdx pos = new PositionPdx("IBM", 100);
            PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
            for (int i = 0; i < queries.length; i++) {
                try {
                    if (i == 0) {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
                    } else if (i == 1) {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
                    } else {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute();
                    }
                    assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
                    assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
                    for (Object result : sr) {
                        if (result instanceof Struct) {
                            Object[] r = ((Struct) result).getFieldValues();
                            for (int j = 0; j < r.length; j++) {
                                if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
                                    fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
                                }
                            }
                        } else if (result instanceof PdxInstance || result instanceof PdxString) {
                            fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
                        }
                    }
                } catch (Exception e) {
                    Assert.fail("Failed executing query " + queries[i], e);
                }
            }
            return null;
        }
    });
    // query locally on server1
    server1.invoke(new SerializableCallable("query locally on server1") {

        @Override
        public Object call() throws Exception {
            GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            QueryService qs = null;
            SelectResults sr = null;
            // Execute query locally
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            PositionPdx pos = new PositionPdx("IBM", 100);
            PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
            for (int i = 0; i < queries.length; i++) {
                try {
                    if (i == 0) {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
                    } else if (i == 1) {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
                    } else {
                        sr = (SelectResults) qs.newQuery(queries[i]).execute();
                    }
                    assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
                    assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
                    for (Object result : sr) {
                        if (result instanceof Struct) {
                            Object[] r = ((Struct) result).getFieldValues();
                            for (int j = 0; j < r.length; j++) {
                                if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
                                    fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
                                }
                            }
                        } else if (result instanceof PdxInstance || result instanceof PdxString) {
                            fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
                        }
                    }
                } catch (Exception e) {
                    Assert.fail("Failed executing query " + queries[i], e);
                }
            }
            return null;
        }
    });
    // query locally on server2
    server2.invoke(new SerializableCallable("query locally on server2") {

        @Override
        public Object call() throws Exception {
            GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            QueryService qs = null;
            SelectResults[][] sr = new SelectResults[queries.length][2];
            // Execute query locally
            try {
                qs = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            PositionPdx pos = new PositionPdx("IBM", 100);
            PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
            for (int i = 0; i < queries.length; i++) {
                try {
                    if (i == 0) {
                        sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
                    } else if (i == 1) {
                        sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
                    } else {
                        sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute();
                    }
                    assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][0].size() > 0);
                    assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][0].size());
                    for (Object result : sr[i][0]) {
                        if (result instanceof Struct) {
                            Object[] r = ((Struct) result).getFieldValues();
                            for (int j = 0; j < r.length; j++) {
                                if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
                                    fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
                                }
                            }
                        } else if (result instanceof PdxInstance || result instanceof PdxString) {
                            fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
                        }
                    }
                } catch (Exception e) {
                    Assert.fail("Failed executing query " + queries[i], e);
                }
            }
            // create index
            qs.createIndex("statusIndex", "p.status", name + " p");
            qs.createIndex("IDIndex", "ID", name);
            qs.createIndex("pIdIndex", "pos.getPid()", name + " p, p.positions.values pos");
            qs.createIndex("secIdIndex", "pos.secId", name + " p, p.positions.values pos");
            for (int i = 0; i < queries.length; i++) {
                try {
                    if (i == 0) {
                        sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
                    } else if (i == 1) {
                        sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
                    } else {
                        sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute();
                    }
                    assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][1].size() > 0);
                    assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][1].size());
                    for (Object result : sr[i][1]) {
                        if (result instanceof Struct) {
                            Object[] r = ((Struct) result).getFieldValues();
                            for (int j = 0; j < r.length; j++) {
                                if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
                                    fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
                                }
                            }
                        } else if (result instanceof PdxInstance || result instanceof PdxString) {
                            fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
                        }
                    }
                } catch (Exception e) {
                    Assert.fail("Failed executing query " + queries[i], e);
                }
            }
            StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
            ssOrrs.CompareQueryResultsWithoutAndWithIndexes(sr, queries.length, queries);
            return null;
        }
    });
    this.closeClient(client);
    this.closeClient(server1);
    this.closeClient(server2);
}
Also used : PositionPdx(org.apache.geode.cache.query.data.PositionPdx) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) PdxString(org.apache.geode.pdx.internal.PdxString) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) CacheServer(org.apache.geode.cache.server.CacheServer) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) PdxString(org.apache.geode.pdx.internal.PdxString) CacheException(org.apache.geode.cache.CacheException) PdxInstance(org.apache.geode.pdx.PdxInstance) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 15 with StructSetOrResultsSet

use of org.apache.geode.cache.query.functional.StructSetOrResultsSet in project geode by apache.

the class QueryIndexDUnitTest method createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults.

@Test
public void createIndexOnOverflowRegionsWithMultipleWhereClauseAndValidateResults() throws Exception {
    Host host = Host.getHost(0);
    VM[] vms = new VM[] { host.getVM(0), host.getVM(1) };
    // Create and load regions on all vms.
    for (int i = 0; i < vms.length; i++) {
        int finalI = i;
        vms[i].invoke(() -> QueryIndexDUnitTest.createAndLoadOverFlowRegions("testOfValid2" + "vm" + finalI, new Boolean(true), new Boolean(false)));
    }
    vms[0].invoke(new CacheSerializableRunnable("Execute query validate results") {

        public void run2() throws CacheException {
            Cache cache = basicGetCache();
            String[] regionNames = new String[] { "replicateOverFlowRegion", "replicatePersistentOverFlowRegion", "prOverFlowRegion", "prPersistentOverFlowRegion" };
            QueryService qs = cache.getQueryService();
            Region region = null;
            int numObjects = 10;
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    region.put(new Portfolio(cnt), new String("XX" + cnt));
                }
            }
            String[] qString = new String[] { "SELECT * FROM /REGION_NAME pf WHERE pf = 'XX1'", "SELECT * FROM /REGION_NAME pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.values pf WHERE pf IN SET( 'XX5', 'XX6', 'XX7')", "SELECT * FROM /REGION_NAME.keys k WHERE k.ID = 1", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID = 1", "SELECT ID, status FROM /REGION_NAME.keys WHERE ID = 1", "SELECT k.ID, k.status FROM /REGION_NAME.keys k WHERE k.ID = 1 and k.status = 'active'", "SELECT * FROM /REGION_NAME.keys key WHERE key.ID > 5", "SELECT key.ID FROM /REGION_NAME.keys key WHERE key.ID > 5 and key.status = 'active'" };
            // Execute Query without index.
            SelectResults[] srWithoutIndex = new SelectResults[qString.length * regionNames.length];
            String[] queryString = new String[qString.length * regionNames.length];
            int r = 0;
            try {
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        queryString[r] = queryStr;
                        srWithoutIndex[r] = (SelectResults) query.execute();
                        r++;
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Create index.
            String indexName = "";
            try {
                for (int i = 0; i < regionNames.length; i++) {
                    region = cache.getRegion(regionNames[i]);
                    indexName = "idIndex" + regionNames[i];
                    cache.getLogger().fine("createIndexOnOverFlowRegions() checking for index: " + indexName);
                    try {
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + " pf");
                        }
                        indexName = "valueIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i1 = qs.createIndex(indexName, "pf", "/" + regionNames[i] + ".values pf");
                        }
                        indexName = "keyIdIndex" + regionNames[i];
                        if (qs.getIndex(region, indexName) == null) {
                            cache.getLogger().fine("createIndexOnOverFlowRegions() Index doesn't exist, creating index: " + indexName);
                            Index i2 = qs.createIndex(indexName, "key.ID", "/" + regionNames[i] + ".keys key");
                        }
                        indexName = "keyIdIndex2" + regionNames[i];
                    } catch (IndexNameConflictException ice) {
                    // Ignore. The pr may have created the index through
                    // remote index create message from peer.
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to create index", ex);
                fail("Failed to create index." + indexName);
            }
            // Execute Query with index.
            SelectResults[] srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute();
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
            // Compare results with and without index.
            StructSetOrResultsSet ssORrs = new StructSetOrResultsSet();
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < srWithIndex.length; i++) {
                sr[0][0] = srWithoutIndex[i];
                sr[0][1] = srWithIndex[i];
                logger.info("Comparing the result for the query : " + queryString[i] + " Index in ResultSet is: " + i);
                ssORrs.CompareQueryResultsWithoutAndWithIndexes(sr, 1, queryString);
            }
            // The index should get updated accordingly.
            for (int i = 0; i < regionNames.length; i++) {
                region = cache.getRegion(regionNames[i]);
                for (int cnt = 1; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.destroy(new Portfolio(cnt));
                    }
                }
                for (int cnt = 10; cnt < numObjects; cnt++) {
                    if (cnt % 2 == 0) {
                        region.put(new Portfolio(cnt), new String("XX" + cnt));
                    }
                }
            }
            // Execute Query with index.
            srWithIndex = new SelectResults[qString.length * regionNames.length];
            try {
                r = 0;
                for (int q = 0; q < qString.length; q++) {
                    for (int i = 0; i < regionNames.length; i++) {
                        QueryObserverImpl observer = new QueryObserverImpl();
                        QueryObserverHolder.setInstance(observer);
                        String queryStr = qString[q].replace("REGION_NAME", regionNames[i]);
                        Query query = qs.newQuery(queryStr);
                        srWithIndex[r++] = (SelectResults) query.execute();
                        if (!observer.isIndexesUsed) {
                            fail("Index not used for query. " + queryStr);
                        }
                    }
                }
            } catch (Exception ex) {
                logger.info("Failed to Execute query", ex);
                fail("Failed to Execute query.");
            }
        }
    });
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) CacheException(org.apache.geode.cache.CacheException) QueryException(org.apache.geode.cache.query.QueryException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

StructSetOrResultsSet (org.apache.geode.cache.query.functional.StructSetOrResultsSet)35 QueryService (org.apache.geode.cache.query.QueryService)23 Test (org.junit.Test)23 SelectResults (org.apache.geode.cache.query.SelectResults)19 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)17 Query (org.apache.geode.cache.query.Query)16 LocalRegion (org.apache.geode.internal.cache.LocalRegion)16 Region (org.apache.geode.cache.Region)15 Index (org.apache.geode.cache.query.Index)13 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)13 Cache (org.apache.geode.cache.Cache)12 QueryException (org.apache.geode.cache.query.QueryException)12 Iterator (java.util.Iterator)9 CancelException (org.apache.geode.CancelException)8 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)8 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)8 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)8 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)8