use of org.apache.geode.cache.query.data.PortfolioPdx in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncAvg.
@Override
@Test
public void testAggregateFuncAvg() throws Exception {
Region region = this.createRegion("portfolio", PortfolioPdx.class);
for (int i = 1; i < 200; ++i) {
PortfolioPdx pf = new PortfolioPdx(i);
pf.shortID = (short) ((short) i / 5);
region.put("key-" + 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()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.getID() > 0) {
if (pf.status.equals("active")) {
sumIDActive += pf.getID();
++numActive;
} else if (pf.status.equals("inactive")) {
sumIDInactive += pf.getID();
++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());
}
use of org.apache.geode.cache.query.data.PortfolioPdx in project geode by apache.
the class PdxGroupByTestImpl method testComplexValueAggregateFuncAvgDistinct.
@Override
@Test
public void testComplexValueAggregateFuncAvgDistinct() throws Exception {
Region region = this.createRegion("portfolio", PortfolioPdx.class);
for (int i = 1; i < 200; ++i) {
PortfolioPdx pf = new PortfolioPdx(i);
pf.shortID = (short) ((short) i / 5);
region.put("key-" + i, pf);
}
String queryStr = "select p.status as status, avg(distinct element(select iter.shortID from /portfolio iter where iter.ID = 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");
Set<Short> sumIDActiveSet = new HashSet<Short>(), sumIDInactiveSet = new HashSet<Short>();
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.getID() > 0) {
if (pf.status.equals("active")) {
sumIDActiveSet.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
sumIDInactiveSet.add(pf.shortID);
}
}
}
double sumActive = 0, sumInactive = 0;
for (Short shortt : sumIDActiveSet) {
sumActive += shortt.doubleValue();
}
for (Short shortt : sumIDInactiveSet) {
sumInactive += shortt.doubleValue();
}
Number avgActive = AbstractAggregator.downCast(sumActive / sumIDActiveSet.size());
Number avgInactive = AbstractAggregator.downCast(sumInactive / sumIDInactiveSet.size());
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());
}
use of org.apache.geode.cache.query.data.PortfolioPdx in project geode by apache.
the class SelectStarQueryDUnitTest method startReplicatedCacheServer.
private int startReplicatedCacheServer(VM vm) {
final int port = (Integer) vm.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName);
Region r2 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName2);
// put domain objects
for (int i = 0; i < 10; i++) {
r1.put("key-" + i, new PortfolioPdx(i));
r2.put("key-" + i, new PortfolioPdx(i));
}
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
return port;
}
use of org.apache.geode.cache.query.data.PortfolioPdx in project geode by apache.
the class SelectStarQueryDUnitTest method testSelectStarQueryForPdxObjects.
@Test
public void testSelectStarQueryForPdxObjects() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM client = host.getVM(3);
// create servers and regions
final int port1 = startReplicatedCacheServer(server1);
server1.invoke(new SerializableCallable("Set observer") {
@Override
public Object call() throws Exception {
oldObserver = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
return null;
}
});
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(getServerHostName(server1.getHost()), port1);
ClientCache cache = getClientCache(cf);
cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
return null;
}
});
// Update with serialized PortfolioPdx objects
client.invoke(new SerializableCallable("Put objects") {
@Override
public Object call() throws Exception {
Region r1 = getRootRegion(regName);
for (int i = 0; i < 20; i++) {
r1.put("key-" + i, new PortfolioPdx(i));
}
return null;
}
});
// query remotely from client
client.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
getLogWriter().info("Querying remotely from client");
QueryService localQS = null;
QueryService remoteQS = null;
try {
localQS = ((ClientCache) getCache()).getLocalQueryService();
remoteQS = ((ClientCache) getCache()).getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
SelectResults[][] sr = new SelectResults[1][2];
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) localQS.newQuery(queries[i]).execute();
sr[0][0] = res;
res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
sr[0][1] = res;
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PortfolioPdx || obj instanceof PositionPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + obj.getClass());
}
}
} else if (rs instanceof PortfolioPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + rs.getClass());
}
}
}
}
return null;
}
});
// verify if objects iterated by query are serialized
server1.invoke(new SerializableCallable("Get observer") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertTrue(resultObserver.isObjectSerialized());
return null;
}
});
// verify if objects returned by local server query are not serialized
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
QueryObserver observer = QueryObserverHolder.setInstance(new QueryResultTrackingObserver());
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PortfolioPdx || obj instanceof PositionPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + obj.getClass());
}
}
} else if (rs instanceof PortfolioPdx) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + rs.getClass());
}
}
}
}
observer = QueryObserverHolder.getInstance();
assertTrue(QueryObserverHolder.hasObserver());
assertTrue(observer instanceof QueryResultTrackingObserver);
QueryResultTrackingObserver resultObserver = (QueryResultTrackingObserver) observer;
assertFalse(resultObserver.isObjectSerialized());
QueryObserverHolder.setInstance(oldObserver);
return null;
}
});
// verify if Pdx instances are returned by local server query
// if read-serialized is set true
server1.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService qs = null;
try {
qs = getCache().getQueryService();
} catch (Exception e) {
fail("Exception getting query service ", e);
}
SelectResults res = null;
for (int i = 0; i < queries.length; i++) {
try {
res = (SelectResults) qs.newQuery(queries[i]).execute();
} catch (Exception e) {
fail("Error executing query: " + queries[i], e);
}
assertEquals(resultSize[i], res.size());
if (i == 3) {
int cnt = ((Integer) res.iterator().next());
assertEquals(20, cnt);
} else {
for (Object rs : res) {
if (rs instanceof StructImpl) {
for (Object obj : ((StructImpl) rs).getFieldValues()) {
if (obj instanceof PdxInstance) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PdxInstance and not " + obj.getClass());
}
}
} else if (rs instanceof PdxInstance) {
} else {
fail("Result objects for remote client query: " + queries[i] + " should be instance of PdxInstance and not " + rs.getClass());
}
}
}
}
return null;
}
});
closeCache(client);
closeCache(server1);
}
use of org.apache.geode.cache.query.data.PortfolioPdx in project geode by apache.
the class PdxStringQueryDUnitTest method testRepliacatedRegionCompactRangeIndex.
@Test
public void testRepliacatedRegionCompactRangeIndex() throws CacheException {
final Host host = Host.getHost(0);
VM server0 = host.getVM(0);
VM server1 = host.getVM(1);
VM server2 = host.getVM(2);
VM client = host.getVM(3);
final int numberOfEntries = 10;
// Start server1 and create index
server0.invoke(new CacheSerializableRunnable("Create Server1") {
public void run2() throws CacheException {
configAndStartBridgeServer(false, false, false);
// create a local query service
QueryService localQueryService = null;
try {
localQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
// Verify the type of index created
Index index = null;
try {
index = localQueryService.createIndex("statusIndex", "status", regName);
if (!(index instanceof CompactRangeIndex)) {
fail("CompactRange Index should have been created instead of " + index.getClass());
}
} catch (Exception ex) {
fail("Failed to create index." + ex.getMessage());
}
}
});
// Start server2
server1.invoke(new CacheSerializableRunnable("Create Server2") {
public void run2() throws CacheException {
configAndStartBridgeServer(false, false, false);
Region region = getRootRegion().getSubregion(regionName);
}
});
// Start server3
server2.invoke(new CacheSerializableRunnable("Create Server3") {
public void run2() throws CacheException {
configAndStartBridgeServer(false, false, false);
Region region = getRootRegion().getSubregion(regionName);
}
});
// Client pool.
final int port0 = server0.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
final int port1 = server1.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
final int port2 = server2.invoke(() -> PdxStringQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server0.getHost());
// Create client pool.
final String poolName = "testClientServerQueryPool";
createPool(client, poolName, new String[] { host0 }, new int[] { port0, port1, port2 }, true);
// Create client region and put PortfolioPdx objects (PdxInstances)
client.invoke(new CacheSerializableRunnable("Create client") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
Region region = createRegion(regionName, rootRegionName, factory.create());
LogWriterUtils.getLogWriter().info("Put PortfolioPdx");
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new PortfolioPdx(i));
}
}
});
// Verify if all the index keys are PdxStrings
server0.invoke(new CacheSerializableRunnable("Create Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
QueryService localQueryService = getCache().getQueryService();
Index index = localQueryService.getIndex(region, "statusIndex");
CloseableIterator<IndexStoreEntry> iter = ((CompactRangeIndex) index).getIndexStorage().iterator(null);
while (iter.hasNext()) {
Object key = iter.next().getDeserializedKey();
if (!(key instanceof PdxString)) {
fail("All keys of the CompactRangeIndex should be PdxStrings and not " + key.getClass());
}
}
}
});
// Execute queries from client to server and locally on client
SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
QueryService remoteQueryService = null;
QueryService localQueryService = null;
SelectResults[][] rs = new SelectResults[1][2];
try {
remoteQueryService = (PoolManager.find(poolName)).getQueryService();
localQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
for (int i = 0; i < queryString.length; i++) {
try {
LogWriterUtils.getLogWriter().info("### Executing Query on remote server:" + queryString[i]);
Query query = remoteQueryService.newQuery(queryString[i]);
rs[0][0] = (SelectResults) query.execute();
LogWriterUtils.getLogWriter().info("RR remote indexType: CompactRange size of resultset: " + rs[0][0].size() + " for query: " + queryString[i]);
;
checkForPdxString(rs[0][0].asList(), queryString[i]);
LogWriterUtils.getLogWriter().info("### Executing Query locally on client:" + queryString[i]);
query = localQueryService.newQuery(queryString[i]);
rs[0][1] = (SelectResults) query.execute();
LogWriterUtils.getLogWriter().info("RR client local indexType: CompactRange size of resultset: " + rs[0][1].size() + " for query: " + queryString[i]);
;
checkForPdxString(rs[0][1].asList(), queryString[i]);
if (i < orderByQueryIndex) {
// Compare local and remote query results.
if (!compareResultsOfWithAndWithoutIndex(rs)) {
fail("Local and Remote Query Results are not matching for query :" + queryString[i]);
}
} else {
// compare the order of results returned
compareResultsOrder(rs, false);
}
} catch (Exception e) {
Assert.fail("Failed executing " + queryString[i], e);
}
}
}
};
client.invoke(executeQueries);
// Put Non Pdx objects on server execute queries locally
server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
LogWriterUtils.getLogWriter().info("Put Objects locally on server");
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
region.put("key-" + i, new Portfolio(i));
}
QueryService localQueryService = getCache().getQueryService();
// Query server1 locally to check if PdxString is not being returned
for (int i = 0; i < queryString.length; i++) {
try {
LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
LogWriterUtils.getLogWriter().info("RR server local indexType:Range size of resultset: " + rs.size() + " for query: " + queryString[i]);
// The results should not be PdxString
checkForPdxString(rs.asList(), queryString[i]);
} catch (Exception e) {
Assert.fail("Failed executing " + queryString[i], e);
}
}
}
});
// test for readSerialized flag
server0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService localQueryService = getCache().getQueryService();
// Query server1 locally to check if PdxString is not being returned
for (int i = 0; i < queryString.length; i++) {
try {
LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
SelectResults rs = (SelectResults) localQueryService.newQuery(queryString[i]).execute();
LogWriterUtils.getLogWriter().info("RR server local readSerializedTrue: indexType: CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
// The results should not be PdxString
checkForPdxString(rs.asList(), queryString[i]);
} catch (Exception e) {
Assert.fail("Failed executing " + queryString[i], e);
}
}
}
});
// test for readSerialized flag on client
client.invoke(new CacheSerializableRunnable("Create client") {
public void run2() throws CacheException {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService remoteQueryService = (PoolManager.find(poolName)).getQueryService();
// Query server1 remotely to check if PdxString is not being returned
for (int i = 0; i < queryString.length; i++) {
try {
LogWriterUtils.getLogWriter().info("### Executing Query locally on server:" + queryString[i]);
SelectResults rs = (SelectResults) remoteQueryService.newQuery(queryString[i]).execute();
LogWriterUtils.getLogWriter().info("RR server remote readSerializedTrue: indexType:CompactRange size of resultset: " + rs.size() + " for query: " + queryString[i]);
// The results should not be PdxString
checkForPdxString(rs.asList(), queryString[i]);
} catch (Exception e) {
Assert.fail("Failed executing " + queryString[i], e);
}
}
}
});
closeClient(server2);
closeClient(client);
closeClient(server1);
closeClient(server0);
}
Aggregations