Exemplo n.º 1
0
  /**
   * Remove all the local of a context (but keep global).
   *
   * @param context a counter context
   * @return a version of {@code context} where no shards are local.
   */
  public ByteBuffer clearAllLocal(ByteBuffer context) {
    int count = Math.abs(context.getShort(context.position()));
    if (count == 0) return context; // no local or global shards present.

    List<Short> globalShardIndexes = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
      short elt = context.getShort(context.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH);
      if (elt < 0) globalShardIndexes.add(elt);
    }

    if (count == globalShardIndexes.size()) return context; // no local shards detected.

    // allocate a smaller BB for the cleared context - with no local header elts.
    ByteBuffer cleared =
        ByteBuffer.allocate(
            context.remaining() - (count - globalShardIndexes.size()) * HEADER_ELT_LENGTH);

    cleared.putShort(cleared.position(), (short) globalShardIndexes.size());
    for (int i = 0; i < globalShardIndexes.size(); i++)
      cleared.putShort(
          cleared.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH,
          globalShardIndexes.get(i));

    int origHeaderLength = headerLength(context);
    ByteBufferUtil.arrayCopy(
        context,
        context.position() + origHeaderLength,
        cleared,
        cleared.position() + headerLength(cleared),
        context.remaining() - origHeaderLength);

    return cleared;
  }
 public Validator(TreeRequest request) {
   this(
       request,
       // TODO: memory usage (maxsize) should either be tunable per
       // CF, globally, or as shared for all CFs in a cluster
       new MerkleTree(
           DatabaseDescriptor.getPartitioner(),
           request.range,
           MerkleTree.RECOMMENDED_DEPTH,
           (int) Math.pow(2, 15)));
 }
Exemplo n.º 3
0
  /** Detects whether or not the context has any legacy (local or remote) shards in it. */
  public boolean hasLegacyShards(ByteBuffer context) {
    int totalCount = (context.remaining() - headerLength(context)) / STEP_LENGTH;
    int localAndGlobalCount = Math.abs(context.getShort(context.position()));

    if (localAndGlobalCount < totalCount) return true; // remote shard(s) present

    for (int i = 0; i < localAndGlobalCount; i++)
      if (context.getShort(context.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH) >= 0)
        return true; // found a local shard

    return false;
  }
Exemplo n.º 4
0
 private static int headerLength(ByteBuffer context) {
   return HEADER_SIZE_LENGTH + Math.abs(context.getShort(context.position())) * HEADER_ELT_LENGTH;
 }