You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2016/03/05 01:51:29 UTC
[29/38] incubator-geode git commit: renamed ObjectStoredAsAddress to
TinyStoredObject
renamed ObjectStoredAsAddress to TinyStoredObject
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/bccc02c2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/bccc02c2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/bccc02c2
Branch: refs/heads/feature/GEODE-982
Commit: bccc02c2ad5c4e0697f3e7a0adfb4de25af764ad
Parents: 1a49aa4
Author: Darrel Schneider <ds...@pivotal.io>
Authored: Fri Mar 4 13:40:07 2016 -0800
Committer: Darrel Schneider <ds...@pivotal.io>
Committed: Fri Mar 4 13:40:07 2016 -0800
----------------------------------------------------------------------
.../internal/offheap/ObjectStoredAsAddress.java | 229 ------------
.../offheap/OffHeapRegionEntryHelper.java | 4 +-
.../offheap/SimpleMemoryAllocatorImpl.java | 2 +-
.../internal/offheap/TinyStoredObject.java | 229 ++++++++++++
.../cache/OldValueImporterTestBase.java | 6 +-
.../offheap/ObjectStoredAsAddressJUnitTest.java | 353 -------------------
.../OffHeapRegionEntryHelperJUnitTest.java | 12 +-
.../OffHeapWriteObjectAsByteArrayJUnitTest.java | 4 +-
.../internal/offheap/StoredObjectTestSuite.java | 2 +-
.../offheap/TinyStoredObjectJUnitTest.java | 353 +++++++++++++++++++
10 files changed, 597 insertions(+), 597 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddress.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddress.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddress.java
deleted file mode 100644
index 8fc2bb2..0000000
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddress.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.gemstone.gemfire.internal.offheap;
-
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor;
-import com.gemstone.gemfire.internal.cache.EntryBits;
-import com.gemstone.gemfire.internal.cache.EntryEventImpl;
-import com.gemstone.gemfire.internal.cache.RegionEntry;
-import com.gemstone.gemfire.internal.cache.RegionEntryContext;
-
-/**
- * Used to represent stored objects that can be stored
- * in the address field.
- * The RegionEntry for an off-heap region uses a primitive
- * long to store the off-heap address of the entry's value.
- * If the value can be encoded as a long (i.e. its serialized
- * representation will fit in the 8 bytes of a long without looking
- * like an actual off-heap address) then these small values on an
- * off-heap regions are actually stored on the heap in the primitive
- * long field. When these values are "objectified" they will be an
- * instance of this class.
- * Instances of this class have a very short lifetime.
- */
-public class ObjectStoredAsAddress extends AbstractStoredObject {
- private final long address;
-
- public ObjectStoredAsAddress(long addr) {
- this.address = addr;
- }
-
- @Override
- public long getAddress() {
- return this.address;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o instanceof ObjectStoredAsAddress) {
- return getAddress() == ((ObjectStoredAsAddress) o).getAddress();
- }
- return false;
- }
-
- @Override
- public int hashCode() {
- long value = getAddress();
- return (int)(value ^ (value >>> 32));
- }
-
- @Override
- public int getSizeInBytes() {
- return 0;
- }
-
- public byte[] getDecompressedBytes(RegionEntryContext r) {
- byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, true);
- if (isCompressed()) {
- long time = r.getCachePerfStats().startDecompression();
- bytes = r.getCompressor().decompress(bytes);
- r.getCachePerfStats().endDecompression(time);
- }
- return bytes;
- }
-
- /**
- * If we contain a byte[] return it.
- * Otherwise return the serialize bytes in us in a byte array.
- */
- public byte[] getRawBytes() {
- return OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, false);
- }
-
- @Override
- public byte[] getSerializedValue() {
- byte[] value = OffHeapRegionEntryHelper.decodeAddressToBytes(this.address, true, false);
- if (!isSerialized()) {
- value = EntryEventImpl.serialize(value);
- }
- return value;
- }
-
- @Override
- public Object getDeserializedValue(Region r, RegionEntry re) {
- return OffHeapRegionEntryHelper.decodeAddressToObject(this.address);
- }
-
- @Override
- public void fillSerializedValue(BytesAndBitsForCompactor wrapper,
- byte userBits) {
- byte[] value;
- if (isSerialized()) {
- value = getSerializedValue();
- userBits = EntryBits.setSerialized(userBits, true);
- } else {
- value = (byte[]) getDeserializedForReading();
- }
- wrapper.setData(value, userBits, value.length, true);
- }
-
- @Override
- public int getValueSizeInBytes() {
- return 0;
- }
-
- @Override
- public boolean isSerialized() {
- return OffHeapRegionEntryHelper.isSerialized(this.address);
- }
-
- @Override
- public boolean isCompressed() {
- return OffHeapRegionEntryHelper.isCompressed(this.address);
- }
-
- @Override
- public void release() {
- // nothing needed
- }
-
- @Override
- public boolean retain() {
- return true;
- }
-
- @Override
- public int getRefCount() {
- return -1;
- }
-
- @Override
- public int getSize() {
- return Long.BYTES;
- }
-
- @Override
- public int getDataSize() {
- return OffHeapRegionEntryHelper.decodeAddressToDataSize(this.address);
- }
-
- @Override
- public byte readDataByte(int offset) {
- // TODO OFFHEAP: what if the data is compressed?
- return getRawBytes()[offset];
- }
-
- @Override
- public void writeDataByte(int offset, byte value) {
- throw new UnsupportedOperationException("ObjectStoredAsAddress does not support modifying the data bytes");
- }
-
- @Override
- public void readDataBytes(int offset, byte[] bytes) {
- readDataBytes(offset, bytes, 0, bytes.length);
- }
-
- @Override
- public void writeDataBytes(int offset, byte[] bytes) {
- writeDataBytes(offset, bytes, 0, bytes.length);
- }
-
- @Override
- public void readDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
- // TODO OFFHEAP: what if the data is compressed?
- byte[] src = getRawBytes();
- int dstIdx = bytesOffset;
- for (int i = offset; i < offset+size; i++) {
- bytes[dstIdx++] = src[i];
- }
- }
-
- @Override
- public void writeDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
- throw new UnsupportedOperationException("ObjectStoredAsAddress does not support modifying the data bytes");
- }
-
- @Override
- public ByteBuffer createDirectByteBuffer() {
- return null;
- }
-
- @Override
- public boolean hasRefCount() {
- return false;
- }
-
- @Override
- public boolean checkDataEquals(StoredObject so) {
- // TODO OFFHEAP: what if the data is compressed?
- return equals(so);
- }
-
- @Override
- public boolean checkDataEquals(byte[] serializedObj) {
- // TODO OFFHEAP: what if the data is compressed?
- byte[] myBytes = getSerializedValue();
- return Arrays.equals(myBytes, serializedObj);
- }
-
- @Override
- public long getAddressForReadingData(int offset, int size) {
- throw new UnsupportedOperationException("ObjectStoredAsAddress does not support reading at an address");
- }
-
- @Override
- public StoredObject slice(int offset, int limit) {
- throw new UnsupportedOperationException("ObjectStoredAsAddress does not support slice");
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelper.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelper.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelper.java
index 273b550..eb1774e 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelper.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelper.java
@@ -112,7 +112,7 @@ public class OffHeapRegionEntryHelper {
}
return result;
} else if ((ohAddress & ENCODED_BIT) != 0) {
- ObjectStoredAsAddress daa = new ObjectStoredAsAddress(ohAddress);
+ TinyStoredObject daa = new TinyStoredObject(ohAddress);
Object result = daa;
if (decompress && daa.isCompressed()) {
byte[] decompressedBytes = daa.getDecompressedBytes(context);
@@ -130,7 +130,7 @@ public class OffHeapRegionEntryHelper {
}
}
- public static int getSerializedLengthFromDataAsAddress(ObjectStoredAsAddress dataAsAddress) {
+ public static int getSerializedLengthFromDataAsAddress(TinyStoredObject dataAsAddress) {
final long ohAddress = dataAsAddress.getAddress();
if ((ohAddress & ENCODED_BIT) != 0) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
index 24a57b4..584b6de 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
@@ -330,7 +330,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed, byte[] originalHeapData) {
long addr = OffHeapRegionEntryHelper.encodeDataAsAddress(v, isSerialized, isCompressed);
if (addr != 0L) {
- return new ObjectStoredAsAddress(addr);
+ return new TinyStoredObject(addr);
}
ObjectStoredInMemory result = allocateChunk(v.length);
//debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()), true);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/TinyStoredObject.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/TinyStoredObject.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/TinyStoredObject.java
new file mode 100644
index 0000000..e8878fa
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/TinyStoredObject.java
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor;
+import com.gemstone.gemfire.internal.cache.EntryBits;
+import com.gemstone.gemfire.internal.cache.EntryEventImpl;
+import com.gemstone.gemfire.internal.cache.RegionEntry;
+import com.gemstone.gemfire.internal.cache.RegionEntryContext;
+
+/**
+ * Used to represent stored objects that can be stored
+ * in the address field.
+ * The RegionEntry for an off-heap region uses a primitive
+ * long to store the off-heap address of the entry's value.
+ * If the value can be encoded as a long (i.e. its serialized
+ * representation will fit in the 8 bytes of a long without looking
+ * like an actual off-heap address) then these tiny values on an
+ * off-heap regions are actually stored on the heap in the primitive
+ * long field. When these values are "objectified" they will be an
+ * instance of this class.
+ * Instances of this class have a very short lifetime.
+ */
+public class TinyStoredObject extends AbstractStoredObject {
+ private final long address;
+
+ public TinyStoredObject(long addr) {
+ this.address = addr;
+ }
+
+ @Override
+ public long getAddress() {
+ return this.address;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o instanceof TinyStoredObject) {
+ return getAddress() == ((TinyStoredObject) o).getAddress();
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ long value = getAddress();
+ return (int)(value ^ (value >>> 32));
+ }
+
+ @Override
+ public int getSizeInBytes() {
+ return 0;
+ }
+
+ public byte[] getDecompressedBytes(RegionEntryContext r) {
+ byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, true);
+ if (isCompressed()) {
+ long time = r.getCachePerfStats().startDecompression();
+ bytes = r.getCompressor().decompress(bytes);
+ r.getCachePerfStats().endDecompression(time);
+ }
+ return bytes;
+ }
+
+ /**
+ * If we contain a byte[] return it.
+ * Otherwise return the serialize bytes in us in a byte array.
+ */
+ public byte[] getRawBytes() {
+ return OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, false);
+ }
+
+ @Override
+ public byte[] getSerializedValue() {
+ byte[] value = OffHeapRegionEntryHelper.decodeAddressToBytes(this.address, true, false);
+ if (!isSerialized()) {
+ value = EntryEventImpl.serialize(value);
+ }
+ return value;
+ }
+
+ @Override
+ public Object getDeserializedValue(Region r, RegionEntry re) {
+ return OffHeapRegionEntryHelper.decodeAddressToObject(this.address);
+ }
+
+ @Override
+ public void fillSerializedValue(BytesAndBitsForCompactor wrapper,
+ byte userBits) {
+ byte[] value;
+ if (isSerialized()) {
+ value = getSerializedValue();
+ userBits = EntryBits.setSerialized(userBits, true);
+ } else {
+ value = (byte[]) getDeserializedForReading();
+ }
+ wrapper.setData(value, userBits, value.length, true);
+ }
+
+ @Override
+ public int getValueSizeInBytes() {
+ return 0;
+ }
+
+ @Override
+ public boolean isSerialized() {
+ return OffHeapRegionEntryHelper.isSerialized(this.address);
+ }
+
+ @Override
+ public boolean isCompressed() {
+ return OffHeapRegionEntryHelper.isCompressed(this.address);
+ }
+
+ @Override
+ public void release() {
+ // nothing needed
+ }
+
+ @Override
+ public boolean retain() {
+ return true;
+ }
+
+ @Override
+ public int getRefCount() {
+ return -1;
+ }
+
+ @Override
+ public int getSize() {
+ return Long.BYTES;
+ }
+
+ @Override
+ public int getDataSize() {
+ return OffHeapRegionEntryHelper.decodeAddressToDataSize(this.address);
+ }
+
+ @Override
+ public byte readDataByte(int offset) {
+ // TODO OFFHEAP: what if the data is compressed?
+ return getRawBytes()[offset];
+ }
+
+ @Override
+ public void writeDataByte(int offset, byte value) {
+ throw new UnsupportedOperationException("ObjectStoredAsAddress does not support modifying the data bytes");
+ }
+
+ @Override
+ public void readDataBytes(int offset, byte[] bytes) {
+ readDataBytes(offset, bytes, 0, bytes.length);
+ }
+
+ @Override
+ public void writeDataBytes(int offset, byte[] bytes) {
+ writeDataBytes(offset, bytes, 0, bytes.length);
+ }
+
+ @Override
+ public void readDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
+ // TODO OFFHEAP: what if the data is compressed?
+ byte[] src = getRawBytes();
+ int dstIdx = bytesOffset;
+ for (int i = offset; i < offset+size; i++) {
+ bytes[dstIdx++] = src[i];
+ }
+ }
+
+ @Override
+ public void writeDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
+ throw new UnsupportedOperationException("ObjectStoredAsAddress does not support modifying the data bytes");
+ }
+
+ @Override
+ public ByteBuffer createDirectByteBuffer() {
+ return null;
+ }
+
+ @Override
+ public boolean hasRefCount() {
+ return false;
+ }
+
+ @Override
+ public boolean checkDataEquals(StoredObject so) {
+ // TODO OFFHEAP: what if the data is compressed?
+ return equals(so);
+ }
+
+ @Override
+ public boolean checkDataEquals(byte[] serializedObj) {
+ // TODO OFFHEAP: what if the data is compressed?
+ byte[] myBytes = getSerializedValue();
+ return Arrays.equals(myBytes, serializedObj);
+ }
+
+ @Override
+ public long getAddressForReadingData(int offset, int size) {
+ throw new UnsupportedOperationException("ObjectStoredAsAddress does not support reading at an address");
+ }
+
+ @Override
+ public StoredObject slice(int offset, int limit) {
+ throw new UnsupportedOperationException("ObjectStoredAsAddress does not support slice");
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
index 0838812..a63ce9a 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
@@ -27,7 +27,7 @@ import org.junit.Test;
import com.gemstone.gemfire.internal.HeapDataOutputStream;
import com.gemstone.gemfire.internal.cache.EntryEventImpl.OldValueImporter;
import com.gemstone.gemfire.internal.offheap.ObjectStoredInMemory;
-import com.gemstone.gemfire.internal.offheap.ObjectStoredAsAddress;
+import com.gemstone.gemfire.internal.offheap.TinyStoredObject;
import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;
@@ -113,7 +113,7 @@ public abstract class OldValueImporterTestBase {
SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
try {
byte[] baValue = new byte[] {1,2};
- ObjectStoredAsAddress baValueSO = (ObjectStoredAsAddress) sma.allocateAndInitialize(baValue, false, false);
+ TinyStoredObject baValueSO = (TinyStoredObject) sma.allocateAndInitialize(baValue, false, false);
OldValueImporter omsg = createImporter();
omsg.importOldObject(baValueSO, false);
hdos = new HeapDataOutputStream(bytes);
@@ -148,7 +148,7 @@ public abstract class OldValueImporterTestBase {
try {
String baValue = "12";
byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
- ObjectStoredAsAddress baValueSO = (ObjectStoredAsAddress) sma.allocateAndInitialize(baValueBlob, true, false);
+ TinyStoredObject baValueSO = (TinyStoredObject) sma.allocateAndInitialize(baValueBlob, true, false);
OldValueImporter omsg = createImporter();
omsg.importOldObject(baValueSO, true);
hdos = new HeapDataOutputStream(bytes);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddressJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddressJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddressJUnitTest.java
deleted file mode 100644
index cc23e91..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddressJUnitTest.java
+++ /dev/null
@@ -1,353 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.gemstone.gemfire.internal.offheap;
-
-import com.gemstone.gemfire.compression.Compressor;
-import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor;
-import com.gemstone.gemfire.internal.cache.CachePerfStats;
-import com.gemstone.gemfire.internal.cache.EntryEventImpl;
-import com.gemstone.gemfire.internal.cache.RegionEntryContext;
-import com.gemstone.gemfire.internal.offheap.ObjectStoredAsAddress;
-
-import com.gemstone.gemfire.internal.offheap.OffHeapRegionEntryHelper;
-import com.gemstone.gemfire.test.junit.categories.UnitTest;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.mockito.Mock;
-
-import java.nio.ByteBuffer;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.*;
-
-@Category(UnitTest.class)
-public class ObjectStoredAsAddressJUnitTest extends AbstractStoredObjectTestBase {
-
- @Override
- public Object getValue() {
- return Integer.valueOf(123456789);
- }
-
- @Override
- public byte[] getValueAsByteArray() {
- return convertValueToByteArray(getValue());
- }
-
- private byte[] convertValueToByteArray(Object value) {
- return ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) value).array();
- }
-
- @Override
- public Object convertByteArrayToObject(byte[] valueInByteArray) {
- return ByteBuffer.wrap(valueInByteArray).getInt();
- }
-
- @Override
- public Object convertSerializedByteArrayToObject(byte[] valueInSerializedByteArray) {
- return EntryEventImpl.deserialize(valueInSerializedByteArray);
- }
-
- @Override
- public ObjectStoredAsAddress createValueAsUnserializedStoredObject(Object value) {
- byte[] valueInByteArray;
- if(value instanceof Integer) {
- valueInByteArray = convertValueToByteArray(value);
- } else {
- valueInByteArray = (byte[]) value;
- }
- //encode a non-serialized entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInByteArray, false, false);
- return new ObjectStoredAsAddress(encodedAddress);
- }
-
- @Override
- public ObjectStoredAsAddress createValueAsSerializedStoredObject(Object value) {
- byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
- //encode a serialized entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false);
- return new ObjectStoredAsAddress(encodedAddress);
- }
-
- public ObjectStoredAsAddress createValueAsCompressedStoredObject(Object value) {
- byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
- //encode a serialized, compressed entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, true);
- return new ObjectStoredAsAddress(encodedAddress);
- }
-
- public ObjectStoredAsAddress 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 ObjectStoredAsAddress(encodedAddress);
- }
-
- @Test
- public void shouldReturnCorrectEncodingAddress() {
-
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- assertNotNull(address1);
- assertEquals("Encoding address should be:", 10001, address1.getAddress());
-
- ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L);
- assertNotNull(address2);
- assertEquals("Returning always 10001 expected 10002", 10002, address2.getAddress());
- }
-
- @Test
- public void twoAddressesShouldBeEqualIfEncodingAddressIsSame() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10001L);
-
- assertEquals("Two addresses are equal if encoding address is same", true, address1.equals(address2));
- }
-
- @Test
- public void twoAddressesShouldNotBeEqualIfEncodingAddressIsNotSame() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L);
-
- assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2));
- }
-
- @Test
- public void twoAddressesAreNotEqualIfTheyAreNotTypeDataAsAddress() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- Long address2 = new Long(10002L);
-
- assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2));
- }
-
- @Test
- public void addressHashCodeShouldBe() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- assertEquals("", 10001, address1.hashCode());
- }
-
- @Test
- public void getSizeInBytesAlwaysReturnsZero() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L);
-
- assertEquals("getSizeInBytes", 0, address1.getSizeInBytes());
- assertEquals("getSizeInBytes", 0, address2.getSizeInBytes());
- }
-
- @Test
- public void getValueSizeInBytesAlwaysReturnsZero() {
- ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L);
- ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L);
-
- assertEquals("getSizeInBytes", 0, address1.getValueSizeInBytes());
- assertEquals("getSizeInBytes", 0, address2.getValueSizeInBytes());
- }
-
- @Test
- public void isCompressedShouldReturnTrueIfCompressed() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsCompressedStoredObject(regionEntryValue);
-
- assertEquals("Should return true as it is compressed", true, offheapAddress.isCompressed());
- }
-
- @Test
- public void isCompressedShouldReturnFalseIfNotCompressed() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue);
-
- assertEquals("Should return false as it is compressed", false, offheapAddress.isCompressed());
- }
-
- @Test
- public void isSerializedShouldReturnTrueIfSeriazlied() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
-
- assertEquals("Should return true as it is serialized", true, offheapAddress.isSerialized());
- }
-
- @Test
- public void isSerializedShouldReturnFalseIfNotSeriazlied() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsUnserializedStoredObject(regionEntryValue);
-
- assertEquals("Should return false as it is serialized", false, offheapAddress.isSerialized());
- }
-
- @Test
- public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- //encode a non-serialized and compressed entry value to address - last argument is to let that it is compressed
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, true);
- ObjectStoredAsAddress offheapAddress = new ObjectStoredAsAddress(encodedAddress);
-
- RegionEntryContext regionContext = mock(RegionEntryContext.class);
- CachePerfStats cacheStats = mock(CachePerfStats.class);
- Compressor compressor = mock(Compressor.class);
-
- long startTime = 10000L;
-
- //mock required things
- when(regionContext.getCompressor()).thenReturn(compressor);
- when(compressor.decompress(regionEntryValueAsBytes)).thenReturn(regionEntryValueAsBytes);
- when(regionContext.getCachePerfStats()).thenReturn(cacheStats);
- when(cacheStats.startDecompression()).thenReturn(startTime);
-
- //invoke the thing
- byte[] bytes = offheapAddress.getDecompressedBytes(regionContext);
-
- //verify the thing happened
- verify(cacheStats, atLeastOnce()).startDecompression();
- verify(compressor, times(1)).decompress(regionEntryValueAsBytes);
- verify(cacheStats, atLeastOnce()).endDecompression(startTime);
-
- assertArrayEquals(regionEntryValueAsBytes, bytes);
- }
-
- @Test
- public void getDecompressedBytesShouldNotTryToDecompressIfNotCompressed() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue);
-
- //mock the thing
- RegionEntryContext regionContext = mock(RegionEntryContext.class);
- Compressor compressor = mock(Compressor.class);
- when(regionContext.getCompressor()).thenReturn(compressor);
-
- //invoke the thing
- byte[] actualValueInBytes = offheapAddress.getDecompressedBytes(regionContext);
-
- //createValueAsUncompressedStoredObject does uses a serialized value - so convert it to object
- Object actualRegionValue = convertSerializedByteArrayToObject(actualValueInBytes);
-
- //verify the thing happened
- verify(regionContext, never()).getCompressor();
- assertEquals(regionEntryValue, actualRegionValue);
- }
-
- @Test
- public void getRawBytesShouldReturnAByteArray() {
- byte[] regionEntryValueAsBytes = getValueAsByteArray();
-
- ObjectStoredAsAddress offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes);
- byte[] actual = offheapAddress.getRawBytes();
-
- assertArrayEquals(regionEntryValueAsBytes, actual);
- }
-
- @Test
- public void getSerializedValueShouldReturnASerializedByteArray() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
-
- byte[] actualSerializedValue = offheapAddress.getSerializedValue();
-
- Object actualRegionEntryValue = convertSerializedByteArrayToObject(actualSerializedValue);
-
- assertEquals(regionEntryValue, actualRegionEntryValue);
- }
-
- @Test
- public void getDeserializedObjectShouldReturnADeserializedObject() {
- Object regionEntryValue = getValue();
-
- ObjectStoredAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
-
- Integer actualRegionEntryValue = (Integer) offheapAddress.getDeserializedValue(null, null);
-
- assertEquals(regionEntryValue, actualRegionEntryValue);
- }
-
- @Test
- public void getDeserializedObjectShouldReturnAByteArrayAsIsIfNotSerialized() {
- byte[] regionEntryValueAsBytes = getValueAsByteArray();
-
- ObjectStoredAsAddress offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes);
-
- byte[] deserializeValue = (byte[]) offheapAddress.getDeserializedValue(null, null);
-
- assertArrayEquals(regionEntryValueAsBytes, deserializeValue);
- }
-
- @Test
- public void fillSerializedValueShouldFillWrapperWithSerializedValueIfValueIsSerialized() {
- Object regionEntryValue = getValue();
- byte[] serializedRegionEntryValue = EntryEventImpl.serialize(regionEntryValue);
-
- //encode a serialized entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedRegionEntryValue, true, false);
-
- ObjectStoredAsAddress offheapAddress = new ObjectStoredAsAddress(encodedAddress);
-
- //mock the things
- BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
-
- byte userBits = 1;
- offheapAddress.fillSerializedValue(wrapper, userBits);
-
- verify(wrapper, times(1)).setData(serializedRegionEntryValue, userBits, serializedRegionEntryValue.length, true);
- }
-
- @Test
- public void fillSerializedValueShouldFillWrapperWithDeserializedValueIfValueIsNotSerialized() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- //encode a un serialized entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, false);
-
- ObjectStoredAsAddress offheapAddress = new ObjectStoredAsAddress(encodedAddress);
-
- //mock the things
- BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
-
- byte userBits = 1;
- offheapAddress.fillSerializedValue(wrapper, userBits);
-
- verify(wrapper, times(1)).setData(regionEntryValueAsBytes, userBits, regionEntryValueAsBytes.length, true);
- }
-
- @Test
- public void getStringFormShouldCatchExceptionAndReturnErrorMessageAsString() {
- Object regionEntryValueAsBytes = getValue();
-
- byte[] serializedValue = EntryEventImpl.serialize(regionEntryValueAsBytes);
-
- //store -127 (DSCODE.ILLEGAL) - in order the deserialize to throw exception
- serializedValue[0] = -127;
-
- //encode a un serialized entry value to address
- long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedValue, true, false);
-
- ObjectStoredAsAddress offheapAddress = new ObjectStoredAsAddress(encodedAddress);
-
- String errorMessage = offheapAddress.getStringForm();
-
- assertEquals(true, errorMessage.contains("Could not convert object to string because "));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
index 1b6bd02..ad6fa35 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
@@ -341,7 +341,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
reset(re);
// testing when the newValue is DataAsAddress
- ObjectStoredAsAddress newAddress1 = new ObjectStoredAsAddress(2L);
+ TinyStoredObject newAddress1 = new TinyStoredObject(2L);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
when(re.setAddress(oldAddress, newAddress1.getAddress())).thenReturn(Boolean.TRUE);
@@ -472,7 +472,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
long oldAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(oldData, false, false);
byte[] newData = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) Integer.MAX_VALUE - 1).array();
- ObjectStoredAsAddress newAddress = new ObjectStoredAsAddress(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false));
+ TinyStoredObject newAddress = new TinyStoredObject(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false));
// mock Chunk static methods - in-order to verify that release is never called
PowerMockito.spy(ObjectStoredInMemory.class);
@@ -695,10 +695,10 @@ public class OffHeapRegionEntryHelperJUnitTest {
byte[] data = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt(Integer.MAX_VALUE).array();
long address = OffHeapRegionEntryHelper.encodeDataAsAddress(data, false, false);
- ObjectStoredAsAddress expected = new ObjectStoredAsAddress(address);
+ TinyStoredObject expected = new TinyStoredObject(address);
Object actual = OffHeapRegionEntryHelper.addressToObject(address, false, null);
- assertThat(actual).isInstanceOf(ObjectStoredAsAddress.class);
+ assertThat(actual).isInstanceOf(TinyStoredObject.class);
assertThat(actual).isEqualTo(expected);
}
@@ -795,7 +795,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
boolean isCompressed = true;
long address = OffHeapRegionEntryHelper.encodeDataAsAddress(data, isSerialized, isCompressed);
- ObjectStoredAsAddress daa = new ObjectStoredAsAddress(address);
+ TinyStoredObject daa = new TinyStoredObject(address);
int actualLength = OffHeapRegionEntryHelper.getSerializedLengthFromDataAsAddress(daa);
@@ -804,7 +804,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
@Test
public void getSerializedLengthFromDataAsAddressShouldReturnZeroForNonEncodedAddress() {
- ObjectStoredAsAddress nonEncodedAddress = new ObjectStoredAsAddress(100000L);
+ TinyStoredObject nonEncodedAddress = new TinyStoredObject(100000L);
int actualLength = OffHeapRegionEntryHelper.getSerializedLengthFromDataAsAddress(nonEncodedAddress);
assertThat(actualLength).isZero();
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
index 1bc028b..eb60d1d 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
@@ -76,7 +76,7 @@ public class OffHeapWriteObjectAsByteArrayJUnitTest {
public void testByteArrayDataAsAddress() throws IOException, ClassNotFoundException {
byte[] expected = new byte[] {1, 2, 3};
StoredObject so = createStoredObject(expected, false, false);
- assertTrue(so instanceof ObjectStoredAsAddress);
+ assertTrue(so instanceof TinyStoredObject);
HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
DataSerializer.writeObjectAsByteArray(so, hdos);
DataInputStream in = createInput(hdos);
@@ -101,7 +101,7 @@ public class OffHeapWriteObjectAsByteArrayJUnitTest {
public void testStringDataAsAddress() throws IOException, ClassNotFoundException {
byte[] expected = EntryEventImpl.serialize("1234");
StoredObject so = createStoredObject(expected, true, false);
- assertTrue(so instanceof ObjectStoredAsAddress);
+ assertTrue(so instanceof TinyStoredObject);
HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
DataSerializer.writeObjectAsByteArray(so, hdos);
DataInputStream in = createInput(hdos);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/StoredObjectTestSuite.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/StoredObjectTestSuite.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/StoredObjectTestSuite.java
index b682800..35d5cc2 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/StoredObjectTestSuite.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/StoredObjectTestSuite.java
@@ -21,7 +21,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@Suite.SuiteClasses({
- ObjectStoredAsAddressJUnitTest.class,
+ TinyStoredObjectJUnitTest.class,
ObjectChunkJUnitTest.class,
ObjectChunkWithHeapFormJUnitTest.class,
ObjectChunkSliceJUnitTest.class,
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/bccc02c2/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java
new file mode 100644
index 0000000..94559d6
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java
@@ -0,0 +1,353 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.gemstone.gemfire.internal.offheap;
+
+import com.gemstone.gemfire.compression.Compressor;
+import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor;
+import com.gemstone.gemfire.internal.cache.CachePerfStats;
+import com.gemstone.gemfire.internal.cache.EntryEventImpl;
+import com.gemstone.gemfire.internal.cache.RegionEntryContext;
+import com.gemstone.gemfire.internal.offheap.TinyStoredObject;
+
+import com.gemstone.gemfire.internal.offheap.OffHeapRegionEntryHelper;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.mockito.Mock;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.*;
+
+@Category(UnitTest.class)
+public class TinyStoredObjectJUnitTest extends AbstractStoredObjectTestBase {
+
+ @Override
+ public Object getValue() {
+ return Integer.valueOf(123456789);
+ }
+
+ @Override
+ public byte[] getValueAsByteArray() {
+ return convertValueToByteArray(getValue());
+ }
+
+ private byte[] convertValueToByteArray(Object value) {
+ return ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) value).array();
+ }
+
+ @Override
+ public Object convertByteArrayToObject(byte[] valueInByteArray) {
+ return ByteBuffer.wrap(valueInByteArray).getInt();
+ }
+
+ @Override
+ public Object convertSerializedByteArrayToObject(byte[] valueInSerializedByteArray) {
+ return EntryEventImpl.deserialize(valueInSerializedByteArray);
+ }
+
+ @Override
+ public TinyStoredObject createValueAsUnserializedStoredObject(Object value) {
+ byte[] valueInByteArray;
+ if(value instanceof Integer) {
+ valueInByteArray = convertValueToByteArray(value);
+ } else {
+ valueInByteArray = (byte[]) value;
+ }
+ //encode a non-serialized entry value to address
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInByteArray, false, 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);
+ }
+
+ public TinyStoredObject createValueAsCompressedStoredObject(Object value) {
+ byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
+ //encode a serialized, compressed entry value to address
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, true);
+ return new TinyStoredObject(encodedAddress);
+ }
+
+ 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);
+ }
+
+ @Test
+ public void shouldReturnCorrectEncodingAddress() {
+
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ assertNotNull(address1);
+ assertEquals("Encoding address should be:", 10001, address1.getAddress());
+
+ TinyStoredObject address2 = new TinyStoredObject(10002L);
+ assertNotNull(address2);
+ assertEquals("Returning always 10001 expected 10002", 10002, address2.getAddress());
+ }
+
+ @Test
+ public void twoAddressesShouldBeEqualIfEncodingAddressIsSame() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ TinyStoredObject address2 = new TinyStoredObject(10001L);
+
+ assertEquals("Two addresses are equal if encoding address is same", true, address1.equals(address2));
+ }
+
+ @Test
+ public void twoAddressesShouldNotBeEqualIfEncodingAddressIsNotSame() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ TinyStoredObject address2 = new TinyStoredObject(10002L);
+
+ assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2));
+ }
+
+ @Test
+ public void twoAddressesAreNotEqualIfTheyAreNotTypeDataAsAddress() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ Long address2 = new Long(10002L);
+
+ assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2));
+ }
+
+ @Test
+ public void addressHashCodeShouldBe() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ assertEquals("", 10001, address1.hashCode());
+ }
+
+ @Test
+ public void getSizeInBytesAlwaysReturnsZero() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ TinyStoredObject address2 = new TinyStoredObject(10002L);
+
+ assertEquals("getSizeInBytes", 0, address1.getSizeInBytes());
+ assertEquals("getSizeInBytes", 0, address2.getSizeInBytes());
+ }
+
+ @Test
+ public void getValueSizeInBytesAlwaysReturnsZero() {
+ TinyStoredObject address1 = new TinyStoredObject(10001L);
+ TinyStoredObject address2 = new TinyStoredObject(10002L);
+
+ assertEquals("getSizeInBytes", 0, address1.getValueSizeInBytes());
+ assertEquals("getSizeInBytes", 0, address2.getValueSizeInBytes());
+ }
+
+ @Test
+ public void isCompressedShouldReturnTrueIfCompressed() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsCompressedStoredObject(regionEntryValue);
+
+ assertEquals("Should return true as it is compressed", true, offheapAddress.isCompressed());
+ }
+
+ @Test
+ public void isCompressedShouldReturnFalseIfNotCompressed() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue);
+
+ assertEquals("Should return false as it is compressed", false, offheapAddress.isCompressed());
+ }
+
+ @Test
+ public void isSerializedShouldReturnTrueIfSeriazlied() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
+
+ assertEquals("Should return true as it is serialized", true, offheapAddress.isSerialized());
+ }
+
+ @Test
+ public void isSerializedShouldReturnFalseIfNotSeriazlied() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValue);
+
+ assertEquals("Should return false as it is serialized", false, offheapAddress.isSerialized());
+ }
+
+ @Test
+ public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ //encode a non-serialized and compressed entry value to address - last argument is to let that it is compressed
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, true);
+ TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress);
+
+ RegionEntryContext regionContext = mock(RegionEntryContext.class);
+ CachePerfStats cacheStats = mock(CachePerfStats.class);
+ Compressor compressor = mock(Compressor.class);
+
+ long startTime = 10000L;
+
+ //mock required things
+ when(regionContext.getCompressor()).thenReturn(compressor);
+ when(compressor.decompress(regionEntryValueAsBytes)).thenReturn(regionEntryValueAsBytes);
+ when(regionContext.getCachePerfStats()).thenReturn(cacheStats);
+ when(cacheStats.startDecompression()).thenReturn(startTime);
+
+ //invoke the thing
+ byte[] bytes = offheapAddress.getDecompressedBytes(regionContext);
+
+ //verify the thing happened
+ verify(cacheStats, atLeastOnce()).startDecompression();
+ verify(compressor, times(1)).decompress(regionEntryValueAsBytes);
+ verify(cacheStats, atLeastOnce()).endDecompression(startTime);
+
+ assertArrayEquals(regionEntryValueAsBytes, bytes);
+ }
+
+ @Test
+ public void getDecompressedBytesShouldNotTryToDecompressIfNotCompressed() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue);
+
+ //mock the thing
+ RegionEntryContext regionContext = mock(RegionEntryContext.class);
+ Compressor compressor = mock(Compressor.class);
+ when(regionContext.getCompressor()).thenReturn(compressor);
+
+ //invoke the thing
+ byte[] actualValueInBytes = offheapAddress.getDecompressedBytes(regionContext);
+
+ //createValueAsUncompressedStoredObject does uses a serialized value - so convert it to object
+ Object actualRegionValue = convertSerializedByteArrayToObject(actualValueInBytes);
+
+ //verify the thing happened
+ verify(regionContext, never()).getCompressor();
+ assertEquals(regionEntryValue, actualRegionValue);
+ }
+
+ @Test
+ public void getRawBytesShouldReturnAByteArray() {
+ byte[] regionEntryValueAsBytes = getValueAsByteArray();
+
+ TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes);
+ byte[] actual = offheapAddress.getRawBytes();
+
+ assertArrayEquals(regionEntryValueAsBytes, actual);
+ }
+
+ @Test
+ public void getSerializedValueShouldReturnASerializedByteArray() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
+
+ byte[] actualSerializedValue = offheapAddress.getSerializedValue();
+
+ Object actualRegionEntryValue = convertSerializedByteArrayToObject(actualSerializedValue);
+
+ assertEquals(regionEntryValue, actualRegionEntryValue);
+ }
+
+ @Test
+ public void getDeserializedObjectShouldReturnADeserializedObject() {
+ Object regionEntryValue = getValue();
+
+ TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue);
+
+ Integer actualRegionEntryValue = (Integer) offheapAddress.getDeserializedValue(null, null);
+
+ assertEquals(regionEntryValue, actualRegionEntryValue);
+ }
+
+ @Test
+ public void getDeserializedObjectShouldReturnAByteArrayAsIsIfNotSerialized() {
+ byte[] regionEntryValueAsBytes = getValueAsByteArray();
+
+ TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes);
+
+ byte[] deserializeValue = (byte[]) offheapAddress.getDeserializedValue(null, null);
+
+ assertArrayEquals(regionEntryValueAsBytes, deserializeValue);
+ }
+
+ @Test
+ public void fillSerializedValueShouldFillWrapperWithSerializedValueIfValueIsSerialized() {
+ Object regionEntryValue = getValue();
+ byte[] serializedRegionEntryValue = EntryEventImpl.serialize(regionEntryValue);
+
+ //encode a serialized entry value to address
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedRegionEntryValue, true, false);
+
+ TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress);
+
+ //mock the things
+ BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
+
+ byte userBits = 1;
+ offheapAddress.fillSerializedValue(wrapper, userBits);
+
+ verify(wrapper, times(1)).setData(serializedRegionEntryValue, userBits, serializedRegionEntryValue.length, true);
+ }
+
+ @Test
+ public void fillSerializedValueShouldFillWrapperWithDeserializedValueIfValueIsNotSerialized() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ //encode a un serialized entry value to address
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, false);
+
+ TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress);
+
+ //mock the things
+ BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
+
+ byte userBits = 1;
+ offheapAddress.fillSerializedValue(wrapper, userBits);
+
+ verify(wrapper, times(1)).setData(regionEntryValueAsBytes, userBits, regionEntryValueAsBytes.length, true);
+ }
+
+ @Test
+ public void getStringFormShouldCatchExceptionAndReturnErrorMessageAsString() {
+ Object regionEntryValueAsBytes = getValue();
+
+ byte[] serializedValue = EntryEventImpl.serialize(regionEntryValueAsBytes);
+
+ //store -127 (DSCODE.ILLEGAL) - in order the deserialize to throw exception
+ serializedValue[0] = -127;
+
+ //encode a un serialized entry value to address
+ long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedValue, true, false);
+
+ TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress);
+
+ String errorMessage = offheapAddress.getStringForm();
+
+ assertEquals(true, errorMessage.contains("Could not convert object to string because "));
+ }
+}