You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ha...@apache.org on 2017/11/03 23:57:46 UTC

hive git commit: HIVE-16834 : Review org.apache.hadoop.hive.serde2.ByteStream (Beluga Behr via Ashutosh Chauhan)

Repository: hive
Updated Branches:
  refs/heads/master 1b021e26d -> 7006ade0d


HIVE-16834 : Review org.apache.hadoop.hive.serde2.ByteStream (Beluga Behr via Ashutosh Chauhan)

Signed-off-by: Ashutosh Chauhan <ha...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/7006ade0
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/7006ade0
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/7006ade0

Branch: refs/heads/master
Commit: 7006ade0d0f0b2f4cd4eaa82fdd85c012d848c4b
Parents: 1b021e2
Author: BELUGA BEHR <da...@gmail.com>
Authored: Fri Nov 3 16:56:57 2017 -0700
Committer: Ashutosh Chauhan <ha...@apache.org>
Committed: Fri Nov 3 16:56:57 2017 -0700

----------------------------------------------------------------------
 .../apache/hadoop/hive/serde2/ByteStream.java   | 87 ++++++++++++--------
 1 file changed, 52 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/7006ade0/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java
----------------------------------------------------------------------
diff --git a/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java b/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java
index 7916a6f..534bc99 100644
--- a/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java
+++ b/serde/src/java/org/apache/hadoop/hive/serde2/ByteStream.java
@@ -23,9 +23,6 @@ import java.util.Arrays;
 
 import org.apache.hadoop.hive.common.io.NonSyncByteArrayInputStream;
 import org.apache.hadoop.hive.common.io.NonSyncByteArrayOutputStream;
-import org.apache.hadoop.hive.serde2.binarysortable.BinarySortableSerDe;
-import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
-import org.apache.hadoop.hive.serde2.ByteStream.Output;
 
 /**
  * Extensions to bytearrayinput/output streams.
@@ -36,26 +33,27 @@ public class ByteStream {
    *
    */
   public static class Input extends NonSyncByteArrayInputStream {
-    public byte[] getData() {
-      return buf;
+
+    public Input() {
+      super();
     }
 
-    public int getCount() {
-      return count;
+    public Input(byte[] buf) {
+      super(buf);
     }
 
-    public void reset(byte[] argBuf, int argCount) {
-      buf = argBuf;
-      mark = pos = 0;
-      count = argCount;
+    public byte[] getData() {
+      return buf;
     }
 
-    public Input() {
-      super(new byte[1]);
+    public int getCount() {
+      return count;
     }
 
-    public Input(byte[] buf) {
-      super(buf);
+    public void reset(byte[] buf, int count) {
+      super.buf = buf;
+      super.count = count;
+      super.mark = super.pos = 0;
     }
 
     public Input(byte[] buf, int offset, int length) {
@@ -69,10 +67,8 @@ public class ByteStream {
    */
   public static final class Output
     extends NonSyncByteArrayOutputStream implements RandomAccessOutput {
-    @Override
-    public byte[] getData() {
-      return buf;
-    }
+    
+    private static final byte[] RESERVE_INT = { 0x00, 0x00, 0x00, 0x00 };
 
     public Output() {
       super();
@@ -83,46 +79,67 @@ public class ByteStream {
     }
 
     @Override
+    public byte[] getData() {
+      return buf;
+    }
+
+    @Override
     public void writeInt(long offset, int value) {
-      int offset2 = (int)offset;
-      getData()[offset2++] = (byte) (value >> 24);
-      getData()[offset2++] = (byte) (value >> 16);
-      getData()[offset2++] = (byte) (value >> 8);
-      getData()[offset2] = (byte) (value);
+      int i = (int) offset;
+      buf[i + 0] = (byte) (value >> 24);
+      buf[i + 1] = (byte) (value >> 16);
+      buf[i + 2] = (byte) (value >> 8);
+      buf[i + 3] = (byte) (value);
     }
 
     @Override
     public void writeByte(long offset, byte value) {
-      getData()[(int) offset] = value;
+      buf[(int) offset] = value;
     }
 
+    /**
+     * Optimize for the common cases:
+     * <ul>
+     *   <li>Reserve 1 byte</li>
+     *   <li>Reserve 1 int (4 bytes)</li>
+     * </ul>
+     */
     @Override
     public void reserve(int byteCount) {
-      for (int i = 0; i < byteCount; ++i) {
+      switch (byteCount) {
+      case 0:
+        break;
+      case 1:
         write(0);
+        break;
+      case 4:
+        write(RESERVE_INT, 0, 4);
+        break;
+      default:
+        for (int i = 0; i < byteCount; ++i) {
+          write(0);
+        }
       }
     }
 
     public boolean arraysEquals(Output output) {
-      if (count != output.count) {
-        return false;
-      }
-      for (int i = 0; i < count; i++) {
-        if (buf[i] != output.buf[i]) {
-          return false;
-        }
-      }
-      return true;
+      return Arrays.equals(super.buf, output.buf);
     }
   }
 
   public static interface RandomAccessOutput {
     public void writeByte(long offset, byte value);
+
     public void writeInt(long offset, int value);
+
     public void reserve(int byteCount);
+
     public void write(int b);
+
     public void write(byte b[]) throws IOException;
+
     public void write(byte b[], int off, int len);
+
     public int getLength();
   }
 }