Github user sryza commented on a diff in the pull request:
https://github.com/apache/spark/pull/4450#discussion_r29191682
--- Diff:
core/src/main/scala/org/apache/spark/util/collection/PartitionedSerializedPairBuffer.scala
---
@@ -0,0 +1,254 @@
+/*
+ * 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.InputStream
+import java.nio.IntBuffer
+import java.util.Comparator
+
+import org.apache.spark.SparkEnv
+import org.apache.spark.serializer.{JavaSerializerInstance,
SerializerInstance}
+import org.apache.spark.storage.BlockObjectWriter
+import org.apache.spark.util.collection.PartitionedSerializedPairBuffer._
+
+/**
+ * Append-only buffer of key-value pairs, each with a corresponding
partition ID, that serializes
+ * its records upon insert and stores them as raw bytes.
+ *
+ * We use two data-structures to store the contents. The serialized
records are stored in a
+ * ChainedBuffer that can expand gracefully as records are added. This
buffer is accompanied by a
+ * metadata buffer that stores pointers into the data buffer as well as
the partition ID of each
+ * record. Each entry in the metadata buffer takes up a fixed amount of
space.
+ *
+ * Sorting the collection means swapping entries in the metadata buffer -
the record buffer need not
+ * be modified at all. Storing the partition IDs in the metadata buffer
means that comparisons can
+ * happen without following any pointers, which should minimize cache
misses.
+ *
+ * Currently, only sorting by partition is supported.
+ *
+ * @param metaInitialRecords The initial number of entries in the metadata
buffer.
+ * @param kvBlockSize The size of each byte buffer in the ChainedBuffer
used to store the records.
+ * @param serializerInstance the serializer used for serializing inserted
records.
+ */
+private[spark] class PartitionedSerializedPairBuffer[K, V](
+ metaInitialRecords: Int,
+ kvBlockSize: Int,
+ serializerInstance: SerializerInstance =
SparkEnv.get.serializer.newInstance)
+ extends WritablePartitionedPairCollection[K, V] {
+
+ if (serializerInstance.isInstanceOf[JavaSerializerInstance]) {
+ throw new IllegalArgumentException("PartitionedSerializedPairBuffer
does not support" +
+ " Java-serialized objects.")
+ }
+
+ private var metaBuffer = IntBuffer.allocate(metaInitialRecords * NMETA)
+
+ private val kvBuffer: ChainedBuffer = new ChainedBuffer(kvBlockSize)
+ private val kvOutputStream = new ChainedBufferOutputStream(kvBuffer)
+ private val kvSerializationStream =
serializerInstance.serializeStream(kvOutputStream)
+
+ def insert(partition: Int, key: K, value: V): Unit = {
+ if (metaBuffer.position == metaBuffer.capacity) {
+ growMetaBuffer()
+ }
+
+ val keyStart = kvBuffer.size
+ if (keyStart < 0) {
+ throw new Exception(s"Can't grow buffer beyond ${1 << 31} bytes")
+ }
+ kvSerializationStream.writeObject[Any](key)
+ kvSerializationStream.flush()
+ val valueStart = kvBuffer.size
+ kvSerializationStream.writeObject[Any](value)
+ kvSerializationStream.flush()
+ val valueEnd = kvBuffer.size
+
+ metaBuffer.put(keyStart)
+ metaBuffer.put(valueStart)
+ metaBuffer.put(valueEnd)
+ metaBuffer.put(partition)
+ }
+
+ /** Double the size of the array because we've reached capacity */
+ private def growMetaBuffer(): Unit = {
+ if (metaBuffer.capacity * 4 >= (1 << 30)) {
+ // Doubling the capacity would create an array bigger than
Int.MaxValue, so don't
+ throw new Exception(
+ s"Can't grow buffer beyond ${(1 << 30) / (NMETA * 4)} elements")
+ }
+ val newMetaBuffer = IntBuffer.allocate(metaBuffer.capacity * 2)
+ newMetaBuffer.put(metaBuffer.array)
+ metaBuffer = newMetaBuffer
+ }
+
+ /** Iterate through the data in a given order. For this class this is
not really destructive. */
+ override def partitionedDestructiveSortedIterator(keyComparator:
Comparator[K])
+ : Iterator[((Int, K), V)] = {
+ sort(keyComparator)
+ val is = orderedInputStream
+ val deserStream = serializerInstance.deserializeStream(is)
+ new Iterator[((Int, K), V)] {
+ var metaBufferPos = 0
+ def hasNext: Boolean = metaBufferPos < metaBuffer.position
+ def next(): ((Int, K), V) = {
+ val key = deserStream.readObject[Any]().asInstanceOf[K]
+ val value = deserStream.readObject[Any]().asInstanceOf[V]
+ val partition = metaBuffer.get(metaBufferPos + PARTITION)
+ metaBufferPos += NMETA
+ ((partition, key), value)
+ }
+ }
+ }
+
+ override def estimateSize: Long = metaBuffer.capacity * 4 +
kvBuffer.capacity
--- End diff --
metaBuffer is an IntBuffer, so 4 is there because it's the number of bytes
in an integer. It shouldn't change if NMETA changes.
---
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]