vcrfxia commented on code in PR #13143:
URL: https://github.com/apache/kafka/pull/13143#discussion_r1094000371


##########
streams/src/test/java/org/apache/kafka/streams/state/internals/LogicalKeyValueSegmentTest.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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 static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.common.serialization.Serializer;
+import org.apache.kafka.common.serialization.StringDeserializer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.streams.errors.InvalidStateStoreException;
+import org.apache.kafka.streams.processor.StateStoreContext;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.internals.metrics.RocksDBMetricsRecorder;
+import org.apache.kafka.test.InternalMockProcessorContext;
+import org.apache.kafka.test.StreamsTestUtils;
+import org.apache.kafka.test.TestUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class LogicalKeyValueSegmentTest {
+
+    private static final String STORE_NAME = "physical-rocks";
+    private static final String METRICS_SCOPE = "metrics-scope";
+    private static final String DB_FILE_DIR = "rocksdb";
+    private static final Serializer<String> STRING_SERIALIZER = new 
StringSerializer();
+    private static final Deserializer<String> STRING_DESERIALIZER = new 
StringDeserializer();
+
+    private RocksDBStore physicalStore;
+
+    private LogicalKeyValueSegment segment1;
+    private LogicalKeyValueSegment segment2;
+
+    @Before
+    public void setUp() {
+        physicalStore = new RocksDBStore(STORE_NAME, DB_FILE_DIR, new 
RocksDBMetricsRecorder(METRICS_SCOPE, STORE_NAME), false);
+        physicalStore.init((StateStoreContext) new 
InternalMockProcessorContext<>(
+            TestUtils.tempDirectory(),
+            Serdes.String(),
+            Serdes.String(),
+            new StreamsConfig(StreamsTestUtils.getStreamsConfig())
+        ), physicalStore);
+
+        segment1 = new LogicalKeyValueSegment(1, "segment-1", physicalStore);
+        segment2 = new LogicalKeyValueSegment(2, "segment-2", physicalStore);
+    }
+
+    @After
+    public void tearDown() {
+        segment1.close();
+        segment2.close();
+        physicalStore.close();
+    }
+
+    @Test
+    public void shouldPut() {
+        final KeyValue<String, String> kv0 = new KeyValue<>("1", "a");
+        final KeyValue<String, String> kv1 = new KeyValue<>("2", "b");
+
+        segment1.put(new Bytes(kv0.key.getBytes(UTF_8)), 
kv0.value.getBytes(UTF_8));
+        segment1.put(new Bytes(kv1.key.getBytes(UTF_8)), 
kv1.value.getBytes(UTF_8));
+        segment2.put(new Bytes(kv0.key.getBytes(UTF_8)), 
kv0.value.getBytes(UTF_8));
+        segment2.put(new Bytes(kv1.key.getBytes(UTF_8)), 
kv1.value.getBytes(UTF_8));
+
+        assertEquals("a", getAndDeserialize(segment1, "1"));
+        assertEquals("b", getAndDeserialize(segment1, "2"));
+        assertEquals("a", getAndDeserialize(segment2, "1"));
+        assertEquals("b", getAndDeserialize(segment2, "2"));
+    }
+
+    @Test
+    public void shouldPutAll() {
+        final List<KeyValue<Bytes, byte[]>> entries = new ArrayList<>();
+        entries.add(new KeyValue<>(
+            new Bytes(STRING_SERIALIZER.serialize(null, "1")),
+            STRING_SERIALIZER.serialize(null, "a")));
+        entries.add(new KeyValue<>(
+            new Bytes(STRING_SERIALIZER.serialize(null, "2")),
+            STRING_SERIALIZER.serialize(null, "b")));
+        entries.add(new KeyValue<>(
+            new Bytes(STRING_SERIALIZER.serialize(null, "3")),
+            STRING_SERIALIZER.serialize(null, "c")));
+
+        segment1.putAll(entries);
+        segment2.putAll(entries);
+
+        assertEquals("a", getAndDeserialize(segment1, "1"));
+        assertEquals("b", getAndDeserialize(segment1, "2"));
+        assertEquals("c", getAndDeserialize(segment1, "3"));
+        assertEquals("a", getAndDeserialize(segment2, "1"));
+        assertEquals("b", getAndDeserialize(segment2, "2"));
+        assertEquals("c", getAndDeserialize(segment2, "3"));
+    }
+
+    @Test
+    public void shouldPutIfAbsent() {
+        final Bytes keyBytes = new Bytes(STRING_SERIALIZER.serialize(null, 
"one"));
+        final byte[] valueBytes = STRING_SERIALIZER.serialize(null, "A");
+        final byte[] valueBytesUpdate = STRING_SERIALIZER.serialize(null, "B");
+
+        segment1.putIfAbsent(keyBytes, valueBytes);
+        segment1.putIfAbsent(keyBytes, valueBytesUpdate);
+        segment2.putIfAbsent(keyBytes, valueBytesUpdate);
+
+        assertEquals("A", STRING_DESERIALIZER.deserialize(null, 
segment1.get(keyBytes)));
+        assertEquals("B", STRING_DESERIALIZER.deserialize(null, 
segment2.get(keyBytes)));
+    }
+
+    @Test
+    public void shouldDelete() {
+        final KeyValue<String, String> kv0 = new KeyValue<>("1", "a");
+        final KeyValue<String, String> kv1 = new KeyValue<>("2", "b");
+
+        segment1.put(new Bytes(kv0.key.getBytes(UTF_8)), 
kv0.value.getBytes(UTF_8));
+        segment1.put(new Bytes(kv1.key.getBytes(UTF_8)), 
kv1.value.getBytes(UTF_8));
+        segment2.put(new Bytes(kv0.key.getBytes(UTF_8)), 
kv0.value.getBytes(UTF_8));
+        segment2.put(new Bytes(kv1.key.getBytes(UTF_8)), 
kv1.value.getBytes(UTF_8));
+        segment1.delete(new Bytes(kv0.key.getBytes(UTF_8)));
+
+        assertNull(segment1.get(new Bytes(kv0.key.getBytes(UTF_8))));
+        assertEquals("b", getAndDeserialize(segment1, "2"));
+        assertEquals("a", getAndDeserialize(segment2, "1"));
+        assertEquals("b", getAndDeserialize(segment2, "2"));
+    }
+
+    @Test
+    public void shouldReturnValuesOnRange() {
+        final KeyValue<String, String> kv0 = new KeyValue<>("0", "zero");
+        final KeyValue<String, String> kv1 = new KeyValue<>("1", "one");
+        final KeyValue<String, String> kv2 = new KeyValue<>("2", "two");
+        final KeyValue<String, String> kvOther = new KeyValue<>("1", "other");
+
+        segment1.put(new Bytes(kv0.key.getBytes(UTF_8)), 
kv0.value.getBytes(UTF_8));
+        segment1.put(new Bytes(kv1.key.getBytes(UTF_8)), 
kv1.value.getBytes(UTF_8));
+        segment1.put(new Bytes(kv2.key.getBytes(UTF_8)), 
kv2.value.getBytes(UTF_8));
+        segment2.put(new Bytes(kvOther.key.getBytes(UTF_8)), 
kvOther.value.getBytes(UTF_8));
+
+        final LinkedList<KeyValue<String, String>> expectedContents = new 
LinkedList<>();
+        expectedContents.add(kv0);
+        expectedContents.add(kv1);
+
+        try (final KeyValueIterator<Bytes, byte[]> iterator = 
segment1.range(null, new Bytes(STRING_SERIALIZER.serialize(null, "1")))) {

Review Comment:
   Heh, this additional test coverage caught a bug. Pushed a fix in the latest 
commit.



-- 
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