Github user JoshRosen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5400#discussion_r31356558
  
    --- Diff: 
network/common/src/main/java/org/apache/spark/network/buffer/WrappedLargeByteBuffer.java
 ---
    @@ -0,0 +1,269 @@
    +/*
    +* 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.spark.network.buffer;
    +
    +import com.google.common.annotations.VisibleForTesting;
    +import sun.nio.ch.DirectBuffer;
    +
    +import java.io.IOException;
    +import java.nio.BufferUnderflowException;
    +import java.nio.ByteBuffer;
    +import java.nio.MappedByteBuffer;
    +import java.nio.channels.WritableByteChannel;
    +import java.util.Arrays;
    +import java.util.List;
    +
    +public class WrappedLargeByteBuffer implements LargeByteBuffer {
    +
    +  @VisibleForTesting
    +  public final ByteBuffer[] underlying;
    +
    +  private final long size;
    +  /**
    +   * each sub-ByteBuffer (except for the last one) must be exactly this 
size.  Note that this
    +   * class *really* expects this to be LargeByteBufferHelper.MAX_CHUNK.  
The only reason it isn't
    +   * is so that we can do tests without creating ginormous buffers.  
Public methods force it to
    +   * be LargeByteBufferHelper.MAX_CHUNK
    +   */
    +  private final int subBufferSize;
    +  private long _pos;
    +  @VisibleForTesting
    +  int currentBufferIdx;
    +  @VisibleForTesting
    +  ByteBuffer currentBuffer;
    +
    +
    +  public WrappedLargeByteBuffer(ByteBuffer[] underlying) {
    +    this(underlying, LargeByteBufferHelper.MAX_CHUNK);
    +  }
    +
    +  /**
    +   * you do **not** want to call this version.  It leads to a buffer which 
doesn't properly
    +   * support {@link #asByteBuffer}.  The only reason it exists is to we 
can have tests which
    +   * don't require 2GB of memory
    +   *
    +   * @param underlying
    +   * @param subBufferSize
    +   */
    +  @VisibleForTesting
    +  WrappedLargeByteBuffer(ByteBuffer[] underlying, int subBufferSize) {
    +    if (underlying.length == 0) {
    +      throw new IllegalArgumentException("must wrap at least one 
ByteBuffer");
    +    }
    +    this.underlying = underlying;
    +    this.subBufferSize = subBufferSize;
    +    long sum = 0L;
    +    boolean startFound = false;
    +    long initialPosition = -1;
    +    for (int i = 0; i < underlying.length; i++) {
    +      ByteBuffer b = underlying[i];
    +      if (i != underlying.length -1 && b.capacity() != subBufferSize) {
    +        throw new IllegalArgumentException("All buffers, except for the 
final one, must have " +
    +          "size = " + subBufferSize);
    +      }
    +      if (startFound) {
    +        if (b.position() != 0) {
    +          throw new IllegalArgumentException("ByteBuffers have 
inconsistent positions");
    +        }
    +      } else if (b.position() != b.capacity()) {
    +        startFound = true;
    +        initialPosition = sum + b.position();
    +      }
    +      sum += b.capacity();
    +    }
    +    _pos = initialPosition;
    +    currentBufferIdx = 0;
    +    currentBuffer = underlying[0];
    +    size = sum;
    +  }
    +
    +  @Override
    +  public void get(byte[] dest, int offset, int length) {
    +    if (length > remaining()) {
    +      throw new BufferUnderflowException();
    +    }
    +    int moved = 0;
    +    while (moved < length) {
    +      int toRead = Math.min(length - moved, currentBuffer.remaining());
    +      currentBuffer.get(dest, offset + moved, toRead);
    +      moved += toRead;
    +      updateCurrentBuffer();
    +    }
    +    _pos += moved;
    +  }
    +
    +  @Override
    +  public LargeByteBuffer rewind() {
    +    if (currentBuffer != null) {
    +      currentBuffer.rewind();
    +    }
    +    while (currentBufferIdx > 0) {
    +      currentBufferIdx -= 1;
    +      currentBuffer = underlying[currentBufferIdx];
    +      currentBuffer.rewind();
    +    }
    +    _pos = 0;
    +    return this;
    +  }
    +
    +  @Override
    +  public WrappedLargeByteBuffer deepCopy() {
    +    ByteBuffer[] dataCopy = new ByteBuffer[underlying.length];
    +    for (int i = 0; i < underlying.length; i++) {
    +      ByteBuffer b = underlying[i];
    +      dataCopy[i] = ByteBuffer.allocate(b.capacity());
    +      int originalPosition = b.position();
    +      b.rewind();
    +      dataCopy[i].put(b);
    +      dataCopy[i].position(0);
    +      b.position(originalPosition);
    +    }
    +    return new WrappedLargeByteBuffer(dataCopy, subBufferSize);
    +  }
    +
    +  @Override
    +  public byte get() {
    +    byte r = currentBuffer.get();
    +    _pos += 1;
    +    updateCurrentBuffer();
    +    return r;
    +  }
    +
    +  private void updateCurrentBuffer() {
    --- End diff --
    
    Add a comment here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to