szetszwo commented on a change in pull request #205: HDDS-2386. Implement 
incremental ChunkBuffer.
URL: https://github.com/apache/hadoop-ozone/pull/205#discussion_r350985644
 
 

 ##########
 File path: 
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/IncrementalChunkBuffer.java
 ##########
 @@ -0,0 +1,274 @@
+/*
+ * 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.common;
+
+import com.google.common.base.Preconditions;
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Function;
+
+/**
+ * Use a list of {@link ByteBuffer} to implement a single {@link ChunkBuffer}
+ * so that the buffer can be allocated incrementally.
+ */
+final class IncrementalChunkBuffer implements ChunkBuffer {
+  /**
+   * The limit of the entire {@link ChunkBuffer},
+   * but not individual {@link ByteBuffer}(s) in the list.
+   */
+  private final int limit;
+  /** Increment is the capacity of each {@link ByteBuffer} in the list. */
+  private final int increment;
+  /** Buffer list to be allocated incrementally. */
+  private final List<ByteBuffer> buffers = new ArrayList<>();
+  /** Is this a duplicated buffer? (for debug only) */
+  private final boolean isDuplicated;
+
+  IncrementalChunkBuffer(int limit, int increment, boolean isDuplicated) {
+    Preconditions.checkArgument(limit >= 0);
+    Preconditions.checkArgument(increment > 0);
+    this.limit = limit;
+    this.increment = increment;
+    this.isDuplicated = isDuplicated;
+  }
+
+  /** @return the capacity for the buffer at the given index. */
+  private int getBufferCapacityAtIndex(int i) {
+    Preconditions.checkArgument(i >= 0);
+    if (i < limit/increment) {
+      return increment;
+    }
+    return limit % increment;
+  }
+
+  private void assertInt(int expected, int computed, String name, int i) {
+    ChunkBuffer.assertInt(expected, computed,
+        () -> this + ": Unexpected " + name + " at index " + i);
+  }
+
+  /** @return the i-th buffer if it exists; otherwise, return null. */
+  private ByteBuffer getAtIndex(int i) {
+    final ByteBuffer ith = i < buffers.size() ? buffers.get(i) : null;
+    if (ith != null) {
+      // assert limit/capacity
+      if (!isDuplicated) {
+        assertInt(getBufferCapacityAtIndex(i), ith.capacity(), "capacity", i);
+      } else {
+        final int limitIndex = limit / increment;
+        if (i < limitIndex) {
+          assertInt(increment, ith.capacity(), "capacity", i);
+        } else if (i == limitIndex) {
+          assertInt(getBufferCapacityAtIndex(i), ith.limit(), "capacity", i);
+        } else {
+          assertInt(0, ith.limit(), "capacity", i);
+        }
+      }
+    }
+    return ith;
+  }
+
+  /** @return the i-th buffer. It may allocate buffers. */
+  private ByteBuffer getAndAllocateAtIndex(int index) {
+    final int limitIndex = limit / increment;
+    // never allocate over limit
+    if (limit % increment == 0) {
+      Preconditions.checkArgument(index < limitIndex);
+    } else {
+      Preconditions.checkArgument(index <= limitIndex);
+    }
+
+    int size = buffers.size();
+    if (index < size) {
+      return getAtIndex(index);
+    }
+
+    // allocate upto the given index
+    ByteBuffer b = null;
+    for (; size <= index; size++) {
+      b = ByteBuffer.allocate(getBufferCapacityAtIndex(size));
+      buffers.add(b);
+    }
+    return b;
+  }
+
+  /** @return the buffer containing the position. It may allocate buffers. */
+  private ByteBuffer getAndAllocateAtPosition(int position) {
+    Preconditions.checkArgument(position < limit);
+    final int i = position / increment;
+    final ByteBuffer ith = getAndAllocateAtIndex(i);
+    assertInt(position%increment, ith.position(), "position", i);
+    return ith;
+  }
+
+  @Override
+  public int position() {
+    // The buffers list must be in the following orders:
+    // full buffers, buffer containing the position, empty buffers, null 
buffers
+    int i = 0;
+    // buffers must be full
+    for (; i < buffers.size(); i++) {
+      if (getAtIndex(i).position() != increment) {
 
 Review comment:
   Sure.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to