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


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/ReadBlockComputation.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.container.keyvalue;
+
+import java.util.List;
+import 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChunkInfo;
+import org.apache.ratis.util.Preconditions;
+
+/**
+ * Utility class to compute checksum-aligned offsets, lengths, and buffer
+ * limits for streaming readBlock operations.
+ */
+class ReadBlockComputation {
+
+  private final int responseDataSize;
+  private final int bitMask;
+  private final List<ChunkInfo> chunks;
+  private int chunkIndex;
+  private long lastPosition;
+
+  ReadBlockComputation(int responseDataSize, int bytesPerChecksum,
+      List<ChunkInfo> chunks, int firstChunkIndex) {
+    this.responseDataSize = responseDataSize;
+
+    Preconditions.assertSame(1, Long.bitCount(bytesPerChecksum), "bitCount"); 
// check power of 2.
+    // Suppose bytesPerChecksum = 00001000 (a power of 2), then bitMask = 
11111000.
+    // The following two computations are the same:
+    // We will use (n & bitMask) to compute ((n / bytesPerChecksum) * 
bytesPerChecksum).
+    this.bitMask = -bytesPerChecksum;
+
+    this.chunks = chunks;
+    this.chunkIndex = firstChunkIndex;
+    this.lastPosition = 0;
+  }
+
+  /**
+   * Binary-search for the index of the chunk whose start offset is the
+   * largest value &le; {@code targetOffset}.
+   */
+  static int searchChunk(long targetOffset, List<ChunkInfo> chunkInfoList) {
+    int low = 0;
+    int high = chunkInfoList.size() - 1;
+    while (low <= high) {
+      int mid = (low + high) >>> 1;
+      long midVal = chunkInfoList.get(mid).getOffset();
+      if (midVal <= targetOffset) {
+        low = mid + 1;
+      } else {
+        high = mid - 1;
+      }
+    }
+    return high;
+  }
+
+  /**
+   * Return the read offset aligned back to the previous checksum boundary
+   * within the chunk that contains {@code blockOffset}.
+   */
+  static long computeAdjustedOffset(int startChunkIndex, long blockOffset,
+      long bytesPerChecksum, List<ChunkInfo> chunkInfos) {
+    long offsetAlignment = (blockOffset - 
chunkInfos.get(startChunkIndex).getOffset()) % bytesPerChecksum;
+    return blockOffset - offsetAlignment;
+  }
+
+  /**
+   * Return the length of data that must be read so that the range
+   * {@code [blockOffset, blockOffset + blockLength)} is fully covered
+   * with checksum-aligned boundaries.
+   */
+  static long computeAdjustedLength(long blockOffset, long blockLength, long 
adjustedOffset,
+      long bytesPerChecksum, List<ChunkInfo> chunkInfos) {
+    long blockEnd = blockOffset + blockLength - 1; // inclusive
+    ChunkInfo lastChunk = chunkInfos.get(searchChunk(blockEnd, chunkInfos));
+    long chunkOffset = lastChunk.getOffset();
+    long chunkLength = Math.min(
+        (getEndChecksumIndex(blockEnd, chunkOffset, bytesPerChecksum) + 1) * 
bytesPerChecksum,

Review Comment:
   This is (n / bytesPerChecksum) * bytesPerChecksum.  We should use bitMask 
and remove getEndChecksumIndex(..).
   ```java
     long computeAdjustedLength(long readOffset, long readLength, long 
adjustedOffset) {
       final long readEnd = readOffset + readLength - 1; // inclusive
       final ChunkInfo lastChunk = chunks.get(searchChunk(readEnd, chunks));
       final long chunkOffset = lastChunk.getOffset();
       final long roundedUp = (Math.toIntExact(readEnd - chunkOffset) & 
bitMask) + bytesPerChecksum;
       final long chunkLength = Math.min(roundedUp, lastChunk.getLen());
       return chunkOffset + chunkLength - adjustedOffset;
     }
   ```



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