Author: jbellis
Date: Mon May 23 01:30:55 2011
New Revision: 1126289

URL: http://svn.apache.org/viewvc?rev=1126289&view=rev
Log:
fix UUIDTypefor direct buffers
patch by Ed Anuff; reviewed by jbellis for CASSANDRA-2682

Modified:
    cassandra/branches/cassandra-0.8.0/CHANGES.txt
    
cassandra/branches/cassandra-0.8.0/src/java/org/apache/cassandra/db/marshal/UUIDType.java
    
cassandra/branches/cassandra-0.8.0/test/unit/org/apache/cassandra/db/marshal/UUIDTypeTest.java

Modified: cassandra/branches/cassandra-0.8.0/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8.0/CHANGES.txt?rev=1126289&r1=1126288&r2=1126289&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.8.0/CHANGES.txt (original)
+++ cassandra/branches/cassandra-0.8.0/CHANGES.txt Mon May 23 01:30:55 2011
@@ -3,6 +3,7 @@
  * add ant generate-cql-html target (CASSANDRA-2526)
  * update CQL consistency levels (CASSANDRA-2566)
  * debian packaging fixes (CASSANDRA-2481, 2647)
+ * fix UUIDType for direct buffers (CASSANDRA-2682)
 
 
 0.8.0-rc1
@@ -90,6 +91,7 @@
  * cli no longer divides read_repair_chance by 100 (CASSANDRA-2458)
  * made CompactionInfo.getTaskType return an enum (CASSANDRA-2482)
  * add a server-wide cap on measured memtable memory usage (CASSANDRA-2006)
+ * add unified UUIDType (CASSANDRA-2233)
 
 
 0.7.5

Modified: 
cassandra/branches/cassandra-0.8.0/src/java/org/apache/cassandra/db/marshal/UUIDType.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8.0/src/java/org/apache/cassandra/db/marshal/UUIDType.java?rev=1126289&r1=1126288&r2=1126289&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.8.0/src/java/org/apache/cassandra/db/marshal/UUIDType.java
 (original)
+++ 
cassandra/branches/cassandra-0.8.0/src/java/org/apache/cassandra/db/marshal/UUIDType.java
 Mon May 23 01:30:55 2011
@@ -25,11 +25,10 @@ import java.nio.ByteBuffer;
 import java.text.ParseException;
 import java.util.UUID;
 
-import org.apache.commons.lang.time.DateUtils;
-
 import org.apache.cassandra.utils.ByteBufferUtil;
 import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.UUIDGen;
+import org.apache.commons.lang.time.DateUtils;
 
 /**
  * Compares UUIDs using the following criteria:<br>
@@ -39,9 +38,7 @@ import org.apache.cassandra.utils.UUIDGe
  * - compare timestamps if both are time-based<br>
  * - compare lexically, unsigned msb-to-lsb comparison<br>
  * 
- * @author edanuff
  * @see "com.fasterxml.uuid.UUIDComparator"
- * 
  */
 public class UUIDType extends AbstractUUIDType
 {
@@ -65,25 +62,13 @@ public class UUIDType extends AbstractUU
             return 1;
         }
 
-        int s1 = b1.arrayOffset() + b1.position();
-        byte[] o1 = b1.array();
-
-        int s2 = b2.arrayOffset() + b2.position();
-        byte[] o2 = b2.array();
-
-        if (o1.length == s1)
-        {
-            return o2.length == s2 ? 0 : -1;
-        }
-        if (o2.length == s2)
-        {
-            return 1;
-        }
+        int s1 = b1.position();
+        int s2 = b2.position();
 
         // Compare versions
 
-        int v1 = (o1[s1 + 6] >> 4) & 0x0f;
-        int v2 = (o2[s2 + 6] >> 4) & 0x0f;
+        int v1 = (b1.get(s1 + 6) >> 4) & 0x0f;
+        int v2 = (b2.get(s2 + 6) >> 4) & 0x0f;
 
         if (v1 != v2)
         {
@@ -95,7 +80,7 @@ public class UUIDType extends AbstractUU
         if (v1 == 1)
         {
             // if both time-based, compare as timestamps
-            int c = compareTimestampBytes(s1, o1, s2, o2);
+            int c = compareTimestampBytes(b1, b2);
             if (c != 0)
             {
                 return c;
@@ -109,10 +94,9 @@ public class UUIDType extends AbstractUU
         // Appendix A - Sample Implementation.
         // Note: java.util.UUID.compareTo is not a lexical
         // comparison
-
         for (int i = 0; i < 16; i++)
         {
-            int c = ((o1[s1 + i]) & 0xFF) - ((o2[s2 + i]) & 0xFF);
+            int c = ((b1.get(s1 + i)) & 0xFF) - ((b2.get(s2 + i)) & 0xFF);
             if (c != 0)
             {
                 return c;
@@ -122,45 +106,54 @@ public class UUIDType extends AbstractUU
         return 0;
     }
 
-    private static int compareTimestampBytes(int s1, byte[] o1, int s2,
-            byte[] o2)
+    private static int compareTimestampBytes(ByteBuffer o1, ByteBuffer o2)
     {
-        int d = (o1[s1 + 6] & 0xF) - (o2[s2 + 6] & 0xF);
+        int o1Pos = o1.position();
+        int o2Pos = o2.position();
+
+        int d = (o1.get(o1Pos + 6) & 0xF) - (o2.get(o2Pos + 6) & 0xF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 7] & 0xFF) - (o2[s2 + 7] & 0xFF);
+
+        d = (o1.get(o1Pos + 7) & 0xFF) - (o2.get(o2Pos + 7) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 4] & 0xFF) - (o2[s2 + 4] & 0xFF);
+
+        d = (o1.get(o1Pos + 4) & 0xFF) - (o2.get(o2Pos + 4) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 5] & 0xFF) - (o2[s2 + 5] & 0xFF);
+
+        d = (o1.get(o1Pos + 5) & 0xFF) - (o2.get(o2Pos + 5) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 0] & 0xFF) - (o2[s2 + 0] & 0xFF);
+
+        d = (o1.get(o1Pos) & 0xFF) - (o2.get(o2Pos) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 1] & 0xFF) - (o2[s2 + 1] & 0xFF);
+
+        d = (o1.get(o1Pos + 1) & 0xFF) - (o2.get(o2Pos + 1) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        d = (o1[s1 + 2] & 0xFF) - (o2[s2 + 2] & 0xFF);
+
+        d = (o1.get(o1Pos + 2) & 0xFF) - (o2.get(o2Pos + 2) & 0xFF);
         if (d != 0)
         {
             return d;
         }
-        return (o1[s1 + 3] & 0xFF) - (o2[s2 + 3] & 0xFF);
+
+        return (o1.get(o1Pos + 3) & 0xFF) - (o2.get(o2Pos + 3) & 0xFF);
     }
 
     public UUID compose(ByteBuffer bytes)

Modified: 
cassandra/branches/cassandra-0.8.0/test/unit/org/apache/cassandra/db/marshal/UUIDTypeTest.java
URL: 
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8.0/test/unit/org/apache/cassandra/db/marshal/UUIDTypeTest.java?rev=1126289&r1=1126288&r2=1126289&view=diff
==============================================================================
--- 
cassandra/branches/cassandra-0.8.0/test/unit/org/apache/cassandra/db/marshal/UUIDTypeTest.java
 (original)
+++ 
cassandra/branches/cassandra-0.8.0/test/unit/org/apache/cassandra/db/marshal/UUIDTypeTest.java
 Mon May 23 01:30:55 2011
@@ -1,4 +1,5 @@
 package org.apache.cassandra.db.marshal;
+
 /*
  * 
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -20,7 +21,6 @@ package org.apache.cassandra.db.marshal;
  * 
  */
 
-
 import static org.junit.Assert.assertEquals;
 
 import java.net.InetAddress;
@@ -30,6 +30,7 @@ import java.util.Arrays;
 import java.util.Random;
 import java.util.UUID;
 
+import org.apache.cassandra.utils.ByteBufferUtil;
 import org.apache.cassandra.utils.UUIDGen;
 import org.apache.log4j.Logger;
 import org.junit.Test;
@@ -48,6 +49,9 @@ public class UUIDTypeTest
         UUID t1 = newTimeBasedUUID();
         UUID t2 = newTimeBasedUUID();
 
+        testCompare(null, t2, -1);
+        testCompare(t1, null, 1);
+
         testCompare(t1, t2, -1);
         testCompare(t1, t1, 0);
         testCompare(t2, t2, 0);
@@ -110,8 +114,8 @@ public class UUIDTypeTest
 
     public String describeCompare(UUID u1, UUID u2, int c)
     {
-        String tb1 = (u1.version() == 1) ? "time-based " : "random ";
-        String tb2 = (u2.version() == 1) ? "time-based " : "random ";
+        String tb1 = (u1 == null) ? "null" : (u1.version() == 1) ? "time-based 
" : "random ";
+        String tb2 = (u2 == null) ? "null" : (u2.version() == 1) ? "time-based 
" : "random ";
         String comp = (c < 0) ? " < " : ((c == 0) ? " = " : " > ");
         return tb1 + u1 + comp + tb2 + u2;
     }
@@ -131,6 +135,9 @@ public class UUIDTypeTest
 
     public static ByteBuffer bytebuffer(UUID uuid)
     {
+        if (uuid == null)
+            return ByteBufferUtil.EMPTY_BYTE_BUFFER;
+
         long msb = uuid.getMostSignificantBits();
         long lsb = uuid.getLeastSignificantBits();
         byte[] bytes = new byte[16];
@@ -149,29 +156,23 @@ public class UUIDTypeTest
 
     public void logJdkUUIDCompareToVariance(UUID u1, UUID u2, int expC)
     {
+        if ((u1 == null) || (u2 == null))
+            return;
         if (u1.version() != u2.version())
-        {
             return;
-        }
         if (u1.version() == 1)
-        {
             return;
-        }
         if (u1.compareTo(u2) != expC)
-        {
             logger.info("*** Note: java.util.UUID.compareTo() would have 
compared this differently");
-        }
-
     }
 
     public void testCompare(UUID u1, UUID u2, int expC)
     {
         int c = sign(uuidType.compare(bytebuffer(u1), bytebuffer(u2)));
         expC = sign(expC);
-        assertEquals("Expected " + describeCompare(u1, u2, expC) + ", got "
-                + describeCompare(u1, u2, c), expC, c);
+        assertEquals("Expected " + describeCompare(u1, u2, expC) + ", got " + 
describeCompare(u1, u2, c), expC, c);
 
-        if (u1.version() == 1 && u2.version() == 1)
+        if (((u1 != null) && (u1.version() == 1)) && ((u2 != null) && 
(u2.version() == 1)))
             assertEquals(c, sign(TimeUUIDType.instance.compare(bytebuffer(u1), 
bytebuffer(u2))));
 
         logJdkUUIDCompareToVariance(u1, u2, c);


Reply via email to