Search in sources :

Example 61 with QueryObserverAdapter

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttributeWithIndex.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults . This test contains
   * projection attributes. Since the attribute is unique every time, the limit will be satisfied
   * with first 5 iterations
   * 
   * Tests StructBag behaviour
   * 
   */
@Test
public void testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttributeWithIndex() {
    try {
        Query query;
        SelectResults result;
        query = qs.newQuery("SELECT DISTINCT pf.ID, pf.createTime FROM /portfolios pf WHERE pf.ID > 0 limit 5");
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios pf");
        assertNotNull(index);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
        assertTrue(wrapper.getCollectionType().getElementType() instanceof StructType);
        assertTrue(observer.limitAppliedAtIndex);
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 62 with QueryObserverAdapter

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

the class LimitClauseJUnitTest method testLimitDistinctQueryWithDuplicateValues.

@Test
public void testLimitDistinctQueryWithDuplicateValues() {
    try {
        Query query;
        SelectResults result;
        Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
        for (int i = 1; i < 10; i++) {
            Portfolio p = new Portfolio(i);
            if (i == 2)
                p = new Portfolio(1);
            region.put(Integer.toString(i), p);
        }
        // Create Index on ID
        Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
        assertNotNull(index);
        String queryString = "select DISTINCT * from /portfolios1 where status ='inactive' AND ID > 0 limit 2";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals(2, result.size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
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) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 63 with QueryObserverAdapter

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

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