cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From slebre...@apache.org
Subject git commit: Pack consistency level in binary protocol
Date Wed, 17 Oct 2012 16:33:28 GMT
Updated Branches:
  refs/heads/trunk f5dda349b -> 954dc5e5b


Pack consistency level in binary protocol

patch by slebresne; reviewed by jbellis for CASSANDRA-4824


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

Branch: refs/heads/trunk
Commit: 954dc5e5b7475ccde4001526312b430e2afa8bcf
Parents: f5dda34
Author: Sylvain Lebresne <sylvain@datastax.com>
Authored: Wed Oct 17 18:32:37 2012 +0200
Committer: Sylvain Lebresne <sylvain@datastax.com>
Committed: Wed Oct 17 18:32:37 2012 +0200

----------------------------------------------------------------------
 CHANGES.txt                                        |    2 +-
 doc/native_protocol.spec                           |    8 ++-
 .../org/apache/cassandra/db/ConsistencyLevel.java  |   46 ++++++++++++---
 .../org/apache/cassandra/transport/CBUtil.java     |   17 +-----
 4 files changed, 47 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/954dc5e5/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 72803cc..902b8b4 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -30,7 +30,7 @@
  * Run leveled compaction in parallel (CASSANDRA-4310)
  * Fix potential NPE during CFS reload (CASSANDRA-4786)
  * Composite indexes may miss results (CASSANDRA-4796)
- * Move consistency level to the protocol level (CASSANDRA-4734)
+ * Move consistency level to the protocol level (CASSANDRA-4734, 4824)
 
 
 1.2-beta1

http://git-wip-us.apache.org/repos/asf/cassandra/blob/954dc5e5/doc/native_protocol.spec
----------------------------------------------------------------------
diff --git a/doc/native_protocol.spec b/doc/native_protocol.spec
index b8152bc..b1db8a0 100644
--- a/doc/native_protocol.spec
+++ b/doc/native_protocol.spec
@@ -171,10 +171,14 @@ Table of Contents
     [option list]  A [short] n, followed by n [option].
     [inet]         An address (ip and port) to a node. It consists of one
                    [byte] n, that represents the address size, followed by n
-                   [byte] repesenting the IP address (in practice n can only be
+                   [byte] representing the IP address (in practice n can only be
                    either 4 (IPv4) or 16 (IPv6)), following by one [int]
                    representing the port.
-    [consistency]  A consistency level specification. This is a [string] having
+    [consistency]  A consistency level specification. This is a [short]
+                   representing a consistency level with the following
+                   correspondence:
+
+                   having
                    one of the following value: "", "ANY", "ONE", "TWO", "THREE",
                    "QUORUM", "ALL", "LOCAL_QUORUM", "EACH_QUORUM". It is
                    possible to provide an empty string, in which case a default

http://git-wip-us.apache.org/repos/asf/cassandra/blob/954dc5e5/src/java/org/apache/cassandra/db/ConsistencyLevel.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/ConsistencyLevel.java b/src/java/org/apache/cassandra/db/ConsistencyLevel.java
index ea7660c..9664fcd 100644
--- a/src/java/org/apache/cassandra/db/ConsistencyLevel.java
+++ b/src/java/org/apache/cassandra/db/ConsistencyLevel.java
@@ -23,17 +23,47 @@ import org.apache.cassandra.exceptions.InvalidRequestException;
 import org.apache.cassandra.locator.AbstractReplicationStrategy;
 import org.apache.cassandra.locator.NetworkTopologyStrategy;
 import org.apache.cassandra.utils.FBUtilities;
+import org.apache.cassandra.transport.ProtocolException;
 
 public enum ConsistencyLevel
 {
-    ANY,
-    ONE,
-    TWO,
-    THREE,
-    QUORUM,
-    ALL,
-    LOCAL_QUORUM,
-    EACH_QUORUM;
+    ANY         (0),
+    ONE         (1),
+    TWO         (2),
+    THREE       (3),
+    QUORUM      (4),
+    ALL         (5),
+    LOCAL_QUORUM(6),
+    EACH_QUORUM (7);
+
+    // Used by the binary protocol
+    public final int code;
+    private static final ConsistencyLevel[] codeIdx;
+    static
+    {
+        int maxCode = -1;
+        for (ConsistencyLevel cl : ConsistencyLevel.values())
+            maxCode = Math.max(maxCode, cl.code);
+        codeIdx = new ConsistencyLevel[maxCode + 1];
+        for (ConsistencyLevel cl : ConsistencyLevel.values())
+        {
+            if (codeIdx[cl.code] != null)
+                throw new IllegalStateException("Duplicate code");
+            codeIdx[cl.code] = cl;
+        }
+    }
+
+    private ConsistencyLevel(int code)
+    {
+        this.code = code;
+    }
+
+    public static ConsistencyLevel fromCode(int code)
+    {
+        if (code < 0 || code >= codeIdx.length)
+            throw new ProtocolException(String.format("Unknown code %d for a consistency
level", code));
+        return codeIdx[code];
+    }
 
     public int blockFor(String table)
     {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/954dc5e5/src/java/org/apache/cassandra/transport/CBUtil.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/transport/CBUtil.java b/src/java/org/apache/cassandra/transport/CBUtil.java
index 45affc6..ba04335 100644
--- a/src/java/org/apache/cassandra/transport/CBUtil.java
+++ b/src/java/org/apache/cassandra/transport/CBUtil.java
@@ -135,25 +135,12 @@ public abstract class CBUtil
 
     public static ChannelBuffer consistencyLevelToCB(ConsistencyLevel consistency)
     {
-        if (consistency == null)
-            return shortToCB(0);
-        else
-            return stringToCB(consistency.toString());
+        return shortToCB(consistency.code);
     }
 
     public static ConsistencyLevel readConsistencyLevel(ChannelBuffer cb)
     {
-        String cl = CBUtil.readString(cb);
-        try
-        {
-            if (cl.isEmpty())
-                return null;
-            return Enum.valueOf(ConsistencyLevel.class, cl.toUpperCase());
-        }
-        catch (IllegalArgumentException e)
-        {
-            throw new ProtocolException("Unknown consistency level: " + cl);
-        }
+        return ConsistencyLevel.fromCode(cb.readUnsignedShort());
     }
 
     public static ChannelBuffer longStringToCB(String str)


Mime
View raw message