Search in sources :

Example 51 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class IndexUseJUnitTest method testRangeGroupingBehaviourOfCompactMapIndex.

@Test
public void testRangeGroupingBehaviourOfCompactMapIndex() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
    int ID = 1;
    // and so on
    for (; ID <= 30; ++ID) {
        MapKeyIndexData mkid = new MapKeyIndexData(ID);
        for (int j = 1; j <= ID; ++j) {
            mkid.addKeyValue("key1", j * 1);
            mkid.addKeyValue("key2", j * 2);
            mkid.addKeyValue("key3", j * 3);
        }
        testRgn.put(ID, mkid);
    }
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3 and itr1.maap['key2'] <=18", "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key3'] >= 3  and  itr1.maap['key3'] >= 13 ", "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] >= 13 ", "SELECT DISTINCT * FROM /testRgn itr1  WHERE itr1.maap['key2'] >= 3  and  itr1.maap['key3'] < 18 " };
    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]);
            r[i][0] = q.execute();
            CacheUtils.log("Executed query: " + queries[i]);
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "objs.maap['key2','key3']", "/testRgn objs");
    assertTrue(i1 instanceof CompactMapRangeIndex);
    // 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);
            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) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 52 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class IndexMaintenanceJUnitTest method validateIndexForEntries.

private void validateIndexForEntries(CompactRangeIndex ri) {
    assertEquals(6, ri.getIndexStorage().size());
    Iterator itr = ri.getIndexStorage().iterator(null);
    while (itr.hasNext()) {
        Object obj = itr.next();
        assertFalse(obj instanceof Collection);
        MemoryIndexStoreEntry re = (MemoryIndexStoreEntry) obj;
        Portfolio pf = (Portfolio) re.getRegionEntry().getValueInVM((LocalRegion) ri.getRegion());
        assertTrue(idSet.contains(String.valueOf(pf.getID())));
    }
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) CloseableIterator(org.apache.geode.internal.cache.persistence.query.CloseableIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) LocalRegion(org.apache.geode.internal.cache.LocalRegion)

Example 53 with LocalRegion

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

Example 54 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class IndexUseJUnitTest method testIndexUsageWithOrderBy2.

@Test
public void testIndexUsageWithOrderBy2() 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 % 2);
        p.createTime = (numObjects - i);
        testRgn.put("" + i, p);
    }
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime asc limit 1", "SELECT DISTINCT p.key, p.value FROM /testRgn.entrySet p  WHERE p.value.ID <= 10 order by p.value.createTime 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++) {
                Struct s = (Struct) results.get(0);
                Portfolio p = (Portfolio) s.get("value");
                CacheUtils.getLogger().info("p: " + p);
                if (i == 0) {
                    assertEquals(p.createTime, 1);
                } else if (i == 1) {
                    assertEquals(p.createTime, 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.value.ID", "/testRgn.entrySet 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++) {
                Struct s = (Struct) results.get(0);
                Portfolio p = (Portfolio) s.get("value");
                CacheUtils.getLogger().info("index p: " + p);
                if (i == 0) {
                    assertEquals(p.createTime, 1);
                } else if (i == 1) {
                    assertEquals(p.createTime, 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) Struct(org.apache.geode.cache.query.Struct) 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)

Example 55 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class IndexUseJUnitTest method testSizeEstimateLTInCompactRangeIndexForNullMap.

@Test
public void testSizeEstimateLTInCompactRangeIndexForNullMap() throws Exception {
    QueryService qs = CacheUtils.getQueryService();
    LocalRegion testRgn = (LocalRegion) CacheUtils.createRegion("testRgn", null);
    // Create indexes
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "p.status", "/testRgn p");
    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "p.ID", "/testRgn p");
    // put values
    testRgn.put(0, new Portfolio(0));
    testRgn.put(1, new Portfolio(1));
    // Set TestHook in RangeIndex
    TestHook hook = new RangeIndexTestHook();
    CompactRangeIndex.setTestHook(hook);
    // Execute Queries without Indexes
    Query q = CacheUtils.getQueryService().newQuery("<trace> SELECT * FROM /testRgn p where p.status = 'active' AND p.ID > 0 ");
    // Following should throw NullPointerException.
    SelectResults sr = (SelectResults) q.execute();
    assertTrue("RangeIndexTestHook was not hooked for spot 2", ((RangeIndexTestHook) hook).isHooked(2));
    CompactRangeIndex.setTestHook(null);
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) LocalRegion(org.apache.geode.internal.cache.LocalRegion) TestHook(org.apache.geode.cache.query.internal.index.IndexManager.TestHook) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

LocalRegion (org.apache.geode.internal.cache.LocalRegion)243 Test (org.junit.Test)103 Region (org.apache.geode.cache.Region)70 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)54 IOException (java.io.IOException)50 AttributesFactory (org.apache.geode.cache.AttributesFactory)42 VM (org.apache.geode.test.dunit.VM)39 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)38 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)37 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)34 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)34 Cache (org.apache.geode.cache.Cache)31 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)31 Host (org.apache.geode.test.dunit.Host)31 Iterator (java.util.Iterator)29 QueryService (org.apache.geode.cache.query.QueryService)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)27 CancelException (org.apache.geode.CancelException)26 CacheException (org.apache.geode.cache.CacheException)26 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)25