@Override public String toString() { return String.format( "CounterCacheKey(%s, %s, %s)", cfId, ByteBufferUtil.bytesToHex(ByteBuffer.wrap(partitionKey)), ByteBufferUtil.bytesToHex(ByteBuffer.wrap(cellName))); }
/** * 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; }
@Test public void testGetSliceWithCollision() throws Exception { Keyspace keyspace = Keyspace.open(KEYSPACE); ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF); cfs.clearUnsafe(); insert("k1", "k2", "k3"); // token = 2 insert("key1", "key2", "key3"); // token = 4 insert("longKey1", "longKey2"); // token = 8 List<Row> rows = cfs.getRangeSlice( new Bounds<RowPosition>(dk("k2"), dk("key2")), null, new IdentityQueryFilter(), 10000); assert rows.size() == 4 : "Expecting 4 keys, got " + rows.size(); assert rows.get(0).key.key.equals(ByteBufferUtil.bytes("k2")); assert rows.get(1).key.key.equals(ByteBufferUtil.bytes("k3")); assert rows.get(2).key.key.equals(ByteBufferUtil.bytes("key1")); assert rows.get(3).key.key.equals(ByteBufferUtil.bytes("key2")); }
/** * Mark context to delete local references afterward. Marking is done by multiply #elt by -1 to * preserve header length and #elt count in order to clear all local refs later. * * @param context a counter context * @return context that marked to delete local refs */ public ByteBuffer markLocalToBeCleared(ByteBuffer context) { short count = context.getShort(context.position()); if (count <= 0) return context; // already marked or all are remote. boolean hasLocalShards = false; for (int i = 0; i < count; i++) { if (context.getShort(context.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH) >= 0) { hasLocalShards = true; break; } } if (!hasLocalShards) return context; // all shards are global or remote. ByteBuffer marked = ByteBuffer.allocate(context.remaining()); marked.putShort(marked.position(), (short) (count * -1)); ByteBufferUtil.arrayCopy( context, context.position() + HEADER_SIZE_LENGTH, marked, marked.position() + HEADER_SIZE_LENGTH, context.remaining() - HEADER_SIZE_LENGTH); return marked; }
static final FilterKey bytes(String s) { return new BufferDecoratedKey(new LongToken(0L), ByteBufferUtil.bytes(s)); }
private void insert(String key) { Mutation rm; rm = new Mutation(KEYSPACE, ByteBufferUtil.bytes(key)); rm.add(CF, Util.cellname("column"), ByteBufferUtil.bytes("asdf"), 0); rm.apply(); }
public Token<BigInteger> fromByteArray(ByteBuffer bytes) { return new BigIntegerToken(new BigInteger(ByteBufferUtil.getArray(bytes))); }
private CounterCacheKey(UUID cfId, ByteBuffer partitionKey, CellName cellName) { this.cfId = cfId; this.partitionKey = ByteBufferUtil.getArray(partitionKey); this.cellName = ByteBufferUtil.getArray(cellName.toByteBuffer()); }
private static int compareId(ByteBuffer bb1, int pos1, ByteBuffer bb2, int pos2) { return ByteBufferUtil.compareSubArrays(bb1, pos1, bb2, pos2, CounterId.LENGTH); }