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

    https://github.com/apache/spark/pull/4450#discussion_r29113605
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/collection/ChainedBuffer.scala ---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.util.collection
    +
    +import java.io.OutputStream
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.spark.storage.BlockObjectWriter
    +
    +/**
    + * A logical byte buffer that wraps a list of byte arrays. All the byte 
arrays have equal size. The
    + * advantage of this over a standard ArrayBuffer is that it can grow 
without claiming large amounts
    + * of memory and needing to copy the full contents.
    + */
    +private[spark] class ChainedBuffer(chunkSize: Int) {
    +  private val chunkSizeLog2 = (math.log(chunkSize) / math.log(2)).toInt
    +  assert(math.pow(2, chunkSizeLog2).toInt == chunkSize)
    +  private val chunks: ArrayBuffer[Array[Byte]] = new 
ArrayBuffer[Array[Byte]]()
    +  private var _size: Int = _
    +
    +  /**
    +   * Feed bytes from this buffer into a BlockObjectWriter.
    +   *
    +   * @param pos Offset in the buffer to read from.
    +   * @param writer BlockObjectWriter to read into.
    +   * @param len Number of bytes to read.
    +   */
    +  def read(pos: Int, writer: BlockObjectWriter, len: Int): Unit = {
    +    var chunkIndex = pos >> chunkSizeLog2
    +    var posInChunk = pos - (chunkIndex << chunkSizeLog2)
    +    var moved = 0
    +    while (moved < len) {
    +      val toRead = math.min(len - moved, chunkSize - posInChunk)
    +      writer.writeBytes(chunks(chunkIndex), posInChunk, toRead)
    +      moved += toRead
    +      chunkIndex += 1
    +      posInChunk = 0
    +    }
    +  }
    +
    +  /**
    +   * Read bytes from this buffer into a byte array.
    +   *
    +   * @param pos Offset in the buffer to read from.
    +   * @param bytes Byte array to read into.
    +   * @param offs Offset in the byte array to read to.
    +   * @param len Number of bytes to read.
    +   */
    +  def read(pos: Int, bytes: Array[Byte], offs: Int, len: Int): Unit = {
    +    var chunkIndex = pos >> chunkSizeLog2
    +    var posInChunk = pos - (chunkIndex << chunkSizeLog2)
    +    var moved = 0
    +    while (moved < len) {
    +      val toRead = math.min(len - moved, chunkSize - posInChunk)
    +      System.arraycopy(chunks(chunkIndex), posInChunk, bytes, offs + 
moved, toRead)
    +      moved += toRead
    +      chunkIndex += 1
    +      posInChunk = 0
    +    }
    +  }
    +
    +  /**
    +   * Write bytes from a byte array into this buffer.
    +   *
    +   * @param pos Offset in the buffer to write to.
    +   * @param bytes Byte array to write from.
    +   * @param offs Offset in the byte array to write from.
    +   * @param len Number of bytes to write.
    +   */
    +  def write(pos: Int, bytes: Array[Byte], offs: Int, len: Int): Unit = {
    +    // Grow if needed
    +    val endChunkIndex = (pos + len - 1) >> chunkSizeLog2
    +    while (endChunkIndex >= chunks.length) {
    +      chunks += new Array[Byte](chunkSize)
    +    }
    +
    +    var chunkIndex = pos >> chunkSizeLog2
    +    var posInChunk = pos - (chunkIndex << chunkSizeLog2)
    +    var moved = 0
    --- End diff --
    
    also feel like this should be written (minor)


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