consolodate BasicUtilities and FBUtilities

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

git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@834185 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Eric Evans 2009-11-09 19:32:09 +00:00
parent c9a6b7de39
commit b294cd555a
4 changed files with 24 additions and 80 deletions

View File

@ -22,12 +22,12 @@ import java.io.IOException;
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 @@ public class SystemTable
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 @@ public class SystemTable
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();

View File

@ -29,7 +29,6 @@ import java.io.DataInputStream;
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 @@ public class UdpConnection extends SelectionKeyHandler
{
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 @@ public class UdpConnection extends SelectionKeyHandler
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 )
{

View File

@ -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;
}
}

View File

@ -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 @@ public class FBUtilitiesTest
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);
}
}
}