Return-Path: X-Original-To: apmail-geode-commits-archive@minotaur.apache.org Delivered-To: apmail-geode-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 621D81809F for ; Sat, 5 Mar 2016 00:52:29 +0000 (UTC) Received: (qmail 33204 invoked by uid 500); 5 Mar 2016 00:52:29 -0000 Delivered-To: apmail-geode-commits-archive@geode.apache.org Received: (qmail 33174 invoked by uid 500); 5 Mar 2016 00:52:29 -0000 Mailing-List: contact commits-help@geode.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.incubator.apache.org Delivered-To: mailing list commits@geode.incubator.apache.org Received: (qmail 33165 invoked by uid 99); 5 Mar 2016 00:52:29 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 05 Mar 2016 00:52:29 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id C10701806E2 for ; Sat, 5 Mar 2016 00:52:28 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -3.549 X-Spam-Level: X-Spam-Status: No, score=-3.549 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.329] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id sd7WR_6hpbdH for ; Sat, 5 Mar 2016 00:52:19 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 2E35E5FBC3 for ; Sat, 5 Mar 2016 00:52:17 +0000 (UTC) Received: (qmail 29068 invoked by uid 99); 5 Mar 2016 00:51:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 05 Mar 2016 00:51:01 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6356EDFF93; Sat, 5 Mar 2016 00:51:01 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dschneider@apache.org To: commits@geode.incubator.apache.org Date: Sat, 05 Mar 2016 00:51:13 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [13/38] incubator-geode git commit: renamed DataAsAddress to ObjectStoredAsAddress renamed DataAsAddress to ObjectStoredAsAddress Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/8246ee5e Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/8246ee5e Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/8246ee5e Branch: refs/heads/feature/GEODE-982 Commit: 8246ee5e298a8df0f27ff2561711e4071634d551 Parents: e700059 Author: Darrel Schneider Authored: Fri Feb 26 17:27:32 2016 -0800 Committer: Darrel Schneider Committed: Fri Feb 26 17:27:32 2016 -0800 ---------------------------------------------------------------------- .../internal/cache/tier/sockets/Part.java | 6 +- .../gemfire/internal/offheap/DataAsAddress.java | 131 ------- .../internal/offheap/ObjectStoredAsAddress.java | 138 +++++++ .../offheap/OffHeapRegionEntryHelper.java | 6 +- .../offheap/SimpleMemoryAllocatorImpl.java | 2 +- .../cache/OldValueImporterTestBase.java | 6 +- .../offheap/DataAsAddressJUnitTest.java | 368 ------------------- .../offheap/ObjectStoredAsAddressJUnitTest.java | 368 +++++++++++++++++++ .../OffHeapRegionEntryHelperJUnitTest.java | 12 +- .../OffHeapWriteObjectAsByteArrayJUnitTest.java | 4 +- .../internal/offheap/StoredObjectTestSuite.java | 2 +- 11 files changed, 525 insertions(+), 518 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java index 36c7cdc..52e3488 100755 --- a/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java +++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/Part.java @@ -20,7 +20,7 @@ import com.gemstone.gemfire.internal.*; import com.gemstone.gemfire.internal.cache.CachedDeserializable; import com.gemstone.gemfire.internal.offheap.ObjectChunk; import com.gemstone.gemfire.internal.offheap.AddressableMemoryManager; -import com.gemstone.gemfire.internal.offheap.DataAsAddress; +import com.gemstone.gemfire.internal.offheap.ObjectStoredAsAddress; import com.gemstone.gemfire.internal.offheap.StoredObject; import java.io.*; @@ -128,8 +128,8 @@ public class Part { } else { this.typeCode = BYTE_CODE; } - if (so instanceof DataAsAddress) { - this.part = ((DataAsAddress)so).getRawBytes(); + if (so instanceof ObjectStoredAsAddress) { + this.part = ((ObjectStoredAsAddress)so).getRawBytes(); } else { this.part = (ObjectChunk)so; } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/DataAsAddress.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/DataAsAddress.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/DataAsAddress.java deleted file mode 100644 index 96957ac..0000000 --- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/DataAsAddress.java +++ /dev/null @@ -1,131 +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.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 offheap addresses whose - * value encodes actual data instead a memory - * location. - * Instances of this class have a very short lifetime. - */ -public class DataAsAddress extends AbstractStoredObject { - private final long address; - - public DataAsAddress(long addr) { - this.address = addr; - } - - public long getEncodedAddress() { - return this.address; - } - - @Override - public boolean equals(Object o) { - if (o instanceof DataAsAddress) { - return getEncodedAddress() == ((DataAsAddress) o).getEncodedAddress(); - } - return false; - } - - @Override - public int hashCode() { - long value = getEncodedAddress(); - return (int)(value ^ (value >>> 32)); - } - - @Override - public int getSizeInBytes() { - return 0; - } - - public byte[] getDecompressedBytes(RegionEntryContext r) { - byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToBytes(getEncodedAddress(), 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(getEncodedAddress(), 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 boolean retain() { - // nothing needed - return true; - } - @Override - public void release() { - // nothing needed - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/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 new file mode 100644 index 0000000..9acd1f7 --- /dev/null +++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddress.java @@ -0,0 +1,138 @@ +/* + * 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.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; + } + + public long getEncodedAddress() { + return this.address; + } + + @Override + public boolean equals(Object o) { + if (o instanceof ObjectStoredAsAddress) { + return getEncodedAddress() == ((ObjectStoredAsAddress) o).getEncodedAddress(); + } + return false; + } + + @Override + public int hashCode() { + long value = getEncodedAddress(); + return (int)(value ^ (value >>> 32)); + } + + @Override + public int getSizeInBytes() { + return 0; + } + + public byte[] getDecompressedBytes(RegionEntryContext r) { + byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToBytes(getEncodedAddress(), 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(getEncodedAddress(), 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 boolean retain() { + // nothing needed + return true; + } + @Override + public void release() { + // nothing needed + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/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 7b7efd0..5a9cf70 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 @@ -64,7 +64,7 @@ public class OffHeapRegionEntryHelper { private static long objectToAddress(@Unretained Object v) { if (v instanceof ObjectChunk) return ((ObjectChunk) v).getMemoryAddress(); - if (v instanceof DataAsAddress) return ((DataAsAddress) v).getEncodedAddress(); + if (v instanceof ObjectStoredAsAddress) return ((ObjectStoredAsAddress) v).getEncodedAddress(); if (v == null) return NULL_ADDRESS; if (v == Token.TOMBSTONE) return TOMBSTONE_ADDRESS; if (v == Token.INVALID) return INVALID_ADDRESS; @@ -113,7 +113,7 @@ public class OffHeapRegionEntryHelper { } return result; } else if ((ohAddress & ENCODED_BIT) != 0) { - DataAsAddress daa = new DataAsAddress(ohAddress); + ObjectStoredAsAddress daa = new ObjectStoredAsAddress(ohAddress); Object result = daa; if (decompress && daa.isCompressed()) { byte[] decompressedBytes = daa.getDecompressedBytes(context); @@ -131,7 +131,7 @@ public class OffHeapRegionEntryHelper { } } - public static int getSerializedLengthFromDataAsAddress(DataAsAddress dataAsAddress) { + public static int getSerializedLengthFromDataAsAddress(ObjectStoredAsAddress dataAsAddress) { final long ohAddress = dataAsAddress.getEncodedAddress(); if ((ohAddress & ENCODED_BIT) != 0) { http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/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 e59ce9d..1c03fc0 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 @@ -326,7 +326,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator { public StoredObject allocateAndInitialize(byte[] v, boolean isSerialized, boolean isCompressed) { long addr = OffHeapRegionEntryHelper.encodeDataAsAddress(v, isSerialized, isCompressed); if (addr != 0L) { - return new DataAsAddress(addr); + return new ObjectStoredAsAddress(addr); } ObjectChunk 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/8246ee5e/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 d9fc883..c9eec45 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.ObjectChunk; -import com.gemstone.gemfire.internal.offheap.DataAsAddress; +import com.gemstone.gemfire.internal.offheap.ObjectStoredAsAddress; 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}; - DataAsAddress baValueSO = (DataAsAddress) sma.allocateAndInitialize(baValue, false, false); + ObjectStoredAsAddress baValueSO = (ObjectStoredAsAddress) 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); - DataAsAddress baValueSO = (DataAsAddress) sma.allocateAndInitialize(baValueBlob, true, false); + ObjectStoredAsAddress baValueSO = (ObjectStoredAsAddress) 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/8246ee5e/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/DataAsAddressJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/DataAsAddressJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/DataAsAddressJUnitTest.java deleted file mode 100644 index 8a251fd..0000000 --- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/DataAsAddressJUnitTest.java +++ /dev/null @@ -1,368 +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.DataAsAddress; - -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 DataAsAddressJUnitTest 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 DataAsAddress 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 DataAsAddress(encodedAddress); - } - - @Override - public DataAsAddress createValueAsSerializedStoredObject(Object value) { - byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); - //encode a serialized entry value to address - long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false); - return new DataAsAddress(encodedAddress); - } - - public DataAsAddress 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 DataAsAddress(encodedAddress); - } - - public DataAsAddress 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 DataAsAddress(encodedAddress); - } - - @Test - public void shouldReturnCorrectEncodingAddress() { - - DataAsAddress address1 = new DataAsAddress(10001L); - assertNotNull(address1); - assertEquals("Encoding address should be:", 10001, address1.getEncodedAddress()); - - DataAsAddress address2 = new DataAsAddress(10002L); - assertNotNull(address2); - assertEquals("Returning always 10001 expected 10002", 10002, address2.getEncodedAddress()); - } - - @Test - public void twoAddressesShouldBeEqualIfEncodingAddressIsSame() { - DataAsAddress address1 = new DataAsAddress(10001L); - DataAsAddress address2 = new DataAsAddress(10001L); - - assertEquals("Two addresses are equal if encoding address is same", true, address1.equals(address2)); - } - - @Test - public void twoAddressesShouldNotBeEqualIfEncodingAddressIsNotSame() { - DataAsAddress address1 = new DataAsAddress(10001L); - DataAsAddress address2 = new DataAsAddress(10002L); - - assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2)); - } - - @Test - public void twoAddressesAreNotEqualIfTheyAreNotTypeDataAsAddress() { - DataAsAddress address1 = new DataAsAddress(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() { - DataAsAddress address1 = new DataAsAddress(10001L); - assertEquals("", 10001, address1.hashCode()); - } - - @Test - public void getSizeInBytesAlwaysReturnsZero() { - DataAsAddress address1 = new DataAsAddress(10001L); - DataAsAddress address2 = new DataAsAddress(10002L); - - assertEquals("getSizeInBytes", 0, address1.getSizeInBytes()); - assertEquals("getSizeInBytes", 0, address2.getSizeInBytes()); - } - - @Test - public void getValueSizeInBytesAlwaysReturnsZero() { - DataAsAddress address1 = new DataAsAddress(10001L); - DataAsAddress address2 = new DataAsAddress(10002L); - - assertEquals("getSizeInBytes", 0, address1.getValueSizeInBytes()); - assertEquals("getSizeInBytes", 0, address2.getValueSizeInBytes()); - } - - @Test - public void retainShouldAlwaysBeTrue() { - DataAsAddress address1 = new DataAsAddress(10001L); - DataAsAddress address2 = new DataAsAddress(10002L); - - assertEquals("retain", true, address1.retain()); - assertEquals("retain", true, address2.retain()); - } - - @Test - public void dataAsAddressShouldImplementReleaseToAdhereToStoredObject() { - DataAsAddress address = new DataAsAddress(10001L); - address.release(); - } - - @Test - public void isCompressedShouldReturnTrueIfCompressed() { - Object regionEntryValue = getValue(); - - DataAsAddress offheapAddress = createValueAsCompressedStoredObject(regionEntryValue); - - assertEquals("Should return true as it is compressed", true, offheapAddress.isCompressed()); - } - - @Test - public void isCompressedShouldReturnFalseIfNotCompressed() { - Object regionEntryValue = getValue(); - - DataAsAddress offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue); - - assertEquals("Should return false as it is compressed", false, offheapAddress.isCompressed()); - } - - @Test - public void isSerializedShouldReturnTrueIfSeriazlied() { - Object regionEntryValue = getValue(); - - DataAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); - - assertEquals("Should return true as it is serialized", true, offheapAddress.isSerialized()); - } - - @Test - public void isSerializedShouldReturnFalseIfNotSeriazlied() { - Object regionEntryValue = getValue(); - - DataAsAddress 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); - DataAsAddress offheapAddress = new DataAsAddress(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(); - - DataAsAddress 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(); - - DataAsAddress offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes); - byte[] actual = offheapAddress.getRawBytes(); - - assertArrayEquals(regionEntryValueAsBytes, actual); - } - - @Test - public void getSerializedValueShouldReturnASerializedByteArray() { - Object regionEntryValue = getValue(); - - DataAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); - - byte[] actualSerializedValue = offheapAddress.getSerializedValue(); - - Object actualRegionEntryValue = convertSerializedByteArrayToObject(actualSerializedValue); - - assertEquals(regionEntryValue, actualRegionEntryValue); - } - - @Test - public void getDeserializedObjectShouldReturnADeserializedObject() { - Object regionEntryValue = getValue(); - - DataAsAddress offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); - - Integer actualRegionEntryValue = (Integer) offheapAddress.getDeserializedValue(null, null); - - assertEquals(regionEntryValue, actualRegionEntryValue); - } - - @Test - public void getDeserializedObjectShouldReturnAByteArrayAsIsIfNotSerialized() { - byte[] regionEntryValueAsBytes = getValueAsByteArray(); - - DataAsAddress 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); - - DataAsAddress offheapAddress = new DataAsAddress(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); - - DataAsAddress offheapAddress = new DataAsAddress(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); - - DataAsAddress offheapAddress = new DataAsAddress(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/8246ee5e/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 new file mode 100644 index 0000000..9d52c59 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectStoredAsAddressJUnitTest.java @@ -0,0 +1,368 @@ +/* + * 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.getEncodedAddress()); + + ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L); + assertNotNull(address2); + assertEquals("Returning always 10001 expected 10002", 10002, address2.getEncodedAddress()); + } + + @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 retainShouldAlwaysBeTrue() { + ObjectStoredAsAddress address1 = new ObjectStoredAsAddress(10001L); + ObjectStoredAsAddress address2 = new ObjectStoredAsAddress(10002L); + + assertEquals("retain", true, address1.retain()); + assertEquals("retain", true, address2.retain()); + } + + @Test + public void dataAsAddressShouldImplementReleaseToAdhereToStoredObject() { + ObjectStoredAsAddress address = new ObjectStoredAsAddress(10001L); + address.release(); + } + + @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/8246ee5e/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 5d53109..34e418e 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 - DataAsAddress newAddress1 = new DataAsAddress(2L); + ObjectStoredAsAddress newAddress1 = new ObjectStoredAsAddress(2L); // mock region entry methods required for test when(re.getAddress()).thenReturn(oldAddress); when(re.setAddress(oldAddress, newAddress1.getEncodedAddress())).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(); - DataAsAddress newAddress = new DataAsAddress(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false)); + ObjectStoredAsAddress newAddress = new ObjectStoredAsAddress(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false)); // mock Chunk static methods - in-order to verify that release is never called PowerMockito.spy(ObjectChunk.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); - DataAsAddress expected = new DataAsAddress(address); + ObjectStoredAsAddress expected = new ObjectStoredAsAddress(address); Object actual = OffHeapRegionEntryHelper.addressToObject(address, false, null); - assertThat(actual).isInstanceOf(DataAsAddress.class); + assertThat(actual).isInstanceOf(ObjectStoredAsAddress.class); assertThat(actual).isEqualTo(expected); } @@ -795,7 +795,7 @@ public class OffHeapRegionEntryHelperJUnitTest { boolean isCompressed = true; long address = OffHeapRegionEntryHelper.encodeDataAsAddress(data, isSerialized, isCompressed); - DataAsAddress daa = new DataAsAddress(address); + ObjectStoredAsAddress daa = new ObjectStoredAsAddress(address); int actualLength = OffHeapRegionEntryHelper.getSerializedLengthFromDataAsAddress(daa); @@ -804,7 +804,7 @@ public class OffHeapRegionEntryHelperJUnitTest { @Test public void getSerializedLengthFromDataAsAddressShouldReturnZeroForNonEncodedAddress() { - DataAsAddress nonEncodedAddress = new DataAsAddress(100000L); + ObjectStoredAsAddress nonEncodedAddress = new ObjectStoredAsAddress(100000L); int actualLength = OffHeapRegionEntryHelper.getSerializedLengthFromDataAsAddress(nonEncodedAddress); assertThat(actualLength).isZero(); } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/8246ee5e/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 8ef2d42..e93b5a1 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 DataAsAddress); + assertTrue(so instanceof ObjectStoredAsAddress); 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 DataAsAddress); + assertTrue(so instanceof ObjectStoredAsAddress); 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/8246ee5e/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 feb5de8..b682800 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({ - DataAsAddressJUnitTest.class, + ObjectStoredAsAddressJUnitTest.class, ObjectChunkJUnitTest.class, ObjectChunkWithHeapFormJUnitTest.class, ObjectChunkSliceJUnitTest.class,