Search in sources :

Example 6 with KeyValueTable

use of io.pravega.client.tables.KeyValueTable in project pravega by pravega.

the class KeyValueTableTestBase method checkGlobalIterator.

private void checkGlobalIterator(KeyValueTable keyValueTable, Versions versions, int iteration) {
    val itemsAtOnce = getSecondaryKeyCount() / 5;
    // All expected Entries, indexed by Keys.
    val allEntries = new HashMap<TableKey, TableEntry>();
    forEveryKey((pk, sk) -> {
        val k = new TableKey(pk, sk);
        val e = new TableEntry(k, versions.get(getUniqueKeyId(k.getPrimaryKey(), k.getSecondaryKey())), getValue(pk, sk, iteration));
        allEntries.put(k, e);
    });
    // All Keys, sorted.
    val allKeys = allEntries.keySet().stream().sorted(KEY_COMPARATOR).collect(Collectors.toList());
    // All unique Primary Keys.
    val allPrimaryKeys = allKeys.stream().map(TableKey::getPrimaryKey).distinct().sorted(KEY_COMPARATOR::compare).collect(Collectors.toList());
    // Issue various iterators (range) and verify against expected values.
    for (int startIndex = 0; startIndex < allPrimaryKeys.size() / 2; startIndex++) {
        val endIndex = allPrimaryKeys.size() - startIndex - 1;
        val firstPK = allPrimaryKeys.get(startIndex);
        val lastPK = allPrimaryKeys.get(endIndex);
        val expectedKeys = allKeys.stream().filter(k -> KEY_COMPARATOR.compare(k.getPrimaryKey(), firstPK) >= 0 && KEY_COMPARATOR.compare(k.getPrimaryKey(), lastPK) <= 0).collect(Collectors.toList());
        val iterator = keyValueTable.iterator().maxIterationSize(itemsAtOnce).forRange(firstPK, lastPK);
        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 = expectedKeys.stream().map(allEntries::get).collect(Collectors.toList());
        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()));
    }
    // Check all() iterator.
    val allIterator = keyValueTable.iterator().maxIterationSize(itemsAtOnce).all();
    val expectedAllEntries = allKeys.stream().map(allEntries::get).collect(Collectors.toList());
    val allIteratorEntries = new ArrayList<TableEntry>();
    allIterator.entries().collectRemaining(ii -> allIteratorEntries.addAll(ii.getItems())).join();
    AssertExtensions.assertListEquals("Unexpected entries returned from allIterator.entries()", expectedAllEntries, allIteratorEntries, (e1, e2) -> areEqual(e1, e2) && e1.getVersion().equals(e2.getVersion()));
}
Also used : lombok.val(lombok.val) TableEntry(io.pravega.client.tables.TableEntry) 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) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) TableKey(io.pravega.client.tables.TableKey)

Aggregations

KeyValueTableInfo (io.pravega.client.admin.KeyValueTableInfo)6 Serializer (io.pravega.client.stream.Serializer)6 BadKeyVersionException (io.pravega.client.tables.BadKeyVersionException)6 Insert (io.pravega.client.tables.Insert)6 KeyValueTable (io.pravega.client.tables.KeyValueTable)6 KeyValueTableConfiguration (io.pravega.client.tables.KeyValueTableConfiguration)6 Put (io.pravega.client.tables.Put)6 Remove (io.pravega.client.tables.Remove)6 TableEntry (io.pravega.client.tables.TableEntry)6 TableKey (io.pravega.client.tables.TableKey)6 TableModification (io.pravega.client.tables.TableModification)6 Version (io.pravega.client.tables.Version)6 BitConverter (io.pravega.common.util.BitConverter)6 AssertExtensions (io.pravega.test.common.AssertExtensions)6 LeakDetectorTestSuite (io.pravega.test.common.LeakDetectorTestSuite)6 ByteBuffer (java.nio.ByteBuffer)6 Duration (java.time.Duration)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)6