JingsongLi commented on code in PR #281:
URL: https://github.com/apache/flink-table-store/pull/281#discussion_r963238410


##########
flink-table-store-connector/src/main/java/org/apache/flink/table/store/connector/lookup/RocksDBSetState.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.store.connector.lookup;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.table.data.RowData;
+
+import org.rocksdb.ColumnFamilyHandle;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.rocksdb.RocksIterator;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** Rocksdb state for key -> Set values. */
+public class RocksDBSetState extends RocksDBState<List<byte[]>> {
+
+    private static final byte[] EMPTY = new byte[0];
+
+    public RocksDBSetState(
+            RocksDB db,
+            ColumnFamilyHandle columnFamily,
+            TypeSerializer<RowData> keySerializer,
+            TypeSerializer<RowData> valueSerializer,
+            long lruCacheSize) {
+        super(db, columnFamily, keySerializer, valueSerializer, lruCacheSize);
+    }
+
+    public List<RowData> get(RowData key) throws IOException {
+        ByteArray keyBytes = wrap(serializeKey(key));
+        List<byte[]> valueBytes = cache.getIfPresent(keyBytes);
+        if (valueBytes == null) {
+            valueBytes = new ArrayList<>();
+            try (RocksIterator iterator = db.newIterator(columnFamily)) {
+                iterator.seek(keyBytes.bytes);
+
+                while (iterator.isValid() && 
startWithKeyPrefix(keyBytes.bytes, iterator.key())) {
+                    byte[] rawKeyBytes = iterator.key();
+                    byte[] value =
+                            Arrays.copyOfRange(
+                                    rawKeyBytes, keyBytes.bytes.length, 
rawKeyBytes.length);
+                    valueBytes.add(value);
+                    iterator.next();
+                }
+            }
+            cache.put(keyBytes, valueBytes);
+        }
+
+        List<RowData> values = new ArrayList<>(valueBytes.size());
+        for (byte[] value : valueBytes) {
+            valueInputView.setBuffer(value);
+            values.add(valueSerializer.deserialize(valueInputView));
+        }
+        return values;
+    }
+
+    public void retract(RowData key, RowData value) throws IOException {
+        try {
+            byte[] bytes = invalidKeyAndGetKVBytes(key, value);
+            if (db.get(columnFamily, bytes) != null) {
+                db.delete(columnFamily, writeOptions, bytes);

Review Comment:
   The cache is `key -> List<V>`, here just one `k -> v`, it is hard to 
maintain, so just invalid Key in cache.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to