Author: eevans
Date: Mon Nov  9 19:32:09 2009
New Revision: 834185

URL: http://svn.apache.org/viewvc?rev=834185&view=rev
Log:
consolodate BasicUtilities and FBUtilities

Patch by Gary Dusbabek; reviewed by eevans for CASSANDRA-516

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/net/UdpConnection.java
    
incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/BasicUtilities.java
    
incubator/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java?rev=834185&r1=834184&r2=834185&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java 
(original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/db/SystemTable.java 
Mon Nov  9 19:32:09 2009
@@ -22,12 +22,12 @@
 import java.io.UnsupportedEncodingException;
 import java.io.IOError;
 
+import org.apache.cassandra.utils.FBUtilities;
 import org.apache.log4j.Logger;
 
 import org.apache.cassandra.service.StorageService;
 import org.apache.cassandra.dht.Token;
 import org.apache.cassandra.dht.IPartitioner;
-import org.apache.cassandra.utils.BasicUtilities;
 import org.apache.cassandra.db.filter.IdentityQueryFilter;
 import org.apache.cassandra.db.filter.QueryPath;
 import org.apache.cassandra.db.filter.QueryFilter;
@@ -138,7 +138,7 @@
             RowMutation rm = new RowMutation(Table.SYSTEM_TABLE, LOCATION_KEY);
             cf = ColumnFamily.create(Table.SYSTEM_TABLE, 
SystemTable.STATUS_CF);
             cf.addColumn(new Column(TOKEN, 
p.getTokenFactory().toByteArray(token)));
-            cf.addColumn(new Column(GENERATION, 
BasicUtilities.intToByteArray(generation)));
+            cf.addColumn(new Column(GENERATION, 
FBUtilities.toByteArray(generation)));
             rm.add(cf);
             rm.apply();
             metadata = new StorageMetadata(token, generation);
@@ -151,11 +151,11 @@
         logger.info("Saved Token found: " + token);
 
         IColumn generation = cf.getColumn(GENERATION);
-        int gen = Math.max(BasicUtilities.byteArrayToInt(generation.value()) + 
1, (int) (System.currentTimeMillis() / 1000));
+        int gen = Math.max(FBUtilities.byteArrayToInt(generation.value()) + 1, 
(int) (System.currentTimeMillis() / 1000));
         
         RowMutation rm = new RowMutation(Table.SYSTEM_TABLE, LOCATION_KEY);
         cf = ColumnFamily.create(Table.SYSTEM_TABLE, SystemTable.STATUS_CF);
-        Column generation2 = new Column(GENERATION, 
BasicUtilities.intToByteArray(gen), generation.timestamp() + 1);
+        Column generation2 = new Column(GENERATION, 
FBUtilities.toByteArray(gen), generation.timestamp() + 1);
         cf.addColumn(generation2);
         rm.add(cf);
         rm.apply();

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/net/UdpConnection.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/net/UdpConnection.java?rev=834185&r1=834184&r2=834185&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/net/UdpConnection.java 
(original)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/net/UdpConnection.java 
Mon Nov  9 19:32:09 2009
@@ -29,7 +29,6 @@
 import java.io.DataOutputStream;
 import java.io.IOException;
 
-import org.apache.cassandra.utils.BasicUtilities;
 import org.apache.cassandra.utils.LogUtil;
 import org.apache.log4j.Logger;
 
@@ -72,7 +71,7 @@
         {  
             if (logger_.isTraceEnabled())
                 logger_.trace("Size of Gossip packet " + data.length);
-            byte[] protocol = BasicUtilities.intToByteArray(protocol_);
+            byte[] protocol = FBUtilities.toByteArray(protocol_);
             ByteBuffer buffer = ByteBuffer.allocate(data.length + 
protocol.length);
             buffer.put( protocol );
             buffer.put(data);
@@ -110,7 +109,7 @@
         byte[] body = new byte[0];        
         byte[] protocol = new byte[4];
         buffer = buffer.get(protocol, 0, protocol.length);
-        int value = BasicUtilities.byteArrayToInt(protocol);
+        int value = FBUtilities.byteArrayToInt(protocol);
         
         if ( protocol_ != value )
         {

Modified: 
incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/BasicUtilities.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/BasicUtilities.java?rev=834185&r1=834184&r2=834185&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/BasicUtilities.java
 (original)
+++ 
incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/BasicUtilities.java
 Mon Nov  9 19:32:09 2009
@@ -1,72 +0,0 @@
-/**
- * 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.utils;
-
-import java.nio.ByteBuffer;
-
-
-public class BasicUtilities
-{        
-       public static byte[] longToByteArray(long arg)
-       {      
-        byte[] retVal = new byte[8];
-        ByteBuffer.wrap(retVal).putLong(arg);
-        return retVal; 
-        }
-       
-       public static long byteArrayToLong(byte[] arg)
-       {
-        return ByteBuffer.wrap(arg).getLong();
-       }
-       
-       public static byte[] intToByteArray(int arg)
-       {      
-        byte[] retVal = new byte[4];
-        ByteBuffer.wrap(retVal).putInt(arg);
-        return retVal; 
-        }
-       
-       public static int byteArrayToInt(byte[] arg)
-       {
-        return ByteBuffer.wrap(arg).getInt();
-       }
-       
-       public static byte[] shortToByteArray(short arg)
-       {      
-        byte[] retVal = new byte[2];
-        ByteBuffer bb= ByteBuffer.wrap(retVal);
-        bb.putShort(arg);
-        return retVal; 
-        }
-       
-       public static short byteArrayToShort(byte[] arg)
-       {
-        return ByteBuffer.wrap(arg).getShort();
-    }
-       
-       public static byte[] booleanToByteArray(boolean b)
-       {
-           return b ? shortToByteArray((short)1) : shortToByteArray((short)0);
-       }
-       
-       public static boolean byteArrayToBoolean(byte[] arg)
-       {
-           return (byteArrayToShort(arg) == (short) 1) ? true : false;
-       }
-}

Modified: 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java?rev=834185&r1=834184&r2=834185&view=diff
==============================================================================
--- 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java
 (original)
+++ 
incubator/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java
 Mon Nov  9 19:32:09 2009
@@ -19,10 +19,10 @@
 package org.apache.cassandra.utils;
 
 import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
 
 import org.junit.Test;
 
-
 public class FBUtilitiesTest 
 {
        @Test
@@ -33,4 +33,21 @@
        byte[] c = FBUtilities.hexToBytes(s);
        assertArrayEquals(b, c);
     }
+
+    @Test
+    public void testIntBytesConversions()
+    {
+        // positive, negative, 1 and 2 byte cases, including a few edges that 
would foul things up unless you're careful
+        // about masking away sign extension.
+        int[] ints = new int[]
+        {
+            -20, -127, -128, 0, 1, 127, 128, 65534, 65535, -65534, -65535
+        };
+
+        for (int i : ints) {
+            byte[] ba = FBUtilities.toByteArray(i);
+            int actual = FBUtilities.byteArrayToInt(ba);
+            assertEquals(i, actual);
+        }
+    }
 }


Reply via email to