Search in sources :

Example 26 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class DistributedCacheOperation method writeValue.

/**
   * @param deserializationPolicy must be one of the following: DESERIALIZATION_POLICY_NONE,
   *        DESERIALIZATION_POLICY_LAZY.
   */
public static void writeValue(final byte deserializationPolicy, final Object vObj, final byte[] vBytes, final DataOutput out) throws IOException {
    if (vObj != null) {
        if (deserializationPolicy == DESERIALIZATION_POLICY_NONE) {
            // We only have NONE with a vObj when vObj is off-heap and not serialized.
            StoredObject so = (StoredObject) vObj;
            assert !so.isSerialized();
            so.sendAsByteArray(out);
        } else {
            // LAZY
            DataSerializer.writeObjectAsByteArray(vObj, out);
        }
    } else {
        DataSerializer.writeByteArray(vBytes, out);
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 27 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class EntryEventImpl method release.

@Override
@Released({ ENTRY_EVENT_NEW_VALUE, ENTRY_EVENT_OLD_VALUE })
public void release() {
    // noop if already freed or values can not be off-heap
    if (!this.offHeapOk)
        return;
    if (!mayHaveOffHeapReferences()) {
        return;
    }
    synchronized (this.offHeapLock) {
        // Note that this method does not set the old/new values to null but
        // leaves them set to the off-heap value so that future calls to getOld/NewValue
        // will fail with an exception.
        testHookReleaseInProgress();
        Object ov = basicGetOldValue();
        Object nv = basicGetNewValue();
        this.offHeapOk = false;
        if (ov instanceof StoredObject) {
            // System.identityHashCode(ov));
            if (ReferenceCountHelper.trackReferenceCounts()) {
                ReferenceCountHelper.setReferenceCountOwner(new OldValueOwner());
                ((Releasable) ov).release();
                ReferenceCountHelper.setReferenceCountOwner(null);
            } else {
                ((Releasable) ov).release();
            }
        }
        OffHeapHelper.releaseAndTrackOwner(nv, this);
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Releasable(org.apache.geode.internal.offheap.Releasable) Released(org.apache.geode.internal.offheap.annotations.Released)

Example 28 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class EntryEventImpl method exportOldValue.

/**
   * Export the event's old value to the given importer.
   */
public void exportOldValue(OldValueImporter importer) {
    final boolean prefersSerialized = importer.prefersOldSerialized();
    if (prefersSerialized) {
        if (this.oldValueBytes != null && this.oldValue instanceof CachedDeserializable) {
            importer.importOldBytes(this.oldValueBytes, true);
            return;
        }
    }
    @Unretained(ENTRY_EVENT_OLD_VALUE) final Object ov = getRawOldValue();
    if (ov instanceof StoredObject) {
        final StoredObject so = (StoredObject) ov;
        final boolean isSerialized = so.isSerialized();
        if (importer.isUnretainedOldReferenceOk()) {
            importer.importOldObject(ov, isSerialized);
        } else if (!isSerialized || prefersSerialized) {
            importer.importOldBytes(so.getValueAsHeapByteArray(), isSerialized);
        } else {
            importer.importOldObject(so.getValueAsDeserializedHeapObject(), true);
        }
    } else if (ov instanceof byte[]) {
        importer.importOldBytes((byte[]) ov, false);
    } else if (!importer.isCachedDeserializableValueOk() && ov instanceof CachedDeserializable) {
        CachedDeserializable cd = (CachedDeserializable) ov;
        Object cdV = cd.getValue();
        if (cdV instanceof byte[]) {
            importer.importOldBytes((byte[]) cdV, true);
        } else {
            importer.importOldObject(cdV, true);
        }
    } else {
        importer.importOldObject(AbstractRegion.handleNotAvailable(ov), true);
    }
}
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 29 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class GatewaySenderEventImpl method getSerializedValue.

/**
   * If the value owned of this event is just bytes return that byte array; otherwise serialize the
   * value object and return the serialized bytes. Use {@link #getValueIsObject()} to determine if
   * the result is raw or serialized bytes.
   */
public byte[] getSerializedValue() {
    byte[] result = this.value;
    if (result == null) {
        if (this.substituteValue != null) {
            // The substitute value is set. Serialize it
            isSerializingValue.set(Boolean.TRUE);
            result = EntryEventImpl.serialize(this.substituteValue);
            isSerializingValue.set(Boolean.FALSE);
            return result;
        }
        @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) Object vo = this.valueObj;
        if (vo instanceof StoredObject) {
            synchronized (this) {
                result = this.value;
                if (result == null) {
                    StoredObject so = (StoredObject) vo;
                    result = so.getValueAsHeapByteArray();
                    this.value = result;
                }
            }
        } else {
            synchronized (this) {
                result = this.value;
                if (result == null && vo != null && !(vo instanceof Token)) {
                    isSerializingValue.set(Boolean.TRUE);
                    result = EntryEventImpl.serialize(vo);
                    isSerializingValue.set(Boolean.FALSE);
                    this.value = result;
                } else if (result == null) {
                    if (this.valueObjReleased) {
                        this.serializedValueNotAvailable = true;
                        throw new IllegalStateException("Value is no longer available. getSerializedValue must be called before processEvents returns.");
                    }
                }
            }
        }
    }
    return result;
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Token(org.apache.geode.internal.cache.Token) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Example 30 with StoredObject

use of org.apache.geode.internal.offheap.StoredObject in project geode by apache.

the class GatewaySenderEventImpl method initializeValue.

// Initializes the value object. This function need a relook because the
// serialization of the value looks unnecessary.
@Retained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE)
protected void initializeValue(EntryEventImpl event) throws IOException {
    // substituteValue (if set).
    if (this.substituteValue == null) {
        // If the value is already serialized, use it.
        this.valueIsObject = 0x01;
        /**
       * so ends up being stored in this.valueObj
       */
        @Retained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE) StoredObject so = null;
        {
            ReferenceCountHelper.setReferenceCountOwner(this);
            so = event.getOffHeapNewValue();
            ReferenceCountHelper.setReferenceCountOwner(null);
        }
        if (so != null) {
            // if (so != null && !event.hasDelta()) {
            // Since GatewaySenderEventImpl instances can live for a long time in the gateway region
            // queue
            // we do not want the StoredObject to be one that keeps the heap form cached.
            // fixes 51999
            so = so.getStoredObjectWithoutHeapForm();
            this.valueObj = so;
            if (!so.isSerialized()) {
                this.valueIsObject = 0x00;
            }
        } else if (event.getCachedSerializedNewValue() != null) {
            // We want this to have lower precedence than StoredObject so that the gateway
            // can share a reference to the off-heap value.
            this.value = event.getCachedSerializedNewValue();
        } else {
            final Object newValue = event.getRawNewValue();
            // since we already called getOffHeapNewValue()
            assert !(newValue instanceof StoredObject);
            // and it returned null
            if (newValue instanceof CachedDeserializable) {
                this.value = ((CachedDeserializable) newValue).getSerializedValue();
            } else if (newValue instanceof byte[]) {
                // The value is byte[]. Set _valueIsObject flag to 0x00 (not an object)
                this.value = (byte[]) newValue;
                this.valueIsObject = 0x00;
            } else {
                // The value is an object. It will be serialized later when getSerializedValue is called.
                this.valueObj = newValue;
                // to prevent bug 48281 we need to serialize it now
                this.getSerializedValue();
                this.valueObj = null;
            }
        }
    } else {
        // The substituteValue is set. Use it.
        if (this.substituteValue instanceof byte[]) {
            // The substituteValue is byte[]. Set valueIsObject flag to 0x00 (not an object)
            this.value = (byte[]) this.substituteValue;
            this.valueIsObject = 0x00;
        } else if (this.substituteValue == TOKEN_NULL) {
            // The substituteValue represents null. Set the value and substituteValue to null.
            this.value = null;
            this.substituteValue = null;
            this.valueIsObject = 0x01;
        } else {
            // The substituteValue is an object. Leave it as is.
            this.valueIsObject = 0x01;
        }
    }
}
Also used : Retained(org.apache.geode.internal.offheap.annotations.Retained) StoredObject(org.apache.geode.internal.offheap.StoredObject) CachedDeserializable(org.apache.geode.internal.cache.CachedDeserializable) StoredObject(org.apache.geode.internal.offheap.StoredObject) Retained(org.apache.geode.internal.offheap.annotations.Retained)

Aggregations

StoredObject (org.apache.geode.internal.offheap.StoredObject)34 Test (org.junit.Test)11 UnitTest (org.apache.geode.test.junit.categories.UnitTest)10 Retained (org.apache.geode.internal.offheap.annotations.Retained)7 Unretained (org.apache.geode.internal.offheap.annotations.Unretained)7 Released (org.apache.geode.internal.offheap.annotations.Released)6 CachedDeserializable (org.apache.geode.internal.cache.CachedDeserializable)5 NewValueImporter (org.apache.geode.internal.cache.EntryEventImpl.NewValueImporter)4 OldValueImporter (org.apache.geode.internal.cache.EntryEventImpl.OldValueImporter)4 IOException (java.io.IOException)3 PdxInstance (org.apache.geode.pdx.PdxInstance)2 InterruptedIOException (java.io.InterruptedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CacheException (org.apache.geode.cache.CacheException)1 CacheWriterException (org.apache.geode.cache.CacheWriterException)1 DiskAccessException (org.apache.geode.cache.DiskAccessException)1 EntryEvent (org.apache.geode.cache.EntryEvent)1 Region (org.apache.geode.cache.Region)1