use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class IndexCreationJUnitTest method testIndexCreationFromXMLForDiskLocalScope.
@Test
public void testIndexCreationFromXMLForDiskLocalScope() throws Exception {
InternalDistributedSystem.getAnyInstance().disconnect();
// TODO: use TemporaryFolder
File file = new File("persistData0");
file.mkdir();
Properties props = new Properties();
props.setProperty(NAME, "test");
props.setProperty(MCAST_PORT, "0");
props.setProperty(CACHE_XML_FILE, getClass().getResource("index-creation-without-eviction.xml").toURI().getPath());
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
Region localDiskRegion = cache.getRegion("localDiskRegion");
for (int i = 0; i < 100; i++) {
Portfolio pf = new Portfolio(i);
localDiskRegion.put("" + i, pf);
}
QueryService qs = cache.getQueryService();
Index ind = qs.getIndex(localDiskRegion, "localDiskIndex");
assertNotNull("Index localIndex should have been created ", ind);
// verify that a query on the creation time works as expected
SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM " + localDiskRegion.getFullPath() + " Where status = 'active'").execute();
assertEquals("OQL index results did not match", 50, results.size());
ds.disconnect();
FileUtils.deleteDirectory(file);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class IndexCreationJUnitTest method testIndexCreationFromXML.
/**
* Test for bug 46872, make sure we recover the index correctly if the cache.xml changes for a
* persistent region.
*/
@Test
public void testIndexCreationFromXML() throws Exception {
InternalDistributedSystem.getAnyInstance().disconnect();
File file = new File("persistData0");
file.mkdir();
{
Properties props = new Properties();
props.setProperty(NAME, "test");
props.setProperty(MCAST_PORT, "0");
props.setProperty(CACHE_XML_FILE, getClass().getResource("index-creation-with-eviction.xml").toURI().getPath());
DistributedSystem ds = DistributedSystem.connect(props);
// Create the cache which causes the cache-xml-file to be parsed
Cache cache = CacheFactory.create(ds);
QueryService qs = cache.getQueryService();
Region region = cache.getRegion("mainReportRegion");
for (int i = 0; i < 100; i++) {
Portfolio pf = new Portfolio(i);
pf.setCreateTime(i);
region.put("" + i, pf);
}
// verify that a query on the creation time works as expected
SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
assertEquals("OQL index results did not match", 1, results.size());
cache.close();
ds.disconnect();
}
{
Properties props = new Properties();
props.setProperty(NAME, "test");
props.setProperty(MCAST_PORT, "0");
// Using a different cache.xml that changes some region properties
// That will force the disk code to copy the region entries.
props.setProperty(CACHE_XML_FILE, getClass().getResource("index-creation-without-eviction.xml").toURI().getPath());
DistributedSystem ds = DistributedSystem.connect(props);
Cache cache = CacheFactory.create(ds);
QueryService qs = cache.getQueryService();
Region region = cache.getRegion("mainReportRegion");
// verify that a query on the creation time works as expected
SelectResults results = (SelectResults) qs.newQuery("<trace>SELECT * FROM /mainReportRegion.entrySet mr Where mr.value.createTime > 1L and mr.value.createTime < 3L").execute();
assertEquals("OQL index results did not match", 1, results.size());
ds.disconnect();
FileUtils.deleteDirectory(file);
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class IndexCreationJUnitTest method testIndexOnOverflowRegion.
@Test
public void testIndexOnOverflowRegion() throws Exception {
String regionName = "portfolios_overflow";
// overflow region.
AttributesFactory attributesFactory = new AttributesFactory();
attributesFactory.setValueConstraint(Portfolio.class);
attributesFactory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.OVERFLOW_TO_DISK));
Region region = CacheUtils.createRegion(regionName, attributesFactory.create(), true);
for (int i = 0; i < 4; i++) {
region.put(new Portfolio(i), new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
// Currently supported with compact range-index.
Index i1 = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "pf.ID", "/portfolios_overflow pf");
Index i2 = qs.createIndex("keyIdIndex", IndexType.FUNCTIONAL, "key.ID", "/portfolios_overflow.keys key");
// Not yet supported with range-index.
try {
Index i3 = qs.createIndex("idIndex2", IndexType.FUNCTIONAL, "pf.ID", "/portfolios_overflow pf, pf.positions pos");
fail("Range index not supported on overflow region.");
} catch (UnsupportedOperationException ex) {
// Expected.
}
// Execute query.
String[] queryStr = new String[] { "Select * from /portfolios_overflow pf where pf.ID = 2", "Select * from /portfolios_overflow.keys key where key.ID = 2", "Select * from /portfolios_overflow pf where pf.ID > 1", "Select * from /portfolios_overflow pf where pf.ID < 2" };
int[] resultSize = new int[] { 1, 1, 2, 2 };
for (int i = 0; i < queryStr.length; i++) {
Query q = qs.newQuery(queryStr[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
SelectResults results = (SelectResults) q.execute();
if (!observer.isIndexesUsed) {
fail("Index not used for query. " + queryStr[i]);
}
assertEquals(results.size(), resultSize[i]);
}
for (int i = 0; i < 10; i++) {
region.put(new Portfolio(i), new Portfolio(i));
}
// Persistent overflow region.
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class IUMRCompositeIteratorJUnitTest method testQueryWithCompositeIter2.
@Ignore
@Test
public void testQueryWithCompositeIter2() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
String[] queries = { // Test Case No. IUMR
"Select distinct * from /countries c, /employees e, c.citizens[e.empId].arr where e.name='empName'" };
SelectResults[][] r = new SelectResults[queries.length][2];
// Execute Query without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
r[i][0] = (SelectResults) q.execute();
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create Indexes and Execute Queries
qs.createIndex("nameIndex", IndexType.FUNCTIONAL, "e.name", "/employees e");
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = (SelectResults) q.execute();
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
int indxs = observer.indexesUsed.size();
CacheUtils.log("***********Indexes Used :::: " + indxs + " IndexName::" + observer.IndexName);
if (indxs != 1) {
fail("FAILED: The Index should be used. Presently only " + indxs + " Index(es) is used");
}
Iterator itr = observer.indexesUsed.iterator();
String temp;
while (itr.hasNext()) {
temp = itr.next().toString();
if (temp.equals("nameIndex")) {
break;
} else {
fail("indices used do not match with the which is expected to be used" + "<nameIndex> was expected but found " + itr.next());
}
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Verifying the query results
CacheUtils.compareResultsOfWithAndWithoutIndex(r, this);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class GroupByTestImpl method testAggregateFuncAvg.
@Test
public void testAggregateFuncAvg() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
String queryStr = "select p.status as status, Avg(p.ID) as average from " + "/portfolio p where p.ID > 0 group by status";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
CompiledSelect cs = ((DefaultQuery) query).getSelect();
SelectResults sr = (SelectResults) query.execute();
assertTrue(sr.getCollectionType().getElementType().isStructType());
assertEquals(2, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
double sumIDActive = 0, sumIDInactive = 0;
int numActive = 0, numInactive = 0;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.ID > 0) {
if (pf.status.equals("active")) {
sumIDActive += pf.ID;
++numActive;
} else if (pf.status.equals("inactive")) {
sumIDInactive += pf.ID;
++numInactive;
}
}
}
Number avgActive = AbstractAggregator.downCast(sumIDActive / numActive);
Number avgInactive = AbstractAggregator.downCast(sumIDInactive / numInactive);
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(avgActive, struct.get("average"));
} else if (struct.get("status").equals("inactive")) {
assertEquals(avgInactive, struct.get("average"));
} else {
fail("unexpected value of status");
}
}
ObjectType elementType = sr.getCollectionType().getElementType();
assertTrue(elementType.isStructType());
StructType structType = (StructType) elementType;
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
}
Aggregations