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

 ##########
 File path: 
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/IncrementalChunkBuffer.java
 ##########
 @@ -0,0 +1,217 @@
+/*
+ * 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 {
+  /** Limit, which cannot be changed. */
+  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<>();
+
+  IncrementalChunkBuffer(int limit, int increment) {
+    Preconditions.checkArgument(limit >= 0);
+    Preconditions.checkArgument(increment > 0);
+    this.limit = limit;
+    this.increment = increment;
+  }
+
+  /** @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) {
+      Preconditions.checkState(ith.capacity() == increment);
+    }
+    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
+    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(increment);
+      buffers.add(b);
+    }
+    if (index == limitIndex) {
+      b.limit(limit % increment);
+    }
+    return b;
+  }
+
+  /** @return the buffer containing the position. It may allocate buffers. */
+  private ByteBuffer getAndAllocateAtPosition(int position) {
+    final int i = position / increment;
+    final ByteBuffer ith = getAndAllocateAtIndex(i);
+    Preconditions.checkState(ith.position() == position % increment);
+    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;
+    ByteBuffer ith = null;
+    // buffers must be full
+    for (; i < buffers.size(); i++) {
+      ith = getAtIndex(i);
+      if (ith.position() != increment) {
+        break; // found the first non-full buffer
+      }
+    }
+    // buffer containing the position
+    final int position = i * increment + Optional.ofNullable(getAtIndex(i))
+        .map(ByteBuffer::position).orElse(0);
+    assertRemainingList(ith, i);
+    return position;
+  }
+
+  private void assertRemainingList(ByteBuffer ith, int i) {
+    if (ith != null) {
+      // buffers must be empty
+      for (i++; i < buffers.size(); i++) {
+        ith = getAtIndex(i);
+        if (ith == null) {
+          break; // found the first non-null
+        }
+        Preconditions.checkState(ith.position() == 0);
+      }
+    }
+    // buffers must be null
+    for (i++; i < buffers.size(); i++) {
+      Preconditions.checkState(getAtIndex(i) == null);
+    }
+  }
+
+  @Override
+  public int remaining() {
+    return limit - position();
+  }
+
+  @Override
+  public void clear() {
+    buffers.forEach(ByteBuffer::clear);
+  }
+
+  @Override
+  public void put(ByteBuffer that) {
+    if (that.remaining() > this.remaining()) {
+      final BufferOverflowException boe = new BufferOverflowException();
+      boe.initCause(new IllegalArgumentException(
+          "Failed to put since that.remaining() = " + that.remaining()
+              + " > this.remaining() = " + this.remaining()));
+      throw boe;
+    }
+
+    final int thatLimit = that.limit();
+    int p = position();
+    for (; that.position() < thatLimit;) {
 
 Review comment:
   > I tries to avoid while-loop in general since for-loop is more powerful. :).
   
   Thanks.
   
   > For example, I may (Indeed, I should) move "int p = position();" inside.
   
   I was considering mentioning this.  Similarly, `p += min` might be more 
conventional in the last section of the `for` instead of inside the loop body. 
:)

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: ozone-issues-h...@hadoop.apache.org

Reply via email to