// write a tuple (counter id, clock, count) at an absolute (bytebuffer-wise) offset private void writeElementAtOffset( ByteBuffer ctx, int offset, CounterId id, long clock, long count) { ctx = ctx.duplicate(); ctx.position(offset); ctx.put(id.bytes().duplicate()); ctx.putLong(clock); ctx.putLong(count); }
/** Finds the position of a shard with the given id within the context (via binary search). */ @VisibleForTesting public int findPositionOf(ByteBuffer context, CounterId id) { int headerLength = headerLength(context); int offset = context.position() + headerLength; int left = 0; int right = (context.remaining() - headerLength) / STEP_LENGTH - 1; while (right >= left) { int middle = (left + right) / 2; int cmp = compareId(context, offset + middle * STEP_LENGTH, id.bytes(), id.bytes().position()); if (cmp == -1) left = middle + 1; else if (cmp == 0) return offset + middle * STEP_LENGTH; else right = middle - 1; } return -1; // position not found }