Search in sources :

Example 1 with Allocator

use of org.apache.cassandra.utils.Allocator in project eiger by wlloyd.

the class CounterColumnTest method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws IOException {
    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);
    state.writeElement(NodeId.fromInt(4), 4L, 4L, true);
    CounterColumn original = new CounterColumn(ByteBufferUtil.bytes("x"), state.context, 1L);
    DataOutputBuffer bufOut = new DataOutputBuffer();
    Column.serializer().serialize(original, bufOut);
    byte[] serialized = bufOut.getData();
    ByteArrayInputStream bufIn = new ByteArrayInputStream(serialized, 0, serialized.length);
    CounterColumn deserialized = (CounterColumn) Column.serializer().deserialize(new DataInputStream(bufIn));
    assert original.equals(deserialized);
    bufIn = new ByteArrayInputStream(serialized, 0, serialized.length);
    CounterColumn deserializedOnRemote = (CounterColumn) Column.serializer().deserialize(new DataInputStream(bufIn), IColumnSerializer.Flag.FROM_REMOTE);
    assert deserializedOnRemote.name().equals(original.name());
    assert deserializedOnRemote.total() == original.total();
    assert deserializedOnRemote.value().equals(cc.clearAllDelta(original.value()));
    assert deserializedOnRemote.timestamp() == deserialized.timestamp();
    assert deserializedOnRemote.timestampOfLastDelete() == deserialized.timestampOfLastDelete();
}
Also used : HeapAllocator(org.apache.cassandra.utils.HeapAllocator) Allocator(org.apache.cassandra.utils.Allocator) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputStream(java.io.DataInputStream) ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) CounterContext(org.apache.cassandra.db.context.CounterContext) Test(org.junit.Test)

Example 2 with Allocator

use of org.apache.cassandra.utils.Allocator in project eiger by wlloyd.

the class CounterColumnTest method testUpdateDigest.

@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, allocator);
    state.writeElement(NodeId.fromInt(1), 4L, 4L);
    state.writeElement(NodeId.fromInt(2), 4L, 4L, true);
    state.writeElement(NodeId.fromInt(3), 4L, 4L);
    state.writeElement(NodeId.fromInt(4), 4L, 4L, true);
    CounterColumn original = new CounterColumn(ByteBufferUtil.bytes("x"), state.context, 1L);
    CounterColumn cleared = new CounterColumn(ByteBufferUtil.bytes("x"), cc.clearAllDelta(state.context), 1L);
    original.updateDigest(digest1);
    cleared.updateDigest(digest2);
    assert Arrays.equals(digest1.digest(), digest2.digest());
}
Also used : HeapAllocator(org.apache.cassandra.utils.HeapAllocator) Allocator(org.apache.cassandra.utils.Allocator) MessageDigest(java.security.MessageDigest) ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) CounterContext(org.apache.cassandra.db.context.CounterContext) Test(org.junit.Test)

Example 3 with Allocator

use of org.apache.cassandra.utils.Allocator in project eiger by wlloyd.

the class CounterColumnTest method testDiff.

@Test
public void testDiff() throws UnknownHostException {
    Allocator allocator = HeapAllocator.instance;
    ContextState left;
    ContextState right;
    CounterColumn leftCol;
    CounterColumn rightCol;
    // timestamp
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 2L);
    assert rightCol == leftCol.diff(rightCol);
    assert null == rightCol.diff(leftCol);
    // timestampOfLastDelete
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), 0, 1L, 2L);
    assert rightCol == leftCol.diff(rightCol);
    assert null == rightCol.diff(leftCol);
    // equality: equal nodes, all counts same
    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 = new ContextState(ByteBufferUtil.clone(left.context), 2);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert null == leftCol.diff(rightCol);
    // greater than: left has superset of nodes (counts equal)
    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, allocator);
    right.writeElement(NodeId.fromInt(3), 3L, 0L);
    right.writeElement(NodeId.fromInt(6), 2L, 0L);
    right.writeElement(NodeId.fromInt(9), 1L, 0L);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert null == leftCol.diff(rightCol);
    // less than: right has subset of nodes (counts equal)
    assert leftCol == rightCol.diff(leftCol);
    // disjoint: right and left have disjoint node sets
    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, allocator);
    right.writeElement(NodeId.fromInt(3), 1L, 0L);
    right.writeElement(NodeId.fromInt(6), 1L, 0L);
    right.writeElement(NodeId.fromInt(9), 1L, 0L);
    leftCol = new CounterColumn(ByteBufferUtil.bytes("x"), left.context, 1L);
    rightCol = new CounterColumn(ByteBufferUtil.bytes("x"), right.context, 1L);
    assert rightCol == leftCol.diff(rightCol);
    assert leftCol == rightCol.diff(leftCol);
}
Also used : HeapAllocator(org.apache.cassandra.utils.HeapAllocator) Allocator(org.apache.cassandra.utils.Allocator) ContextState(org.apache.cassandra.db.context.CounterContext.ContextState) Test(org.junit.Test)

Aggregations

ContextState (org.apache.cassandra.db.context.CounterContext.ContextState)3 Allocator (org.apache.cassandra.utils.Allocator)3 HeapAllocator (org.apache.cassandra.utils.HeapAllocator)3 Test (org.junit.Test)3 CounterContext (org.apache.cassandra.db.context.CounterContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 MessageDigest (java.security.MessageDigest)1 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)1