use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.
the class PdxStringQueryJUnitTest method testQueriesWithRangeIndexWithREUpdateInProgress.
@Test
public void testQueriesWithRangeIndexWithREUpdateInProgress() throws Exception {
Index index = qs.createIndex("index2", "p.secId", "/exampleRegion p, p.positions.values");
assertTrue(index instanceof RangeIndex);
PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 111);
pf.writeString("secId", "IBM");
pf.writeString("status", "active");
HashMap positions = new HashMap();
positions.put("price", "50");
positions.put("price", "60");
pf.writeObject("positions", positions);
PdxInstance pi = pf.create();
r.put("IBM", pi);
positions = new HashMap();
positions.put("price", "100");
positions.put("price", "120");
r.put("YHOO", new TestObject(222, "YHOO", positions, "inactive"));
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 333);
pf.writeString("secId", "GOOGL");
pf.writeString("status", "active");
positions = new HashMap();
positions.put("price", "130");
positions.put("price", "150");
pf.writeObject("positions", positions);
pi = pf.create();
positions = new HashMap();
positions.put("price", "200");
positions.put("price", "220");
r.put("VMW", new TestObject(111, "VMW", positions, "inactive"));
r.put("GOOGL", pi);
makeREUpdateInProgress();
Map map = ((RangeIndex) index).getValueToEntriesMap();
for (Object key : map.keySet()) {
assertTrue(key instanceof PdxString);
}
DefaultQuery.setPdxReadSerialized(true);
executeQueriesValidateResults(INDEX_TYPE_RANGE);
qs.removeIndex(index);
r.clear();
}
use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.
the class PdxStringQueryJUnitTest method putPdxInstancesWithREUpdateInProgress.
public void putPdxInstancesWithREUpdateInProgress() throws Exception {
PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 111);
pf.writeString("status", "active");
pf.writeString("secId", "IBM");
PdxInstance pi = pf.create();
r.put("IBM", pi);
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 222);
pf.writeString("status", "inactive");
pf.writeString("secId", "YHOO");
pi = pf.create();
r.put("YHOO", pi);
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 333);
pf.writeString("status", "active");
pf.writeString("secId", "GOOGL");
pi = pf.create();
r.put("GOOGL", pi);
pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
pf.writeInt("ID", 111);
pf.writeString("status", "inactive");
pf.writeString("secId", "VMW");
pi = pf.create();
r.put("VMW", pi);
makeREUpdateInProgress();
}
use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.
the class PdxQueryDUnitTest method testPdxInstanceNoFieldNoMethod.
/**
* Test to query a field that is not present in the Pdx object Also the implicit method is absent
* in the class
*
* @throws CacheException
*/
@Test
public void testPdxInstanceNoFieldNoMethod() throws CacheException {
final Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm3 = host.getVM(3);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String[] qs = { "select * from " + name + " where pdxStatus = 'active'", "select pdxStatus from " + name + " where id > 4" };
// Start server1
final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// create client and load only version 1 objects with no pdxStatus field
vm3.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
// Load version 1 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
pdxInstanceFactory.writeInt("id", i);
pdxInstanceFactory.writeString("status", (i % 2 == 0 ? "active" : "inactive"));
PdxInstance pdxInstance = pdxInstanceFactory.create();
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// Version1 class loader
vm3.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
// Load version 1 classloader
QueryService remoteQueryService = null;
// Execute query remotely
try {
remoteQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
for (int i = 0; i < qs.length; i++) {
try {
SelectResults sr = (SelectResults) remoteQueryService.newQuery(qs[i]).execute();
if (i == 1) {
assertEquals(5, sr.size());
for (Object o : sr) {
if (!(o instanceof Undefined)) {
fail("Result should be Undefined and not " + o.getClass());
}
}
} else {
assertEquals(0, sr.size());
}
} catch (Exception e) {
Assert.fail("Failed executing " + qs[i], e);
}
}
return null;
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.
the class PdxQueryDUnitTest method testDefaultValuesInPdxFieldTypes.
/**
* Test query execution when default values of {@link FieldType} are used. This happens when one
* version of Pdx object does not have a field but other version has.
*
* @throws Exception
*/
@Test
public void testDefaultValuesInPdxFieldTypes() throws Exception {
final Host host = Host.getHost(0);
final VM vm0 = host.getVM(0);
final VM vm1 = host.getVM(1);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String query = "select stringField, booleanField, charField, shortField, intField, longField, floatField, doubleField from " + name;
// Start server1
final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// client loads version1 and version2 objects on server
vm1.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
// Load version 1 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeString("stringField", "" + i);
pdxInstanceFactory.writeBoolean("booleanField", (i % 2 == 0 ? true : false));
pdxInstanceFactory.writeChar("charField", ((char) i));
pdxInstanceFactory.writeShort("shortField", new Integer(i).shortValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
// Load version 2 objects
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeInt("intField", i);
pdxInstanceFactory.writeLong("longField", new Integer(i + 1).longValue());
pdxInstanceFactory.writeFloat("floatField", new Integer(i + 2).floatValue());
pdxInstanceFactory.writeDouble("doubleField", new Integer(i + 3).doubleValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// query locally on server, create index, verify results with and without index
vm0.invoke(new SerializableCallable("Create index") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
cache.setReadSerialized(true);
QueryService qs = null;
SelectResults[][] sr = new SelectResults[1][2];
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
sr[0][0] = (SelectResults) qs.newQuery(query).execute();
assertEquals(20, sr[0][0].size());
} catch (Exception e) {
Assert.fail("Failed executing " + qs, e);
}
// create index
try {
qs.createIndex("stringIndex", "stringField", name);
qs.createIndex("booleanIndex", "booleanField", name);
qs.createIndex("shortIndex", "shortField", name);
qs.createIndex("charIndex", "charField", name);
qs.createIndex("intIndex", "intField", name);
qs.createIndex("longIndex", "longField", name);
qs.createIndex("floatIndex", "floatField", name);
qs.createIndex("doubleIndex", "doubleField", name);
} catch (Exception e) {
Assert.fail("Exception creating index ", e);
}
// query after index creation
try {
sr[0][1] = (SelectResults) qs.newQuery(query).execute();
assertEquals(20, sr[0][1].size());
} catch (Exception e) {
Assert.fail("Failed executing " + qs, e);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
return null;
}
});
// Update index
vm1.invoke(new SerializableCallable("update index") {
@Override
public Object call() throws Exception {
Region region = getCache().getRegion(regionName);
// Load version 1 objects
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeString("stringField", "" + i);
pdxInstanceFactory.writeBoolean("booleanField", (i % 2 == 0 ? true : false));
pdxInstanceFactory.writeChar("charField", ((char) i));
pdxInstanceFactory.writeShort("shortField", new Integer(i).shortValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
// Load version 2 objects
for (int i = 0; i < numberOfEntries; i++) {
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedFieldType", false);
pdxInstanceFactory.writeInt("intField", i);
pdxInstanceFactory.writeLong("longField", new Integer(i + 1).longValue());
pdxInstanceFactory.writeFloat("floatField", new Integer(i + 2).floatValue());
pdxInstanceFactory.writeDouble("doubleField", new Integer(i + 3).doubleValue());
PdxInstance pdxInstance = pdxInstanceFactory.create();
logger.info("Putting object: " + pdxInstance);
region.put("key-" + i, pdxInstance);
}
return null;
}
});
// query remotely from client
vm1.invoke(new SerializableCallable("query") {
@Override
public Object call() throws Exception {
QueryService remoteQueryService = null;
QueryService localQueryService = null;
SelectResults[][] sr = new SelectResults[1][2];
// Execute query locally
try {
remoteQueryService = getCache().getQueryService();
localQueryService = ((ClientCache) getCache()).getLocalQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
sr[0][0] = (SelectResults) remoteQueryService.newQuery(query).execute();
assertEquals(20, sr[0][0].size());
sr[0][1] = (SelectResults) localQueryService.newQuery(query).execute();
assertEquals(20, sr[0][1].size());
} catch (Exception e) {
fail("Failed executing query " + e);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
return null;
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.
the class PdxQueryDUnitTest method testPdxReadSerializedForPRQuery.
/**
* Tests query on with PR.
*/
@Test
public void testPdxReadSerializedForPRQuery() throws CacheException {
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final int numberOfEntries = 100;
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer(true, true);
}
});
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer(true, false);
}
});
vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer(true, false);
}
});
vm3.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer(true, false);
}
});
// Load region using class loader and execute query on the same thread.
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
try {
// Load TestObject
for (int i = 0; i < numberOfEntries; i++) {
PortfolioPdxVersion portfolioPdxVersion = new PortfolioPdxVersion(new Integer(i), new Integer(i));
PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PortfolioPdxVersion", false);
PdxInstance pdxInstance = portfolioPdxVersion.createPdxInstance(pdxInstanceFactory);
region.put("key-" + i, pdxInstance);
}
} catch (Exception ex) {
fail("Failed to load the class.");
}
QueryService localQueryService = null;
try {
localQueryService = region.getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
for (int i = 1; i < 3; i++) {
try {
logger.info("### Executing Query on server:" + queryString[i]);
Query query = localQueryService.newQuery(queryString[i]);
SelectResults rs = (SelectResults) query.execute();
assertEquals(numberOfEntries, rs.size());
} catch (Exception e) {
Assert.fail("Failed executing " + queryString[i], e);
}
}
}
});
final String[] qs = new String[] { "SELECT * FROM " + regName, "SELECT * FROM " + regName + " WHERE ID > 5", "SELECT p FROM " + regName + " p, p.positions.values pos WHERE p.ID > 2 or pos.secId = 'vmware'" };
// Execute query on node without class and with pdxReadSerialized.
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
GemFireCacheImpl c = (GemFireCacheImpl) region.getCache();
try {
// Set read serialized.
c.setReadSerialized(true);
QueryService localQueryService = null;
try {
localQueryService = region.getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
// This should not throw class not found exception.
for (int i = 1; i < qs.length; i++) {
try {
logger.info("### Executing Query on server:" + qs[i]);
Query query = localQueryService.newQuery(qs[i]);
SelectResults rs = (SelectResults) query.execute();
for (Object o : rs.asSet()) {
if (!(o instanceof PdxInstance)) {
fail("Expected type PdxInstance, not found in result set. Found type :" + o.getClass());
}
}
} catch (Exception e) {
Assert.fail("Failed executing " + qs[i], e);
}
}
} finally {
c.setReadSerialized(false);
}
}
});
this.closeClient(vm2);
this.closeClient(vm3);
this.closeClient(vm1);
this.closeClient(vm0);
}
Aggregations