Search in sources :

Example 21 with Position

use of org.apache.geode.cache.query.data.Position in project geode by apache.

the class QueryDataInconsistencyDUnitTest method testRangeIndexWithIndexAndQueryFromCluaseMisMatch.

// GEODE-925: time sensitive, async actions, short timeouts
@Category(FlakyTest.class)
@Test
public void testRangeIndexWithIndexAndQueryFromCluaseMisMatch() {
    // TODO: fix misspelling
    // Create caches
    Properties props = new Properties();
    server.invoke(() -> PRClientServerTestBase.createCacheInVm(props));
    server.invoke(new CacheSerializableRunnable("create indexes") {

        @Override
        public void run2() throws CacheException {
            cache = CacheFactory.getAnyInstance();
            Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(repRegionName);
            IndexManager.testHook = null;
            // Create common Portfolios and NewPortfolios
            Position.cnt = 0;
            for (int j = cnt; j < cntDest; j++) {
                region.put(new Integer(j), new Portfolio(j));
            }
            QueryService qs = CacheFactory.getAnyInstance().getQueryService();
            try {
                Index index = qs.createIndex("posIndex", "pos.secId", "/" + repRegionName + " p, p.collectionHolderMap.values coll, p.positions.values pos");
                assertEquals(12, index.getStatistics().getNumberOfKeys());
            } catch (Exception e) {
                fail("Index creation failed");
            }
        }
    });
    // Invoke update from client and stop in updateIndex
    // first before updating the RegionEntry and second after updating
    // the RegionEntry.
    AsyncInvocation putThread = server.invokeAsync(new CacheSerializableRunnable("update a Region Entry") {

        @Override
        public void run2() throws CacheException {
            Region repRegion = CacheFactory.getAnyInstance().getRegion(repRegionName);
            IndexManager.testHook = new IndexManagerTestHook();
            // This portfolio with same ID must have different positions.
            repRegion.put(new Integer("1"), new Portfolio(1));
        // above call must be hooked in BEFORE_UPDATE_OP call.
        }
    });
    server.invoke(new CacheSerializableRunnable("query on server") {

        @Override
        public void run2() throws CacheException {
            QueryService qs = CacheFactory.getAnyInstance().getQueryService();
            Position pos1 = null;
            while (!hooked) {
                Wait.pause(100);
            }
            try {
                Object rs = qs.newQuery("<trace> select pos from /" + repRegionName + " p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
                CacheFactory.getAnyInstance().getLogger().fine("Shobhit: " + rs);
                assertTrue(rs instanceof SelectResults);
                pos1 = (Position) ((SelectResults) rs).iterator().next();
                if (!pos1.secId.equals("APPL")) {
                    fail("Query thread did not verify index results even when RE is under update");
                    IndexManager.testHook = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("Query execution failed on server.", e);
                IndexManager.testHook = null;
            } finally {
                // Let client put go further.
                hooked = false;
            }
            while (!hooked) {
                Wait.pause(100);
            }
            try {
                Object rs = qs.newQuery("select pos from /" + repRegionName + " p, p.positions.values pos where pos.secId = 'APPL' AND p.ID = 1").execute();
                assertTrue(rs instanceof SelectResults);
                if (((SelectResults) rs).size() > 0) {
                    Position pos2 = (Position) ((SelectResults) rs).iterator().next();
                    if (pos2.equals(pos1)) {
                        fail("Query thread did not verify index results even when RE is under update and " + "RegionEntry value has been modified before releasing the lock");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail("Query execution failed on server.");
            } finally {
                // Let client put go further.
                hooked = false;
                IndexManager.testHook = null;
            }
        }
    });
    // GEODE-925 occurs here and this is very short join 200
    ThreadUtils.join(putThread, 200);
// millis
}
Also used : CacheException(org.apache.geode.cache.CacheException) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) Properties(java.util.Properties) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheException(org.apache.geode.cache.CacheException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 22 with Position

use of org.apache.geode.cache.query.data.Position in project geode by apache.

the class LimitClauseJUnitTest method testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField.

@Test
public void testLimitOnJunctionWithCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 1;
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", "P.ID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 20 AND P.shortID > 1 AND P.shortID < 3 LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) 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 23 with Position

use of org.apache.geode.cache.query.data.Position in project geode by apache.

the class LimitClauseJUnitTest method testLimitOnCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField.

@Test
public void testLimitOnCompactRangeIndexedFieldWithAndClauseJunctionNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 1;
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", "P.ID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID = 10 AND P.shortID > 1 AND P.shortID < 3 LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertTrue(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) 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 24 with Position

use of org.apache.geode.cache.query.data.Position in project geode by apache.

the class IndexWithSngleFrmAndMultCondQryJUnitTest method testNonDistinctOrCondResults.

@Test
public void testNonDistinctOrCondResults() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 10; i++) {
        region.put("" + i, new Portfolio(i));
    }
    for (int i = 10; i < 20; i++) {
        Portfolio p = new Portfolio(i);
        if (i % 2 == 0) {
            p.status = null;
        }
        region.put("" + i, p);
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT * FROM /pos pf,  positions.values pos where pf.ID > 0 OR pf.status='active' OR  pos.secId != 'IBM'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status='active'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status LIKE 'act%'", "SELECT * FROM /pos pf where pf.ID > 0 OR pf.status IN SET('active', 'inactive')" };
    SelectResults[] sr = new SelectResults[queries.length];
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer);
            sr[i] = (SelectResults) q.execute();
            if (observer.isIndexesUsed) {
                fail("How could index be present when not created!?");
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Verify Results
    for (int i = 0; i < sr.length; i++) {
        Set resultSet = sr[i].asSet();
        // Check Element Type
        if (queries[i].contains("values")) {
            assertTrue("Checking Element type for Query: [" + queries[i] + "] results", sr[i].getCollectionType().getElementType().toString().equals("struct<pf:org.apache.geode.cache.query.data.Portfolio,pos:java.lang.Object>"));
            // Check Size of results
            assertEquals("Checking Element type for Query: [" + queries[i] + "] results", 40, resultSet.size());
        } else {
            assertTrue("Checking Element type for Query: [" + queries[i] + "] results", sr[i].getCollectionType().getElementType().toString().equals("org.apache.geode.cache.query.data.Portfolio"));
            // Check Size of results
            assertEquals("Checking Element type for Query: [" + queries[i] + "] results", 20, resultSet.size());
        }
        Iterator itr = resultSet.iterator();
        while (itr.hasNext()) {
            Object obj = itr.next();
            if (sr[i].getCollectionType().getElementType().toString().equals("struct<pf:org.apache.geode.cache.query.data.Portfolio,pos:java.lang.Object>")) {
                Object[] values = ((Struct) obj).getFieldValues();
                Portfolio port = (Portfolio) values[0];
                Position pos = (Position) values[1];
                if (!(port.getID() > 0 || port.status.equals("active") || pos.secId.equals("IBM"))) {
                    fail("Result object" + obj + " failed to satisfy all OR conditions of where clause of query " + queries[i]);
                }
            } else {
                Portfolio port = (Portfolio) obj;
                if (!(port.getID() > 0 || port.status.equals("active"))) {
                    fail("Result object" + port + " failed to satisfy all OR conditions of where clause of query " + queries[i]);
                }
            }
        }
    }
}
Also used : Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) 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) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 25 with Position

use of org.apache.geode.cache.query.data.Position in project geode by apache.

the class LimitClauseJUnitTest method testLimitJunctionOnRangeIndexedFieldWithAndClauseOnNonIndexedField.

@Test
public void testLimitJunctionOnRangeIndexedFieldWithAndClauseOnNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(i);
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(i);
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values POS");
    String queryString = "SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID > 9 AND P.ID < 21 AND POS.secId = 'VMW' LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) 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)

Aggregations

Position (org.apache.geode.cache.query.data.Position)31 Portfolio (org.apache.geode.cache.query.data.Portfolio)30 Region (org.apache.geode.cache.Region)27 SelectResults (org.apache.geode.cache.query.SelectResults)27 Test (org.junit.Test)26 Query (org.apache.geode.cache.query.Query)24 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)23 Index (org.apache.geode.cache.query.Index)20 LocalRegion (org.apache.geode.internal.cache.LocalRegion)14 QueryService (org.apache.geode.cache.query.QueryService)12 Struct (org.apache.geode.cache.query.Struct)9 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)9 Iterator (java.util.Iterator)8 HashMap (java.util.HashMap)5 CacheException (org.apache.geode.cache.CacheException)5 Properties (java.util.Properties)3 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)3 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)3 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3