aliehsaeedii commented on code in PR #21830:
URL: https://github.com/apache/kafka/pull/21830#discussion_r3712783504


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MappingKeyValueIteratorAdapter.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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 org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.state.HeadersBytesStore;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.SessionStore;
+import org.apache.kafka.streams.state.SessionStoreWithHeaders;
+import org.apache.kafka.streams.state.TimestampedKeyValueStore;
+import org.apache.kafka.streams.state.TimestampedKeyValueStoreWithHeaders;
+import org.apache.kafka.streams.state.WindowStoreIterator;
+
+import java.util.function.Function;
+
+/**
+ * Delegates to an inner {@link KeyValueIterator} and maps each value byte 
array
+ * through the given function (e.g. header-format conversion).
+ */
+class MappingKeyValueIteratorAdapter<K> implements KeyValueIterator<K, byte[]> 
{
+
+    private final KeyValueIterator<K, byte[]> innerIterator;
+    private final Function<byte[], byte[]> valueMapper;
+
+    MappingKeyValueIteratorAdapter(
+        final KeyValueIterator<K, byte[]> innerIterator,
+        final Function<byte[], byte[]> valueMapper
+    ) {
+        this.innerIterator = innerIterator;
+        this.valueMapper = valueMapper;
+    }
+
+    /**
+     * Ensures backward compatibility between {@link 
TimestampedKeyValueStoreWithHeaders}
+     * and plain {@link KeyValueStore}: values are wrapped with empty headers
+     * and timestamp {@code -1}.
+     *
+     * @see PlainToHeadersStoreAdapter
+     */
+    static <K> KeyValueIterator<K, byte[]> plainToHeaders(final 
KeyValueIterator<K, byte[]> inner) {
+        return new MappingKeyValueIteratorAdapter<>(inner, 
HeadersBytesStore::convertFromPlainToHeaderFormat);
+    }
+
+    /**
+     * Ensures backward compatibility between {@link 
TimestampedKeyValueStoreWithHeaders}
+     * and {@link TimestampedKeyValueStore}.
+     *
+     * @see TimestampedToHeadersStoreAdapter
+     */
+    static <K> KeyValueIterator<K, byte[]> timestampedToHeaders(final 
KeyValueIterator<K, byte[]> inner) {
+        return new MappingKeyValueIteratorAdapter<>(inner, 
HeadersBytesStore::convertToHeaderFormat);
+    }
+
+    /**
+     * Ensures backward compatibility between {@link SessionStoreWithHeaders}
+     * and {@link SessionStore}.
+     *
+     * @see SessionToHeadersStoreAdapter
+     */
+    static KeyValueIterator<Windowed<Bytes>, byte[]> sessionToHeaders(

Review Comment:
   This uses the same mapper as `timestampedToHeaders` above, so it's just a 
type-narrowed alias. Can the session call sites call `timestampedToHeaders` 
instead?



##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MappingKeyValueIteratorAdapter.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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 org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.state.HeadersBytesStore;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.SessionStore;
+import org.apache.kafka.streams.state.SessionStoreWithHeaders;
+import org.apache.kafka.streams.state.TimestampedKeyValueStore;
+import org.apache.kafka.streams.state.TimestampedKeyValueStoreWithHeaders;
+import org.apache.kafka.streams.state.WindowStoreIterator;
+
+import java.util.function.Function;
+
+/**
+ * Delegates to an inner {@link KeyValueIterator} and maps each value byte 
array
+ * through the given function (e.g. header-format conversion).
+ */
+class MappingKeyValueIteratorAdapter<K> implements KeyValueIterator<K, byte[]> 
{
+
+    private final KeyValueIterator<K, byte[]> innerIterator;
+    private final Function<byte[], byte[]> valueMapper;
+
+    MappingKeyValueIteratorAdapter(
+        final KeyValueIterator<K, byte[]> innerIterator,
+        final Function<byte[], byte[]> valueMapper
+    ) {
+        this.innerIterator = innerIterator;
+        this.valueMapper = valueMapper;
+    }
+
+    /**
+     * Ensures backward compatibility between {@link 
TimestampedKeyValueStoreWithHeaders}
+     * and plain {@link KeyValueStore}: values are wrapped with empty headers
+     * and timestamp {@code -1}.
+     *
+     * @see PlainToHeadersStoreAdapter
+     */
+    static <K> KeyValueIterator<K, byte[]> plainToHeaders(final 
KeyValueIterator<K, byte[]> inner) {

Review Comment:
   The doc only mentions the key-value adapter, but `plainToHeaders` is also 
used by `PlainToHeadersWindowStoreAdapter` for its key-range, `fetchAll` and 
`all()` iterators. Should we add that `@see` (same for `timestampedToHeaders`).



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/TimestampedToHeadersStoreAdapterTest.java:
##########
@@ -333,54 +341,83 @@ public void shouldDelegateOtherQueryTypesToStore() {
 
     @Test
     public void shouldCollectExecutionInfoForKeyQuery() {
+        adapter = createAdapter();
         final Bytes key = new Bytes("test-key".getBytes());
-        final byte[] rawTimestampedValue =
-            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
+        final byte[] timestampedValue = "test-value".getBytes();
         final KeyQuery<Bytes, byte[]> query = KeyQuery.withKey(key);
 
-        final QueryResult<byte[]> mockResult = 
QueryResult.forResult(rawTimestampedValue);
+        final QueryResult<byte[]> mockResult = 
QueryResult.forResult(timestampedValue);
         when(mockStore.query(eq(query), any(PositionBound.class), 
any(QueryConfig.class)))
             .thenReturn(mockResult);
 
-        final QueryResult<byte[]> result =
-            adapter.query(query, PositionBound.unbounded(), new 
QueryConfig(true));
+        final QueryResult<byte[]> result = adapter.query(query, 
PositionBound.unbounded(), new QueryConfig(true));
 
         assertTrue(result.isSuccess());
-        assertFalse(result.getExecutionInfo().isEmpty(),
-            "Expected execution info to be collected");
+        assertFalse(result.getExecutionInfo().isEmpty(), "Expected execution 
info to be collected");
         final String executionInfo = String.join("\n", 
result.getExecutionInfo());
-        assertTrue(executionInfo.contains("Handled in"));
-        
assertTrue(executionInfo.contains(TimestampedToHeadersStoreAdapter.class.getName()));
+        assertTrue(executionInfo.contains("Handled in"), "Expected execution 
info to contain handling information");
+        
assertTrue(executionInfo.contains(TimestampedToHeadersStoreAdapter.class.getName()),
+            "Expected execution info to mention 
TimestampedToHeadersStoreAdapter");
+    }
+
+    @Test
+    public void shouldCollectExecutionInfoForRangeQuery() {

Review Comment:
   `addExecutionInfo` sits outside the query-type branches, so this hits the 
same line as `shouldCollectExecutionInfoForKeyQuery`, and the RangeQuery branch 
is already covered by `shouldHandleRangeQuery`. Drop it, or at least add the 
`contains(className)` check the KeyQuery test has.



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/TimestampedToHeadersStoreAdapterTest.java:
##########
@@ -37,268 +36,276 @@
 import org.mockito.quality.Strictness;
 
 import java.util.Arrays;
+import java.util.List;
 
 import static 
org.apache.kafka.streams.state.HeadersBytesStore.convertToHeaderFormat;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.lenient;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.withSettings;
 
 @ExtendWith(MockitoExtension.class)
 @MockitoSettings(strictness = Strictness.STRICT_STUBS)
 public class TimestampedToHeadersStoreAdapterTest {
 
+    @Mock(extraInterfaces = TimestampedBytesStore.class)
+    private KeyValueStore<Bytes, byte[]> mockStore;
+
     @Mock
     private KeyValueIterator<Bytes, byte[]> mockIterator;
 
-    @SuppressWarnings("unchecked")
-    private KeyValueStore<Bytes, byte[]> mockStore;
-
     private TimestampedToHeadersStoreAdapter adapter;
 
-    @SuppressWarnings("unchecked")
-    @BeforeEach
-    public void setUp() {
-        mockStore = mock(KeyValueStore.class, 
withSettings().extraInterfaces(TimestampedBytesStore.class));
-        // lenient: this fixture stub is consumed by the adapter constructor 
for most tests, but the
-        // constructor-validation tests build their own store and never touch 
this one.
-        lenient().when(mockStore.persistent()).thenReturn(true);
-        adapter = new TimestampedToHeadersStoreAdapter(mockStore);
+    private TimestampedToHeadersStoreAdapter createAdapter() {
+        when(mockStore.persistent()).thenReturn(true);
+        return new TimestampedToHeadersStoreAdapter(mockStore);
+    }
+
+    private void assertConvertsTimestampedToHeaders(final 
KeyValueIterator<Bytes, byte[]> result) {
+        final Bytes key = new Bytes("k".getBytes());
+        final byte[] timestampedValue = "value".getBytes();
+        when(mockIterator.hasNext()).thenReturn(true);
+        when(mockIterator.next()).thenReturn(KeyValue.pair(key, 
timestampedValue));
+
+        assertTrue(result.hasNext());
+        final KeyValue<Bytes, byte[]> entry = result.next();
+        assertEquals(key, entry.key);
+        // Timestamped format only prepends empty headers; the plain 
conversion would also insert
+        // an 8-byte timestamp, so this array comparison proves 
timestampedToHeaders was wired.
+        assertArrayEquals(convertToHeaderFormat(timestampedValue), 
entry.value);
     }
 
     @Test
-    @SuppressWarnings("unchecked")
     public void shouldThrowIfStoreIsNotPersistent() {
-        final KeyValueStore<Bytes, byte[]> nonPersistentStore =
-            mock(KeyValueStore.class, 
withSettings().extraInterfaces(TimestampedBytesStore.class));
-        when(nonPersistentStore.persistent()).thenReturn(false);
+        when(mockStore.persistent()).thenReturn(false);
 
         final IllegalArgumentException exception = assertThrows(
             IllegalArgumentException.class,
-            () -> new TimestampedToHeadersStoreAdapter(nonPersistentStore)
+            () -> new TimestampedToHeadersStoreAdapter(mockStore)
         );
 
         assertTrue(exception.getMessage().contains("Provided store must be a 
persistent store"));
     }
 
     @Test
-    @SuppressWarnings("unchecked")
     public void shouldThrowIfStoreIsNotTimestamped() {
-        final KeyValueStore<Bytes, byte[]> nonTimestampedStore = 
mock(KeyValueStore.class);
-        when(nonTimestampedStore.persistent()).thenReturn(true);
+        @SuppressWarnings("unchecked")
+        final KeyValueStore<Bytes, byte[]> plainStore = 
mock(KeyValueStore.class);
+        when(plainStore.persistent()).thenReturn(true);
 
         final IllegalArgumentException exception = assertThrows(
             IllegalArgumentException.class,
-            () -> new TimestampedToHeadersStoreAdapter(nonTimestampedStore)
+            () -> new TimestampedToHeadersStoreAdapter(plainStore)
         );
 
         assertTrue(exception.getMessage().contains("Provided store must be a 
timestamped store"));
     }
 
     @Test
     public void shouldPutRawTimestampedValueToStore() {
+        adapter = createAdapter();
         final Bytes key = new Bytes("key".getBytes());
-        final byte[] rawTimestampedValue =
-            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
-        final byte[] valueWithHeaders = 
convertToHeaderFormat(rawTimestampedValue);
+        final byte[] timestampedValue = "value".getBytes();

Review Comment:
   This was `{0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'}` before. 
`"value".getBytes()` has no 8-byte timestamp prefix, so the test no longer 
shows the format its name refers to — please keep the old fixture here and in 
the other put/get/delete tests.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to