Search in sources :

Example 6 with Put

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);
}
Also used : lombok.val(lombok.val) BadKeyVersionException(io.pravega.client.tables.BadKeyVersionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(io.pravega.client.tables.Version) TableKey(io.pravega.client.tables.TableKey) Cleanup(lombok.Cleanup) Put(io.pravega.client.tables.Put) Test(org.junit.Test)

Example 7 with Put

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());
    }
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Insert(io.pravega.client.tables.Insert) IntStream(java.util.stream.IntStream) Put(io.pravega.client.tables.Put) Getter(lombok.Getter) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) Random(java.util.Random) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) BitConverter(io.pravega.common.util.BitConverter) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) AccessLevel(lombok.AccessLevel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyValueTableInfo(io.pravega.client.admin.KeyValueTableInfo) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Timeout(org.junit.rules.Timeout) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Serializer(io.pravega.client.stream.Serializer) LeakDetectorTestSuite(io.pravega.test.common.LeakDetectorTestSuite) TableEntry(io.pravega.client.tables.TableEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) lombok.val(lombok.val) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Version(io.pravega.client.tables.Version) TimeUnit(java.util.concurrent.TimeUnit) TableModification(io.pravega.client.tables.TableModification) List(java.util.List) Rule(org.junit.Rule) KeyValueTable(io.pravega.client.tables.KeyValueTable) TableKey(io.pravega.client.tables.TableKey) Assert(org.junit.Assert) BadKeyVersionException(io.pravega.client.tables.BadKeyVersionException) Remove(io.pravega.client.tables.Remove) ArrayList(java.util.ArrayList) TableKey(io.pravega.client.tables.TableKey) Cleanup(lombok.Cleanup) Put(io.pravega.client.tables.Put) Test(org.junit.Test)

Aggregations

Put (io.pravega.client.tables.Put)7 TableKey (io.pravega.client.tables.TableKey)7 Cleanup (lombok.Cleanup)7 lombok.val (lombok.val)7 Test (org.junit.Test)7 BadKeyVersionException (io.pravega.client.tables.BadKeyVersionException)6 Insert (io.pravega.client.tables.Insert)6 Remove (io.pravega.client.tables.Remove)6 Version (io.pravega.client.tables.Version)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Random (java.util.Random)5 KeyValueTableInfo (io.pravega.client.admin.KeyValueTableInfo)4 Serializer (io.pravega.client.stream.Serializer)4 KeyValueTable (io.pravega.client.tables.KeyValueTable)4 KeyValueTableConfiguration (io.pravega.client.tables.KeyValueTableConfiguration)4 TableEntry (io.pravega.client.tables.TableEntry)4 TableModification (io.pravega.client.tables.TableModification)4 BitConverter (io.pravega.common.util.BitConverter)4 AssertExtensions (io.pravega.test.common.AssertExtensions)4 LeakDetectorTestSuite (io.pravega.test.common.LeakDetectorTestSuite)4