Till Westmann has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/1783

Change subject: More code reuse for numeric conversions
......................................................................

More code reuse for numeric conversions

Change-Id: I2cc5189c3c216a952745bc64e496aefe6b5f6bc4
---
D 
asterixdb/asterix-om/src/main/java/org/apache/asterix/dataflow/data/common/SerializationUtil.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/AbstractIntegerTypeConvertComputer.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
M 
asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
4 files changed, 52 insertions(+), 142 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/83/1783/1

diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/dataflow/data/common/SerializationUtil.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/dataflow/data/common/SerializationUtil.java
deleted file mode 100644
index 7482b47..0000000
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/dataflow/data/common/SerializationUtil.java
+++ /dev/null
@@ -1,35 +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 org.apache.asterix.dataflow.data.common;
-
-public final class SerializationUtil {
-
-    public static void writeIntToByteArray(byte[] array, int value, int 
offset) {
-        array[offset] = (byte) (0xff & (value >> 24));
-        array[offset + 1] = (byte) (0xff & (value >> 16));
-        array[offset + 2] = (byte) (0xff & (value >> 8));
-        array[offset + 3] = (byte) (0xff & value);
-    }
-
-    public static void writeShortToByteArray(byte[] array, short value, int 
offset) {
-        array[offset] = (byte) (0xff & (value >> 8));
-        array[offset + 1] = (byte) (0xff & value);
-    }
-
-}
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/AbstractIntegerTypeConvertComputer.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/AbstractIntegerTypeConvertComputer.java
index c646288..a48d447 100644
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/AbstractIntegerTypeConvertComputer.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/AbstractIntegerTypeConvertComputer.java
@@ -22,6 +22,10 @@
 import java.io.IOException;
 
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.data.std.primitive.BytePointable;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+import org.apache.hyracks.data.std.primitive.LongPointable;
+import org.apache.hyracks.data.std.primitive.ShortPointable;
 
 public abstract class AbstractIntegerTypeConvertComputer implements 
ITypeConvertComputer {
 
@@ -30,67 +34,59 @@
     // https://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html
     public void convertIntegerType(byte[] data, int start, int length, 
DataOutput out, ATypeTag targetType,
             int targetTypeLength) throws IOException {
-        long num = 0;
-        //        for (int i = start; i < start + length; i++) {
-        //            num += (data[i] & 0xff) << ((length - 1 - (i - start)) * 
8);
-        //        }
+        final long num = asLong(data, start, length);
+        boundaryCheck(targetType, num);
+        out.writeByte(targetType.serialize());
+        // Write actual target value
+        writeTargetValue(out, num, targetTypeLength);
+    }
 
-        // Read source values
+    static long asLong(byte[] data, int start, int length) throws IOException {
+        long num;// Read source values
         switch (length) {
-            case 1:
-                // TINYINT
-                num = (data[start] & 0xff);
+            case 1: // TINYINT
+                num = BytePointable.getByte(data, start);
                 break;
-
-            case 2:
-                // SMALLINT
-                num = (short) ((data[start] << 8) | (data[start + 1] & 0xff));
+            case 2: // SMALLINT
+                num = ShortPointable.getShort(data, start);
                 break;
-
-            case 4:
-                // INTEGER
-                num = (int) (((data[start] & 0xff) << 24) | ((data[start + 1] 
& 0xff) << 16)
-                        | ((data[start + 2] & 0xff) << 8) | (data[start + 3] & 
0xff));
+            case 4: // INTEGER
+                num = IntegerPointable.getInteger(data, start);
                 break;
-
-            case 8:
-                // BIGINT
-                num = (((long) (data[start] & 0xff) << 56) | ((long) 
(data[start + 1] & 0xff) << 48)
-                        | ((long) (data[start + 2] & 0xff) << 40) | ((long) 
(data[start + 3] & 0xff) << 32)
-                        | ((long) (data[start + 4] & 0xff) << 24) | ((long) 
(data[start + 5] & 0xff) << 16)
-                        | ((long) (data[start + 6] & 0xff) << 8) | ((long) 
(data[start + 7] & 0xff)));
-
+            case 8: // BIGINT
+                num = LongPointable.getLong(data, start);
                 break;
-
             default:
                 throw new IOException("Can't convert integer types. The source 
type should be one of "
                         + "tinyint/smallint/integer/bigint.");
-
         }
+        return num;
+    }
 
+    static void boundaryCheck(ATypeTag targetType, long num) throws 
IOException {
         // Boundary check
         switch (targetType) {
             case TINYINT:
                 if (num > Byte.MAX_VALUE || num < Byte.MIN_VALUE) {
-                    throw new IOException("Source value " + num
-                            + " is out of range that TINYINT can hold - 
TINYINT.MAX_VALUE:" + Byte.MAX_VALUE
-                            + ", TINYINT.MIN_VALUE:" + Byte.MIN_VALUE);
+                    throw new IOException(
+                            "Source value " + num + " is out of range that 
TINYINT can hold - TINYINT.MAX_VALUE:"
+                                    + Byte.MAX_VALUE + ", TINYINT.MIN_VALUE:" 
+ Byte.MIN_VALUE);
                 }
                 break;
 
             case SMALLINT:
                 if (num > Short.MAX_VALUE || num < Short.MIN_VALUE) {
-                    throw new IOException("Source value " + num
-                            + " is out of range that SMALLINT can hold - 
SMALLINT.MAX_VALUE:" + Short.MAX_VALUE
-                            + ", SMALLINT.MIN_VALUE:" + Short.MIN_VALUE);
+                    throw new IOException(
+                            "Source value " + num + " is out of range that 
SMALLINT can hold - SMALLINT.MAX_VALUE:"
+                                    + Short.MAX_VALUE + ", 
SMALLINT.MIN_VALUE:" + Short.MIN_VALUE);
                 }
                 break;
 
             case INTEGER:
                 if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) {
-                    throw new IOException("Source value " + num
-                            + " is out of range that INTEGER can hold - 
INTEGER.MAX_VALUE:" + Integer.MAX_VALUE
-                            + ", INTEGER.MIN_VALUE:" + Integer.MIN_VALUE);
+                    throw new IOException(
+                            "Source value " + num + " is out of range that 
INTEGER can hold - INTEGER.MAX_VALUE:"
+                                    + Integer.MAX_VALUE + ", 
INTEGER.MIN_VALUE:" + Integer.MIN_VALUE);
                 }
                 break;
 
@@ -98,10 +94,9 @@
             default:
                 break;
         }
+    }
 
-        out.writeByte(targetType.serialize());
-
-        // Write actual target value
+    static void writeTargetValue(DataOutput out, long num, int 
targetTypeLength) throws IOException {
         switch (targetTypeLength) {
             case 1:
                 // TINYINT
@@ -137,11 +132,6 @@
             default:
                 throw new IOException("Can't convert integer types. The target 
type should be one of "
                         + "tinyint/smallint/integer/bigint.");
-
         }
-
-//        for (int i = targetTypeLength - 1; i >= 0; i--) {
-//            out.writeByte((byte) ((num >>> (i * 8)) & 0xFF));
-//        }
     }
 }
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
index 0227937..cedb060 100644
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToDoubleTypeConvertComputer.java
@@ -22,6 +22,10 @@
 import java.io.IOException;
 
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.data.std.primitive.BytePointable;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+import org.apache.hyracks.data.std.primitive.LongPointable;
+import org.apache.hyracks.data.std.primitive.ShortPointable;
 import 
org.apache.hyracks.dataflow.common.data.marshalling.DoubleSerializerDeserializer;
 
 public class IntegerToDoubleTypeConvertComputer implements 
ITypeConvertComputer {
@@ -29,47 +33,12 @@
     public static final IntegerToDoubleTypeConvertComputer INSTANCE = new 
IntegerToDoubleTypeConvertComputer();
 
     private IntegerToDoubleTypeConvertComputer() {
-
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput 
out) throws IOException {
-        long val = 0L;
-
-        // In order to convert a negative number correctly,
-        // proper casting per INT type is needed.
-        //
-        switch (length) {
-            case 1:
-                // TINYINT
-                val = (data[start] & 0xff);
-                break;
-
-            case 2:
-                // SMALLINT
-                val = (short) ((data[start] << 8) | (data[start + 1] & 0xff));
-                break;
-
-            case 4:
-                // INTEGER
-                val = (int) (((data[start] & 0xff) << 24) | ((data[start + 1] 
& 0xff) << 16)
-                        | ((data[start + 2] & 0xff) << 8) | (data[start + 3] & 
0xff));
-                break;
-
-            case 8:
-                // BIGINT
-                val = (((long) (data[start] & 0xff) << 56) | ((long) 
(data[start + 1] & 0xff) << 48)
-                        | ((long) (data[start + 2] & 0xff) << 40) | ((long) 
(data[start + 3] & 0xff) << 32)
-                        | ((long) (data[start + 4] & 0xff) << 24) | ((long) 
(data[start + 5] & 0xff) << 16)
-                        | ((long) (data[start + 6] & 0xff) << 8) | ((long) 
(data[start + 7] & 0xff)));
-
-                break;
-
-            default:
-                break;
-        }
+        long val = AbstractIntegerTypeConvertComputer.asLong(data, start, 
length);
         out.writeByte(ATypeTag.DOUBLE.serialize());
         DoubleSerializerDeserializer.INSTANCE.serialize(Double.valueOf(val), 
out);
     }
-
 }
diff --git 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
index 85c4202..302a05f 100644
--- 
a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
+++ 
b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/types/hierachy/IntegerToFloatTypeConvertComputer.java
@@ -22,6 +22,10 @@
 import java.io.IOException;
 
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.data.std.primitive.BytePointable;
+import org.apache.hyracks.data.std.primitive.IntegerPointable;
+import org.apache.hyracks.data.std.primitive.LongPointable;
+import org.apache.hyracks.data.std.primitive.ShortPointable;
 import 
org.apache.hyracks.dataflow.common.data.marshalling.FloatSerializerDeserializer;
 
 public class IntegerToFloatTypeConvertComputer implements ITypeConvertComputer 
{
@@ -29,47 +33,29 @@
     public static final IntegerToFloatTypeConvertComputer INSTANCE = new 
IntegerToFloatTypeConvertComputer();
 
     private IntegerToFloatTypeConvertComputer() {
-
     }
 
     @Override
     public void convertType(byte[] data, int start, int length, DataOutput 
out) throws IOException {
         float val = 0;
-        // In order to convert a negative number correctly,
-        // proper casting per INT type is needed.
-        //
+        // In order to convert a negative number correctly, proper casting per 
INT type is needed.
         switch (length) {
-            case 1:
-                // TINYINT
-                val = (data[start] & 0xff);
+            case 1: // TINYINT
+                val = BytePointable.getByte(data, start);
                 break;
-
-            case 2:
-                // SMALLINT
-                val = (short) ((data[start] << 8) | (data[start + 1] & 0xff));
+            case 2: // SMALLINT
+                val = ShortPointable.getShort(data, start);
                 break;
-
-            case 4:
-                // INTEGER
-                val = (int) (((data[start] & 0xff) << 24) | ((data[start + 1] 
& 0xff) << 16)
-                        | ((data[start + 2] & 0xff) << 8) | (data[start + 3] & 
0xff));
+            case 4: // INTEGER
+                val = IntegerPointable.getInteger(data, start);
                 break;
-
-            case 8:
-                // BIGINT
-                val = (((long) (data[start] & 0xff) << 56) | ((long) 
(data[start + 1] & 0xff) << 48)
-                        | ((long) (data[start + 2] & 0xff) << 40) | ((long) 
(data[start + 3] & 0xff) << 32)
-                        | ((long) (data[start + 4] & 0xff) << 24) | ((long) 
(data[start + 5] & 0xff) << 16)
-                        | ((long) (data[start + 6] & 0xff) << 8) | ((long) 
(data[start + 7] & 0xff)));
-
+            case 8: // BIGINT
+                val = LongPointable.getLong(data, start);
                 break;
-
             default:
                 break;
         }
         out.writeByte(ATypeTag.FLOAT.serialize());
         FloatSerializerDeserializer.INSTANCE.serialize(val, out);
-
     }
-
 }

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1783
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2cc5189c3c216a952745bc64e496aefe6b5f6bc4
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Till Westmann <[email protected]>

Reply via email to