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();
}
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());
}
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;
}
});
}
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);
}
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);
}
Aggregations