Search in sources :

Example 26 with Position

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

the class LimitClauseJUnitTest method testLimitJunctionOnRangeIndexedFieldWithAndClauseCompactRangeIndexedField.

// This is one where we are no longer applying index due to multiple index usage but old code
// would. should take a look and see how/ or why old code can
@Test
public void testLimitJunctionOnRangeIndexedFieldWithAndClauseCompactRangeIndexedField() 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.shortID = 1;
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 21; i < 100; i++) {
        Portfolio p = new Portfolio(i);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(i);
        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", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values POS");
    Index shortIdIndex = qs.createIndex("shortIdIndex", IndexType.FUNCTIONAL, "P.shortID", "/portfolios1 P");
    String queryString = "<trace>SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 21 AND P.shortID = 2 LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    // assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : 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 27 with Position

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

the class LimitClauseJUnitTest method testLimitOnJunctionWithCompactRangeIndexedFieldWithAndOnCompactRangeIndexedField.

@Test
public void testLimitOnJunctionWithCompactRangeIndexedFieldWithAndOnCompactRangeIndexedField() 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");
    Index shortIdIndex = qs.createIndex("shortIdIndex", "P.shortID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID > 9 AND P.ID < 20 AND P.shortID = 2 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 28 with Position

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

the class LimitClauseJUnitTest method testLimitOnEqualsRangeIndexedFieldWithAndClauseCompactRangeIndexedField.

// This is one where limit is applied at index for old code but we do not apply
@Test
public void testLimitOnEqualsRangeIndexedFieldWithAndClauseCompactRangeIndexedField() 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, P.positions.values POS");
    Index shortIdIndex = qs.createIndex("shortIdIndex", "P.shortID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID = 10 AND P.shortID = 2 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 29 with Position

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

the class CopyOnReadIndexDUnitTest method helpTestTransactionsOnReplicatedRegion.

public void helpTestTransactionsOnReplicatedRegion(final String queryString, final int numPortfolios, final int numExpectedResults, final boolean hasIndex) throws Exception {
    resetInstanceCount(vm0);
    resetInstanceCount(vm1);
    resetInstanceCount(vm2);
    createReplicatedRegion(vm0, "portfolios");
    createReplicatedRegion(vm1, "portfolios");
    createReplicatedRegion(vm2, "portfolios");
    // counts
    if (hasIndex) {
        vm0.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                QueryTestUtils utils = new QueryTestUtils();
                utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
                return null;
            }
        });
        // let's not create index on vm1 to check different scenarios
        vm2.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                QueryTestUtils utils = new QueryTestUtils();
                utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
                return null;
            }
        });
    }
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            for (int i = 0; i < numPortfolios; i++) {
                Portfolio p = new Portfolio(i);
                p.status = "testStatus";
                p.positions = new HashMap();
                p.positions.put("" + i, new Position("" + i, 20));
                region.put("key " + i, p);
            }
            // We should have the same number of portfolio objects that we created for the put
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 5000, 200, true);
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            // At this point, we should only have serialized values in this vm
            Region region = getCache().getRegion("/portfolios");
            Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            // There is an index for vm2, so we should have deserialized values at this point,
            Region region = getCache().getRegion("/portfolios");
            if (hasIndex) {
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 0, 200, true);
            } else {
                Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
            }
            return null;
        }
    });
    // start transaction
    // execute query
    // modify results
    // check instance count
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            CacheTransactionManager txManager = region.getCache().getCacheTransactionManager();
            try {
                txManager.begin();
                QueryService qs = getCache().getQueryService();
                Query query = qs.newQuery(queryString);
                SelectResults results = (SelectResults) query.execute();
                assertEquals(numExpectedResults, results.size());
                for (Object o : results) {
                    if (o instanceof Portfolio) {
                        Portfolio p = (Portfolio) o;
                        p.status = "discardStatus";
                    } else {
                        Struct struct = (Struct) o;
                        Portfolio p = (Portfolio) struct.getFieldValues()[0];
                        p.status = "discardStatus";
                    }
                }
                txManager.commit();
            } catch (CommitConflictException conflict) {
                Assert.fail("commit conflict exception", conflict);
            }
            // We have created puts from our previous callable
            // Now we have copied the results from the query
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
            return null;
        }
    });
    // Check objects in cache on vm1
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                }
            }
            // first it must deserialize the portfolios in the replicated region
            // then we do a copy on read of these deserialized objects for the final result set
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
            results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            // we never created index on vm1
            // so in this case, we always have to deserialize the value from the region
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
            return null;
        }
    });
    // Check objects in cache on vm2
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                }
            }
            // with or without index, the values had to have been deserialized at one point
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults), 0, 200, true);
            results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            if (hasIndex) {
                // we have an index, so the values are already deserialized
                // total is now our original deserialization amount : numPortfolios
                // two query results copied.
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults * 2), 0, 200, true);
            } else {
                // we never created index on vm1
                // so in this case, we always have to deserialize the value from the region
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
            }
            return null;
        }
    });
    // Check objects in cache on vm0
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            // with or without index, the values we put in the region were already deserialized values
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults * 2 + numPortfolios), 0, 200, true);
            return null;
        }
    });
    destroyRegion("portfolio", vm0);
}
Also used : Query(org.apache.geode.cache.query.Query) HashMap(java.util.HashMap) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) CacheException(org.apache.geode.cache.CacheException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Struct(org.apache.geode.cache.query.Struct) CommitConflictException(org.apache.geode.cache.CommitConflictException) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryTestUtils(org.apache.geode.cache.query.QueryTestUtils) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 30 with Position

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

the class IUM6Bug32345ReJUnitTest method testComparisonBetnWithAndWithoutIndexCreation.

@Test
public void testComparisonBetnWithAndWithoutIndexCreation() throws Exception {
    Region region = CacheUtils.createRegion("pos", Portfolio.class);
    for (int i = 0; i < 4; i++) {
        region.put("" + i, new Portfolio(i));
    }
    QueryService qs;
    qs = CacheUtils.getQueryService();
    String[] queries = { "SELECT DISTINCT * FROM /pos pf,  positions.values pos where pf.status='active' and pos.secId= 'IBM' and ID = 0" };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    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][0] = (SelectResults) q.execute();
            resType1 = (StructType) (sr[i][0]).getCollectionType().getElementType();
            resSize1 = ((sr[i][0]).size());
            strg1 = resType1.getFieldNames();
            set1 = ((sr[i][0]).asSet());
            Iterator iter = set1.iterator();
            while (iter.hasNext()) {
                Struct stc1 = (Struct) iter.next();
                valPf1 = stc1.get(strg1[0]);
                valPos1 = stc1.get(strg1[1]);
                isActive1 = ((Portfolio) stc1.get(strg1[0])).isActive();
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    // Create an Index on status and execute the same query again.
    qs = CacheUtils.getQueryService();
    qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "pf.status", "/pos pf, pf.positions.values pos");
    // Retesting BUG # 32345
    // Index index2 = (Index)qs.createIndex("secIdIndex", IndexType.FUNCTIONAL,"pos.secId","/pos pf,
    // pf.positions.values pos");
    qs.createIndex("IDIndex", IndexType.FUNCTIONAL, "pf.ID", "/pos pf, pf.positions.values pos");
    String[] queries2 = { "SELECT DISTINCT * FROM /pos pf,  positions.values pos where pf.status='active' and pos.secId= 'IBM' and ID = 0" };
    for (int i = 0; i < queries.length; i++) {
        Query q = null;
        try {
            q = CacheUtils.getQueryService().newQuery(queries[i]);
            QueryObserverImpl observer2 = new QueryObserverImpl();
            QueryObserverHolder.setInstance(observer2);
            sr[i][1] = (SelectResults) q.execute();
            if (!observer2.isIndexesUsed) {
                fail("Index NOT Used");
            }
            resType2 = (StructType) (sr[i][1]).getCollectionType().getElementType();
            resSize2 = ((sr[i][1]).size());
            // CacheUtils.log(resType2);
            strg2 = resType2.getFieldNames();
            // CacheUtils.log(strg2[0]);
            // CacheUtils.log(strg2[1]);
            set2 = ((sr[i][1]).asSet());
            Iterator iter = set2.iterator();
            while (iter.hasNext()) {
                Struct stc2 = (Struct) iter.next();
                valPf2 = stc2.get(strg2[0]);
                valPos2 = stc2.get(strg2[1]);
                isActive2 = ((Portfolio) stc2.get(strg2[0])).isActive();
            // CacheUtils.log(valPf2);
            // CacheUtils.log(valPos2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(q.getQueryString());
        }
    }
    if ((resType1).equals(resType2)) {
        CacheUtils.log("Both Search Results are of the same Type i.e.--> " + resType1);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    if (resSize1 == resSize2 || resSize1 != 0) {
        CacheUtils.log("Both Search Results are Non-zero and are of Same Size i.e.  Size= " + resSize1);
    } else {
        fail("FAILED:Search result Type is different in both the cases");
    }
    itert2 = set2.iterator();
    itert1 = set1.iterator();
    while (itert1.hasNext()) {
        Struct stc2 = (Struct) itert2.next();
        Struct stc1 = (Struct) itert1.next();
        if (stc2.get(strg2[0]) != stc1.get(strg1[0]))
            fail("FAILED: In both the Cases the first member of StructSet i.e. Portfolio are different. ");
        if (stc2.get(strg2[1]) != stc1.get(strg1[1]))
            fail("FAILED: In both the cases Positions are different");
        if (!StringUtils.equals(((Position) stc2.get(strg2[1])).secId, ((Position) stc1.get(strg1[1])).secId))
            fail("FAILED: In both the cases Positions secIds are different");
        if (((Portfolio) stc2.get(strg2[0])).isActive() != ((Portfolio) stc1.get(strg1[0])).isActive())
            fail("FAILED: Status of the Portfolios found are different");
        if (((Portfolio) stc2.get(strg2[0])).getID() != ((Portfolio) stc1.get(strg1[0])).getID())
            fail("FAILED: IDs of the Portfolios found are different");
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : 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)

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