@Test public void testDiff() throws UnknownHostException { 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); 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); 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.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); 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.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); }
/** Creates a counter context with a single global, 2.1+ shard (a result of increment). */ public ByteBuffer createGlobal(CounterId id, long clock, long count) { ContextState state = ContextState.allocate(1, 0, 0); state.writeGlobal(id, clock, count); return state.context; }
/** * Creates a counter context with a single remote shard. For use by tests of compatibility with * pre-2.1 counters only. */ public ByteBuffer createRemote(CounterId id, long clock, long count) { ContextState state = ContextState.allocate(0, 0, 1); state.writeRemote(id, clock, count); return state.context; }
/** * Return a context w/ an aggregated count for each counter id. * * @param left counter context. * @param right counter context. */ public ByteBuffer merge(ByteBuffer left, ByteBuffer right) { boolean leftIsSuperSet = true; boolean rightIsSuperSet = true; int globalCount = 0; int localCount = 0; int remoteCount = 0; ContextState leftState = ContextState.wrap(left); ContextState rightState = ContextState.wrap(right); while (leftState.hasRemaining() && rightState.hasRemaining()) { int cmp = leftState.compareIdTo(rightState); if (cmp == 0) { Relationship rel = compare(leftState, rightState); if (rel == Relationship.GREATER_THAN) rightIsSuperSet = false; else if (rel == Relationship.LESS_THAN) leftIsSuperSet = false; else if (rel == Relationship.DISJOINT) leftIsSuperSet = rightIsSuperSet = false; if (leftState.isGlobal() || rightState.isGlobal()) globalCount += 1; else if (leftState.isLocal() || rightState.isLocal()) localCount += 1; else remoteCount += 1; leftState.moveToNext(); rightState.moveToNext(); } else if (cmp > 0) { leftIsSuperSet = false; if (rightState.isGlobal()) globalCount += 1; else if (rightState.isLocal()) localCount += 1; else remoteCount += 1; rightState.moveToNext(); } else // cmp < 0 { rightIsSuperSet = false; if (leftState.isGlobal()) globalCount += 1; else if (leftState.isLocal()) localCount += 1; else remoteCount += 1; leftState.moveToNext(); } } if (leftState.hasRemaining()) rightIsSuperSet = false; else if (rightState.hasRemaining()) leftIsSuperSet = false; // if one of the contexts is a superset, return it early. if (leftIsSuperSet) return left; else if (rightIsSuperSet) return right; while (leftState.hasRemaining()) { if (leftState.isGlobal()) globalCount += 1; else if (leftState.isLocal()) localCount += 1; else remoteCount += 1; leftState.moveToNext(); } while (rightState.hasRemaining()) { if (rightState.isGlobal()) globalCount += 1; else if (rightState.isLocal()) localCount += 1; else remoteCount += 1; rightState.moveToNext(); } leftState.reset(); rightState.reset(); return merge( ContextState.allocate(globalCount, localCount, remoteCount), leftState, rightState); }
/** * Creates a counter context with a single local shard. For use by tests of compatibility with * pre-2.1 counters only. */ public ByteBuffer createLocal(long count) { ContextState state = ContextState.allocate(0, 1, 0); state.writeLocal(CounterId.getLocalId(), 1L, count); return state.context; }