geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sai_boorlaga...@apache.org
Subject [10/50] [abbrv] incubator-geode git commit: GEODE-1451: refactor decodeAddressToBytes into two methods
Date Tue, 31 May 2016 21:25:40 GMT
GEODE-1451: refactor decodeAddressToBytes into two methods

The boolean parameters have been removed and two
methods now exist:
1. decodeAddressToRawBytes(long)
2. decodeUncompressedAddressToBytes(long)


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/f73f2525
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/f73f2525
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/f73f2525

Branch: refs/heads/feature/GEODE-93
Commit: f73f25251feccb61b3c3699bf98ab7d5a90edacd
Parents: cae188c
Author: Darrel Schneider <dschneider@pivotal.io>
Authored: Tue May 24 17:22:54 2016 -0700
Committer: Darrel Schneider <dschneider@pivotal.io>
Committed: Thu May 26 11:23:48 2016 -0700

----------------------------------------------------------------------
 .../offheap/OffHeapRegionEntryHelper.java       | 24 +++++++----
 .../internal/offheap/TinyStoredObject.java      |  9 ++--
 .../OffHeapRegionEntryHelperJUnitTest.java      | 45 +++++++++++++++++---
 3 files changed, 59 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f73f2525/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 8b57c43..0909306 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
@@ -261,7 +261,7 @@ public class OffHeapRegionEntryHelper {
   }
 
   static Object decodeAddressToObject(long ohAddress) {
-      byte[] bytes = decodeAddressToBytes(ohAddress, true, false);
+      byte[] bytes = decodeUncompressedAddressToBytes(ohAddress);
 
       boolean isSerialized = (ohAddress & SERIALIZED_BIT) != 0;
       if (isSerialized) {
@@ -280,9 +280,22 @@ public class OffHeapRegionEntryHelper {
     return (int) ((addr & SIZE_MASK) >> SIZE_SHIFT);
   }
   
-  static byte[] decodeAddressToBytes(long addr, boolean decompress, boolean compressedOk)
{
+  /**
+   * Returns the bytes encoded in the given address.
+   * Note that compressed addresses are not supported by this method.
+   * @throws UnsupportedOperationException if the address has compressed data
+   */
+  static byte[] decodeUncompressedAddressToBytes(long addr) {
+    assert (addr & COMPRESSED_BIT) == 0 : "Did not expect encoded address to be compressed";
+    return decodeAddressToRawBytes(addr);
+  }
+  
+  /**
+   * Returns the "raw" bytes that have been encoded in the given address.
+   * Note that if address is compressed then the raw bytes are the compressed bytes.
+   */
+  static byte[] decodeAddressToRawBytes(long addr) {
     assert (addr & ENCODED_BIT) != 0;
-    boolean isCompressed = (addr & COMPRESSED_BIT) != 0;
     int size = (int) ((addr & SIZE_MASK) >> SIZE_SHIFT);
     boolean isLong = (addr & LONG_BIT) != 0;
     byte[] bytes;
@@ -305,11 +318,6 @@ public class OffHeapRegionEntryHelper {
         bytes[i] = (byte) (addr & 0x00ff);
       }
     }
-    if (decompress && isCompressed) {
-      if (!compressedOk) {
-        throw new UnsupportedOperationException("Did not expect encoded address to be compressed");
-      }
-    }
     return bytes;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f73f2525/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
index aee1b15..9404865 100644
--- 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
@@ -74,13 +74,14 @@ public class TinyStoredObject extends AbstractStoredObject {
   }
 
   public byte[] getDecompressedBytes(RegionEntryContext r) {
-    byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, true);
     if (isCompressed()) {
+        byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToRawBytes(getAddress());
         long time = r.getCachePerfStats().startDecompression();
         bytes = r.getCompressor().decompress(bytes);
         r.getCachePerfStats().endDecompression(time);
+        return bytes;
     }
-    return bytes;
+    return getRawBytes();
   }
 
   /**
@@ -88,12 +89,12 @@ public class TinyStoredObject extends AbstractStoredObject {
    * Otherwise return the serialize bytes in us in a byte array.
    */
   public byte[] getRawBytes() {
-    return OffHeapRegionEntryHelper.decodeAddressToBytes(getAddress(), true, false);
+    return OffHeapRegionEntryHelper.decodeUncompressedAddressToBytes(getAddress());
   }
   
   @Override
   public byte[] getSerializedValue() {
-    byte[] value = OffHeapRegionEntryHelper.decodeAddressToBytes(this.address, true, false);
+    byte[] value = getRawBytes();
     if (!isSerialized()) {
       value = EntryEventImpl.serialize(value);
     }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f73f2525/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 b9b3dfc..7190e8b 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
@@ -22,7 +22,9 @@ import static org.mockito.Mockito.*;
 import java.nio.ByteBuffer;
 
 import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
@@ -53,6 +55,25 @@ public class OffHeapRegionEntryHelperJUnitTest {
 
   private static final Long VALUE_IS_NOT_ENCODABLE = 0L;
 
+  private static Boolean assertionsEnabled;
+
+  @BeforeClass
+  public static void setUpOnce() {
+    try {
+      assert false;
+      assertionsEnabled = false;
+    } catch (AssertionError e) {
+      assertionsEnabled = true;
+    }
+    ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
+    System.out.println("assertionsEnabled = " + assertionsEnabled);
+  }
+
+  @AfterClass
+  public static void tearDownOnce() {
+    ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(assertionsEnabled);
+  }
+
   private MemoryAllocator ma;
 
   @Before
@@ -208,31 +229,41 @@ public class OffHeapRegionEntryHelperJUnitTest {
   }
 
   @Test
-  public void decodeAddressToBytesShouldReturnActualBytes() {
+  public void decodeUncompressedAddressToBytesShouldReturnActualBytes() {
     long encodedAddress = 549755813697L;
     Integer value = Integer.MAX_VALUE;
 
-    byte[] actual = OffHeapRegionEntryHelper.decodeAddressToBytes(encodedAddress, false,
false);
+    byte[] actual = OffHeapRegionEntryHelper.decodeUncompressedAddressToBytes(encodedAddress);
     byte[] expectedValue = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt(value).array();
 
     assertThat(actual).isEqualTo(expectedValue);
   }
 
   @Test
-  public void decodeDataAsAddressShouldDecodeLongIfItsSerializedAndIfItsNotTooBig() {
+  public void decodeUncompressedAddressToBytesShouldDecodeLongIfItsSerializedAndIfItsNotTooBig()
{
     Long value = 0L;
     long encodedAddress = 123L;
 
-    byte[] actual = OffHeapRegionEntryHelper.decodeAddressToBytes(encodedAddress, false,
false);
+    byte[] actual = OffHeapRegionEntryHelper.decodeUncompressedAddressToBytes(encodedAddress);
     byte[] expectedValue = EntryEventImpl.serialize(value);
 
     assertThat(actual).isEqualTo(expectedValue);
   }
 
-  @Test(expected = UnsupportedOperationException.class)
-  public void decodeDataAsAddressShouldThrowExceptionIfDataIsCompressedAndItsNotOkToBeCompressed()
{
+  @Test(expected = AssertionError.class)
+  public void decodeUncompressedAddressToBytesWithCompressedAddressShouldThrowException()
{
     long encodedAddress = 549755813703L;
-    OffHeapRegionEntryHelper.decodeAddressToBytes(encodedAddress, true, false);
+    OffHeapRegionEntryHelper.decodeUncompressedAddressToBytes(encodedAddress);
+  }
+  
+  @Test
+  public void decodeCompressedDataAsAddressToRawBytes() {
+    long encodedAddress = 549755813703L;
+    byte[] expected = new byte[]{127, -1, -1, -1};
+
+    byte[] bytes = OffHeapRegionEntryHelper.decodeAddressToRawBytes(encodedAddress);
+
+    assertThat(bytes).isEqualTo(expected);
   }
 
   @Test


Mime
View raw message