smengcl commented on code in PR #9508:
URL: https://github.com/apache/ozone/pull/9508#discussion_r2636954185


##########
hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBSstFileWriter.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.hdds.utils.db;
+
+import static 
org.apache.hadoop.hdds.utils.NativeConstants.ROCKS_TOOLS_NATIVE_PROPERTY;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.google.common.collect.ImmutableList;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import org.apache.hadoop.hdds.StringUtils;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedOptions;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedRawSSTFileIterator;
+import org.apache.hadoop.hdds.utils.db.managed.ManagedRawSSTFileReader;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
+import org.junit.jupiter.api.io.TempDir;
+import org.rocksdb.RocksDBException;
+
+/**
+ * Test for RDBSstFileWriter.
+ */
+public class TestRDBSstFileWriter {
+
+  @TempDir
+  private Path path;
+
+  @EnabledIfSystemProperty(named = ROCKS_TOOLS_NATIVE_PROPERTY, matches = 
"true")
+  @Test
+  public void testSstFileTombstoneCreationWithCodecBufferReuse() throws 
IOException, RocksDBException {
+    ManagedRawSSTFileReader.tryLoadLibrary();
+    Path sstPath = path.resolve("test.sst").toAbsolutePath();
+    try (CodecBuffer codecBuffer = CodecBuffer.allocateDirect(1024);
+         RDBSstFileWriter sstFileWriter = new 
RDBSstFileWriter(sstPath.toFile());
+         CodecBuffer emptyBuffer = CodecBuffer.getEmptyBuffer()) {
+      Queue<String> keys = new LinkedList<>(ImmutableList.of("key1_renamed", 
"key1", "key1_renamed"));
+      PutToByteBuffer<IOException> putFunc = byteBuffer -> {
+        byte[] keyBytes = StringUtils.string2Bytes(keys.peek());
+        byteBuffer.put(keyBytes);
+        return keyBytes.length;
+      };
+      int len = codecBuffer.putFromSource(putFunc);
+      assertEquals(codecBuffer.readableBytes(), len);
+      assertEquals(keys.poll(), 
StringUtils.bytes2String(codecBuffer.getArray()));
+      codecBuffer.clear();
+      int idx = 0;
+      while (!keys.isEmpty()) {
+        codecBuffer.putFromSource(putFunc);
+        byte[] keyBytes = new byte[codecBuffer.readableBytes()];
+        assertEquals(keyBytes.length, 
codecBuffer.getInputStream().read(keyBytes));
+        if (idx++ % 2 == 0) {
+          sstFileWriter.delete(codecBuffer);
+        } else {
+          sstFileWriter.put(codecBuffer, emptyBuffer);
+        }
+        assertEquals(keys.poll(), 
StringUtils.bytes2String(codecBuffer.getArray()));
+        codecBuffer.clear();
+      }
+    }
+    Assertions.assertTrue(sstPath.toFile().exists());
+    try (ManagedOptions options = new ManagedOptions();
+         ManagedRawSSTFileReader<ManagedRawSSTFileIterator.KeyValue> reader = 
new ManagedRawSSTFileReader<>(options,
+             sstPath.toString(), 1024);
+         ManagedRawSSTFileIterator<ManagedRawSSTFileIterator.KeyValue> itr =
+             reader.newIterator(kv -> kv, null, null, 
IteratorType.KEY_AND_VALUE)) {
+
+      int idx = 0;
+      List<String> keys = ImmutableList.of("key1", "key1_rename");
+      while (itr.hasNext()) {
+        ManagedRawSSTFileIterator.KeyValue kv = itr.next();
+        assertEquals(idx, kv.getType());
+        assertEquals(keys.get(idx), keys.get(idx++));

Review Comment:
   This statement effectively compares the element to itself. Since Java 
evaluates argument list from left to right.
   
   In general I disrecommend writing such error-prone statements. Just make it 
two lines.



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