You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2011/11/03 05:52:02 UTC
svn commit: r1196941 - in /cassandra/branches/cassandra-1.0: CHANGES.txt
src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java
src/java/org/apache/cassandra/db/marshal/DecimalType.java
test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java
Author: jbellis
Date: Thu Nov 3 04:52:01 2011
New Revision: 1196941
URL: http://svn.apache.org/viewvc?rev=1196941&view=rev
Log:
fix DecimalType bytebuffer marshalling
patch by Rick Shaw; reviewed by jbellis for CASSANDRA-3421
Added:
cassandra/branches/cassandra-1.0/test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java
Modified:
cassandra/branches/cassandra-1.0/CHANGES.txt
cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java
cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/db/marshal/DecimalType.java
Modified: cassandra/branches/cassandra-1.0/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-1.0/CHANGES.txt?rev=1196941&r1=1196940&r2=1196941&view=diff
==============================================================================
--- cassandra/branches/cassandra-1.0/CHANGES.txt (original)
+++ cassandra/branches/cassandra-1.0/CHANGES.txt Thu Nov 3 04:52:01 2011
@@ -8,6 +8,8 @@
Merged from 0.8:
* acquire compactionlock during truncate (CASSANDRA-3399)
* fix bug that caused first column in per row indexes to be ignored (CASSANDRA-3441)
+ * fix DecimalType bytebuffer marshalling (CASSANDRA-3421)
+
1.0.1
* acquire references during index build to prevent delete problems
Modified: cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java?rev=1196941&r1=1196940&r2=1196941&view=diff
==============================================================================
--- cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java (original)
+++ cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/cql/jdbc/JdbcDecimal.java Thu Nov 3 04:52:01 2011
@@ -86,12 +86,14 @@ public class JdbcDecimal extends Abstrac
public BigDecimal compose(ByteBuffer bytes)
{
if (bytes == null) return null;
-
+
+ // do not consume the contents of the ByteBuffer
+ bytes = bytes.duplicate();
int scale = bytes.getInt();
byte[] bibytes = new byte[bytes.remaining()];
- bytes.get(bibytes, 0, bytes.remaining());
+ bytes.get(bibytes);
+
BigInteger bi = new BigInteger(bibytes);
-
return new BigDecimal(bi,scale);
}
}
Modified: cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/db/marshal/DecimalType.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/db/marshal/DecimalType.java?rev=1196941&r1=1196940&r2=1196941&view=diff
==============================================================================
--- cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/db/marshal/DecimalType.java (original)
+++ cassandra/branches/cassandra-1.0/src/java/org/apache/cassandra/db/marshal/DecimalType.java Thu Nov 3 04:52:01 2011
@@ -35,6 +35,15 @@ public class DecimalType extends Abstrac
public int compare(ByteBuffer bb0, ByteBuffer bb1)
{
+ if (bb0.remaining() == 0)
+ {
+ return bb1.remaining() == 0 ? 0 : -1;
+ }
+ if (bb1.remaining() == 0)
+ {
+ return 1;
+ }
+
return compose(bb0).compareTo(compose(bb1));
}
Added: cassandra/branches/cassandra-1.0/test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java
URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-1.0/test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java?rev=1196941&view=auto
==============================================================================
--- cassandra/branches/cassandra-1.0/test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java (added)
+++ cassandra/branches/cassandra-1.0/test/unit/org/apache/cassandra/db/marshal/DecimalTypeTest.java Thu Nov 3 04:52:01 2011
@@ -0,0 +1,101 @@
+/*
+ * 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.cassandra.db.marshal;
+
+import static org.junit.Assert.*;
+
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.cassandra.db.marshal.DecimalType;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.junit.Test;
+
+public class DecimalTypeTest
+{
+ private static final String LOW = "12.34";
+ private static final String HIGH = "34.5678";
+
+ private static BigDecimal zero = new BigDecimal("0.0");
+ private static BigDecimal minus = new BigDecimal("-1.000001");
+ private static BigDecimal low = new BigDecimal(LOW);
+ private static BigDecimal high = new BigDecimal(HIGH);
+
+ @Test
+ public void test1Decompose_compose()
+ {
+ ByteBuffer bb = DecimalType.instance.decompose(low);
+
+ String string = DecimalType.instance.compose(bb).toPlainString();
+
+ // check that the decomposed buffer when re-composed is equal to the initial string.
+ assertEquals(LOW, string);
+
+ // check that a null argument yields an empty byte buffer
+ bb = DecimalType.instance.decompose(null);
+ assertEquals(bb, ByteBufferUtil.EMPTY_BYTE_BUFFER);
+ }
+
+ @Test
+ public void test2Compare()
+ {
+ ByteBuffer lowBB = DecimalType.instance.decompose(low);
+ ByteBuffer low2BB = DecimalType.instance.decompose(low);
+ ByteBuffer highBB = DecimalType.instance.decompose(high);
+ assertEquals(-1, DecimalType.instance.compare(lowBB, highBB));
+
+ lowBB = DecimalType.instance.decompose(low);
+ highBB = DecimalType.instance.decompose(high);
+ assertEquals(1, DecimalType.instance.compare(highBB, lowBB));
+
+ lowBB = DecimalType.instance.decompose(low);
+ assertEquals(0, DecimalType.instance.compare(low2BB, lowBB));
+
+ lowBB = DecimalType.instance.decompose(low);
+ assertEquals(-1, DecimalType.instance.compare(ByteBufferUtil.EMPTY_BYTE_BUFFER, lowBB));
+
+ lowBB = DecimalType.instance.decompose(low);
+ assertEquals(1, DecimalType.instance.compare(lowBB,ByteBufferUtil.EMPTY_BYTE_BUFFER));
+
+ assertEquals(0, DecimalType.instance.compare(ByteBufferUtil.EMPTY_BYTE_BUFFER,ByteBufferUtil.EMPTY_BYTE_BUFFER));
+ }
+
+ @Test
+ public void test3Sort()
+ {
+ ByteBuffer zeroBB = DecimalType.instance.decompose(zero);
+ ByteBuffer minusBB = DecimalType.instance.decompose(minus);
+ ByteBuffer lowBB = DecimalType.instance.decompose(low);
+ ByteBuffer highBB = DecimalType.instance.decompose(high);
+
+ ByteBuffer[] array = {highBB,minusBB,lowBB,lowBB,zeroBB,minusBB};
+
+ // Sort the array of ByteBuffer using a DecimalType comparator
+ Arrays.sort(array, DecimalType.instance);
+
+ // Check that the array is in order
+ for (int i = 1; i < array.length; i++)
+ {
+ BigDecimal i0 = DecimalType.instance.compose(array[i - 1]);
+ BigDecimal i1 = DecimalType.instance.compose(array[i]);
+ assertTrue("#" + i, i0.compareTo(i1) <= 0);
+ }
+ }
+}