/** * Serialize an object into a <code>byte[]</code> * * @throws IllegalArgumentException If <code>obj</code> should not be serialized */ public static byte[] serialize(Object obj) { return serialize(obj, null); }
/** * Return the serialized value as a byte[] */ @Override public byte[] getSerializedValue() { Object v = this.value; if (v instanceof byte[]) return (byte[]) v; return EntryEventImpl.serialize(v); }
private byte[] getBytes(Object o) { if (o instanceof byte[]) { return (byte[]) o; } if (o instanceof CachedDeserializable) { return ((CachedDeserializable) o).getSerializedValue(); } else { return EntryEventImpl.serialize(o); } }
/** * Creates a new {@link CompressedCachedDeserializable} with an unserialized value. * * @param obj a region entry value. */ public CompressedCachedDeserializable(final Object obj) { if (obj == null) throw new NullPointerException( "value must not be null"); this.value = getCompressor().compress(EntryEventImpl.serialize(obj)); }
private byte[] toBytes(Object newValue) { byte[] newValueBytes; if (newValue instanceof InternalPdxInstance) { newValueBytes = toBytes((InternalPdxInstance) newValue); } else if (newValue instanceof CachedDeserializable) { newValueBytes = toBytes((CachedDeserializable) newValue); } else { newValueBytes = EntryEventImpl.serialize(newValue); } return newValueBytes; }
@Override public byte[] getSerializedValue() { byte[] result = getRawBytes(); if (!isSerialized()) { // The object is a byte[]. So we need to make it look like a serialized byte[] in our result result = EntryEventImpl.serialize(result); } return result; }
@Override public byte[] getSerializedValue() { byte[] value = getRawBytes(); if (!isSerialized()) { value = EntryEventImpl.serialize(value); } return value; }
@Override public OffHeapStoredObject createValueAsSerializedStoredObject(Object value) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); boolean isSerialized = true; boolean isCompressed = false; return createChunk(valueInSerializedByteArray, isSerialized, isCompressed); }
private OffHeapStoredObject createChunk(Object value) { byte[] v = EntryEventImpl.serialize(value); boolean isSerialized = true; boolean isCompressed = false; OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(v, isSerialized, isCompressed); return chunk; }
private StoredObject createValueAsSerializedStoredObject(Object value, boolean isCompressed) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); boolean isSerialized = true; StoredObject createdObject = createChunk(valueInSerializedByteArray, isSerialized, isCompressed); return createdObject; }
@Override protected StoredObject createValueAsSerializedStoredObject(Object value) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); boolean isSerialized = true; boolean isCompressed = false; StoredObject createdObject = createChunk(valueInSerializedByteArray, isSerialized, isCompressed); return createdObject; }
@Test public void checkEqualsCanCompareCacheDeserializableWithAnObjectIfIsCompressedOffheap() { CachedDeserializable cachedDeserializable = mock(CachedDeserializable.class); when(cachedDeserializable.isSerialized()).thenReturn(true); when(cachedDeserializable.getValue()).thenReturn(EntryEventImpl.serialize(STRING)); assertThat(ValueComparisonHelper.checkEquals(cachedDeserializable, STRING, true, mock(InternalCache.class))).isTrue(); }
@Test public void newValueIsSerializableUsesItsSerializedValue() { String newValue = "newValue"; when(event.basicGetNewValue()).thenReturn(newValue); instance.serializeNewValueIfNeeded(region, event); ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class); verify(event, times(1)).setCachedSerializedNewValue(captor.capture()); assertThat(captor.getValue()).isEqualTo(EntryEventImpl.serialize(newValue)); }
public TinyStoredObject createValueAsUncompressedStoredObject(Object value) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); // encode a serialized, uncompressed entry value to address long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false); return new TinyStoredObject(encodedAddress); }
@Override public TinyStoredObject createValueAsSerializedStoredObject(Object value) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); // encode a serialized entry value to address long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false); return new TinyStoredObject(encodedAddress); }
@Test public void encodedAddressShouldBeDecodableEvenIfValueIsSerialized() { Integer value = Integer.MAX_VALUE; byte[] serializedValue = EntryEventImpl.serialize(value); boolean isSerialized = true; boolean isCompressed = false; long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedValue, isSerialized, isCompressed); Integer actualValue = (Integer) OffHeapRegionEntryHelper.decodeAddressToObject(encodedAddress); assertThat(actualValue).isEqualTo(value); }
@Test public void encodeDataAsAssressShouldSetSerialziedBitIfSerizliaed() { long expectedAddress = 63221918596947L; Integer value = Integer.MAX_VALUE; byte[] valueInBytes = EntryEventImpl.serialize(value); boolean isSerialized = true; boolean isCompressed = false; long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInBytes, isSerialized, isCompressed); assertThat(expectedAddress).isEqualTo(encodedAddress); assertSerializedAndCompressedBits(encodedAddress, isSerialized, isCompressed); }
@Test public void decodeUncompressedAddressToBytesShouldDecodeLongIfItsSerializedAndIfItsNotTooBig() { Long value = 0L; long encodedAddress = 123L; byte[] actual = OffHeapRegionEntryHelper.decodeUncompressedAddressToBytes(encodedAddress); byte[] expectedValue = EntryEventImpl.serialize(value); assertThat(actual).isEqualTo(expectedValue); }
@Test public void putIfAbsentResultHasSameValueReturnTrueIfResultHasSameValueAndNotFromClient() { Object result = new VMCachedDeserializable(EntryEventImpl.serialize(value)); when(region.putIfAbsentResultHasSameValue(false, value, result)).thenCallRealMethod(); assertThat(region.putIfAbsentResultHasSameValue(false, value, result)).isTrue(); verify(region).getAttributes(); }
@Test public void getSerializedValueShouldSerializeTheValue() { Object regionEntryValue = getValue(); byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue); boolean isSerialized = false; boolean isCompressed = false; OffHeapStoredObject chunk = (OffHeapStoredObject) ma .allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed); byte[] serializedValue = chunk.getSerializedValue(); assertThat(serializedValue).isEqualTo(EntryEventImpl.serialize(regionEntryValueAsBytes)); chunk.release(); }