use of org.apache.geode.pdx.internal.PdxInstanceEnum in project geode by apache.
the class PdxLocalQueryDUnitTest method testLocalPdxQueriesReadSerialized.
@Test
public void testLocalPdxQueriesReadSerialized() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String[] queries = { "select * from " + name + " where position1 = $1", "select * from " + name + " where aDay = $1", "select * from " + name + " where status = 'inactive'", "select distinct * from " + name + " where status = 'inactive'", "select p from " + name + " p where p.status = 'inactive'", "select * from " + name + " p, p.positions.values v where v.secId = 'IBM'", "select * from " + name + " p where p.status = 'inactive' or p.ID > 0", "select * from " + name + " p where p.status = 'inactive' and p.ID >= 0", "select * from " + name + " p where p.status in set ('inactive', 'active')", "select * from " + name + " p where p.ID > 0 and p.ID < 10" };
// Start server1
server1.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
for (int i = 0; i < numberOfEntries; i++) {
PortfolioPdx p = new PortfolioPdx(i);
r1.put("key-" + i, p);
}
return null;
}
});
// Start server2
server2.invoke(new SerializableCallable("Create Server2") {
@Override
public Object call() throws Exception {
((GemFireCacheImpl) getCache()).setReadSerialized(true);
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
QueryService qs = null;
SelectResults sr = null;
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PdxInstanceFactory out = PdxInstanceFactoryImpl.newCreator("org.apache.geode.cache.query.data.PositionPdx", false);
out.writeLong("avg20DaysVol", 0);
out.writeString("bondRating", "");
out.writeDouble("convRatio", 0);
out.writeString("country", "");
out.writeDouble("delta", 0);
out.writeLong("industry", 0);
out.writeLong("issuer", 0);
out.writeDouble("mktValue", pos.getMktValue());
out.writeDouble("qty", 0);
out.writeString("secId", pos.secId);
out.writeString("secIdIndexed", pos.secIdIndexed);
out.writeString("secLinks", "");
out.writeDouble("sharesOutstanding", pos.getSharesOutstanding());
out.writeString("underlyer", "");
out.writeLong("volatility", 0);
out.writeInt("pid", pos.getPid());
out.writeInt("portfolioId", 0);
out.markIdentityField("secId");
PdxInstance pi = out.create();
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
PdxInstanceEnum pdxEnum = new PdxInstanceEnum(pDay);
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pi });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pdxEnum });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
for (Object result : sr) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (!(r[j] instanceof PdxInstance)) {
fail("Result object should be a PdxInstance and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (!(result instanceof PdxInstance)) {
fail("Result object should be a PdxInstance and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
return null;
}
});
this.closeClient(server1);
this.closeClient(server2);
}
use of org.apache.geode.pdx.internal.PdxInstanceEnum in project geode by apache.
the class InternalDataSerializer method readPdxInstance.
/**
* Reads a PdxInstance from dataBytes and returns it. If the first object read is not pdx encoded
* returns null.
*/
public static PdxInstance readPdxInstance(final byte[] dataBytes, InternalCache internalCache) {
try {
byte type = dataBytes[0];
if (type == PDX) {
PdxInputStream in = new PdxInputStream(dataBytes);
// throw away the type byte
in.readByte();
int len = in.readInt();
int typeId = in.readInt();
PdxType pdxType = internalCache.getPdxRegistry().getType(typeId);
if (pdxType == null) {
throw new IllegalStateException("Unknown pdx type=" + typeId);
}
return new PdxInstanceImpl(pdxType, in, len);
} else if (type == DSCODE.PDX_ENUM) {
PdxInputStream in = new PdxInputStream(dataBytes);
// throw away the type byte
in.readByte();
int dsId = in.readByte();
int tmp = readArrayLength(in);
int enumId = dsId << 24 | tmp & 0xFFFFFF;
TypeRegistry tr = internalCache.getPdxRegistry();
EnumInfo ei = tr.getEnumInfoById(enumId);
if (ei == null) {
throw new IllegalStateException("Unknown pdx enum id=" + enumId);
}
return ei.getPdxInstance(enumId);
} else if (type == DSCODE.PDX_INLINE_ENUM) {
PdxInputStream in = new PdxInputStream(dataBytes);
// throw away the type byte
in.readByte();
String className = DataSerializer.readString(in);
String enumName = DataSerializer.readString(in);
int enumOrdinal = InternalDataSerializer.readArrayLength(in);
return new PdxInstanceEnum(className, enumName, enumOrdinal);
}
} catch (IOException ignore) {
}
return null;
}
use of org.apache.geode.pdx.internal.PdxInstanceEnum in project geode by apache.
the class PdxLocalQueryDUnitTest method testLocalPdxQueries.
@Test
public void testLocalPdxQueries() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(1);
final VM client = host.getVM(2);
final int numberOfEntries = 10;
final String name = "/" + regionName;
final String name2 = "/" + regionName2;
final String[] queries = { "select * from " + name + " where position1 = $1", "select * from " + name + " where aDay = $1", // numberOfEntries
"select distinct * from " + name + " p where p.status = 'inactive'", // 1
"select distinct p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
"select p from " + name + " p where p.status = 'inactive'", // 4
"select * from " + name + " p, p.positions.values v where v.secId = 'IBM'", // 4
"select v from " + name + " p, p.positions.values v where v.secId = 'IBM'", // numberOfEntries
"select p.status from " + name + " p where p.status = 'inactive'", // numberOfEntries
"select distinct * from " + name + " p where p.status = 'inactive' order by p.ID", // 19
"select * from " + name + " p where p.status = 'inactive' or p.ID > 0", // numberOfEntries
"select * from " + name + " p where p.status = 'inactive' and p.ID >= 0", // numberOfEntries*2
"select * from " + name + " p where p.status in set ('inactive', 'active')", // 9
"select * from " + name + " p where p.ID > 0 and p.ID < 10", // numberOfEntries*2
"select v from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries*2
"select v.secId from " + name + " p, p.positions.values v where p.status = 'inactive'", "select distinct p from " + name + // numberOfEntries
" p, p.positions.values v where p.status = 'inactive' and v.pid >= 0", "select distinct p from " + name + // numberOfEntries*2
" p, p.positions.values v where p.status = 'inactive' or v.pid > 0", // numberOfEntries*2
"select distinct * from " + name + " p, p.positions.values v where p.status = 'inactive'", // numberOfEntries
"select * from " + name + ".values v where v.status = 'inactive'", // 19
"select v from " + name + " v where v in (select p from " + name + " p where p.ID > 0)", "select v from " + name + " v where v.status in (select distinct p.status from " + name + // numberOfEntries
" p where p.status = 'inactive')", // 200
"select * from " + name + " r1, " + name2 + " r2 where r1.status = r2.status", "select * from " + name + " r1, " + name2 + // 100
" r2 where r1.status = r2.status and r1.status = 'active'", "select r2.status from " + name + " r1, " + name2 + // 100
" r2 where r1.status = r2.status and r1.status = 'active'", "select distinct r2.status from " + name + " r1, " + name2 + // 1
" r2 where r1.status = r2.status and r1.status = 'active'", "select * from " + name + " v where v.status = ELEMENT (select distinct p.status from " + name + // numberOfEntries
" p where p.status = 'inactive')" };
final int[] results = { 2, 3, numberOfEntries, 1, numberOfEntries, 4, 4, numberOfEntries, numberOfEntries, 19, numberOfEntries, numberOfEntries * 2, 9, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, numberOfEntries * 2, numberOfEntries * 2, numberOfEntries, 19, numberOfEntries, 200, 100, 100, 1, numberOfEntries };
// Start server1
final int port1 = (Integer) server1.invoke(new SerializableCallable("Create Server1") {
@Override
public Object call() throws Exception {
Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
Region r2 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName2);
for (int i = 0; i < numberOfEntries; i++) {
PortfolioPdx p = new PortfolioPdx(i);
r1.put("key-" + i, p);
r2.put("key-" + i, p);
}
CacheServer server = getCache().addCacheServer();
int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
server.setPort(port);
server.start();
return port;
}
});
// client loads pdx objects on server
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port1);
ClientCache cache = getClientCache(cf);
Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
Region region2 = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName2);
for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
PortfolioPdx p = new PortfolioPdx(i);
region.put("key-" + i, p);
region2.put("key-" + i, p);
}
return null;
}
});
// query locally on server1 to verify pdx objects are not deserialized
server1.invoke(new SerializableCallable("query locally on server1") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
QueryService qs = null;
SelectResults sr = null;
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
int extra = 0;
if (cache.getLogger().fineEnabled()) {
extra = 20;
}
assertEquals(numberOfEntries * 6 + 1 + extra, PortfolioPdx.numInstance);
// set readserealized and query
((GemFireCacheImpl) getCache()).setReadSerialized(true);
PdxInstanceFactory out = PdxInstanceFactoryImpl.newCreator("org.apache.geode.cache.query.data.PositionPdx", false);
out.writeLong("avg20DaysVol", 0);
out.writeString("bondRating", "");
out.writeDouble("convRatio", 0);
out.writeString("country", "");
out.writeDouble("delta", 0);
out.writeLong("industry", 0);
out.writeLong("issuer", 0);
out.writeDouble("mktValue", pos.getMktValue());
out.writeDouble("qty", 0);
out.writeString("secId", pos.secId);
out.writeString("secIdIndexed", pos.secIdIndexed);
out.writeString("secLinks", "");
out.writeDouble("sharesOutstanding", pos.getSharesOutstanding());
out.writeString("underlyer", "");
out.writeLong("volatility", 0);
out.writeInt("pid", pos.getPid());
out.writeInt("portfolioId", 0);
// Identity Field.
out.markIdentityField("secId");
PdxInstance pi = out.create();
PdxInstanceEnum pdxEnum = new PdxInstanceEnum(pDay);
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pi });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pdxEnum });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
// in case of PortfolioPdx
if (queries[i].indexOf("distinct") == -1) {
if (i == 0 || i == 1) {
assertEquals("Expected and actual results do not match for query: " + queries[i], 1, sr.size());
} else {
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
// reset readserealized and query
((GemFireCacheImpl) getCache()).setReadSerialized(false);
return null;
}
});
// query from client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(NetworkUtils.getServerHostName(server1.getHost()), port1);
ClientCache cache = getClientCache(cf);
QueryService qs = null;
SelectResults sr = null;
// Execute query remotely
try {
qs = cache.getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr.size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr.size());
for (Object result : sr) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
return null;
}
});
// query locally on server1
server1.invoke(new SerializableCallable("query locally on server1") {
@Override
public Object call() throws Exception {
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
QueryService qs = null;
SelectResults[][] sr = new SelectResults[queries.length][2];
// Execute query locally
try {
qs = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
int cnt = PositionPdx.cnt;
PositionPdx pos = new PositionPdx("IBM", 100);
PortfolioPdx.Day pDay = new PortfolioPdx(1).aDay;
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr[i][0] = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][0].size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][0].size());
for (Object result : sr[i][0]) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
// create index
qs.createIndex("statusIndex", "status", name);
qs.createIndex("IDIndex", "ID", name);
qs.createIndex("pIdIndex", "pos.getPid()", name + " p, p.positions.values pos");
qs.createIndex("secIdIndex", "pos.secId", name + " p, p.positions.values pos");
for (int i = 0; i < queries.length; i++) {
try {
if (i == 0) {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pos });
} else if (i == 1) {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute(new Object[] { pDay });
} else {
sr[i][1] = (SelectResults) qs.newQuery(queries[i]).execute();
}
assertTrue("Size of resultset should be greater than 0 for query: " + queries[i], sr[i][1].size() > 0);
assertEquals("Expected and actual results do not match for query: " + queries[i], results[i], sr[i][1].size());
for (Object result : sr[i][1]) {
if (result instanceof Struct) {
Object[] r = ((Struct) result).getFieldValues();
for (int j = 0; j < r.length; j++) {
if (r[j] instanceof PdxInstance || r[j] instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + r[j].getClass() + " for query: " + queries[i]);
}
}
} else if (result instanceof PdxInstance || result instanceof PdxString) {
fail("Result object should be a domain object and not an instance of " + result.getClass() + " for query: " + queries[i]);
}
}
} catch (Exception e) {
Assert.fail("Failed executing query " + queries[i], e);
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(sr, queries.length, queries);
return null;
}
});
this.closeClient(client);
this.closeClient(server1);
}
use of org.apache.geode.pdx.internal.PdxInstanceEnum in project geode by apache.
the class InternalDataSerializer method basicWriteObject.
public static void basicWriteObject(Object o, DataOutput out, boolean ensurePdxCompatibility) throws IOException {
checkOut(out);
final boolean isDebugEnabled_SERIALIZER = logger.isTraceEnabled(LogMarker.SERIALIZER);
if (isDebugEnabled_SERIALIZER) {
logger.trace(LogMarker.SERIALIZER, "basicWriteObject: {}", o);
}
// Handle special objects first
if (o == null) {
out.writeByte(NULL);
} else if (o instanceof DataSerializableFixedID) {
checkPdxCompatible(o, ensurePdxCompatibility);
DataSerializableFixedID dsfid = (DataSerializableFixedID) o;
writeDSFID(dsfid, out);
} else if (autoSerialized(o, out)) {
// all done
} else if (o instanceof DataSerializable.Replaceable) {
// do this first to fix bug 31609
// do this before DataSerializable
Object replacement = ((DataSerializable.Replaceable) o).replace();
basicWriteObject(replacement, out, ensurePdxCompatibility);
} else if (o instanceof PdxSerializable) {
writePdx(out, GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed."), o, null);
} else if (o instanceof DataSerializable) {
if (isDebugEnabled_SERIALIZER) {
logger.trace(LogMarker.SERIALIZER, "Writing DataSerializable: {}", o);
}
checkPdxCompatible(o, ensurePdxCompatibility);
Class c = o.getClass();
// Is "c" a user class registered with an Instantiator?
int classId = InternalInstantiator.getClassId(c);
if (classId != 0) {
writeUserDataSerializableHeader(classId, out);
} else {
out.writeByte(DATA_SERIALIZABLE);
DataSerializer.writeClass(c, out);
}
DataSerializable ds = (DataSerializable) o;
invokeToData(ds, out);
} else if (o instanceof Sendable) {
if (!(o instanceof PdxInstance) || o instanceof PdxInstanceEnum) {
checkPdxCompatible(o, ensurePdxCompatibility);
}
((Sendable) o).sendTo(out);
} else if (writeWellKnownObject(o, out, ensurePdxCompatibility)) {
// Nothing more to do...
} else {
checkPdxCompatible(o, ensurePdxCompatibility);
if (logger.isTraceEnabled(LogMarker.DUMP_SERIALIZED)) {
logger.trace(LogMarker.DUMP_SERIALIZED, "DataSerializer Serializing an instance of {}", o.getClass().getName());
}
/*
* If the (internally known) ThreadLocal named "DataSerializer.DISALLOW_JAVA_SERIALIZATION" is
* set, then an exception will be thrown if we try to do standard Java Serialization. This is
* used to catch Java serialization early for the case where the data is being sent to a
* non-Java client
*/
if (disallowJavaSerialization() && o instanceof Serializable) {
throw new NotSerializableException(LocalizedStrings.DataSerializer_0_IS_NOT_DATASERIALIZABLE_AND_JAVA_SERIALIZATION_IS_DISALLOWED.toLocalizedString(o.getClass().getName()));
}
writeSerializableObject(o, out);
}
}
use of org.apache.geode.pdx.internal.PdxInstanceEnum in project geode by apache.
the class InternalDataSerializer method readPdxInlineEnum.
private static Object readPdxInlineEnum(DataInput in) throws IOException, ClassNotFoundException {
InternalCache internalCache = GemFireCacheImpl.getInstance();
if (internalCache != null && internalCache.getPdxReadSerializedByAnyGemFireServices()) {
String className = DataSerializer.readString(in);
String enumName = DataSerializer.readString(in);
int enumOrdinal = InternalDataSerializer.readArrayLength(in);
getDMStats(internalCache).incPdxInstanceCreations();
return new PdxInstanceEnum(className, enumName, enumOrdinal);
} else {
Enum<?> e = readGemFireEnum(in);
InternalDataSerializer.readArrayLength(in);
return e;
}
}
Aggregations