Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/ArrayBackedSortedColumnsTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/ArrayBackedSortedColumnsTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/ArrayBackedSortedColumnsTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/ArrayBackedSortedColumnsTest.java Wed Aug 17 21:52:41 2011 @@ -1,21 +1,15 @@ package org.apache.cassandra.db; -import java.io.ByteArrayInputStream; -import java.io.DataInputStream; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.*; -import org.apache.cassandra.SchemaLoader; import org.junit.Test; -import org.apache.cassandra.io.util.DataOutputBuffer; -import org.apache.cassandra.db.filter.QueryPath; -import static org.apache.cassandra.Util.column; import static org.junit.Assert.*; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.db.marshal.BytesType; +import org.apache.cassandra.utils.HeapAllocator; public class ArrayBackedSortedColumnsTest { @@ -32,7 +26,7 @@ public class ArrayBackedSortedColumnsTes int[] values = new int[]{ 1, 2, 2, 3 }; for (int i = 0; i < values.length; ++i) - map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i]))); + map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i])), HeapAllocator.instance); Iterator<IColumn> iter = map.iterator(); assertEquals("1st column", 1, iter.next().name().getInt(0)); @@ -56,12 +50,12 @@ public class ArrayBackedSortedColumnsTes int[] values2 = new int[]{ 2, 4, 5, 6 }; for (int i = 0; i < values1.length; ++i) - map.addColumn(new Column(ByteBufferUtil.bytes(values1[reversed ? values1.length - 1 - i : i]))); + map.addColumn(new Column(ByteBufferUtil.bytes(values1[reversed ? values1.length - 1 - i : i])), HeapAllocator.instance); for (int i = 0; i < values2.length; ++i) - map2.addColumn(new Column(ByteBufferUtil.bytes(values2[reversed ? values2.length - 1 - i : i]))); + map2.addColumn(new Column(ByteBufferUtil.bytes(values2[reversed ? values2.length - 1 - i : i])), HeapAllocator.instance); - map2.addAll(map); + map2.addAll(map, HeapAllocator.instance); Iterator<IColumn> iter = map2.iterator(); assertEquals("1st column", 1, iter.next().name().getInt(0)); @@ -91,7 +85,7 @@ public class ArrayBackedSortedColumnsTes Collections.reverse(reverseSorted); for (int i = 0; i < values.length; ++i) - map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i]))); + map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i])), HeapAllocator.instance); assertSame(sorted, map.getSortedColumns()); assertSame(reverseSorted, map.getReverseSortedColumns()); @@ -113,7 +107,7 @@ public class ArrayBackedSortedColumnsTes names.add(ByteBufferUtil.bytes(v)); for (int i = 0; i < values.length; ++i) - map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i]))); + map.addColumn(new Column(ByteBufferUtil.bytes(values[reversed ? values.length - 1 - i : i])), HeapAllocator.instance); assertSame(names, map.getColumnNames()); }
Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/ColumnFamilyTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/ColumnFamilyTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/ColumnFamilyTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/ColumnFamilyTest.java Wed Aug 17 21:52:41 2011 @@ -31,6 +31,7 @@ import org.apache.cassandra.io.util.Data import org.apache.cassandra.db.filter.QueryPath; import static org.apache.cassandra.Util.column; import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.HeapAllocator; public class ColumnFamilyTest extends SchemaLoader @@ -126,8 +127,8 @@ public class ColumnFamilyTest extends Sc cf_old.addColumn(QueryPath.column(ByteBufferUtil.bytes("col2")), val2, 1); cf_old.addColumn(QueryPath.column(ByteBufferUtil.bytes("col3")), val2, 2); - cf_result.addAll(cf_new); - cf_result.addAll(cf_old); + cf_result.addAll(cf_new, HeapAllocator.instance); + cf_result.addAll(cf_old, HeapAllocator.instance); assert 3 == cf_result.getColumnCount() : "Count is " + cf_new.getColumnCount(); //addcolumns will only add if timestamp >= old timestamp Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/CounterColumnTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/CounterColumnTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/CounterColumnTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/CounterColumnTest.java Wed Aug 17 21:52:41 2011 @@ -40,6 +40,8 @@ import org.apache.cassandra.db.context.C import static org.apache.cassandra.db.context.CounterContext.ContextState; import org.apache.cassandra.db.marshal.*; import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.utils.Allocator; +import org.apache.cassandra.utils.HeapAllocator; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.FBUtilities; import org.apache.cassandra.utils.NodeId; @@ -205,6 +207,7 @@ public class CounterColumnTest extends S @Test public void testDiff() throws UnknownHostException { + Allocator allocator = HeapAllocator.instance; ContextState left; ContextState right; @@ -226,7 +229,7 @@ public class CounterColumnTest extends S assert null == rightCol.diff(leftCol); // equality: equal nodes, all counts same - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -237,13 +240,13 @@ public class CounterColumnTest extends S assert null == leftCol.diff(rightCol); // greater than: left has superset of nodes (counts equal) - left = ContextState.allocate(4, 0); + left = ContextState.allocate(4, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); left.writeElement(NodeId.fromInt(12), 0L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 3L, 0L); right.writeElement(NodeId.fromInt(6), 2L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -256,12 +259,12 @@ public class CounterColumnTest extends S assert leftCol == rightCol.diff(leftCol); // disjoint: right and left have disjoint node sets - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 1L, 0L); left.writeElement(NodeId.fromInt(4), 1L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 1L, 0L); right.writeElement(NodeId.fromInt(6), 1L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -275,7 +278,8 @@ public class CounterColumnTest extends S @Test public void testSerializeDeserialize() throws IOException { - CounterContext.ContextState state = CounterContext.ContextState.allocate(4, 2); + Allocator allocator = HeapAllocator.instance; + CounterContext.ContextState state = CounterContext.ContextState.allocate(4, 2, allocator); state.writeElement(NodeId.fromInt(1), 4L, 4L); state.writeElement(NodeId.fromInt(2), 4L, 4L, true); state.writeElement(NodeId.fromInt(3), 4L, 4L); @@ -291,7 +295,7 @@ public class CounterColumnTest extends S assert original.equals(deserialized); bufIn = new ByteArrayInputStream(serialized, 0, serialized.length); - CounterColumn deserializedOnRemote = (CounterColumn)Column.serializer().deserialize(new DataInputStream(bufIn), null, true); + CounterColumn deserializedOnRemote = (CounterColumn)Column.serializer().deserialize(new DataInputStream(bufIn), true); assert deserializedOnRemote.name().equals(original.name()); assert deserializedOnRemote.total() == original.total(); assert deserializedOnRemote.value().equals(cc.clearAllDelta(original.value())); @@ -302,10 +306,11 @@ public class CounterColumnTest extends S @Test public void testUpdateDigest() throws Exception { + Allocator allocator = HeapAllocator.instance; MessageDigest digest1 = MessageDigest.getInstance("md5"); MessageDigest digest2 = MessageDigest.getInstance("md5"); - CounterContext.ContextState state = CounterContext.ContextState.allocate(4, 2); + CounterContext.ContextState state = CounterContext.ContextState.allocate(4, 2, allocator); state.writeElement(NodeId.fromInt(1), 4L, 4L); state.writeElement(NodeId.fromInt(2), 4L, 4L, true); state.writeElement(NodeId.fromInt(3), 4L, 4L); Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/CounterMutationTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/CounterMutationTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/CounterMutationTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/CounterMutationTest.java Wed Aug 17 21:52:41 2011 @@ -164,7 +164,7 @@ public class CounterMutationTest extends } // Check that if we merge old and clean on another node, we keep the right count - ByteBuffer onRemote = ctx.merge(ctx.clearAllDelta(state.context), ctx.clearAllDelta(cleaned)); + ByteBuffer onRemote = ctx.merge(ctx.clearAllDelta(state.context), ctx.clearAllDelta(cleaned), HeapAllocator.instance); assert ctx.total(onRemote) == 11; } } Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/ReadMessageTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/ReadMessageTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/ReadMessageTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/ReadMessageTest.java Wed Aug 17 21:52:41 2011 @@ -95,7 +95,7 @@ public class ReadMessageTest extends Sch ReadCommand command = new SliceByNamesReadCommand("Keyspace1", dk.key, new QueryPath("Standard1"), Arrays.asList(ByteBufferUtil.bytes("Column1"))); Row row = command.getRow(table); IColumn col = row.cf.getColumn(ByteBufferUtil.bytes("Column1")); - assert Arrays.equals(col.value().array(), "abcd".getBytes()); + assertEquals(col.value(), ByteBuffer.wrap("abcd".getBytes())); } @Test Modified: cassandra/trunk/test/unit/org/apache/cassandra/db/TimeSortTest.java URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/db/TimeSortTest.java?rev=1158927&r1=1158926&r2=1158927&view=diff ============================================================================== --- cassandra/trunk/test/unit/org/apache/cassandra/db/TimeSortTest.java (original) +++ cassandra/trunk/test/unit/org/apache/cassandra/db/TimeSortTest.java Wed Aug 17 21:52:41 2011 @@ -110,8 +110,8 @@ public class TimeSortTest extends Cleanu columnNames.add(getBytes(10)); columnNames.add(getBytes(0)); cf = cfStore.getColumnFamily(QueryFilter.getNamesFilter(Util.dk("900"), new QueryPath("StandardLong1"), columnNames)); - assert "c".equals(new String(cf.getColumn(getBytes(0)).value().array())); - assert "c".equals(new String(cf.getColumn(getBytes(10)).value().array())); + assert "c".equals(ByteBufferUtil.string(cf.getColumn(getBytes(0)).value())); + assert "c".equals(ByteBufferUtil.string(cf.getColumn(getBytes(10)).value())); } private void validateTimeSort(Table table) throws IOException 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=1158927&r1=1158926&r2=1158927&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 Wed Aug 17 21:52:41 2011 @@ -25,17 +25,15 @@ 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; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; - import org.apache.cassandra.Util; + import org.apache.cassandra.db.context.IContext.ContextRelationship; import static org.apache.cassandra.db.context.CounterContext.ContextState; -import org.apache.cassandra.utils.ByteBufferUtil; -import org.apache.cassandra.utils.NodeId; +import org.apache.cassandra.utils.*; public class CounterContextTest { @@ -55,17 +53,37 @@ public class CounterContextTest stepLength = idLength + clockLength + countLength; } + /** Allocates 1 byte from a new SlabAllocator and returns it. */ + private Allocator bumpedSlab() + { + SlabAllocator allocator = new SlabAllocator(); + allocator.allocate(1); + return allocator; + } + @Test public void testCreate() { - ByteBuffer context = cc.create(4); - assert context.remaining() == stepLength + 4; + runCreate(HeapAllocator.instance); + runCreate(bumpedSlab()); + } + + private void runCreate(Allocator allocator) + { + ByteBuffer bytes = cc.create(4, allocator); + assertEquals(stepLength + 4, bytes.remaining()); } @Test public void testDiff() { - ContextState left = ContextState.allocate(3, 0); + runDiff(HeapAllocator.instance); + runDiff(bumpedSlab()); + } + + private void runDiff(Allocator allocator) + { + ContextState left = ContextState.allocate(3, 0, allocator); ContextState right; // equality: equal nodes, all counts same @@ -78,13 +96,13 @@ public class CounterContextTest cc.diff(left.context, right.context); // greater than: left has superset of nodes (counts equal) - left = ContextState.allocate(4, 0); + left = ContextState.allocate(4, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); left.writeElement(NodeId.fromInt(12), 0L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 3L, 0L); right.writeElement(NodeId.fromInt(6), 2L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -93,12 +111,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // less than: left has subset of nodes (counts equal) - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(4, 0); + right = ContextState.allocate(4, 0, allocator); right.writeElement(NodeId.fromInt(3), 3L, 0L); right.writeElement(NodeId.fromInt(6), 2L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -108,12 +126,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // greater than: equal nodes, but left has higher counts - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 3L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 3L, 0L); right.writeElement(NodeId.fromInt(6), 2L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -122,12 +140,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // less than: equal nodes, but right has higher counts - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 3L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 3L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 3L, 0L); right.writeElement(NodeId.fromInt(6), 9L, 0L); right.writeElement(NodeId.fromInt(9), 3L, 0L); @@ -136,12 +154,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // disjoint: right and left have disjoint node sets - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 1L, 0L); left.writeElement(NodeId.fromInt(4), 1L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 1L, 0L); right.writeElement(NodeId.fromInt(6), 1L, 0L); right.writeElement(NodeId.fromInt(9), 1L, 0L); @@ -149,12 +167,12 @@ public class CounterContextTest assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context); - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 1L, 0L); left.writeElement(NodeId.fromInt(4), 1L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(2), 1L, 0L); right.writeElement(NodeId.fromInt(6), 1L, 0L); right.writeElement(NodeId.fromInt(12), 1L, 0L); @@ -163,12 +181,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // disjoint: equal nodes, but right and left have higher counts in differing nodes - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 1L, 0L); left.writeElement(NodeId.fromInt(6), 3L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 1L, 0L); right.writeElement(NodeId.fromInt(6), 1L, 0L); right.writeElement(NodeId.fromInt(9), 5L, 0L); @@ -176,12 +194,12 @@ public class CounterContextTest assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context); - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 2L, 0L); left.writeElement(NodeId.fromInt(6), 3L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 1L, 0L); right.writeElement(NodeId.fromInt(6), 9L, 0L); right.writeElement(NodeId.fromInt(9), 5L, 0L); @@ -190,13 +208,13 @@ public class CounterContextTest cc.diff(left.context, right.context); // disjoint: left has more nodes, but lower counts - left = ContextState.allocate(4, 0); + left = ContextState.allocate(4, 0, allocator); left.writeElement(NodeId.fromInt(3), 2L, 0L); left.writeElement(NodeId.fromInt(6), 3L, 0L); left.writeElement(NodeId.fromInt(9), 1L, 0L); left.writeElement(NodeId.fromInt(12), 1L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 4L, 0L); right.writeElement(NodeId.fromInt(6), 9L, 0L); right.writeElement(NodeId.fromInt(9), 5L, 0L); @@ -205,12 +223,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // disjoint: left has less nodes, but higher counts - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 5L, 0L); left.writeElement(NodeId.fromInt(6), 3L, 0L); left.writeElement(NodeId.fromInt(9), 2L, 0L); - right = ContextState.allocate(4, 0); + right = ContextState.allocate(4, 0, allocator); right.writeElement(NodeId.fromInt(3), 4L, 0L); right.writeElement(NodeId.fromInt(6), 3L, 0L); right.writeElement(NodeId.fromInt(9), 2L, 0L); @@ -220,12 +238,12 @@ public class CounterContextTest cc.diff(left.context, right.context); // disjoint: mixed nodes and counts - left = ContextState.allocate(3, 0); + left = ContextState.allocate(3, 0, allocator); left.writeElement(NodeId.fromInt(3), 5L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(9), 2L, 0L); - right = ContextState.allocate(4, 0); + right = ContextState.allocate(4, 0, allocator); right.writeElement(NodeId.fromInt(3), 4L, 0L); right.writeElement(NodeId.fromInt(6), 3L, 0L); right.writeElement(NodeId.fromInt(9), 2L, 0L); @@ -234,13 +252,13 @@ public class CounterContextTest assert ContextRelationship.DISJOINT == cc.diff(left.context, right.context); - left = ContextState.allocate(4, 0); + left = ContextState.allocate(4, 0, allocator); left.writeElement(NodeId.fromInt(3), 5L, 0L); left.writeElement(NodeId.fromInt(6), 2L, 0L); left.writeElement(NodeId.fromInt(7), 2L, 0L); left.writeElement(NodeId.fromInt(9), 2L, 0L); - right = ContextState.allocate(3, 0); + right = ContextState.allocate(3, 0, allocator); right.writeElement(NodeId.fromInt(3), 4L, 0L); right.writeElement(NodeId.fromInt(6), 3L, 0L); right.writeElement(NodeId.fromInt(9), 2L, 0L); @@ -252,60 +270,72 @@ public class CounterContextTest @Test public void testMerge() { + runMerge(HeapAllocator.instance); + runMerge(bumpedSlab()); + } + + private void runMerge(Allocator allocator) + { // note: local counts aggregated; remote counts are reconciled (i.e. take max) - ContextState left = ContextState.allocate(4, 1); + ContextState left = ContextState.allocate(4, 1, allocator); left.writeElement(NodeId.fromInt(1), 1L, 1L); left.writeElement(NodeId.fromInt(2), 2L, 2L); left.writeElement(NodeId.fromInt(4), 6L, 3L); left.writeElement(NodeId.getLocalId(), 7L, 3L, true); - ContextState right = ContextState.allocate(3, 1); + ContextState right = ContextState.allocate(3, 1, allocator); right.writeElement(NodeId.fromInt(4), 4L, 4L); right.writeElement(NodeId.fromInt(5), 5L, 5L); right.writeElement(NodeId.getLocalId(), 2L, 9L, true); - ByteBuffer merged = cc.merge(left.context, right.context); + ByteBuffer merged = cc.merge(left.context, right.context, allocator); int hd = 4; assertEquals(hd + 5 * stepLength, merged.remaining()); // local node id's counts are aggregated assert Util.equalsNodeId(NodeId.getLocalId(), merged, hd + 4*stepLength); - assertEquals( 9L, merged.getLong(hd + 4*stepLength + idLength)); - assertEquals(12L, merged.getLong(hd + 4*stepLength + idLength + clockLength)); + assertEquals( 9L, merged.getLong(merged.position() + hd + 4*stepLength + idLength)); + assertEquals(12L, merged.getLong(merged.position() + hd + 4*stepLength + idLength + clockLength)); // remote node id counts are reconciled (i.e. take max) assert Util.equalsNodeId(NodeId.fromInt(4), merged, hd + 2*stepLength); - assertEquals( 6L, merged.getLong(hd + 2*stepLength + idLength)); - assertEquals( 3L, merged.getLong(hd + 2*stepLength + idLength + clockLength)); + assertEquals( 6L, merged.getLong(merged.position() + hd + 2*stepLength + idLength)); + assertEquals( 3L, merged.getLong(merged.position() + hd + 2*stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(5), merged, hd + 3*stepLength); - assertEquals( 5L, merged.getLong(hd + 3*stepLength + idLength)); - assertEquals( 5L, merged.getLong(hd + 3*stepLength + idLength + clockLength)); + assertEquals( 5L, merged.getLong(merged.position() + hd + 3*stepLength + idLength)); + assertEquals( 5L, merged.getLong(merged.position() + hd + 3*stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(2), merged, hd + 1*stepLength); - assertEquals( 2L, merged.getLong(hd + 1*stepLength + idLength)); - assertEquals( 2L, merged.getLong(hd + 1*stepLength + idLength + clockLength)); + assertEquals( 2L, merged.getLong(merged.position() + hd + 1*stepLength + idLength)); + assertEquals( 2L, merged.getLong(merged.position() + hd + 1*stepLength + idLength + clockLength)); assert Util.equalsNodeId(NodeId.fromInt(1), merged, hd + 0*stepLength); - assertEquals( 1L, merged.getLong(hd + 0*stepLength + idLength)); - assertEquals( 1L, merged.getLong(hd + 0*stepLength + idLength + clockLength)); + assertEquals( 1L, merged.getLong(merged.position() + hd + 0*stepLength + idLength)); + assertEquals( 1L, merged.getLong(merged.position() + hd + 0*stepLength + idLength + clockLength)); } @Test public void testTotal() { - ContextState left = ContextState.allocate(4, 1); + runTotal(HeapAllocator.instance); + runTotal(bumpedSlab()); + } + + private void runTotal(Allocator allocator) + { + ContextState left = ContextState.allocate(4, 1, allocator); left.writeElement(NodeId.fromInt(1), 1L, 1L); left.writeElement(NodeId.fromInt(2), 2L, 2L); left.writeElement(NodeId.fromInt(4), 3L, 3L); left.writeElement(NodeId.getLocalId(), 3L, 3L, true); - ContextState right = ContextState.allocate(3, 1); + ContextState right = ContextState.allocate(3, 1, allocator); right.writeElement(NodeId.fromInt(4), 4L, 4L); right.writeElement(NodeId.fromInt(5), 5L, 5L); right.writeElement(NodeId.getLocalId(), 9L, 9L, true); - ByteBuffer merged = cc.merge(left.context, right.context); + ByteBuffer merged = cc.merge(left.context, right.context, allocator); // 127.0.0.1: 12 (3+9) // 0.0.0.1: 1 @@ -319,6 +349,12 @@ public class CounterContextTest @Test public void testMergeOldShards() { + runMergeOldShards(HeapAllocator.instance); + runMergeOldShards(bumpedSlab()); + } + + private void runMergeOldShards(Allocator allocator) + { long now = System.currentTimeMillis(); NodeId id1 = NodeId.fromInt(1); NodeId id3 = NodeId.fromInt(3); @@ -327,7 +363,7 @@ public class CounterContextTest records.add(new NodeId.NodeIdRecord(id3, 4L)); // Destination of merge is a delta - ContextState ctx = ContextState.allocate(5, 2); + ContextState ctx = ContextState.allocate(5, 2, allocator); ctx.writeElement(id1, 1L, 1L); ctx.writeElement(NodeId.fromInt(2), 2L, 2L); ctx.writeElement(id3, 3L, 3L, true); @@ -344,42 +380,48 @@ public class CounterContextTest assert m.getNodeId().equals(id3); assert m.getClock() == 4L; assert m.getCount() == 1L; - assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger)); + assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger, allocator)); // Source of merge is a delta - ctx = ContextState.allocate(4, 1); + ctx = ContextState.allocate(4, 1, allocator); ctx.writeElement(id1, 1L, 1L, true); ctx.writeElement(NodeId.fromInt(2), 2L, 2L); ctx.writeElement(id3, 3L, 3L); ctx.writeElement(NodeId.fromInt(4), 6L, 3L); merger = cc.computeOldShardMerger(ctx.context, records); - assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger)); + assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger, allocator)); // source and destination of merge are deltas - ctx = ContextState.allocate(4, 2); + ctx = ContextState.allocate(4, 2, allocator); ctx.writeElement(id1, 1L, 1L, true); ctx.writeElement(NodeId.fromInt(2), 2L, 2L); ctx.writeElement(id3, 3L, 3L, true); ctx.writeElement(NodeId.fromInt(4), 6L, 3L); merger = cc.computeOldShardMerger(ctx.context, records); - assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger)); + assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger, allocator)); // none of source and destination of merge are deltas - ctx = ContextState.allocate(4, 0); + ctx = ContextState.allocate(4, 0, allocator); ctx.writeElement(id1, 1L, 1L); ctx.writeElement(NodeId.fromInt(2), 2L, 2L); ctx.writeElement(id3, 3L, 3L); ctx.writeElement(NodeId.fromInt(4), 6L, 3L); merger = cc.computeOldShardMerger(ctx.context, records); - assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger)); + assert cc.total(ctx.context) == cc.total(cc.merge(ctx.context, merger, allocator)); } @Test public void testRemoveOldShards() { + runRemoveOldShards(HeapAllocator.instance); + runRemoveOldShards(bumpedSlab()); + } + + private void runRemoveOldShards(Allocator allocator) + { NodeId id1 = NodeId.fromInt(1); NodeId id3 = NodeId.fromInt(3); NodeId id6 = NodeId.fromInt(6); @@ -388,7 +430,7 @@ public class CounterContextTest records.add(new NodeId.NodeIdRecord(id3, 4L)); records.add(new NodeId.NodeIdRecord(id6, 10L)); - ContextState ctx = ContextState.allocate(6, 2); + ContextState ctx = ContextState.allocate(6, 2, allocator); ctx.writeElement(id1, 1L, 1L); ctx.writeElement(NodeId.fromInt(2), 2L, 2L); ctx.writeElement(id3, 3L, 3L, true); @@ -397,7 +439,7 @@ public class CounterContextTest ctx.writeElement(id6, 5L, 6L); ByteBuffer merger = cc.computeOldShardMerger(ctx.context, records); - ByteBuffer merged = cc.merge(ctx.context, merger); + ByteBuffer merged = cc.merge(ctx.context, merger, allocator); assert cc.total(ctx.context) == cc.total(merged); ByteBuffer cleaned = cc.removeOldShards(merged, (int)(System.currentTimeMillis() / 1000) + 1); @@ -405,12 +447,11 @@ public class CounterContextTest assert cleaned.remaining() == ctx.context.remaining() - stepLength; merger = cc.computeOldShardMerger(cleaned, records); - merged = cc.merge(cleaned, merger); + merged = cc.merge(cleaned, merger, allocator); assert cc.total(ctx.context) == cc.total(merged); cleaned = cc.removeOldShards(merged, (int)(System.currentTimeMillis() / 1000) + 1); assert cc.total(ctx.context) == cc.total(cleaned); assert cleaned.remaining() == ctx.context.remaining() - 2 * stepLength - 2; - } }
