use of io.pravega.client.tables.TableKey in project pravega by pravega.
the class KeyValueTableTestBase method testIllegalEntrySizes.
/**
* Verifies that {@link TableEntry} or {@link TableKey} instances that have invalid key/value lengths are rejected.
*/
@Test
public void testIllegalEntrySizes() {
val rnd = new Random(0);
val limitPK = ByteBuffer.wrap(new byte[getPrimaryKeyLength()]);
val shortPK = ByteBuffer.allocate(getPrimaryKeyLength() - 1);
val longPK = ByteBuffer.allocate(getPrimaryKeyLength() + 1);
val limitSK = ByteBuffer.wrap(new byte[getSecondaryKeyLength()]);
val longSK = ByteBuffer.allocate(getSecondaryKeyLength() + 1);
val limitValue = ByteBuffer.wrap(new byte[KeyValueTable.MAXIMUM_VALUE_LENGTH]);
rnd.nextBytes(limitPK.array());
rnd.nextBytes(limitSK.array());
rnd.nextBytes(limitValue.array());
@Cleanup val kvt = createKeyValueTable();
kvt.update(new Insert(new TableKey(limitPK.duplicate(), limitSK.duplicate()), limitValue.duplicate())).join();
val resultValue = kvt.get(new TableKey(limitPK.duplicate(), limitSK.duplicate())).join().getValue();
Assert.assertEquals("Unexpected value returned.", limitValue.duplicate(), resultValue.duplicate());
// Wrong Key Length
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a PK that is too long.", () -> kvt.update(new Put(new TableKey(longPK.duplicate(), limitSK.duplicate()), limitValue.duplicate())), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a PK that is too long.", () -> kvt.update(new Remove(new TableKey(longPK.duplicate(), limitSK.duplicate()))), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a PK that is too short.", () -> kvt.update(new Put(new TableKey(shortPK.duplicate(), limitSK.duplicate()), limitValue.duplicate())), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a PK that is too short.", () -> kvt.update(new Remove(new TableKey(shortPK.duplicate(), limitSK.duplicate()))), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a SK that is too long.", () -> kvt.update(new Put(new TableKey(limitPK.duplicate(), longSK.duplicate()), limitValue.duplicate())), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a SK that is too long.", () -> kvt.update(new Remove(new TableKey(limitPK.duplicate(), longSK.duplicate()))), ex -> ex instanceof IllegalArgumentException);
if (getSecondaryKeyLength() > 0) {
val shortSK = ByteBuffer.allocate(getSecondaryKeyLength() - 1);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a SK that is too short.", () -> kvt.update(new Put(new TableKey(limitPK.duplicate(), shortSK.duplicate()), limitValue.duplicate())), ex -> ex instanceof IllegalArgumentException);
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a SK that is too short.", () -> kvt.update(new Remove(new TableKey(limitPK.duplicate(), shortSK.duplicate()))), ex -> ex instanceof IllegalArgumentException);
}
// Max Value Length exceeded.
AssertExtensions.assertSuppliedFutureThrows("Expected a rejection of a value that is too long.", () -> kvt.update(new Put(new TableKey(limitPK.duplicate(), limitSK.duplicate()), ByteBuffer.allocate(KeyValueTable.MAXIMUM_VALUE_LENGTH + 1))), ex -> ex instanceof IllegalArgumentException);
}
use of io.pravega.client.tables.TableKey in project pravega by pravega.
the class KeyValueTableTestBase method testMultiKeyOperations.
/**
* Tests the ability to perform multi-key updates, replacements and removals. These methods should be exercised:
* - {@link KeyValueTable#update(Iterable)} with {@link Insert} and {@link Put} instances.
* - {@link KeyValueTable#update(Iterable)} with {@link Remove}.
* - {@link KeyValueTable#getAll}
*/
@Test
public void testMultiKeyOperations() {
val versions = new Versions();
@Cleanup val kvt = createKeyValueTable();
// Conditional Insert.
val iteration = new AtomicInteger(0);
forEveryPrimaryKey((pk, secondaryKeys) -> {
List<TableModification> inserts = secondaryKeys.stream().map(sk -> new Insert(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(inserts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, inserts.size(), keyVersions.size());
for (int i = 0; i < inserts.size(); i++) {
versions.add(getUniqueKeyId(pk, inserts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Unconditional update.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
List<TableModification> puts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(puts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, puts.size(), keyVersions.size());
for (int i = 0; i < puts.size(); i++) {
versions.add(getUniqueKeyId(pk, puts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Conditional replace.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
// Failed update (bad version).
val pkValue = Math.abs(PK_SERIALIZER.deserialize(pk));
List<TableModification> badPuts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()), alterVersion(versions.get(getUniqueKeyId(pk, sk)), pkValue % 2 == 0, pkValue % 2 == 1))).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("update(Put) did not throw for bad version.", () -> kvt.update(badPuts), ex -> ex instanceof BadKeyVersionException);
// Correct update.
List<TableModification> puts = secondaryKeys.stream().map(sk -> new Put(new TableKey(pk, sk), getValue(pk, sk, iteration.get()), versions.get(getUniqueKeyId(pk, sk)))).collect(Collectors.toList());
val keyVersions = kvt.update(puts).join();
val hint = getUniqueKeyId(pk);
Assert.assertEquals("Unexpected result size" + hint, puts.size(), keyVersions.size());
for (int i = 0; i < puts.size(); i++) {
versions.add(getUniqueKeyId(pk, puts.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
// Conditional removal.
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
val hint = getUniqueKeyId(pk);
// Failed update (bad version).
val pkValue = Math.abs(PK_SERIALIZER.deserialize(pk));
List<TableModification> badRemovals = secondaryKeys.stream().map(sk -> new Remove(new TableKey(pk, sk), alterVersion(versions.get(getUniqueKeyId(pk, sk)), pkValue % 2 == 0, pkValue % 2 == 1))).collect(Collectors.toList());
AssertExtensions.assertSuppliedFutureThrows("update(Remove) did not throw for bad version." + hint, () -> kvt.update(badRemovals), ex -> ex instanceof BadKeyVersionException);
// Correct update.
List<TableModification> removals = secondaryKeys.stream().map(sk -> new Remove(new TableKey(pk, sk), versions.get(getUniqueKeyId(pk, sk)))).collect(Collectors.toList());
kvt.update(removals).join();
for (val sk : secondaryKeys) {
versions.remove(getUniqueKeyId(pk, sk));
}
});
Assert.assertTrue("Expected all keys to have been removed.", versions.isEmpty());
checkValues(iteration.get(), versions, kvt);
// Reinsert (conditionally)
iteration.incrementAndGet();
forEveryPrimaryKey((pk, secondaryKeys) -> {
val hint = getUniqueKeyId(pk);
List<TableModification> entries = secondaryKeys.stream().map(sk -> new Insert(new TableKey(pk, sk), getValue(pk, sk, iteration.get()))).collect(Collectors.toList());
val keyVersions = kvt.update(entries).join();
Assert.assertEquals("Unexpected result size" + hint, entries.size(), keyVersions.size());
for (int i = 0; i < entries.size(); i++) {
versions.add(getUniqueKeyId(pk, entries.get(i).getKey().getSecondaryKey()), keyVersions.get(i));
}
});
checkValues(iteration.get(), versions, kvt);
}
use of io.pravega.client.tables.TableKey in project pravega by pravega.
the class KeyValueTableTestBase method checkPrimaryKeyIterator.
private void checkPrimaryKeyIterator(KeyValueTable keyValueTable, Versions versions, int iteration) {
val itemsAtOnce = getSecondaryKeyCount() / 5;
forEveryPrimaryKey((pk, secondaryKeys) -> {
// Get all Table Keys and sort them.
val allKeys = secondaryKeys.stream().map(sk -> new TableKey(pk, sk)).sorted(KEY_COMPARATOR).collect(Collectors.toList());
val allEntries = allKeys.stream().map(k -> new TableEntry(k, versions.get(getUniqueKeyId(k.getPrimaryKey(), k.getSecondaryKey())), getValue(k.getPrimaryKey(), k.getSecondaryKey(), iteration))).collect(Collectors.toList());
// Issue various iterators (range) and verify against expected values.
for (int startIndex = 0; startIndex < allKeys.size() / 2; startIndex++) {
val endIndex = allKeys.size() - startIndex;
val expectedKeys = allKeys.subList(startIndex, endIndex);
val iterator = keyValueTable.iterator().maxIterationSize(itemsAtOnce).forPrimaryKey(pk, expectedKeys.get(0).getSecondaryKey(), expectedKeys.get(expectedKeys.size() - 1).getSecondaryKey());
val iteratorKeys = new ArrayList<TableKey>();
iterator.keys().collectRemaining(ii -> iteratorKeys.addAll(ii.getItems())).join();
AssertExtensions.assertListEquals("Unexpected keys returned from iterator.keys()", expectedKeys, iteratorKeys, this::areEqual);
val expectedEntries = allEntries.subList(startIndex, endIndex);
val iteratorEntries = new ArrayList<TableEntry>();
iterator.entries().collectRemaining(ii -> iteratorEntries.addAll(ii.getItems())).join();
AssertExtensions.assertListEquals("Unexpected entries returned from iterator.entries()", expectedEntries, iteratorEntries, (e1, e2) -> areEqual(e1, e2) && e1.getVersion().equals(e2.getVersion()));
}
});
}
use of io.pravega.client.tables.TableKey in project pravega by pravega.
the class KeyValueTableTestBase method testSingleKeyConditionalInserts.
/**
* Tests the ability to perform single-key conditional insertions. These methods are exercised:
* - {@link KeyValueTable#update} with {@link Insert} instances.
* - {@link KeyValueTable#get} and {@link KeyValueTable#getAll}.
*/
@Test
public void testSingleKeyConditionalInserts() {
val versions = new Versions();
@Cleanup val kvt = createKeyValueTable();
// Put (conditional insert)
val iteration = new AtomicInteger(0);
forEveryKey((pk, sk) -> {
val value = getValue(pk, sk, iteration.get());
// First one should work.
val insert = new Insert(new TableKey(pk, sk), value);
Version kv = kvt.update(insert).join();
versions.add(getUniqueKeyId(pk, sk), kv);
// Second one should throw.
AssertExtensions.assertSuppliedFutureThrows("insert() did not throw for already existing key.", () -> kvt.update(insert), ex -> ex instanceof BadKeyVersionException);
});
checkValues(iteration.get(), versions, kvt);
}
use of io.pravega.client.tables.TableKey in project pravega by pravega.
the class KeyValueTableTestBase method checkValues.
private void checkValues(int iteration, Map<ByteBuffer, Version> keyVersions, KeyValueTable kvt) {
// Using single get.
for (val k : keyVersions.entrySet()) {
val expectedValue = k.getValue() == null ? null : new TableEntry(new TableKey(k.getKey()), k.getValue(), getValue(k.getKey(), iteration));
val actualEntry = kvt.get(new TableKey(k.getKey())).join();
Assert.assertTrue(areEqual(expectedValue, actualEntry));
boolean exists = kvt.exists(new TableKey(k.getKey())).join();
Assert.assertEquals(expectedValue != null, exists);
}
// Using multi-get.
val requestKeys = new ArrayList<>(keyVersions.entrySet());
val entries = kvt.getAll(requestKeys.stream().map(k -> new TableKey(k.getKey())).collect(Collectors.toList())).join();
Assert.assertEquals(keyVersions.size(), entries.size());
for (int i = 0; i < requestKeys.size(); i++) {
val k = requestKeys.get(i);
val expectedValue = k.getValue() == null ? null : new TableEntry(new TableKey(k.getKey()), k.getValue(), getValue(k.getKey(), iteration));
val actualEntry = entries.get(i);
Assert.assertTrue(areEqual(expectedValue, actualEntry));
}
}
Aggregations