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