use of io.pravega.client.tables.Put in project pravega by pravega.
the class KeyValueTableTestBase method testSingleKeyUpdates.
/**
* Tests the ability to perform single-key updates and replacements. These methods are exercised:
* - {@link KeyValueTable#update} with {@link Put} instances.
* - {@link KeyValueTable#get} and {@link KeyValueTable#getAll}.
*/
@Test
public void testSingleKeyUpdates() {
val versions = new Versions();
@Cleanup val kvt = createKeyValueTable();
// Put (unconditional update).
val iteration = new AtomicInteger(0);
forEveryKey((pk, sk) -> {
val value = getValue(pk, sk, iteration.get());
Version kv = kvt.update(new Put(new TableKey(pk, sk), value)).join();
versions.add(getUniqueKeyId(pk, sk), kv);
});
checkValues(iteration.get(), versions, kvt);
// Put (conditional update (not insertion)).
iteration.incrementAndGet();
forEveryKey((pk, sk) -> {
val value = getValue(pk, sk, iteration.get());
val keyId = getUniqueKeyId(pk, sk);
val existingVersion = versions.get(keyId);
// Verify that conditions are checked both for segment names and their versions.
val pkValue = Math.abs(PK_SERIALIZER.deserialize(pk));
Version badVersion = alterVersion(existingVersion, pkValue % 2 == 0, pkValue % 2 == 1);
AssertExtensions.assertSuppliedFutureThrows("update(Put-Conditional) did not throw for bad version.", () -> kvt.update(new Put(new TableKey(pk, sk), value, badVersion)), ex -> ex instanceof BadKeyVersionException);
Version kv = kvt.update(new Put(new TableKey(pk, sk), value, existingVersion)).join();
versions.add(keyId, kv);
});
checkValues(iteration.get(), versions, kvt);
}
use of io.pravega.client.tables.Put in project pravega by pravega.
the class KeyValueTableTestBase method testLargeEntryBatchRetrieval.
/**
* Verify that multi-get retrieval from a single segment of keys totalling more than the limit(s) works correctly.
*/
@Test
public void testLargeEntryBatchRetrieval() {
val keyCount = TableSegment.MAXIMUM_BATCH_KEY_COUNT;
Function<Integer, byte[]> getValue = keyId -> {
val result = new byte[Long.BYTES];
BitConverter.writeInt(result, 0, keyId + 1);
return result;
};
@Cleanup val kvt = createKeyValueTable();
// Update the entries one-by-one to make sure we do not exceed the max lengths at this step.
val allKeys = new ArrayList<TableKey>();
for (int keyId = 0; keyId < keyCount; keyId++) {
val pk = new byte[getPrimaryKeyLength()];
val sk = new byte[getSecondaryKeyLength()];
BitConverter.writeLong(pk, 0, keyId);
BitConverter.writeInt(sk, 0, keyId);
val value = getValue.apply(keyId);
kvt.update(new Put(new TableKey(ByteBuffer.wrap(pk), ByteBuffer.wrap(sk)), ByteBuffer.wrap(value))).join();
allKeys.add(new TableKey(ByteBuffer.wrap(pk), ByteBuffer.wrap(sk)));
}
// Bulk-get all the keys. This should work regardless of the size of the data returned; the KeyValueTable and
// TableSegment internally should break down the requests and handle this properly.
val getResult = kvt.getAll(allKeys).join();
Assert.assertEquals("Unexpected number of keys returned.", allKeys.size(), getResult.size());
for (int keyId = 0; keyId < allKeys.size(); keyId++) {
val r = getResult.get(keyId);
val expectedKey = allKeys.get(keyId);
Assert.assertTrue("Unexpected key at index " + keyId, areEqual(expectedKey, r.getKey()));
val expectedValue = getValue.apply(keyId);
Assert.assertEquals("Unexpected value at index " + keyId, ByteBuffer.wrap(expectedValue), r.getValue());
}
}
Aggregations