Search in sources :

Example 1 with RocksStateKeysAndNamespaceIterator

use of org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator in project flink by apache.

the class RocksDBRocksStateKeysAndNamespacesIteratorTest method testIteratorHelper.

@SuppressWarnings("unchecked")
<K> void testIteratorHelper(TypeSerializer<K> keySerializer, int maxKeyGroupNumber, Function<Integer, K> getKeyFunc) throws Exception {
    String testStateName = "aha";
    String namespace = "ns";
    try (RocksDBKeyedStateBackendTestFactory factory = new RocksDBKeyedStateBackendTestFactory()) {
        RocksDBKeyedStateBackend<K> keyedStateBackend = factory.create(tmp, keySerializer, maxKeyGroupNumber);
        ValueState<String> testState = keyedStateBackend.getPartitionedState(namespace, StringSerializer.INSTANCE, new ValueStateDescriptor<>(testStateName, String.class));
        // insert record
        for (int i = 0; i < 1000; ++i) {
            keyedStateBackend.setCurrentKey(getKeyFunc.apply(i));
            testState.update(String.valueOf(i));
        }
        DataOutputSerializer outputStream = new DataOutputSerializer(8);
        boolean ambiguousKeyPossible = CompositeKeySerializationUtils.isAmbiguousKeyPossible(keySerializer, StringSerializer.INSTANCE);
        CompositeKeySerializationUtils.writeNameSpace(namespace, StringSerializer.INSTANCE, outputStream, ambiguousKeyPossible);
        // already created with the state, should be closed with the backend
        ColumnFamilyHandle handle = keyedStateBackend.getColumnFamilyHandle(testStateName);
        try (RocksIteratorWrapper iterator = RocksDBOperationUtils.getRocksIterator(keyedStateBackend.db, handle, keyedStateBackend.getReadOptions());
            RocksStateKeysAndNamespaceIterator<K, String> iteratorWrapper = new RocksStateKeysAndNamespaceIterator<>(iterator, testStateName, keySerializer, StringSerializer.INSTANCE, keyedStateBackend.getKeyGroupPrefixBytes(), ambiguousKeyPossible)) {
            iterator.seekToFirst();
            // valid record
            List<Tuple2<Integer, String>> fetchedKeys = new ArrayList<>(1000);
            while (iteratorWrapper.hasNext()) {
                Tuple2 entry = iteratorWrapper.next();
                entry.f0 = Integer.parseInt(entry.f0.toString());
                fetchedKeys.add((Tuple2<Integer, String>) entry);
            }
            fetchedKeys.sort(Comparator.comparingInt(a -> a.f0));
            Assert.assertEquals(1000, fetchedKeys.size());
            for (int i = 0; i < 1000; ++i) {
                Assert.assertEquals(i, fetchedKeys.get(i).f0.intValue());
                Assert.assertEquals(namespace, fetchedKeys.get(i).f1);
            }
        }
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) Tuple2(org.apache.flink.api.java.tuple.Tuple2) CompositeKeySerializationUtils(org.apache.flink.runtime.state.CompositeKeySerializationUtils) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Test(org.junit.Test) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Function(java.util.function.Function) RocksStateKeysAndNamespaceIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator) ArrayList(java.util.ArrayList) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) List(java.util.List) Rule(org.junit.Rule) ValueState(org.apache.flink.api.common.state.ValueState) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) Assert(org.junit.Assert) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) ArrayList(java.util.ArrayList) RocksStateKeysAndNamespaceIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) Tuple2(org.apache.flink.api.java.tuple.Tuple2)

Aggregations

ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Function (java.util.function.Function)1 ValueState (org.apache.flink.api.common.state.ValueState)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)1 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 RocksStateKeysAndNamespaceIterator (org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator)1 DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)1 CompositeKeySerializationUtils (org.apache.flink.runtime.state.CompositeKeySerializationUtils)1 Assert (org.junit.Assert)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 TemporaryFolder (org.junit.rules.TemporaryFolder)1 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)1