예제 #1
0
 // 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);
 }
예제 #2
0
  /** 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
  }
예제 #3
0
 public CounterId getCounterId() {
   return CounterId.wrap(context, context.position() + bodyOffset);
 }
예제 #4
0
 /**
  * Returns the clock and the count associated with the local counter id, or (0, 0) if no such
  * shard is present.
  */
 public ClockAndCount getLocalClockAndCount(ByteBuffer context) {
   return getClockAndCountOf(context, CounterId.getLocalId());
 }
예제 #5
0
 /**
  * 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;
 }