szetszwo commented on code in PR #9061:
URL: https://github.com/apache/ozone/pull/9061#discussion_r2383090549


##########
hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/spi/impl/TestKeyPrefixContainerCodec.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.hadoop.ozone.recon.spi.impl;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.hadoop.hdds.utils.db.Codec;
+import org.apache.hadoop.hdds.utils.db.CodecBuffer;
+import org.apache.hadoop.ozone.recon.api.types.KeyPrefixContainer;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Class to test {@link KeyPrefixContainerCodec}.
+ */
+public class TestKeyPrefixContainerCodec {

Review Comment:
   We should test different key lengths.  Let's combine the test cases as below:
   ```java
     @Test
     public void testKeyPrefixWithDelimiter() throws Exception {
       runTest("testKey", 123L, 456L);
       runTest("test_key_with_underscores", 789L, 101112L);
       runTest("test___________________________________Key", 1L,2L);
       runTest("", 0, 0);
     }
   
     void runTest(String keyPrefix, long version, long containerId) {
       final KeyPrefixContainer original = KeyPrefixContainer.get(keyPrefix, 
version, containerId);
       final KeyPrefixContainer keyAndVersion = 
KeyPrefixContainer.get(keyPrefix, version);
       final KeyPrefixContainer keyOnly = KeyPrefixContainer.get(keyPrefix);
   
       final CodecBuffer.Allocator allocator = CodecBuffer.Allocator.getHeap();
       try (CodecBuffer originalBuffer = codec.toCodecBuffer(original, 
allocator);
            CodecBuffer keyOnlyBuffer = codec.toCodecBuffer(keyOnly, allocator);
            CodecBuffer keyAndVersionBuffer = 
codec.toCodecBuffer(keyAndVersion, allocator)) {
         assertEquals(original, codec.fromCodecBuffer(originalBuffer));
         assertTrue(originalBuffer.startsWith(keyAndVersionBuffer));
         assertTrue(originalBuffer.startsWith(keyOnlyBuffer));
   
         final byte[] originalBytes = assertCodecBuffer(original, 
originalBuffer);
         assertEquals(original, codec.fromPersistedFormat(originalBytes));
   
         final byte[] keyAndVersionBytes = assertCodecBuffer(keyAndVersion, 
keyAndVersionBuffer);
         assertPrefix(originalBytes.length - 
KeyPrefixContainerCodec.LONG_SERIALIZED_SIZE,
             originalBytes, keyAndVersionBytes);
   
         final byte[] keyOnlyBytes = assertCodecBuffer(keyOnly, keyOnlyBuffer);
         assertPrefix(originalBytes.length - 
2*KeyPrefixContainerCodec.LONG_SERIALIZED_SIZE,
             originalBytes, keyOnlyBytes);
       }
     }
   
     static void assertPrefix(int expectedLength, byte[] array, byte[] prefix) {
       assertEquals(expectedLength, prefix.length);
       for (int i = 0; i < prefix.length; i++) {
         assertEquals(array[i], prefix[i]);
       }
     }
   
     byte[] assertCodecBuffer(KeyPrefixContainer original, CodecBuffer buffer) {
       final byte[] bytes = codec.toPersistedFormat(original);
       assertArrayEquals(bytes, buffer.getArray());
       return bytes;
     }
   ```



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/spi/impl/KeyPrefixContainerCodec.java:
##########
@@ -50,6 +59,113 @@ public Class<KeyPrefixContainer> getTypeClass() {
     return KeyPrefixContainer.class;
   }
 
+  @Override
+  public boolean supportCodecBuffer() {
+    return true;
+  }
+
+  @Override
+  public CodecBuffer toCodecBuffer(@Nonnull KeyPrefixContainer object, 
CodecBuffer.Allocator allocator) {
+    Preconditions.checkNotNull(object, "Null object can't be converted to 
CodecBuffer.");
+
+    final byte[] keyPrefixBytes = object.getKeyPrefix().getBytes(UTF_8);
+    int totalSize = keyPrefixBytes.length;
+
+    if (object.getKeyVersion() != -1) {
+      totalSize += LONG_SERIALIZED_SIZE;
+    }
+    if (object.getContainerId() != -1) {
+      totalSize += LONG_SERIALIZED_SIZE;
+    }
+
+    final CodecBuffer buffer = allocator.apply(totalSize);
+    buffer.put(ByteBuffer.wrap(keyPrefixBytes));
+
+    if (object.getKeyVersion() != -1) {
+      buffer.put(KEY_DELIMITER_BUFFER.duplicate());
+      buffer.putLong(object.getKeyVersion());
+    }
+
+    if (object.getContainerId() != -1) {
+      buffer.put(KEY_DELIMITER_BUFFER.duplicate());
+      buffer.putLong(object.getContainerId());
+    }
+
+    return buffer;
+  }
+
+  @Override
+  public KeyPrefixContainer fromCodecBuffer(@Nonnull CodecBuffer buffer) 
throws CodecException {
+
+    final ByteBuffer byteBuffer = buffer.asReadOnlyByteBuffer();
+    final int totalLength = byteBuffer.remaining();
+    final int startPosition = byteBuffer.position();
+    final int delimiterLength = KEY_DELIMITER_BYTES.length;
+
+    // Check for two delimiters + two longs (version and containerId)
+    if (totalLength >= 2 * (delimiterLength + Long.BYTES)) {
+      // Extract keyPrefix (everything except last 2 delimiters + 2 longs)
+      int keyPrefixLength = totalLength - 2 * delimiterLength - 2 * Long.BYTES;
+      byteBuffer.position(startPosition);
+      String keyPrefix = decodeStringFromBuffer(byteBuffer, keyPrefixLength);
+
+      // Skip delimiter and read version
+      byteBuffer.position(startPosition + keyPrefixLength + delimiterLength);
+      long version = byteBuffer.getLong();
+
+      // Skip delimiter and read containerId
+      byteBuffer.position(startPosition + keyPrefixLength + delimiterLength + 
Long.BYTES + delimiterLength);
+      long containerId = byteBuffer.getLong();
+
+      return KeyPrefixContainer.get(keyPrefix, version, containerId);
+    }
+
+    // Check for one delimiter + one long
+    if (totalLength >= delimiterLength + Long.BYTES) {
+      // Extract keyPrefix (everything except last delimiter + long)
+      int keyPrefixLength = totalLength - delimiterLength - Long.BYTES;
+      byteBuffer.position(startPosition);
+      String keyPrefix = decodeStringFromBuffer(byteBuffer, keyPrefixLength);
+
+      // Skip delimiter and read the long value
+      byteBuffer.position(startPosition + keyPrefixLength + delimiterLength);
+      long longValue = byteBuffer.getLong();
+
+      // Based on encoding logic: if keyVersion != -1, it's encoded first, 
then containerId
+      // So if we have only one long value, it should be the keyVersion
+      return KeyPrefixContainer.get(keyPrefix, longValue, -1);
+    }
+
+    // If we reach here, the buffer contains only the key prefix (no 
delimiters found)
+    byteBuffer.position(startPosition);
+    String keyPrefix = decodeStringFromBuffer(byteBuffer, totalLength);
+    return KeyPrefixContainer.get(keyPrefix, -1, -1);
+  }
+
+  /**
+   * Decode string from ByteBuffer without copying to intermediate byte array.
+   * Uses CharsetDecoder for efficient decoding.
+   */
+  private String decodeStringFromBuffer(ByteBuffer buffer, int length) throws 
CodecException {
+    if (length == 0) {
+      return "";
+    }
+
+    try {
+      ByteBuffer slice = buffer.duplicate();
+      slice.limit(slice.position() + length);
+
+      buffer.position(buffer.position() + length);
+
+      CharsetDecoder decoder = UTF_8.newDecoder();
+      CharBuffer charBuffer = decoder.decode(slice);
+      return charBuffer.toString();

Review Comment:
   The `UTF_8.newDecoder()` behavior is different from `new String(bytes, 
UTF_8)`.  So, let's use `new String(bytes, UTF_8)` as before:
   ```java
     private static String decodeStringFromBuffer(ByteBuffer buffer) {
       if (buffer.remaining() == 0) {
         return "";
       }
   
       final byte[] bytes;
       if (buffer.hasArray()) {
         bytes = buffer.array();
       } else {
         bytes = new byte[buffer.remaining()];
         buffer.get(bytes);
       }
       return new String(bytes, UTF_8);
     }
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to