cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jun...@apache.org
Subject svn commit: r891038 - in /incubator/cassandra/trunk/src/java/org/apache/cassandra/utils: FBUtilities.java Pair.java
Date Tue, 15 Dec 2009 22:28:54 GMT
Author: junrao
Date: Tue Dec 15 22:28:53 2009
New Revision: 891038

URL: http://svn.apache.org/viewvc?rev=891038&view=rev
Log:
utility change; patched by Stu Hood, reviewed by junrao for CASSANDRA-629

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/FBUtilities.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Pair.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/FBUtilities.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/FBUtilities.java?rev=891038&r1=891037&r2=891038&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/FBUtilities.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/FBUtilities.java Tue Dec
15 22:28:53 2009
@@ -135,24 +135,40 @@
         }
         if(null == bytes2) return 1;
 
-        for(int i = 0; i < bytes1.length && i < bytes2.length; i++){
-            int cmp = compareBytes(bytes1[i], bytes2[i]);
-            if(0 != cmp) return cmp;
+        int minLength = Math.min(bytes1.length, bytes2.length);
+        for(int i = 0; i < minLength; i++)
+        {
+            if(bytes1[i] == bytes2[i])
+                continue;
+            // compare non-equal bytes as unsigned
+            return (bytes1[i] & 0xFF) < (bytes2[i] & 0xFF) ? -1 : 1;
         }
         if(bytes1.length == bytes2.length) return 0;
         else return (bytes1.length < bytes2.length)? -1 : 1;
     }
 
-    public static int compareBytes(byte b1, byte b2){
-        return compareBytes((int)b1, (int)b2);
-    }
+    /**
+     * @return The bitwise XOR of the inputs. The output will be the same length as the
+     * longer input, but if either input is null, the output will be null.
+     */
+    public static byte[] xor(byte[] left, byte[] right)
+    {
+        if (left == null || right == null)
+            return null;
+        if (left.length > right.length)
+        {
+            byte[] swap = left;
+            left = right;
+            right = swap;
+        }
 
-    public static int compareBytes(int b1, int b2){
-        int i1 = b1 & 0xFF;
-        int i2 = b2 & 0xFF;
-        if(i1 < i2) return -1;
-        else if(i1 == i2) return 0;
-        else return 1;
+        // left.length is now <= right.length
+        byte[] out = Arrays.copyOf(right, right.length);
+        for (int i = 0; i < left.length; i++)
+        {
+            out[i] = (byte)((left[i] & 0xFF) ^ (right[i] & 0xFF));
+        }
+        return out;
     }
 
     public static BigInteger hash(String data)

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Pair.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Pair.java?rev=891038&r1=891037&r2=891038&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Pair.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Pair.java Tue Dec 15 22:28:53
2009
@@ -18,7 +18,7 @@
 
 package org.apache.cassandra.utils;
 
-public class Pair<T1, T2>
+public final class Pair<T1, T2>
 {
     public final T1 left;
     public final T2 right;



Mime
View raw message