Search in sources :

Example 6 with PdxInstance

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;
}
Also used : StructImpl(org.apache.geode.cache.query.internal.StructImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) InternalCache(org.apache.geode.internal.cache.InternalCache)

Example 7 with PdxInstance

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;
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) InternalCache(org.apache.geode.internal.cache.InternalCache) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry)

Example 8 with PdxInstance

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;
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) LRUEntry(org.apache.geode.internal.cache.lru.LRUEntry)

Example 9 with PdxInstance

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);
    }
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 10 with PdxInstance

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;
}
Also used : MemoryAllocator(org.apache.geode.internal.offheap.MemoryAllocator) IOException(java.io.IOException) ConvertableToBytes(org.apache.geode.pdx.internal.ConvertableToBytes) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) StoredObject(org.apache.geode.internal.offheap.StoredObject) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained) Retained(org.apache.geode.internal.offheap.annotations.Retained)

Aggregations

PdxInstance (org.apache.geode.pdx.PdxInstance)63 Test (org.junit.Test)33 Region (org.apache.geode.cache.Region)25 PdxInstanceFactory (org.apache.geode.pdx.PdxInstanceFactory)22 SelectResults (org.apache.geode.cache.query.SelectResults)18 Host (org.apache.geode.test.dunit.Host)18 VM (org.apache.geode.test.dunit.VM)18 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)18 QueryService (org.apache.geode.cache.query.QueryService)16 CacheException (org.apache.geode.cache.CacheException)14 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)13 ClientCache (org.apache.geode.cache.client.ClientCache)11 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)10 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)10 PdxString (org.apache.geode.pdx.internal.PdxString)10 CacheServer (org.apache.geode.cache.server.CacheServer)8 Query (org.apache.geode.cache.query.Query)7 Collection (java.util.Collection)6 Struct (org.apache.geode.cache.query.Struct)6