Search in sources :

Example 86 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class MapRangeIndexMaintenanceJUnitTest method updatingWithSameKeysDifferentValuesAfterRemovingUsingRegionDestroyShouldReinsertIndexMappings.

@Test
public void updatingWithSameKeysDifferentValuesAfterRemovingUsingRegionDestroyShouldReinsertIndexMappings() throws Exception {
    AttributesFactory af = new AttributesFactory();
    af.setScope(Scope.LOCAL);
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    qs = CacheUtils.getQueryService();
    Index keyIndex1 = qs.createIndex(INDEX_NAME, "positions[*]", "/portfolio");
    Portfolio p1 = new Portfolio(1, 1);
    HashMap map1 = new HashMap();
    map1.put("SUN", 1);
    map1.put("IBM", 2);
    p1.positions = map1;
    region.put(1, p1);
    SelectResults results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
    assertEquals(1, results.size());
    region.destroy(1);
    results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
    assertEquals(0, results.size());
    Portfolio p3 = new Portfolio(1, 1);
    HashMap map3 = new HashMap();
    p3.positions = map3;
    map3.put("SUN", 3);
    map3.put("IBM", 4);
    region.put(1, p3);
    results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
    assertEquals(0, results.size());
    results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 3").execute();
    assertEquals(1, results.size());
    results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['GOOG'] = 1").execute();
    assertEquals(0, results.size());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) HashMap(java.util.HashMap) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 87 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class MapRangeIndexMaintenanceJUnitTest method testUpdateWithSameKeysSameValuesShouldRetainIndexMappings.

@Test
public void testUpdateWithSameKeysSameValuesShouldRetainIndexMappings() throws Exception {
    AttributesFactory af = new AttributesFactory();
    af.setScope(Scope.LOCAL);
    Portfolio p = new Portfolio(1, 1);
    HashMap map1 = new HashMap();
    map1.put("SUN", 1);
    map1.put("IBM", 2);
    p.positions = map1;
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    region.put(1, p);
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, "positions[*]", "/portfolio");
    region.put(1, p);
    SelectResults results = (SelectResults) qs.newQuery("select * from /portfolio p where p.positions['SUN'] = 1").execute();
    assertEquals(1, results.size());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) HashMap(java.util.HashMap) Portfolio(org.apache.geode.cache.query.data.Portfolio) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 88 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class IndexUseJUnitTest method evaluatePKIndexUsetest.

private void evaluatePKIndexUsetest(RegionShortcut regionShortcut, String regionName) throws Exception {
    Cache cache = CacheUtils.getCache();
    Region r = cache.createRegionFactory(regionShortcut).create(regionName);
    Map map0 = new HashMap();
    Map map1 = new HashMap();
    Map map2 = new HashMap();
    r.put("A0", map0);
    r.put("A1", map1);
    r.put("A2", map2);
    qs.createIndex("pkIndex", IndexType.PRIMARY_KEY, "p.pk", "/" + regionName + " p");
    QueryObserverImpl observer = new QueryObserverImpl();
    QueryObserverHolder.setInstance(observer);
    SelectResults sr = (SelectResults) qs.newQuery("select * from /" + regionName + " p where p.pk = 'A1'").execute();
    assertEquals(1, sr.size());
    if (!observer.isIndexesUsed) {
        fail("Index not used for operator '='");
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) HashMap(java.util.HashMap) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashMap(java.util.HashMap) Map(java.util.Map) Cache(org.apache.geode.cache.Cache)

Example 89 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class IndexTrackingQueryObserverDUnitTest method runQuery.

private void runQuery(VM vm) {
    SerializableRunnable runQuery = new SerializableRunnable("Run Query on PR") {

        public void run() {
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryStr);
            Region region = getCache().getRegion("portfolio");
            SelectResults results = null;
            try {
                results = (SelectResults) query.execute();
            } catch (Exception e) {
                Assert.fail("While running query on PR", e);
            }
            // The query should return all elements in region.
            assertEquals(region.size(), results.size());
        }
    };
    vm.invoke(runQuery);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) QueryService(org.apache.geode.cache.query.QueryService) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 90 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class IndexUseJUnitTest method testIndexUsageWithOrderBy.

@Test
public void testIndexUsageWithOrderBy() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
    int numObjects = 30;
    // and so on
    for (int i = 0; i < numObjects; i++) {
        Portfolio p = new Portfolio(i);
        p.pkid = ("" + (numObjects - i));
        testRgn.put("" + i, p);
    }
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by p.pkid asc limit 1", "SELECT DISTINCT * FROM /testRgn p  WHERE p.ID <= 10 order by p.pkid desc limit 1" };
    Object[][] r = new Object[queries.length][2];
    // Execute Queries without Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            // verify individual result
            SelectResults sr = (SelectResults) q.execute();
            List results = sr.asList();
            for (int rows = 0; rows < results.size(); rows++) {
                Portfolio p = (Portfolio) results.get(0);
                CacheUtils.getLogger().info("p: " + p);
                if (i == 0) {
                    assertEquals(p.getID(), 10);
                    assertEquals(p.pkid, "" + (numObjects - 10));
                } else if (i == 1) {
                    assertEquals(p.getID(), 0);
                    assertEquals(p.pkid, "" + numObjects);
                }
            }
            r[i][0] = sr;
            CacheUtils.log("Executed query: " + queries[i]);
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
    // Execute Queries with Indexes
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            CacheUtils.getLogger().info("Executing query: " + queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            SelectResults sr = (SelectResults) q.execute();
            List results = sr.asList();
            for (int rows = 0; rows < results.size(); rows++) {
                Portfolio p = (Portfolio) results.get(0);
                CacheUtils.getLogger().info("index p: " + p);
                if (i == 0) {
                    assertEquals(p.getID(), 10);
                    assertEquals(p.pkid, "" + (numObjects - 10));
                } else if (i == 1) {
                    assertEquals(p.getID(), 0);
                    assertEquals(p.pkid, "" + numObjects);
                }
            }
            r[i][1] = sr;
            // r[i][1] = q.execute();
            CacheUtils.log("Executing query: " + queries[i] + " with index created");
            if (!observer.isIndexesUsed) {
                fail("Index is NOT uesd");
            }
            Iterator itr = observer.indexesUsed.iterator();
            assertTrue(itr.hasNext());
            String temp = itr.next().toString();
            assertEquals(temp, "Index1");
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
    ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, queries);
}
Also used : Query(org.apache.geode.cache.query.Query) StructSetOrResultsSet(org.apache.geode.cache.query.functional.StructSetOrResultsSet) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66