use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class AbstractIndex method verifyAndGetPdxDomainObject.
// package-private to avoid synthetic accessor
Object verifyAndGetPdxDomainObject(Object value) {
if (value instanceof StructImpl) {
// Doing hasPdx check first, since its cheaper.
if (((StructImpl) value).isHasPdx() && !((InternalCache) this.region.getCache()).getPdxReadSerializedByAnyGemFireServices()) {
// Set the pdx values for the struct object.
StructImpl v = (StructImpl) value;
Object[] fieldValues = v.getPdxFieldValues();
return new StructImpl((StructTypeImpl) v.getStructType(), fieldValues);
}
} else if (value instanceof PdxInstance && !((InternalCache) this.region.getCache()).getPdxReadSerializedByAnyGemFireServices()) {
return ((PdxInstance) value).getObject();
}
return value;
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class InternalDataSerializer method readPdxEnum.
/**
* @throws IOException since 6.6.2
*/
private static Object readPdxEnum(DataInput in) throws IOException {
int dsId = in.readByte();
int tmp = readArrayLength(in);
int enumId = dsId << 24 | tmp & 0xFFFFFF;
if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
logger.trace(LogMarker.SERIALIZER, "read PdxEnum id={}", enumId);
}
InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
TypeRegistry tr = internalCache.getPdxRegistry();
Object result = tr.getEnumById(enumId);
if (result instanceof PdxInstance) {
getDMStats(internalCache).incPdxInstanceCreations();
}
return result;
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class VMCachedDeserializable method getDeserializedValue.
public Object getDeserializedValue(Region r, RegionEntry re) {
Object v = this.value;
if (v instanceof byte[]) {
// org.apache.geode.internal.cache.GemFireCache.getInstance().getLogger().info("DEBUG
// getDeserializedValue r=" + r + " re=" + re, new RuntimeException("STACK"));
LRUEntry le = null;
if (re != null) {
assert r != null;
if (re instanceof LRUEntry) {
le = (LRUEntry) re;
}
}
if (le != null) {
if (r instanceof PartitionedRegion) {
r = ((PartitionedRegion) r).getBucketRegion(re.getKey());
}
boolean callFinish = false;
AbstractLRURegionMap lruMap = null;
if (r != null) {
// fix for bug 44795
lruMap = (AbstractLRURegionMap) ((LocalRegion) r).getRegionMap();
}
boolean threadAlreadySynced = Thread.holdsLock(le);
boolean isCacheListenerInvoked = re.isCacheListenerInvocationInProgress();
synchronized (le) {
v = this.value;
if (!(v instanceof byte[]))
return v;
v = EntryEventImpl.deserialize((byte[]) v);
if (threadAlreadySynced && !isCacheListenerInvoked) {
// if the thread that called us was already synced.
return v;
}
if (!(v instanceof PdxInstance)) {
this.value = v;
if (lruMap != null) {
callFinish = lruMap.beginChangeValueForm(le, this, v);
}
}
}
if (callFinish && !isCacheListenerInvoked) {
lruMap.finishChangeValueForm();
}
} else {
// we sync on this so we will only do one deserialize
synchronized (this) {
v = this.value;
if (!(v instanceof byte[]))
return v;
v = EntryEventImpl.deserialize((byte[]) v);
if (!(v instanceof PdxInstance)) {
this.value = v;
}
// ObjectSizer os = null;
// if (r != null) {
// EvictionAttributes ea = r.getAttributes().getEvictionAttributes();
// if (ea != null) {
// os = ea.getObjectSizer();
// }
// int vSize = CachedDeserializableFactory.calcMemSize(v, os, false, false);
// if (vSize != -1) {
// int oldSize = this.valueSize;
// this.valueSize = vSize;
// if (r instanceof BucketRegion) {
// BucketRegion br = (BucketRegion)r;
// br.updateBucketMemoryStats(vSize - oldSize);
// }
// // @todo do we need to update some lru stats since the size changed?
// }
// // If vSize == -1 then leave valueSize as is which is the serialized size.
}
}
}
return v;
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class AbstractRegionEntry method checkCDEquals.
private static boolean checkCDEquals(CachedDeserializable cd, Object obj, boolean isCompressedOffHeap) {
if (!cd.isSerialized()) {
// cd is an actual byte[].
byte[] ba2;
if (obj instanceof CachedDeserializable) {
CachedDeserializable cdObj = (CachedDeserializable) obj;
if (!cdObj.isSerialized()) {
return false;
}
ba2 = (byte[]) cdObj.getDeserializedForReading();
} else if (obj instanceof byte[]) {
ba2 = (byte[]) obj;
} else {
return false;
}
byte[] ba1 = (byte[]) cd.getDeserializedForReading();
return Arrays.equals(ba1, ba2);
}
Object cdVal = cd.getValue();
if (cdVal instanceof byte[]) {
byte[] cdValBytes = (byte[]) cdVal;
PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
if (pi != null) {
return checkPdxEquals(pi, obj);
}
if (isCompressedOffHeap) {
// fix for bug 52248
byte[] serializedObj;
if (obj instanceof CachedDeserializable) {
serializedObj = ((CachedDeserializable) obj).getSerializedValue();
} else {
serializedObj = EntryEventImpl.serialize(obj);
}
return Arrays.equals(cdValBytes, serializedObj);
} else {
/*
* To be more compatible with previous releases do not compare the serialized forms here.
* Instead deserialize and call the equals method.
*/
Object deserializedObj;
if (obj instanceof CachedDeserializable) {
deserializedObj = ((CachedDeserializable) obj).getDeserializedForReading();
} else {
if (obj == null || obj == Token.NOT_AVAILABLE || Token.isInvalidOrRemoved(obj)) {
return false;
}
// TODO OPTIMIZE: Before serializing all of obj we could get the top
// level class name of cdVal and compare it to the top level class name of obj.
deserializedObj = obj;
}
return basicEquals(deserializedObj, cd.getDeserializedForReading());
}
} else {
// prefer object form
if (obj instanceof CachedDeserializable) {
// TODO OPTIMIZE: Before deserializing all of obj we could get the top
// class name of cdVal and the top level class name of obj and compare.
obj = ((CachedDeserializable) obj).getDeserializedForReading();
}
return basicEquals(cdVal, obj);
}
}
use of org.apache.geode.pdx.PdxInstance 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;
}
Aggregations