Search in sources :

Example 66 with QueryObserver

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

the class IndexTrackingQueryObserverJUnitTest method testIndexInfoOnPartitionedRegion.

@Test
public void testIndexInfoOnPartitionedRegion() throws Exception {
    // Query VERBOSE has to be true for the test
    assertEquals("true", System.getProperty(DistributionConfig.GEMFIRE_PREFIX + "Query.VERBOSE"));
    // Create Partition Region
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    paf.setTotalNumBuckets(NUM_BKTS);
    AttributesFactory af = new AttributesFactory();
    af.setPartitionAttributes(paf.create());
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    if (region.size() == 0) {
        for (int i = 1; i <= 100; i++) {
            region.put(Integer.toString(i), new Portfolio(i, i));
        }
    }
    assertEquals(100, region.size());
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
    assertTrue(keyIndex1 instanceof PartitionedIndex);
    Query query = qs.newQuery(queryStr);
    // Inject TestHook in QueryObserver before running query.
    IndexTrackingTestHook th = new IndexTrackingTestHook(region, NUM_BKTS);
    QueryObserver observer = QueryObserverHolder.getInstance();
    assertTrue(QueryObserverHolder.hasObserver());
    ((IndexTrackingQueryObserver) observer).setTestHook(th);
    SelectResults results = (SelectResults) query.execute();
    // The query should return all elements in region.
    assertEquals(region.size(), results.size());
    // Check results size of Map.
    regionMap = ((IndexTrackingTestHook) th).getRegionMap();
    Collection<Integer> rslts = regionMap.getResults().values();
    int totalResults = 0;
    for (Integer i : rslts) {
        totalResults += i.intValue();
    }
    assertEquals(results.size(), totalResults);
    QueryObserverHolder.reset();
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) IndexTrackingTestHook(org.apache.geode.cache.query.internal.index.IndexTrackingQueryObserverDUnitTest.IndexTrackingTestHook) Portfolio(org.apache.geode.cache.query.data.Portfolio) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 67 with QueryObserver

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

the class IndexTrackingQueryObserverJUnitTest method testIndexInfoOnLocalRegion.

@Test
public void testIndexInfoOnLocalRegion() throws Exception {
    // Query VERBOSE has to be true for the test
    assertEquals("true", System.getProperty(DistributionConfig.GEMFIRE_PREFIX + "Query.VERBOSE"));
    // Create Partition Region
    AttributesFactory af = new AttributesFactory();
    af.setScope(Scope.LOCAL);
    region = CacheUtils.createRegion("portfolio", af.create(), false);
    if (region.size() == 0) {
        for (int i = 1; i <= 100; i++) {
            region.put(Integer.toString(i), new Portfolio(i, i));
        }
    }
    assertEquals(100, region.size());
    qs = CacheUtils.getQueryService();
    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
    assertTrue(keyIndex1 instanceof CompactRangeIndex);
    Query query = qs.newQuery(queryStr);
    // Inject TestHook in QueryObserver before running query.
    IndexTrackingTestHook th = new IndexTrackingTestHook(region, 0);
    QueryObserver observer = QueryObserverHolder.getInstance();
    assertTrue(QueryObserverHolder.hasObserver());
    ((IndexTrackingQueryObserver) observer).setTestHook(th);
    SelectResults results = (SelectResults) query.execute();
    // The query should return all elements in region.
    assertEquals(region.size(), results.size());
    regionMap = ((IndexTrackingTestHook) th).getRegionMap();
    Object rslts = regionMap.getResults().get(region.getFullPath());
    assertTrue(rslts instanceof Integer);
    assertEquals(results.size(), ((Integer) rslts).intValue());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) IndexTrackingTestHook(org.apache.geode.cache.query.internal.index.IndexTrackingQueryObserverDUnitTest.IndexTrackingTestHook) Portfolio(org.apache.geode.cache.query.data.Portfolio) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 68 with QueryObserver

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

the class MultiIndexCreationDUnitTest method testConcurrentMultiIndexCreationAndQuery.

@Test
public void testConcurrentMultiIndexCreationAndQuery() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(1);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    // Start server1
    AsyncInvocation a1 = server1.invokeAsync(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                Portfolio p = new Portfolio(i);
                r.put("key-" + i, p);
            }
            IndexManager.testHook = new MultiIndexCreationTestHook();
            QueryService qs = getCache().getQueryService();
            qs.defineIndex("statusIndex", "status", r.getFullPath());
            qs.defineIndex("IDIndex", "ID", r.getFullPath());
            List<Index> indexes = qs.createDefinedIndexes();
            assertEquals("Only 2 indexes should have been created. ", 2, indexes.size());
            return null;
        }
    });
    final String[] queries = { "select * from " + name + " where status = 'active'", "select * from " + name + " where ID > 4" };
    AsyncInvocation a2 = server1.invokeAsync(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            long giveupTime = System.currentTimeMillis() + 60000;
            while (!hooked && System.currentTimeMillis() < giveupTime) {
                LogWriterUtils.getLogWriter().info("Query Waiting for index hook.");
                Wait.pause(100);
            }
            assertTrue(hooked);
            QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

                private boolean indexCalled = false;

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

                public void endQuery() {
                    assertFalse("Index should not have been used. ", indexCalled);
                }
            });
            SelectResults sr = null;
            for (int i = 0; i < queries.length; i++) {
                try {
                    sr = (SelectResults) getCache().getQueryService().newQuery(queries[i]).execute();
                } catch (Exception e) {
                    fail("QueryExecution failed, " + e);
                }
                assertEquals(5, sr.size());
            }
            QueryObserverHolder.setInstance(old);
            hooked = false;
            return null;
        }
    });
    ThreadUtils.join(a1, 120000);
    if (a1.exceptionOccurred()) {
        fail(a1.getException().getMessage());
    }
    ThreadUtils.join(a2, 120000);
    if (a2.exceptionOccurred()) {
        fail(a2.getException().getMessage());
    }
    server1.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            IndexManager.testHook = null;
            QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

                private boolean indexCalled = false;

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

                public void endQuery() {
                    assertTrue("Index should have been used. ", indexCalled);
                }
            });
            SelectResults sr = null;
            for (int i = 0; i < queries.length; i++) {
                try {
                    sr = (SelectResults) getCache().getQueryService().newQuery(queries[i]).execute();
                } catch (Exception e) {
                    fail("QueryExecution failed, " + e);
                }
                assertEquals(5, sr.size());
            }
            QueryObserverHolder.setInstance(old);
            return null;
        }
    });
}
Also used : Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) List(java.util.List) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 69 with QueryObserver

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

the class IndexTrackingQueryObserverDUnitTest method createPR.

/**
   * CReates a PR on a VM with NUM_BKTS buckets.
   * 
   * @param vm
   */
private void createPR(VM vm) {
    SerializableRunnable createDS = new SerializableRunnable("Creating PR Datastore") {

        public void run() {
            QueryObserver observer = QueryObserverHolder.setInstance(new IndexTrackingQueryObserver());
            // Create Partition Region
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setTotalNumBuckets(NUM_BKTS);
            AttributesFactory af = new AttributesFactory();
            af.setPartitionAttributes(paf.create());
            Region region = getCache().createRegion("portfolio", af.create());
        }
    };
    vm.invoke(createDS);
}
Also used : IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) 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) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver)

Example 70 with QueryObserver

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

the class IndexTrackingQueryObserverDUnitTest method createQueryIndex.

private void createQueryIndex(VM vm, final boolean create) {
    SerializableRunnable createIndex = new SerializableRunnable("Create index on PR") {

        public void run() {
            // Query VERBOSE has to be true for the test
            assertTrue(DefaultQuery.QUERY_VERBOSE);
            QueryService qs = getCache().getQueryService();
            Index keyIndex1 = null;
            try {
                if (create) {
                    keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
                    assertNotNull(keyIndex1);
                    assertTrue(keyIndex1 instanceof PartitionedIndex);
                }
            } catch (Exception e) {
                Assert.fail("While creating Index on PR", e);
            }
            Region region = getCache().getRegion("portfolio");
            // Inject TestHook in QueryObserver before running query.
            IndexTrackingTestHook th = new IndexTrackingTestHook(region, NUM_BKTS / 2);
            QueryObserver observer = QueryObserverHolder.getInstance();
            assertTrue(QueryObserverHolder.hasObserver());
            ((IndexTrackingQueryObserver) observer).setTestHook(th);
        }
    };
    vm.invoke(createIndex);
}
Also used : IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) 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) Index(org.apache.geode.cache.query.Index) IndexTrackingQueryObserver(org.apache.geode.cache.query.internal.IndexTrackingQueryObserver)

Aggregations

QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)71 SelectResults (org.apache.geode.cache.query.SelectResults)55 Test (org.junit.Test)48 Region (org.apache.geode.cache.Region)47 Query (org.apache.geode.cache.query.Query)42 Portfolio (org.apache.geode.cache.query.data.Portfolio)42 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)41 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)38 Index (org.apache.geode.cache.query.Index)31 QueryService (org.apache.geode.cache.query.QueryService)25 LocalRegion (org.apache.geode.internal.cache.LocalRegion)23 Collection (java.util.Collection)17 Cache (org.apache.geode.cache.Cache)13 AttributesFactory (org.apache.geode.cache.AttributesFactory)12 Position (org.apache.geode.cache.query.data.Position)9 Iterator (java.util.Iterator)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 Set (java.util.Set)7 Host (org.apache.geode.test.dunit.Host)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7