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


##########
streams/src/test/java/org/apache/kafka/streams/state/internals/RocksDBVersionedStoreSegmentValueFormatterTest.java:
##########
@@ -0,0 +1,316 @@
+/*
+ * 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 org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import 
org.apache.kafka.streams.state.internals.RocksDBVersionedStoreSegmentValueFormatter.SegmentValue;
+import 
org.apache.kafka.streams.state.internals.RocksDBVersionedStoreSegmentValueFormatter.SegmentValue.SegmentSearchResult;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class RocksDBVersionedStoreSegmentValueFormatterTest {
+
+    private static final List<TestCase> TEST_CASES = new ArrayList<>();
+    static {
+        // test cases are expected to have timestamps in strictly decreasing 
order (except for the degenerate case)
+        TEST_CASES.add(new TestCase("degenerate", 10, new TestRecord(null, 
10)));
+        TEST_CASES.add(new TestCase("single record", 10, new 
TestRecord("foo".getBytes(), 1)));
+        TEST_CASES.add(new TestCase("multiple records", 10, new 
TestRecord("foo".getBytes(), 8), new TestRecord("bar".getBytes(), 3), new 
TestRecord("baz".getBytes(), 0)));
+        TEST_CASES.add(new TestCase("single tombstone", 10, new 
TestRecord(null, 1)));
+        TEST_CASES.add(new TestCase("multiple tombstone", 10, new 
TestRecord(null, 4), new TestRecord(null, 1)));
+        TEST_CASES.add(new TestCase("tombstones and records (r, t, r)", 10, 
new TestRecord("foo".getBytes(), 5), new TestRecord(null, 2), new 
TestRecord("bar".getBytes(), 1)));
+        TEST_CASES.add(new TestCase("tombstones and records (t, r, t)", 10, 
new TestRecord(null, 5), new TestRecord("foo".getBytes(), 2), new 
TestRecord(null, 1)));
+        TEST_CASES.add(new TestCase("tombstones and records (r, r, t, t)", 10, 
new TestRecord("foo".getBytes(), 6), new TestRecord("bar".getBytes(), 5), new 
TestRecord(null, 2), new TestRecord(null, 1)));
+        TEST_CASES.add(new TestCase("tombstones and records (t, t, r, r)", 10, 
new TestRecord(null, 7), new TestRecord(null, 6), new 
TestRecord("foo".getBytes(), 2), new TestRecord("bar".getBytes(), 1)));
+        TEST_CASES.add(new TestCase("record with empty bytes", 10, new 
TestRecord(new byte[0], 1)));
+        TEST_CASES.add(new TestCase("records with empty bytes (r, e)", 10, new 
TestRecord("foo".getBytes(), 4), new TestRecord(new byte[0], 1)));
+        TEST_CASES.add(new TestCase("records with empty bytes (e, e, r)", 10, 
new TestRecord(new byte[0], 8), new TestRecord(new byte[0], 2), new 
TestRecord("foo".getBytes(), 1)));
+    }
+
+    private final TestCase testCase;
+
+    public RocksDBVersionedStoreSegmentValueFormatterTest(final TestCase 
testCase) {
+        this.testCase = testCase;
+    }
+
+    @Parameterized.Parameters(name = "{0}")
+    public static Collection<TestCase> data() {
+        return TEST_CASES;
+    }
+
+    @Test
+    public void shouldSerializeAndDeserialize() {
+        final SegmentValue segmentValue = 
buildSegmentWithInsertLatest(testCase);
+
+        final byte[] serialized = segmentValue.serialize();
+        final SegmentValue deserialized = 
RocksDBVersionedStoreSegmentValueFormatter.deserialize(serialized);
+
+        verifySegmentContents(deserialized, testCase);
+    }
+
+    @Test
+    public void shouldBuildWithInsertLatest() {
+        final SegmentValue segmentValue = 
buildSegmentWithInsertLatest(testCase);
+
+        verifySegmentContents(segmentValue, testCase);
+    }
+
+    @Test
+    public void shouldBuildWithInsertEarliest() {
+        final SegmentValue segmentValue = 
buildSegmentWithInsertEarliest(testCase);
+
+        verifySegmentContents(segmentValue, testCase);
+    }
+
+    @Test
+    public void shouldInsertAtIndex() {
+        if (testCase.isDegenerate) {
+            // cannot insert into degenerate segment
+            return;
+        }
+
+        // test inserting at each possible index
+        for (int insertIdx = 0; insertIdx <= testCase.records.size(); 
insertIdx++) {
+            // build record to insert
+            final long newRecordTimestamp;
+            if (insertIdx == 0) {
+                newRecordTimestamp = testCase.records.get(0).timestamp + 1;
+                if (newRecordTimestamp == testCase.nextTimestamp) {
+                    // cannot insert because no timestamp exists between last 
record and nextTimestamp
+                    continue;
+                }
+            } else {
+                newRecordTimestamp = testCase.records.get(insertIdx - 
1).timestamp - 1;
+                if (newRecordTimestamp < 0 || (insertIdx < 
testCase.records.size() && newRecordTimestamp == 
testCase.records.get(insertIdx).timestamp)) {
+                    // cannot insert because timestamps of existing records 
are adjacent
+                    continue;
+                }
+            }
+            final TestRecord newRecord = new TestRecord("new".getBytes(), 
newRecordTimestamp);
+
+            final SegmentValue segmentValue = 
buildSegmentWithInsertLatest(testCase);
+
+            // insert() first requires a call to find()
+            if (insertIdx > 0) {
+                segmentValue.find(testCase.records.get(insertIdx - 
1).timestamp, false);
+            }
+            segmentValue.insert(newRecord.timestamp, newRecord.value, 
insertIdx);
+
+            // create expected results
+            final List<TestRecord> expectedRecords = new 
ArrayList<>(testCase.records);
+            expectedRecords.add(insertIdx, newRecord);
+
+            verifySegmentContents(segmentValue, new TestCase("expected", 
testCase.nextTimestamp, expectedRecords));
+        }
+    }
+
+    @Test
+    public void shouldUpdateAtIndex() {
+        if (testCase.isDegenerate) {
+            // cannot update degenerate segment
+            return;
+        }
+
+        // test updating at each possible index
+        for (int updateIdx = 0; updateIdx < testCase.records.size(); 
updateIdx++) {
+            // build updated record
+            long updatedRecordTimestamp = 
testCase.records.get(updateIdx).timestamp - 1;
+            if (updatedRecordTimestamp < 0 || (updateIdx < 
testCase.records.size() - 1 && updatedRecordTimestamp == 
testCase.records.get(updateIdx + 1).timestamp)) {
+                // found timestamp conflict. try again
+                updatedRecordTimestamp = 
testCase.records.get(updateIdx).timestamp + 1;
+                if (updateIdx > 0 && updatedRecordTimestamp == 
testCase.records.get(updateIdx - 1).timestamp) {
+                    // found timestamp conflict. use original timestamp
+                    updatedRecordTimestamp = 
testCase.records.get(updateIdx).timestamp;
+                }
+            }
+            final TestRecord updatedRecord = new 
TestRecord("updated".getBytes(), updatedRecordTimestamp);
+
+            final SegmentValue segmentValue = 
buildSegmentWithInsertLatest(testCase);
+
+            // updateRecord() first requires a call to find()
+            segmentValue.find(testCase.records.get(updateIdx).timestamp, 
false);
+            segmentValue.updateRecord(updatedRecord.timestamp, 
updatedRecord.value, updateIdx);
+
+            // create expected results
+            final List<TestRecord> expectedRecords = new 
ArrayList<>(testCase.records);
+            expectedRecords.remove(updateIdx);
+            expectedRecords.add(updateIdx, updatedRecord);
+
+            verifySegmentContents(segmentValue, new TestCase("expected", 
testCase.nextTimestamp, expectedRecords));
+        }
+    }
+
+    @Test
+    public void shouldFindByTimestamp() {
+        if (testCase.isDegenerate) {
+            // cannot find() on degenerate segment
+            return;
+        }
+
+        final SegmentValue segmentValue = 
buildSegmentWithInsertLatest(testCase);
+
+        // build expected mapping from timestamp -> record
+        final Map<Long, Integer> expectedRecordIndices = new HashMap<>();
+        for (int recordIdx = testCase.records.size() - 1; recordIdx >= 0; 
recordIdx--) {
+            if (recordIdx < testCase.records.size() - 1) {
+                
expectedRecordIndices.put(testCase.records.get(recordIdx).timestamp - 1, 
recordIdx + 1);
+            }
+            if (recordIdx > 0) {
+                
expectedRecordIndices.put(testCase.records.get(recordIdx).timestamp + 1, 
recordIdx);
+            }
+            
expectedRecordIndices.put(testCase.records.get(recordIdx).timestamp, recordIdx);
+        }
+
+        // verify results
+        for (final Map.Entry<Long, Integer> entry : 
expectedRecordIndices.entrySet()) {
+            final TestRecord expectedRecord = 
testCase.records.get(entry.getValue());
+            final long expectedValidTo = entry.getValue() == 0 ? 
testCase.nextTimestamp : testCase.records.get(entry.getValue() - 1).timestamp;
+
+            final SegmentSearchResult result = 
segmentValue.find(entry.getKey(), true);
+
+            assertThat(result.index(), equalTo(entry.getValue()));
+            assertThat(result.value(), equalTo(expectedRecord.value));
+            assertThat(result.validFrom(), equalTo(expectedRecord.timestamp));
+            assertThat(result.validTo(), equalTo(expectedValidTo));
+        }
+
+        // verify exception when timestamp is out of range
+        assertThrows(IllegalArgumentException.class, () -> 
segmentValue.find(testCase.nextTimestamp, false));
+        assertThrows(IllegalArgumentException.class, () -> 
segmentValue.find(testCase.nextTimestamp + 1, false));

Review Comment:
   You're right, they're redundant. I added both to make it clear that it's 
invalid to find not just at the `nextTimestamp` itself, but for all timestamps 
greater as well. I can remove this if its inclusion is more confusing than it's 
worth.



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