cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bened...@apache.org
Subject cassandra git commit: merge UUIDType and TimeUUIDType parse logic
Date Tue, 10 Mar 2015 17:45:54 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk e585c339d -> d76adf500


merge UUIDType and TimeUUIDType parse logic

patch by benedict; reviewed by ariel for CASSANDRA-8759


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

Branch: refs/heads/trunk
Commit: d76adf50024a064f5c66b8457113e903cf05296e
Parents: e585c33
Author: Benedict Elliott Smith <benedict@apache.org>
Authored: Tue Mar 10 17:45:20 2015 +0000
Committer: Benedict Elliott Smith <benedict@apache.org>
Committed: Tue Mar 10 17:45:20 2015 +0000

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../cassandra/db/marshal/TimeUUIDType.java      | 36 +++-----------
 .../apache/cassandra/db/marshal/UUIDType.java   | 51 +++++++++++++-------
 3 files changed, 40 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/d76adf50/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index b2b33b0..29e1edf 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.0
+ * Merge UUIDType and TimeUUIDType parse logic (CASSANDRA-8759)
  * Avoid memory allocation when searching index summary (CASSANDRA-8793)
  * Optimise (Time)?UUIDType Comparisons (CASSANDRA-8730)
  * Make CRC32Ex into a separate maven dependency (CASSANDRA-8836)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d76adf50/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java b/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java
index 17c7abc..06443c4 100644
--- a/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java
+++ b/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java
@@ -19,20 +19,16 @@ package org.apache.cassandra.db.marshal;
 
 import java.nio.ByteBuffer;
 import java.util.UUID;
-import java.util.regex.Pattern;
 
 import org.apache.cassandra.cql3.CQL3Type;
 import org.apache.cassandra.serializers.TypeSerializer;
 import org.apache.cassandra.serializers.MarshalException;
 import org.apache.cassandra.serializers.TimeUUIDSerializer;
-import org.apache.cassandra.utils.ByteBufferUtil;
 
 public class TimeUUIDType extends AbstractType<UUID>
 {
     public static final TimeUUIDType instance = new TimeUUIDType();
 
-    static final Pattern regexPattern = Pattern.compile("[A-Fa-f0-9]{8}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{12}");
-
     TimeUUIDType()
     {
     } // singleton
@@ -93,35 +89,15 @@ public class TimeUUIDType extends AbstractType<UUID>
 
     public ByteBuffer fromString(String source) throws MarshalException
     {
-        // Return an empty ByteBuffer for an empty string.
-        if (source.isEmpty())
-            return ByteBufferUtil.EMPTY_BYTE_BUFFER;
-
-        ByteBuffer idBytes = null;
-
-        // ffffffff-ffff-ffff-ffff-ffffffffff
-        if (regexPattern.matcher(source).matches())
-        {
-            UUID uuid = null;
-            try
-            {
-                uuid = UUID.fromString(source);
-                idBytes = decompose(uuid);
-            }
-            catch (IllegalArgumentException e)
-            {
-                throw new MarshalException(String.format("Unable to make UUID from '%s'",
source), e);
-            }
-
-            if (uuid.version() != 1)
-                throw new MarshalException("TimeUUID supports only version 1 UUIDs");
-        } else
-        {
+        ByteBuffer parsed = UUIDType.parse(source);
+        if (parsed == null)
             throw new MarshalException(String.format("Unknown timeuuid representation: %s",
source));
-        }
-        return idBytes;
+        if (parsed.remaining() == 16 && UUIDType.version(parsed) != 1)
+            throw new MarshalException("TimeUUID supports only version 1 UUIDs");
+        return parsed;
     }
 
+    @Override
     public CQL3Type asCQL3Type()
     {
         return CQL3Type.Native.TIMEUUID;

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d76adf50/src/java/org/apache/cassandra/db/marshal/UUIDType.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/marshal/UUIDType.java b/src/java/org/apache/cassandra/db/marshal/UUIDType.java
index c925ea0..09dd8b4 100644
--- a/src/java/org/apache/cassandra/db/marshal/UUIDType.java
+++ b/src/java/org/apache/cassandra/db/marshal/UUIDType.java
@@ -19,6 +19,7 @@ package org.apache.cassandra.db.marshal;
 
 import java.nio.ByteBuffer;
 import java.util.UUID;
+import java.util.regex.Pattern;
 
 import com.google.common.primitives.UnsignedLongs;
 
@@ -93,14 +94,41 @@ public class UUIDType extends AbstractType<UUID>
     }
 
     @Override
+    public boolean isValueCompatibleWithInternal(AbstractType<?> otherType)
+    {
+        return otherType instanceof UUIDType || otherType instanceof TimeUUIDType;
+    }
+
+    @Override
     public ByteBuffer fromString(String source) throws MarshalException
     {
         // Return an empty ByteBuffer for an empty string.
+        ByteBuffer parsed = parse(source);
+        if (parsed != null)
+            return parsed;
+
+        throw new MarshalException(String.format("unable to coerce '%s' to UUID", source));
+    }
+
+    @Override
+    public CQL3Type asCQL3Type()
+    {
+        return CQL3Type.Native.UUID;
+    }
+
+    public TypeSerializer<UUID> getSerializer()
+    {
+        return UUIDSerializer.instance;
+    }
+
+    static final Pattern regexPattern = Pattern.compile("[A-Fa-f0-9]{8}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{4}\\-[A-Fa-f0-9]{12}");
+
+    static ByteBuffer parse(String source)
+    {
         if (source.isEmpty())
             return ByteBufferUtil.EMPTY_BYTE_BUFFER;
 
-        // ffffffff-ffff-ffff-ffff-ffffffffff
-        if (TimeUUIDType.regexPattern.matcher(source).matches())
+        if (regexPattern.matcher(source).matches())
         {
             try
             {
@@ -111,24 +139,11 @@ public class UUIDType extends AbstractType<UUID>
                 throw new MarshalException(String.format("unable to make UUID from '%s'",
source), e);
             }
         }
-
-        throw new MarshalException(String.format("unable to coerce '%s' to version 1 UUID",
source));
+        return null;
     }
 
-    @Override
-    public boolean isValueCompatibleWithInternal(AbstractType<?> otherType)
+    static int version(ByteBuffer uuid)
     {
-        return this == otherType || otherType == TimeUUIDType.instance;
+        return (uuid.get(6) & 0xf0) >> 4;
     }
-
-    public CQL3Type asCQL3Type()
-    {
-        return CQL3Type.Native.UUID;
-    }
-
-    public TypeSerializer<UUID> getSerializer()
-    {
-        return UUIDSerializer.instance;
-    }
-
 }


Mime
View raw message