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

    https://github.com/apache/spark/pull/11748#discussion_r56284334
  
    --- Diff: 
core/src/main/scala/org/apache/spark/util/io/ChunkedByteBuffer.scala ---
    @@ -0,0 +1,143 @@
    +/*
    + * 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.io
    +
    +import java.io.InputStream
    +import java.nio.ByteBuffer
    +import java.nio.channels.WritableByteChannel
    +
    +import com.google.common.primitives.UnsignedBytes
    +import io.netty.buffer.{ByteBuf, Unpooled}
    +
    +import org.apache.spark.network.util.ByteArrayWritableChannel
    +import org.apache.spark.storage.BlockManager
    +
    +private[spark] class ChunkedByteBuffer(var chunks: Array[ByteBuffer]) {
    +  require(chunks != null, "chunks must not be null")
    +  require(chunks.nonEmpty, "Cannot create a ChunkedByteBuffer with no 
chunks")
    +  require(chunks.forall(_.limit() > 0), "chunks must be non-empty")
    +  require(chunks.forall(_.position() == 0), "chunks' positions must be 0")
    +
    +  val limit: Long = chunks.map(_.limit().asInstanceOf[Long]).sum
    +
    +  def this(byteBuffer: ByteBuffer) = {
    +    this(Array(byteBuffer))
    +  }
    +
    +  def writeFully(channel: WritableByteChannel): Unit = {
    +    for (bytes <- getChunks()) {
    +      while (bytes.remaining > 0) {
    +        channel.write(bytes)
    +      }
    +    }
    +  }
    +
    +  def toNetty: ByteBuf = {
    +    Unpooled.wrappedBuffer(getChunks(): _*)
    +  }
    +
    +  def toArray: Array[Byte] = {
    +    if (limit >= Integer.MAX_VALUE) {
    +      throw new UnsupportedOperationException(
    +        s"cannot call toArray because buffer size ($limit bytes) exceeds 
maximum array size")
    +    }
    +    val byteChannel = new ByteArrayWritableChannel(limit.toInt)
    +    writeFully(byteChannel)
    +    byteChannel.close()
    +    byteChannel.getData
    +  }
    +
    +  def toByteBuffer: ByteBuffer = {
    +    if (chunks.length == 1) {
    +      chunks.head
    +    } else {
    +      ByteBuffer.wrap(toArray)
    +    }
    +  }
    +
    +  def toInputStream(dispose: Boolean = false): InputStream = {
    +    new ChunkedByteBufferInputStream(this, dispose)
    +  }
    +
    +  def getChunks(): Array[ByteBuffer] = {
    +    chunks.map(_.duplicate())
    +  }
    +
    +  def copy(): ChunkedByteBuffer = {
    +    val copiedChunks = getChunks().map { chunk =>
    +      // TODO: accept an allocator in this copy method to integrate with 
mem. accounting systems
    +      val newChunk = ByteBuffer.allocate(chunk.limit())
    +      newChunk.put(chunk)
    +      newChunk.flip()
    +      newChunk
    +    }
    +    new ChunkedByteBuffer(copiedChunks)
    +  }
    +
    +  /**
    +   * Attempt to clean up a ByteBuffer if it is memory-mapped. This uses an 
*unsafe* Sun API that
    +   * might cause errors if one attempts to read from the unmapped buffer, 
but it's better than
    +   * waiting for the GC to find it because that could lead to huge numbers 
of open files. There's
    +   * unfortunately no standard API to do this.
    +   */
    +  def dispose(): Unit = {
    +    chunks.foreach(BlockManager.dispose)
    +  }
    +}
    +
    +/**
    + * Reads data from a ChunkedByteBuffer, and optionally cleans it up using 
BlockManager.dispose()
    + * at the end of the stream (e.g. to close a memory-mapped file).
    + */
    +private class ChunkedByteBufferInputStream(
    +    var chunkedByteBuffer: ChunkedByteBuffer,
    +    dispose: Boolean)
    --- End diff --
    
    Yep, will do. The semantics of `dispose()` are a bit confusing given some 
other weird semantics surrounding its use elsewhere in the BlockManager.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to