db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1372405 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/CrossConverters.java client/org/apache/derby/client/am/Cursor.java client/org/apache/derby/client/am/Decimal.java drda/org/apache/derby/impl/drda/DDMReader.java
Date Mon, 13 Aug 2012 12:54:49 GMT
Author: kahatlen
Date: Mon Aug 13 12:54:48 2012
New Revision: 1372405

URL: http://svn.apache.org/viewvc?rev=1372405&view=rev
Log:
DERBY-5873: Avoid unnecessary allocation of Number instances in client/server

Use BigDecimal's valueOf() factory methods instead of explicit constructor
calls where possible.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java?rev=1372405&r1=1372404&r2=1372405&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/CrossConverters.java Mon Aug
13 12:54:48 2012
@@ -388,6 +388,8 @@ final class CrossConverters {
                     agent_.logWriter_, "DOUBLE");
             }
             // source passed in is a float, do we need to check if the source already contains
"infinity"??
+            // Convert to Double via String to avoid changing the precision,
+            // which may happen if we cast float to double.
             return Double.valueOf(String.valueOf(source));
 
         case Types.DECIMAL:
@@ -466,7 +468,9 @@ final class CrossConverters {
             return Double.valueOf(source);
 
         case Types.DECIMAL:
-            return new java.math.BigDecimal(String.valueOf(source));  // This matches derby
semantics
+            // Use BigDecimal.valueOf(source) instead of new BigDecimal(source),
+            // as the latter may change the precision.
+            return java.math.BigDecimal.valueOf(source);
         case Types.CHAR:
         case Types.VARCHAR:
         case Types.LONGVARCHAR:

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java?rev=1372405&r1=1372404&r2=1372405&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Cursor.java Mon Aug 13 12:54:48
2012
@@ -937,8 +937,7 @@ public abstract class Cursor {
             float f = get_FLOAT(column);
             return new java.math.BigDecimal(String.valueOf(f));
         case java.sql.Types.DOUBLE:
-            // Can't use the following commented out line because it changes precision of
the result.
-            return new java.math.BigDecimal(String.valueOf(get_DOUBLE(column)));
+            return java.math.BigDecimal.valueOf(get_DOUBLE(column));
         case java.sql.Types.SMALLINT:
             return java.math.BigDecimal.valueOf(get_SMALLINT(column));
         case java.sql.Types.INTEGER:

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java?rev=1372405&r1=1372404&r2=1372405&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Decimal.java Mon Aug 13 12:54:48
2012
@@ -160,34 +160,13 @@ public class Decimal {
             signum = 1;
         }
 
-        if (precision <= 9) {
-            // can be handled by int without overflow.
-            int value = packedNybblesToInt(buffer, offset, 0, length * 2 - 1);
-
-            // convert value to a byte array of magnitude.
-            byte[] magnitude = new byte[4];
-            magnitude[0] = (byte) (value >>> 24);
-            magnitude[1] = (byte) (value >>> 16);
-            magnitude[2] = (byte) (value >>> 8);
-            magnitude[3] = (byte) (value);
-
-            return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude),
scale);
-        } else if (precision <= 18) {
+        if (precision <= 18) {
             // can be handled by long without overflow.
             long value = packedNybblesToLong(buffer, offset, 0, length * 2 - 1);
-
-            // convert value to a byte array of magnitude.
-            byte[] magnitude = new byte[8];
-            magnitude[0] = (byte) (value >>> 56);
-            magnitude[1] = (byte) (value >>> 48);
-            magnitude[2] = (byte) (value >>> 40);
-            magnitude[3] = (byte) (value >>> 32);
-            magnitude[4] = (byte) (value >>> 24);
-            magnitude[5] = (byte) (value >>> 16);
-            magnitude[6] = (byte) (value >>> 8);
-            magnitude[7] = (byte) (value);
-
-            return new java.math.BigDecimal(new java.math.BigInteger(signum, magnitude),
scale);
+            if (signum < 0) {
+                value = -value;
+            }
+            return BigDecimal.valueOf(value, scale);
         } else if (precision <= 27) {
             // get the value of last 9 digits (5 bytes).
             int lo = packedNybblesToInt(buffer, offset, (length - 5) * 2, 9);

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java?rev=1372405&r1=1372404&r2=1372405&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java Mon Aug 13 12:54:48
2012
@@ -870,37 +870,15 @@ class DDMReader
       else
         signum =  1;
 
-      if (precision <= 9) {
-        // can be handled by int without overflow.
-        int value = packedNybblesToInt(buffer, pos, 0, length*2-1);
-
-        // convert value to a byte array of magnitude.
-        byte[] magnitude = new byte[4];
-        magnitude[0] = (byte)(value >>> 24);
-        magnitude[1] = (byte)(value >>> 16);
-        magnitude[2] = (byte)(value >>> 8);
-        magnitude[3] = (byte)(value);
-
-        pos += length;
-        return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale);
-      }
-      else if (precision <= 18) {
+      if (precision <= 18) {
         // can be handled by long without overflow.
         long value = packedNybblesToLong(buffer, pos, 0, length*2-1);
-
-        // convert value to a byte array of magnitude.
-        byte[] magnitude = new byte[8];
-        magnitude[0] = (byte)(value >>> 56);
-        magnitude[1] = (byte)(value >>> 48);
-        magnitude[2] = (byte)(value >>> 40);
-        magnitude[3] = (byte)(value >>> 32);
-        magnitude[4] = (byte)(value >>> 24);
-        magnitude[5] = (byte)(value >>> 16);
-        magnitude[6] = (byte)(value >>>  8);
-        magnitude[7] = (byte)(value);
+        if (signum < 0) {
+            value = -value;
+        }
 
         pos += length;
-        return new java.math.BigDecimal (new java.math.BigInteger(signum, magnitude), scale);
+        return BigDecimal.valueOf(value, scale);
       }
       else if (precision <= 27) {
         // get the value of last 9 digits (5 bytes).



Mime
View raw message