use of org.apache.geode.cache.query.QueryTestUtils in project geode by apache.
the class CopyOnReadQueryJUnitTest method setUp.
@Before
public void setUp() throws java.lang.Exception {
utils = new QueryTestUtils();
utils.createCache(null);
utils.getCache().setCopyOnRead(true);
Portfolio.resetInstanceCount();
}
use of org.apache.geode.cache.query.QueryTestUtils 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);
}
use of org.apache.geode.cache.query.QueryTestUtils in project geode by apache.
the class CopyOnReadIndexDUnitTest method testPRQueryOnLocalNode.
// test different queries against partitioned region
@Test
public void testPRQueryOnLocalNode() throws Exception {
QueryTestUtils utils = new QueryTestUtils();
configureServers();
helpTestPRQueryOnLocalNode(utils.queries.get("545"), 100, 100, true);
helpTestPRQueryOnLocalNode(utils.queries.get("546"), 100, 100, true);
helpTestPRQueryOnLocalNode(utils.queries.get("543"), 100, 100, true);
helpTestPRQueryOnLocalNode(utils.queries.get("544"), 100, 100, true);
helpTestPRQueryOnLocalNode("select * from /portfolios p where p.ID = 1", 100, 1, true);
helpTestPRQueryOnLocalNode(utils.queries.get("545"), 100, 100, false);
helpTestPRQueryOnLocalNode(utils.queries.get("546"), 100, 100, false);
helpTestPRQueryOnLocalNode(utils.queries.get("543"), 100, 100, false);
helpTestPRQueryOnLocalNode(utils.queries.get("544"), 100, 100, false);
helpTestPRQueryOnLocalNode("select * from /portfolios p where p.ID = 1", 100, 1, false);
}
Aggregations