Search in sources :

Example 1 with Unretained

use of org.apache.geode.internal.offheap.annotations.Unretained 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)

Example 2 with Unretained

use of org.apache.geode.internal.offheap.annotations.Unretained in project geode by apache.

the class BucketRegion method setDeltaIfNeeded.

private void setDeltaIfNeeded(EntryEventImpl event) {
    if (this.partitionedRegion.getSystem().getConfig().getDeltaPropagation() && event.getOperation().isUpdate() && event.getDeltaBytes() == null) {
        @Unretained Object rawNewValue = event.getRawNewValue();
        if (!(rawNewValue instanceof CachedDeserializable)) {
            return;
        }
        CachedDeserializable cd = (CachedDeserializable) rawNewValue;
        if (!cd.isSerialized()) {
            // it is a byte[]; not a Delta
            return;
        }
        Object instance = cd.getValue();
        if (instance instanceof org.apache.geode.Delta && ((org.apache.geode.Delta) instance).hasDelta()) {
            try {
                HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
                long start = DistributionStats.getStatTime();
                ((org.apache.geode.Delta) instance).toDelta(hdos);
                event.setDeltaBytes(hdos.toByteArray());
                this.partitionedRegion.getCachePerfStats().endDeltaPrepared(start);
            } catch (RuntimeException re) {
                throw re;
            } catch (Exception e) {
                throw new DeltaSerializationException(LocalizedStrings.DistributionManager_CAUGHT_EXCEPTION_WHILE_SENDING_DELTA.toLocalizedString(), e);
            }
        }
    }
}
Also used : org.apache.geode(org.apache.geode) IOException(java.io.IOException) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) LockObject(org.apache.geode.internal.cache.partitioned.LockObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 3 with Unretained

use of org.apache.geode.internal.offheap.annotations.Unretained in project geode by apache.

the class GatewaySenderEventImpl method getDeserializedValue.

/**
   * Return this event's deserialized value
   * 
   * @return this event's deserialized value
   */
public Object getDeserializedValue() {
    if (this.valueIsObject == 0x00) {
        Object result = this.value;
        if (result == null) {
            @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) Object so = this.valueObj;
            if (this.valueObjReleased) {
                throw new IllegalStateException("Value is no longer available. getDeserializedValue must be called before processEvents returns.");
            }
            if (so instanceof StoredObject) {
                return ((StoredObject) so).getValueAsDeserializedHeapObject();
            } else {
                throw new IllegalStateException("expected valueObj field to be an instance of StoredObject but it was " + so);
            }
        }
        return result;
    } else {
        Object vo = this.valueObj;
        if (vo != null) {
            if (vo instanceof StoredObject) {
                @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) StoredObject so = (StoredObject) vo;
                return so.getValueAsDeserializedHeapObject();
            } else {
                // it is already deserialized
                return vo;
            }
        } else {
            if (this.value != null) {
                Object result = EntryEventImpl.deserialize(this.value);
                this.valueObj = result;
                return result;
            } else if (this.substituteValue != null) {
                // If the substitute value is set, return it.
                return this.substituteValue;
            } else {
                if (this.valueObjReleased) {
                    throw new IllegalStateException("Value is no longer available. getDeserializedValue must be called before processEvents returns.");
                }
                // both value and valueObj are null but we did not free it.
                return null;
            }
        }
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 4 with Unretained

use of org.apache.geode.internal.offheap.annotations.Unretained in project geode by apache.

the class GatewaySenderEventImpl method getSerializedValueSize.

public int getSerializedValueSize() {
    int localSerializedValueSize = this.serializedValueSize;
    if (localSerializedValueSize != DEFAULT_SERIALIZED_VALUE_SIZE) {
        return localSerializedValueSize;
    }
    @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) Object vo = this.valueObj;
    if (vo instanceof StoredObject) {
        localSerializedValueSize = ((StoredObject) vo).getSizeInBytes();
    } else {
        if (this.substituteValue != null) {
            localSerializedValueSize = sizeOf(this.substituteValue);
        } else {
            localSerializedValueSize = CachedDeserializableFactory.calcMemSize(getSerializedValue());
        }
    }
    this.serializedValueSize = localSerializedValueSize;
    return localSerializedValueSize;
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 5 with Unretained

use of org.apache.geode.internal.offheap.annotations.Unretained in project geode by apache.

the class LocalRegion method postPutAllFireEvents.

public void postPutAllFireEvents(DistributedPutAllOperation putAllOp, VersionedObjectList successfulPuts) {
    if (!this.dataPolicy.withStorage() && this.concurrencyChecksEnabled && putAllOp.getBaseEvent().isBridgeEvent()) {
        // if there is no local storage we need to transfer version information
        // to the successfulPuts list for transmission back to the client
        successfulPuts.clear();
        putAllOp.fillVersionedObjectList(successfulPuts);
    }
    Set successfulKeys = new HashSet(successfulPuts.size());
    for (Object key : successfulPuts.getKeys()) {
        successfulKeys.add(key);
    }
    for (Iterator it = putAllOp.eventIterator(); it.hasNext(); ) {
        @Unretained EntryEventImpl event = (EntryEventImpl) it.next();
        if (successfulKeys.contains(event.getKey())) {
            EnumListenerEvent op = event.getOperation().isCreate() ? EnumListenerEvent.AFTER_CREATE : EnumListenerEvent.AFTER_UPDATE;
            invokePutCallbacks(op, event, !event.callbacksInvoked() && !event.isPossibleDuplicate(), this.isUsedForPartitionedRegionBucket);
        }
    }
}
Also used : EnumSet(java.util.EnumSet) HashSet(java.util.HashSet) Set(java.util.Set) AbstractSet(java.util.AbstractSet) Iterator(java.util.Iterator) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained) HashSet(java.util.HashSet)

Aggregations

Unretained (org.apache.geode.internal.offheap.annotations.Unretained)17 StoredObject (org.apache.geode.internal.offheap.StoredObject)12 HashSet (java.util.HashSet)4 Set (java.util.Set)4 CqService (org.apache.geode.cache.query.internal.cq.CqService)4 Retained (org.apache.geode.internal.offheap.annotations.Retained)4 IOException (java.io.IOException)2 AbstractSet (java.util.AbstractSet)2 EnumSet (java.util.EnumSet)2 Iterator (java.util.Iterator)2 TreeSet (java.util.TreeSet)2 CopyOnWriteHashSet (org.apache.geode.internal.CopyOnWriteHashSet)2 FilterInfo (org.apache.geode.internal.cache.FilterRoutingInfo.FilterInfo)2 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)2 org.apache.geode (org.apache.geode)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 PutAllEntryData (org.apache.geode.internal.cache.DistributedPutAllOperation.PutAllEntryData)1 RemoveAllEntryData (org.apache.geode.internal.cache.DistributedRemoveAllOperation.RemoveAllEntryData)1 Token (org.apache.geode.internal.cache.Token)1 LockObject (org.apache.geode.internal.cache.partitioned.LockObject)1