Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/context/CounterContextTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/context/CounterContextTest.java?rev=1063928&r1=1063927&r2=1063928&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/context/CounterContextTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/context/CounterContextTest.java Thu Jan 27 00:29:39 2011 @@ -24,6 +24,7 @@ import static org.junit.Assert.*; import java.net.InetAddress; import java.net.UnknownHostException; +import java.nio.ByteBuffer; import java.util.*; import org.apache.commons.lang.ArrayUtils; @@ -33,6 +34,7 @@ import org.junit.Test; import org.apache.cassandra.Util; import org.apache.cassandra.db.context.IContext.ContextRelationship; import org.apache.cassandra.utils.FBUtilities; +import org.apache.cassandra.utils.ByteBufferUtil; /** * Note: these tests assume IPv4 (4 bytes) is used for id. @@ -68,257 +70,192 @@ public class CounterContextTest @Test public void testCreate() { - byte[] context = cc.create(); - assert context.length == 0; - } - - @Test - public void testUpdatePresent() throws UnknownHostException - { - byte[] context; - - context = new byte[stepLength * defaultEntries]; - - for (int i = 0; i < defaultEntries; i++) - { - cc.writeElementAtStepOffset( - context, - i, - FBUtilities.toByteArray(i), - 1L, - 1L); - } - context = cc.update(context, InetAddress.getByAddress(FBUtilities.toByteArray(defaultEntries - 1)), 10L); - - assertEquals(context.length, stepLength * defaultEntries); - int offset = (defaultEntries - 1) * stepLength; - assertEquals( 2L, FBUtilities.byteArrayToLong(context, offset + idLength)); - assertEquals( 11L, FBUtilities.byteArrayToLong(context, offset + idLength + clockLength)); - for (int i = 0; i < defaultEntries - 1; i++) - { - offset = i * stepLength; - assertEquals( i, FBUtilities.byteArrayToInt(context, offset)); - assertEquals(1L, FBUtilities.byteArrayToLong(context, offset + idLength)); - assertEquals(1L, FBUtilities.byteArrayToLong(context, offset + idLength + clockLength)); - } - } - - @Test - public void testUpdateNotPresent() throws UnknownHostException - { - byte[] context = new byte[stepLength * 3]; - - for (int i = 0; i < 3; i++) - { - cc.writeElementAtStepOffset( - context, - i, - FBUtilities.toByteArray(i * 2), - 1L, - 1L); - } - - context = cc.update(context, InetAddress.getByAddress(FBUtilities.toByteArray(3)), 328L); - - assert context.length == stepLength * 4; - int offset = 2 * stepLength; - assert 1L == FBUtilities.byteArrayToLong(context, offset + idLength); - assert 328L == FBUtilities.byteArrayToLong(context, offset + idLength + clockLength); - for (int i = 1; i < 2; i++) - { - offset = i * stepLength; - assert 2 * i == FBUtilities.byteArrayToInt(context, offset); - assert 1L == FBUtilities.byteArrayToLong(context, offset + idLength); - assert 1L == FBUtilities.byteArrayToLong(context, offset + idLength + clockLength); - } - offset = 3 * stepLength; - assert 4 == FBUtilities.byteArrayToInt(context, offset); - assert 1L == FBUtilities.byteArrayToLong(context, offset + idLength); - assert 1L == FBUtilities.byteArrayToLong(context, offset + idLength + clockLength); + ByteBuffer context = cc.create(4); + assert context.remaining() == stepLength; } @Test public void testDiff() { - byte[] left = new byte[3 * stepLength]; - byte[] right; + ByteBuffer left = ByteBuffer.allocate(3 * stepLength); + ByteBuffer right; // equality: equal nodes, all counts same - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - right = ArrayUtils.clone(left); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + right = ByteBufferUtil.clone(left); assert ContextRelationship.EQUAL == cc.diff(left, right); // greater than: left has superset of nodes (counts equal) - left = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - cc.writeElementAtStepOffset(left, 3, FBUtilities.toByteArray(12), 0L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 1L, 0L); + left = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + cc.writeElementAtOffset(left, 3 * stepLength, FBUtilities.toByteArray(12), 0L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); assert ContextRelationship.GREATER_THAN == cc.diff(left, right); // less than: left has subset of nodes (counts equal) - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - - right = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 1L, 0L); - cc.writeElementAtStepOffset(right, 3, FBUtilities.toByteArray(12), 0L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + + right = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + cc.writeElementAtOffset(right, 3 * stepLength, FBUtilities.toByteArray(12), 0L, 0L); assert ContextRelationship.LESS_THAN == cc.diff(left, right); // greater than: equal nodes, but left has higher counts - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 3L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 1L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 3L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); assert ContextRelationship.GREATER_THAN == cc.diff(left, right); // less than: equal nodes, but right has higher counts - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 3L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 3L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 9L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 3L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 3L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 3L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 9L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 3L, 0L); assert ContextRelationship.LESS_THAN == cc.diff(left, right); // disjoint: right and left have disjoint node sets - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(4), 1L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 1L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 1L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(4), 1L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 1L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(4), 1L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(2), 1L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 1L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(12), 1L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(4), 1L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(2), 1L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 1L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(12), 1L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); // disjoint: equal nodes, but right and left have higher counts in differing nodes - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 1L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 5L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 1L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 5L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 2L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 1L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 9L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 5L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 2L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 1L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 9L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 5L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); // disjoint: left has more nodes, but lower counts - left = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 2L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 1L, 0L); - cc.writeElementAtStepOffset(left, 3, FBUtilities.toByteArray(12), 1L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 4L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 9L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 5L, 0L); + left = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 2L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 1L, 0L); + cc.writeElementAtOffset(left, 3 * stepLength, FBUtilities.toByteArray(12), 1L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 4L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 9L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 5L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); // disjoint: left has less nodes, but higher counts - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 5L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 2L, 0L); - - right = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 4L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 2L, 0L); - cc.writeElementAtStepOffset(right, 3, FBUtilities.toByteArray(12), 1L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 5L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); + + right = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 4L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); + cc.writeElementAtOffset(right, 3 * stepLength, FBUtilities.toByteArray(12), 1L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); // disjoint: mixed nodes and counts - left = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 5L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(9), 2L, 0L); - - right = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 4L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 2L, 0L); - cc.writeElementAtStepOffset(right, 3, FBUtilities.toByteArray(12), 1L, 0L); + left = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 5L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); + + right = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 4L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); + cc.writeElementAtOffset(right, 3 * stepLength, FBUtilities.toByteArray(12), 1L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); - left = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(3), 5L, 0L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(6), 2L, 0L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(7), 2L, 0L); - cc.writeElementAtStepOffset(left, 3, FBUtilities.toByteArray(9), 2L, 0L); - - right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(3), 4L, 0L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(6), 3L, 0L); - cc.writeElementAtStepOffset(right, 2, FBUtilities.toByteArray(9), 2L, 0L); + left = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(3), 5L, 0L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(6), 2L, 0L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(7), 2L, 0L); + cc.writeElementAtOffset(left, 3 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); + + right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(3), 4L, 0L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(6), 3L, 0L); + cc.writeElementAtOffset(right, 2 * stepLength, FBUtilities.toByteArray(9), 2L, 0L); assert ContextRelationship.DISJOINT == cc.diff(left, right); @@ -328,83 +265,83 @@ public class CounterContextTest public void testMerge() { // note: local counts aggregated; remote counts are reconciled (i.e. take max) - byte[] left = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(1), 1L, 1L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(2), 2L, 2L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(4), 6L, 3L); - cc.writeElementAtStepOffset( + ByteBuffer left = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(1), 1L, 1L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(2), 2L, 2L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(4), 6L, 3L); + cc.writeElementAtOffset( left, - 3, + 3 * stepLength, FBUtilities.getLocalAddress().getAddress(), 7L, 3L); - byte[] right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(4), 4L, 4L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(5), 5L, 5L); - cc.writeElementAtStepOffset( + ByteBuffer right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(4), 4L, 4L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(5), 5L, 5L); + cc.writeElementAtOffset( right, - 2, + 2 * stepLength, FBUtilities.getLocalAddress().getAddress(), 2L, 9L); - byte[] merged = cc.merge(left, right); + ByteBuffer merged = cc.merge(left, right); - assertEquals(5 * stepLength, merged.length); + assertEquals(5 * stepLength, merged.remaining()); // local node id's counts are aggregated - assertEquals(0, FBUtilities.compareByteSubArrays( - FBUtilities.getLocalAddress().getAddress(), + assertEquals(0, ByteBufferUtil.compareSubArrays( + ByteBuffer.wrap(FBUtilities.getLocalAddress().getAddress()), 0, merged, 4*stepLength, 4)); - assertEquals( 9L, FBUtilities.byteArrayToLong(merged, 4*stepLength + idLength)); - assertEquals(12L, FBUtilities.byteArrayToLong(merged, 4*stepLength + idLength + clockLength)); + assertEquals( 9L, merged.getLong(4*stepLength + idLength)); + assertEquals(12L, merged.getLong(4*stepLength + idLength + clockLength)); // remote node id counts are reconciled (i.e. take max) - assertEquals( 4, FBUtilities.byteArrayToInt(merged, 2*stepLength)); - assertEquals( 6L, FBUtilities.byteArrayToLong(merged, 2*stepLength + idLength)); - assertEquals( 3L, FBUtilities.byteArrayToLong(merged, 2*stepLength + idLength + clockLength)); - - assertEquals( 5, FBUtilities.byteArrayToInt(merged, 3*stepLength)); - assertEquals( 5L, FBUtilities.byteArrayToLong(merged, 3*stepLength + idLength)); - assertEquals( 5L, FBUtilities.byteArrayToLong(merged, 3*stepLength + idLength + clockLength)); - - assertEquals( 2, FBUtilities.byteArrayToInt(merged, 1*stepLength)); - assertEquals( 2L, FBUtilities.byteArrayToLong(merged, 1*stepLength + idLength)); - assertEquals( 2L, FBUtilities.byteArrayToLong(merged, 1*stepLength + idLength + clockLength)); - - assertEquals( 1, FBUtilities.byteArrayToInt(merged, 0*stepLength)); - assertEquals( 1L, FBUtilities.byteArrayToLong(merged, 0*stepLength + idLength)); - assertEquals( 1L, FBUtilities.byteArrayToLong(merged, 0*stepLength + idLength + clockLength)); + assertEquals( 4, merged.getInt( 2*stepLength)); + assertEquals( 6L, merged.getLong(2*stepLength + idLength)); + assertEquals( 3L, merged.getLong(2*stepLength + idLength + clockLength)); + + assertEquals( 5, merged.getInt( 3*stepLength)); + assertEquals( 5L, merged.getLong(3*stepLength + idLength)); + assertEquals( 5L, merged.getLong(3*stepLength + idLength + clockLength)); + + assertEquals( 2, merged.getInt( 1*stepLength)); + assertEquals( 2L, merged.getLong(1*stepLength + idLength)); + assertEquals( 2L, merged.getLong(1*stepLength + idLength + clockLength)); + + assertEquals( 1, merged.getInt( 0*stepLength)); + assertEquals( 1L, merged.getLong(0*stepLength + idLength)); + assertEquals( 1L, merged.getLong(0*stepLength + idLength + clockLength)); } @Test public void testTotal() { - byte[] left = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(left, 0, FBUtilities.toByteArray(1), 1L, 1L); - cc.writeElementAtStepOffset(left, 1, FBUtilities.toByteArray(2), 2L, 2L); - cc.writeElementAtStepOffset(left, 2, FBUtilities.toByteArray(4), 3L, 3L); - cc.writeElementAtStepOffset( + ByteBuffer left = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(left, 0 * stepLength, FBUtilities.toByteArray(1), 1L, 1L); + cc.writeElementAtOffset(left, 1 * stepLength, FBUtilities.toByteArray(2), 2L, 2L); + cc.writeElementAtOffset(left, 2 * stepLength, FBUtilities.toByteArray(4), 3L, 3L); + cc.writeElementAtOffset( left, - 3, + 3 * stepLength, FBUtilities.getLocalAddress().getAddress(), 3L, 3L); - byte[] right = new byte[3 * stepLength]; - cc.writeElementAtStepOffset(right, 0, FBUtilities.toByteArray(4), 4L, 4L); - cc.writeElementAtStepOffset(right, 1, FBUtilities.toByteArray(5), 5L, 5L); - cc.writeElementAtStepOffset( + ByteBuffer right = ByteBuffer.allocate(3 * stepLength); + cc.writeElementAtOffset(right, 0 * stepLength, FBUtilities.toByteArray(4), 4L, 4L); + cc.writeElementAtOffset(right, 1 * stepLength, FBUtilities.toByteArray(5), 5L, 5L); + cc.writeElementAtOffset( right, - 2, + 2 * stepLength, FBUtilities.getLocalAddress().getAddress(), 9L, 9L); - byte[] merged = cc.merge(left, right); + ByteBuffer merged = cc.merge(left, right); // 127.0.0.1: 12 (3+9) // 0.0.0.1: 1 @@ -412,34 +349,34 @@ public class CounterContextTest // 0.0.0.4: 4 // 0.0.0.5: 5 - assertEquals(24L, FBUtilities.byteArrayToLong(cc.total(merged))); + assertEquals(24L, cc.total(merged)); } @Test public void testCleanNodeCounts() throws UnknownHostException { - byte[] bytes = new byte[4 * stepLength]; - cc.writeElementAtStepOffset(bytes, 0, FBUtilities.toByteArray(1), 1L, 1L); - cc.writeElementAtStepOffset(bytes, 1, FBUtilities.toByteArray(2), 2L, 2L); - cc.writeElementAtStepOffset(bytes, 2, FBUtilities.toByteArray(4), 3L, 3L); - cc.writeElementAtStepOffset(bytes, 3, FBUtilities.toByteArray(8), 4L, 4L); + ByteBuffer bytes = ByteBuffer.allocate(4 * stepLength); + cc.writeElementAtOffset(bytes, 0 * stepLength, FBUtilities.toByteArray(1), 1L, 1L); + cc.writeElementAtOffset(bytes, 1 * stepLength, FBUtilities.toByteArray(2), 2L, 2L); + cc.writeElementAtOffset(bytes, 2 * stepLength, FBUtilities.toByteArray(4), 3L, 3L); + cc.writeElementAtOffset(bytes, 3 * stepLength, FBUtilities.toByteArray(8), 4L, 4L); - assertEquals(4, FBUtilities.byteArrayToInt(bytes, 2*stepLength)); - assertEquals(3L, FBUtilities.byteArrayToLong(bytes, 2*stepLength + idLength)); + assertEquals(4, bytes.getInt( 2*stepLength)); + assertEquals(3L, bytes.getLong(2*stepLength + idLength)); bytes = cc.cleanNodeCounts(bytes, InetAddress.getByAddress(FBUtilities.toByteArray(4))); // node: 0.0.0.4 should be removed - assertEquals(3 * stepLength, bytes.length); + assertEquals(3 * stepLength, bytes.remaining()); // other nodes should be unaffected - assertEquals(1, FBUtilities.byteArrayToInt(bytes, 0*stepLength)); - assertEquals(1L, FBUtilities.byteArrayToLong(bytes, 0*stepLength + idLength)); + assertEquals(1, bytes.getInt( 0*stepLength)); + assertEquals(1L, bytes.getLong(0*stepLength + idLength)); - assertEquals(2, FBUtilities.byteArrayToInt(bytes, 1*stepLength)); - assertEquals(2L, FBUtilities.byteArrayToLong(bytes, 1*stepLength + idLength)); + assertEquals(2, bytes.getInt( 1*stepLength)); + assertEquals(2L, bytes.getLong(1*stepLength + idLength)); - assertEquals(8, FBUtilities.byteArrayToInt(bytes, 2*stepLength)); - assertEquals(4L, FBUtilities.byteArrayToLong(bytes, 2*stepLength + idLength)); + assertEquals(8, bytes.getInt( 2*stepLength)); + assertEquals(4L, bytes.getLong(2*stepLength + idLength)); } }
Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java?rev=1063928&r1=1063927&r2=1063928&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/io/sstable/SSTableWriterAESCommutativeTest.java Thu Jan 27 00:29:39 2011 @@ -76,10 +76,8 @@ public class SSTableWriterAESCommutative ); cf.addColumn(new CounterColumn( ByteBufferUtil.bytes("x"), - ByteBuffer.wrap(cc.total(context)), - 0L, - context - )); + ByteBuffer.wrap(context), + 0L)); context = Util.concatByteArrays( FBUtilities.toByteArray(1), FBUtilities.toByteArray(7L), FBUtilities.toByteArray(12L), FBUtilities.getLocalAddress().getAddress(), @@ -90,10 +88,8 @@ public class SSTableWriterAESCommutative ); cf.addColumn(new CounterColumn( ByteBufferUtil.bytes("y"), - ByteBuffer.wrap(cc.total(context)), - 0L, - context - )); + ByteBuffer.wrap(context), + 0L)); buffer = new DataOutputBuffer(); ColumnFamily.serializer().serializeWithIndexes(cf, buffer); @@ -123,10 +119,8 @@ public class SSTableWriterAESCommutative ); cf.addColumn(new CounterColumn( ByteBufferUtil.bytes("x"), - ByteBuffer.wrap(cc.total(context)), - 0L, - context - )); + ByteBuffer.wrap(context), + 0L)); context = Util.concatByteArrays( FBUtilities.toByteArray(1), FBUtilities.toByteArray(7L), FBUtilities.toByteArray(12L), FBUtilities.toByteArray(3), FBUtilities.toByteArray(2L), FBUtilities.toByteArray(33L), @@ -134,10 +128,8 @@ public class SSTableWriterAESCommutative ); cf.addColumn(new CounterColumn( ByteBufferUtil.bytes("y"), - ByteBuffer.wrap(cc.total(context)), - 0L, - context - )); + ByteBuffer.wrap(context), + 0L)); buffer = new DataOutputBuffer(); ColumnFamily.serializer().serializeWithIndexes(cf, buffer); Modified: cassandra/trunk/test/unit/org/apache/cassandra/service/AntiEntropyServiceTestAbstract.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/service/AntiEntropyServiceTestAbstract.java?rev=1063928&r1=1063927&r2=1063928&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/service/AntiEntropyServiceTestAbstract.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/service/AntiEntropyServiceTestAbstract.java Thu Jan 27 00:29:39 2011 @@ -117,7 +117,7 @@ public abstract class AntiEntropyService List<RowMutation> rms = new LinkedList<RowMutation>(); RowMutation rm; rm = new RowMutation(tablename, ByteBufferUtil.bytes("key1")); - rm.add(new QueryPath(cfname, null, ByteBufferUtil.bytes("Column1")), ByteBufferUtil.bytes("asdf"), 0); + rm.add(new QueryPath(cfname, null, ByteBufferUtil.bytes("Column1")), ByteBufferUtil.bytes("asdfasdf"), 0); rms.add(rm); Util.writeColumnFamily(rms); Modified: cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java?rev=1063928&r1=1063927&r2=1063928&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/utils/FBUtilitiesTest.java Thu Jan 27 00:29:39 2011 @@ -70,88 +70,52 @@ public class FBUtilitiesTest } @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) { - ByteBuffer ba = ByteBufferUtil.bytes(i); - int actual = ByteBufferUtil.toInt(ba); - assertEquals(i, actual); - } - } - - @Test public void testCopyIntoBytes() { int i = 300; long l = 1000; - byte[] b = new byte[20]; - FBUtilities.copyIntoBytes(b, 0, i); - FBUtilities.copyIntoBytes(b, 4, l); - assertEquals(i, FBUtilities.byteArrayToInt(b, 0)); - assertEquals(l, FBUtilities.byteArrayToLong(b, 4)); + ByteBuffer b = ByteBuffer.allocate(20); + FBUtilities.copyIntoBytes(b.array(), 0, i); + FBUtilities.copyIntoBytes(b.array(), 4, l); + assertEquals(i, b.getInt(0)); + assertEquals(l, b.getLong(4)); } @Test - public void testLongBytesConversions() - { - // 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. - long[] longs = new long[] - { - -20L, -127L, -128L, 0L, 1L, 127L, 128L, 65534L, 65535L, -65534L, -65535L, - 4294967294L, 4294967295L, -4294967294L, -4294967295L - }; - - for (long l : longs) { - byte[] ba = FBUtilities.toByteArray(l); - long actual = FBUtilities.byteArrayToLong(ba); - assertEquals(l, actual); - } - } - - @Test public void testCompareByteSubArrays() { - byte[] bytes = new byte[16]; + ByteBuffer bytes = ByteBuffer.allocate(16); // handle null - assert FBUtilities.compareByteSubArrays( + assert ByteBufferUtil.compareSubArrays( null, 0, null, 0, 0) == 0; - assert FBUtilities.compareByteSubArrays( - null, 0, FBUtilities.toByteArray(524255231), 0, 4) == -1; - assert FBUtilities.compareByteSubArrays( - FBUtilities.toByteArray(524255231), 0, null, 0, 4) == 1; + assert ByteBufferUtil.compareSubArrays( + null, 0, ByteBufferUtil.bytes(524255231), 0, 4) == -1; + assert ByteBufferUtil.compareSubArrays( + ByteBufferUtil.bytes(524255231), 0, null, 0, 4) == 1; // handle comparisons - FBUtilities.copyIntoBytes(bytes, 3, 524255231); - assert FBUtilities.compareByteSubArrays( - bytes, 3, FBUtilities.toByteArray(524255231), 0, 4) == 0; - assert FBUtilities.compareByteSubArrays( - bytes, 3, FBUtilities.toByteArray(524255232), 0, 4) == -1; - assert FBUtilities.compareByteSubArrays( - bytes, 3, FBUtilities.toByteArray(524255230), 0, 4) == 1; + FBUtilities.copyIntoBytes(bytes.array(), 3, 524255231); + assert ByteBufferUtil.compareSubArrays( + bytes, 3, ByteBufferUtil.bytes(524255231), 0, 4) == 0; + assert ByteBufferUtil.compareSubArrays( + bytes, 3, ByteBufferUtil.bytes(524255232), 0, 4) == -1; + assert ByteBufferUtil.compareSubArrays( + bytes, 3, ByteBufferUtil.bytes(524255230), 0, 4) == 1; // check that incorrect length throws exception try { - assert FBUtilities.compareByteSubArrays( - bytes, 3, FBUtilities.toByteArray(524255231), 0, 24) == 0; + assert ByteBufferUtil.compareSubArrays( + bytes, 3, ByteBufferUtil.bytes(524255231), 0, 24) == 0; fail("Should raise an AssertionError."); } catch (AssertionError ae) { } try { - assert FBUtilities.compareByteSubArrays( - bytes, 3, FBUtilities.toByteArray(524255231), 0, 12) == 0; + assert ByteBufferUtil.compareSubArrays( + bytes, 3, ByteBufferUtil.bytes(524255231), 0, 12) == 0; fail("Should raise an AssertionError."); } catch (AssertionError ae) {