use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.
the class InternalDataSerializer method readPdxSerializable.
private static Object readPdxSerializable(final DataInput in) throws IOException, ClassNotFoundException {
int len = in.readInt();
int typeId = in.readInt();
InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
PdxType pdxType = internalCache.getPdxRegistry().getType(typeId);
if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
logger.trace(LogMarker.SERIALIZER, "readPdxSerializable pdxType={}", pdxType);
}
if (pdxType == null) {
throw new IllegalStateException("Unknown pdx type=" + typeId);
}
DMStats dmStats = getDMStats(internalCache);
dmStats.incPdxDeserialization(len + 9);
// check if PdxInstance needs to be returned.
if (pdxType.getNoDomainClass() || internalCache.getPdxReadSerializedByAnyGemFireServices()) {
dmStats.incPdxInstanceCreations();
return new PdxInstanceImpl(pdxType, in, len);
} else {
PdxReaderImpl pdxReader = new PdxReaderImpl(pdxType, in, len);
return pdxReader.getObject();
}
}
use of org.apache.geode.pdx.internal.PdxInstanceImpl 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.PdxInstanceImpl in project geode by apache.
the class AbstractRegionEntry method prepareValueForCache.
@Override
@Retained(ABSTRACT_REGION_ENTRY_PREPARE_VALUE_FOR_CACHE)
public Object prepareValueForCache(RegionEntryContext r, @Retained(ABSTRACT_REGION_ENTRY_PREPARE_VALUE_FOR_CACHE) Object val, EntryEventImpl event, boolean isEntryUpdate) {
if (r != null && r.getOffHeap() && okToStoreOffHeap(val, this)) {
if (val instanceof StoredObject) {
// Check to see if val has the same compression settings as this region.
// The recursive calls in this section are safe because
// we only do it after copy the off-heap value to the heap.
// This is needed to fix bug 52057.
StoredObject soVal = (StoredObject) val;
assert !soVal.isCompressed();
if (r.getCompressor() != null) {
// val is uncompressed and we need a compressed value.
// So copy the off-heap value to the heap in a form that can be compressed.
byte[] valAsBytes = soVal.getValueAsHeapByteArray();
Object heapValue;
if (soVal.isSerialized()) {
heapValue = CachedDeserializableFactory.create(valAsBytes);
} else {
heapValue = valAsBytes;
}
return prepareValueForCache(r, heapValue, event, isEntryUpdate);
}
if (soVal.hasRefCount()) {
// if the reused guy has a refcount then need to inc it
if (!soVal.retain()) {
throw new IllegalStateException("Could not use an off heap value because it was freed");
}
}
// else it is has no refCount so just return it as prepared.
} else {
byte[] data;
boolean isSerialized = !(val instanceof byte[]);
if (isSerialized) {
if (event != null && event.getCachedSerializedNewValue() != null) {
data = event.getCachedSerializedNewValue();
} else if (val instanceof CachedDeserializable) {
data = ((CachedDeserializable) val).getSerializedValue();
} else if (val instanceof PdxInstance) {
try {
data = ((ConvertableToBytes) val).toBytes();
} catch (IOException e) {
throw new PdxSerializationException("Could not convert " + val + " to bytes", e);
}
} else {
data = EntryEventImpl.serialize(val);
}
} else {
data = (byte[]) val;
}
byte[] compressedData = compressBytes(r, data);
// TODO: array comparison is broken
boolean isCompressed = compressedData != data;
ReferenceCountHelper.setReferenceCountOwner(this);
// fix for bug 47875
MemoryAllocator ma = MemoryAllocatorImpl.getAllocator();
val = ma.allocateAndInitialize(compressedData, isSerialized, isCompressed, data);
ReferenceCountHelper.setReferenceCountOwner(null);
}
return val;
}
@Unretained Object nv = val;
if (nv instanceof StoredObject) {
// This off heap value is being put into a on heap region.
byte[] data = ((StoredObject) nv).getSerializedValue();
nv = CachedDeserializableFactory.create(data);
}
if (nv instanceof PdxInstanceImpl) {
// So get the serialized bytes and use a CachedDeserializable.
try {
byte[] data = ((ConvertableToBytes) nv).toBytes();
byte[] compressedData = compressBytes(r, data);
// TODO: array comparison is broken
if (data == compressedData) {
nv = CachedDeserializableFactory.create(data);
} else {
nv = compressedData;
}
} catch (IOException e) {
throw new PdxSerializationException("Could not convert " + nv + " to bytes", e);
}
} else {
nv = compress(r, nv, event);
}
return nv;
}
use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.
the class PdxStringQueryJUnitTest method executeQueriesValidateResults.
private void executeQueriesValidateResults(int indexType) throws Exception {
DefaultQuery.setPdxReadSerialized(true);
String[] query = { "select count(*) from /exampleRegion", "select count(*) from /exampleRegion p, p.positions.values v", "select count(*) from /exampleRegion" };
SelectResults res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(4, res.iterator().next());
query = new String[] { "select secId from /exampleRegion where secId = 'IBM'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId = 'IBM'", "select secId from /exampleRegion where secId = 'IBM'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
validateStringResult("IBM", res.iterator().next());
query = new String[] { "select p.secId from /exampleRegion p where p.secId = ELEMENT(select e.secId from /exampleRegion e where e.secId = 'IBM') ", "select p.secId from /exampleRegion p, p.positions.values v where p.secId = ELEMENT(select p1.secId from /exampleRegion p1, p.positions.values v1 where p1.secId = 'IBM')", "select p.secId from /exampleRegion p where p.secId = ELEMENT(select e.secId from /exampleRegion e where e.secId = 'IBM' )" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
validateStringResult("IBM", res.iterator().next());
query = new String[] { "select secId from /exampleRegion where secId LIKE 'VMW'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId LIKE 'VMW'", "select secId from /exampleRegion where secId LIKE 'VMW'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
validateStringResult("VMW", res.iterator().next());
query = new String[] { "select secId from /exampleRegion where secId LIKE 'VM%'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId LIKE 'VM%'", "select secId from /exampleRegion where secId LIKE 'VM%'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
validateStringResult("VMW", res.iterator().next());
query = new String[] { "select secId from /exampleRegion where secId IN SET ('YHOO', 'VMW')", "select p.secId from /exampleRegion p, p.positions.values v where p.secId IN SET ('YHOO', 'VMW')", "select secId from /exampleRegion where secId IN SET ('YHOO', 'VMW')" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(2, res.size());
List secIdsList = new ArrayList();
secIdsList.add("VMW");
secIdsList.add("YHOO");
Iterator iter = res.iterator();
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select p.secId from /exampleRegion p where p.secId IN (select e.secId from /exampleRegion e where e.secId ='YHOO' or e.secId = 'VMW')", "select p.secId from /exampleRegion p, p.positions.values v where p.secId IN (select e.secId from /exampleRegion e where e.secId ='YHOO' or e.secId = 'VMW')", "select p.secId from /exampleRegion p where p.secId IN (select e.secId from /exampleRegion e where e.secId ='YHOO' or e.secId = 'VMW')" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(2, res.size());
secIdsList = new ArrayList();
secIdsList.add("VMW");
secIdsList.add("YHOO");
iter = res.iterator();
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId, status from /exampleRegion where secId = 'IBM'", "select p.secId, p.status from /exampleRegion p, p.positions.values v where p.secId = 'IBM'", "select secId, status from /exampleRegion where secId = 'IBM'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
secIdsList = new ArrayList();
secIdsList.add("active");
secIdsList.add("IBM");
Struct rs = (Struct) res.iterator().next();
Object o1 = rs.getFieldValues()[0];
Object o2 = rs.getFieldValues()[1];
validateResult(secIdsList, o1);
validateResult(secIdsList, o2);
query = new String[] { "select secId from /exampleRegion where secId < 'YHOO'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId < 'YHOO'", "select secId from /exampleRegion where secId < 'YHOO'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(3, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
secIdsList.add("GOOGL");
secIdsList.add("IBM");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where 'YHOO' > secId", "select p.secId from /exampleRegion p, p.positions.values v where 'YHOO' > p.secId", "select secId from /exampleRegion where 'YHOO' > secId" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(3, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
secIdsList.add("GOOGL");
secIdsList.add("IBM");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where secId > 'IBM'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId > 'IBM'", "select secId from /exampleRegion where secId > 'IBM'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(2, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
secIdsList.add("YHOO");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where secId > 'IBM' or ID=333", "select p.secId from /exampleRegion p, p.positions.values v where p.secId > 'IBM' or p.ID=333", "select secId from /exampleRegion where secId = 'VMW' or secId = 'YHOO' or secId = 'GOOGL'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(3, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
secIdsList.add("YHOO");
secIdsList.add("GOOGL");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where secId > 'IBM' and secId < 'YHOO'", "select p.secId from /exampleRegion p, p.positions.values v where p.secId > 'IBM' and p.secId < 'YHOO'", "select secId from /exampleRegion where secId > 'IBM' and secId < 'YHOO'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where ID = 111", "select p.secId from /exampleRegion p, p.positions.values v where p.ID = 111", "select secId from /exampleRegion where secId = 'VMW' or secId = 'IBM'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(2, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
secIdsList.add("IBM");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select distinct ID from /exampleRegion where ID = 111", "select distinct p.ID from /exampleRegion p, p.positions.values v where p.ID = 111", "select distinct secId from /exampleRegion where secId = 'VMW'" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
query = new String[] { "select ID from /exampleRegion where ID = 111 limit 1", "select p.ID from /exampleRegion p, p.positions.values v where p.ID = 111 limit 1", "select secId from /exampleRegion where secId = 'VMW' limit 1" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
query = new String[] { "select distinct secId from /exampleRegion order by secId", "select distinct p.secId from /exampleRegion p, p.positions.values order by p.secId", "select distinct secId from /exampleRegion order by secId" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(4, res.size());
iter = res.iterator();
String[] secIds = { "GOOGL", "IBM", "VMW", "YHOO" };
int i = 0;
while (iter.hasNext()) {
validateStringResult(secIds[i++], iter.next());
}
query = new String[] { "select distinct * from /exampleRegion order by secId", "select distinct * from /exampleRegion p, p.positions.values v order by p.secId", "select distinct * from /exampleRegion order by secId" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(4, res.size());
iter = res.iterator();
secIds = new String[] { "GOOGL", "IBM", "VMW", "YHOO" };
i = 0;
while (iter.hasNext()) {
Object o = iter.next();
if (o instanceof PdxInstanceImpl) {
validateStringResult(secIds[i++], ((PdxInstanceImpl) o).getField("secId"));
} else if (o instanceof TestObject) {
validateStringResult(secIds[i++], ((TestObject) o).getSecId());
}
}
query = new String[] { "select distinct secId from /exampleRegion order by secId limit 2", "select distinct p.secId from /exampleRegion p, p.positions.values v order by p.secId limit 2", "select distinct secId from /exampleRegion order by secId limit 2" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(2, res.size());
iter = res.iterator();
secIds = new String[] { "GOOGL", "IBM" };
i = 0;
while (iter.hasNext()) {
validateStringResult(secIds[i++], iter.next());
}
query = new String[] { "select secId from /exampleRegion where NOT (secId = 'VMW')", "select p.secId from /exampleRegion p, p.positions.values v where NOT (p.secId = 'VMW')", "select secId from /exampleRegion where NOT (secId = 'VMW')" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(3, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("YHOO");
secIdsList.add("IBM");
secIdsList.add("GOOGL");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion p where NOT (p.ID IN SET(111, 222)) ", "select p.secId from /exampleRegion p, p.positions.values v where NOT (p.ID IN SET(111, 222)) ", "select secId from /exampleRegion where NOT (secId IN SET('VMW','IBM','YHOO'))" };
res = (SelectResults) qs.newQuery(query[indexType]).execute();
assertEquals(1, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("GOOGL");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
query = new String[] { "select secId from /exampleRegion where secId = $1", "select p.secId from /exampleRegion p, p.positions.values v where p.secId = $1", "select secId from /exampleRegion where secId = $1" };
res = (SelectResults) qs.newQuery(query[indexType]).execute(new Object[] { "IBM" });
assertEquals(1, res.size());
validateStringResult("IBM", res.iterator().next());
query = new String[] { "select secId from /exampleRegion where secId > $1 and secId < $2", "select p.secId from /exampleRegion p, p.positions.values v where p.secId > $1 and p.secId < $2", "select secId from /exampleRegion where secId > $1 and secId < $2" };
res = (SelectResults) qs.newQuery(query[indexType]).execute(new Object[] { "IBM", "YHOO" });
assertEquals(1, res.size());
iter = res.iterator();
secIdsList.clear();
secIdsList.add("VMW");
while (iter.hasNext()) {
validateResult(secIdsList, iter.next());
}
DefaultQuery.setPdxReadSerialized(false);
}
use of org.apache.geode.pdx.internal.PdxInstanceImpl in project geode by apache.
the class PdxDeleteFieldJUnitTest method testPdxFieldDelete.
@Test
public void testPdxFieldDelete() throws Exception {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
props.setProperty(LOCATORS, "");
try {
Cache cache = (new CacheFactory(props)).create();
try {
PdxValue pdxValue = new PdxValue(1, 2L);
byte[] pdxValueBytes = BlobHelper.serializeToBlob(pdxValue);
{
PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
assertEquals(1, deserializedPdxValue.value);
assertEquals(2L, deserializedPdxValue.fieldToDelete);
}
PdxType pt;
// force PdxInstance on deserialization
DefaultQuery.setPdxReadSerialized(true);
try {
PdxInstanceImpl pi = (PdxInstanceImpl) BlobHelper.deserializeBlob(pdxValueBytes);
pt = pi.getPdxType();
assertEquals(1, pi.getField("value"));
assertEquals(2L, pi.getField("fieldToDelete"));
} finally {
DefaultQuery.setPdxReadSerialized(false);
}
assertEquals(PdxValue.class.getName(), pt.getClassName());
PdxField field = pt.getPdxField("fieldToDelete");
pt.setHasDeletedField(true);
field.setDeleted(true);
assertEquals(null, pt.getPdxField("fieldToDelete"));
assertEquals(2, pt.getFieldCount());
{
PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
assertEquals(1, deserializedPdxValue.value);
// fieldToDelete should now be 0 (the default) instead of 2.
assertEquals(0L, deserializedPdxValue.fieldToDelete);
}
// force PdxInstance on deserialization
DefaultQuery.setPdxReadSerialized(true);
try {
PdxInstance pi = (PdxInstance) BlobHelper.deserializeBlob(pdxValueBytes);
assertEquals(1, pi.getField("value"));
assertEquals(false, pi.hasField("fieldToDelete"));
assertEquals(null, pi.getField("fieldToDelete"));
assertSame(pt, ((PdxInstanceImpl) pi).getPdxType());
PdxValue deserializedPdxValue = (PdxValue) pi.getObject();
assertEquals(1, deserializedPdxValue.value);
assertEquals(0L, deserializedPdxValue.fieldToDelete);
} finally {
DefaultQuery.setPdxReadSerialized(false);
}
TypeRegistry tr = ((GemFireCacheImpl) cache).getPdxRegistry();
// Clear the local registry so we will regenerate a type for the same class
tr.testClearLocalTypeRegistry();
{
PdxInstanceFactory piFactory = cache.createPdxInstanceFactory(PdxValue.class.getName());
piFactory.writeInt("value", 1);
PdxInstance pi = piFactory.create();
assertEquals(1, pi.getField("value"));
assertEquals(null, pi.getField("fieldToDelete"));
PdxType pt2 = ((PdxInstanceImpl) pi).getPdxType();
assertEquals(null, pt2.getPdxField("fieldToDelete"));
assertEquals(1, pt2.getFieldCount());
}
} finally {
if (!cache.isClosed()) {
cache.close();
}
}
} finally {
}
}
Aggregations