guozhangwang commented on a change in pull request #11252:
URL: https://github.com/apache/kafka/pull/11252#discussion_r711198066



##########
File path: 
streams/src/main/java/org/apache/kafka/streams/state/internals/ListValueStore.java
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.kafka.streams.state.internals;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.kafka.common.utils.AbstractIterator;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.common.serialization.Serde;
+
+/**
+ * A wrapper key-value store that serializes the record values bytes as a list.
+ * As a result put calls would be interpreted as a get-append-put to the 
underlying RocksDB store.
+ * A put(k,null) will still delete the key, ie, the full list of all values of 
this key.
+ * Range iterators would also flatten the value lists and return the values 
one-by-one.
+ *
+ * This store is used for cases where we do not want to de-duplicate values of 
the same keys but want to retain all such values.
+ */
+@SuppressWarnings("unchecked")
+public class ListValueStore
+    extends WrappedStateStore<KeyValueStore<Bytes, byte[]>, Bytes, byte[]>
+    implements KeyValueStore<Bytes, byte[]> {
+
+    static private final Serde<List<byte[]>> LIST_SERDE = 
Serdes.ListSerde(ArrayList.class, Serdes.ByteArray());
+
+    ListValueStore(final KeyValueStore<Bytes, byte[]> bytesStore) {
+        super(bytesStore);
+    }
+
+    @Override
+    public void put(final Bytes key, final byte[] addedValue) {
+        // if the value is null we can skip the get and blind delete
+        if (addedValue == null) {
+            wrapped().put(key, null);
+        } else {
+            final byte[] oldValue = wrapped().get(key);
+
+            if (oldValue == null) {
+                wrapped().put(key, LIST_SERDE.serializer().serialize(null, 
Collections.singletonList(addedValue)));
+            } else {
+                final List<byte[]> list = 
LIST_SERDE.deserializer().deserialize(null, oldValue);
+                list.add(addedValue);
+
+                wrapped().put(key, LIST_SERDE.serializer().serialize(null, 
list));
+            }
+        }
+    }
+
+    @Override
+    public byte[] putIfAbsent(final Bytes key, final byte[] addedValue) {
+        final byte[] oldValue = wrapped().get(key);
+
+        if (oldValue != null) {
+            // if the value is null we can skip the get and blind delete
+            if (addedValue == null) {
+                wrapped().put(key, null);
+            } else {
+                final List<byte[]> list = 
LIST_SERDE.deserializer().deserialize(null, oldValue);
+                list.add(addedValue);
+
+                wrapped().put(key, LIST_SERDE.serializer().serialize(null, 
list));
+            }
+        }
+
+        return oldValue;

Review comment:
       The only caller of the `ListValueStore.get()` is the 
`ChangeloggingListValueStore`. So:
   
   * If logging is enabled, then we need to make sure that the correct value is 
returned from `get()` in order to log.
   * If logging is disabled, then there's no caller of that `get()` anyways so 
there's no concern of deserialization errors.




-- 
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: jira-unsubscr...@kafka.apache.org

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


Reply via email to