-
Notifications
You must be signed in to change notification settings - Fork 28.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-30012][CORE][SQL] Change classes extending scala collection cl…
…asses to work with 2.13 ### What changes were proposed in this pull request? Move some classes extending Scala collections into parallel source trees, to support 2.13; other minor collection-related modifications. Modify some classes extending Scala collections to work with 2.13 as well as 2.12. In many cases, this means introducing parallel source trees, as the type hierarchy changed in ways that one class can't support both. ### Why are the changes needed? To support building for Scala 2.13 in the future. ### Does this PR introduce any user-facing change? There should be no behavior change. ### How was this patch tested? Existing tests. Note that the 2.13 changes are not tested by the PR builder, of course. They compile in 2.13 but can't even be tested locally. Later, once the project can be compiled for 2.13, thus tested, it's possible the 2.13 implementations will need updates. Closes #26728 from srowen/SPARK-30012. Authored-by: Sean Owen <sean.owen@databricks.com> Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
- Loading branch information
1 parent
a3394e4
commit 4193d2f
Showing
19 changed files
with
510 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
core/src/main/scala-2.13/org/apache/spark/util/BoundedPriorityQueue.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 | ||
|
||
import java.io.Serializable | ||
import java.util.{PriorityQueue => JPriorityQueue} | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable.Growable | ||
|
||
/** | ||
* Bounded priority queue. This class wraps the original PriorityQueue | ||
* class and modifies it such that only the top K elements are retained. | ||
* The top K elements are defined by an implicit Ordering[A]. | ||
*/ | ||
private[spark] class BoundedPriorityQueue[A](maxSize: Int)(implicit ord: Ordering[A]) | ||
extends Iterable[A] with Growable[A] with Serializable { | ||
|
||
// Note: this class supports Scala 2.13. A parallel source tree has a 2.12 implementation. | ||
|
||
private val underlying = new JPriorityQueue[A](maxSize, ord) | ||
|
||
override def iterator: Iterator[A] = underlying.iterator.asScala | ||
|
||
override def size: Int = underlying.size | ||
|
||
override def knownSize: Int = size | ||
|
||
override def addAll(xs: IterableOnce[A]): this.type = { | ||
xs.foreach { this += _ } | ||
this | ||
} | ||
|
||
override def addOne(elem: A): this.type = { | ||
if (size < maxSize) { | ||
underlying.offer(elem) | ||
} else { | ||
maybeReplaceLowest(elem) | ||
} | ||
this | ||
} | ||
|
||
def poll(): A = { | ||
underlying.poll() | ||
} | ||
|
||
override def clear(): Unit = { underlying.clear() } | ||
|
||
private def maybeReplaceLowest(a: A): Boolean = { | ||
val head = underlying.peek() | ||
if (head != null && ord.gt(a, head)) { | ||
underlying.poll() | ||
underlying.offer(a) | ||
} else { | ||
false | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
core/src/main/scala-2.13/org/apache/spark/util/TimeStampedHashMap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
import java.util.Map.Entry | ||
import java.util.Set | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable | ||
|
||
import org.apache.spark.internal.Logging | ||
|
||
private[spark] case class TimeStampedValue[V](value: V, timestamp: Long) | ||
|
||
/** | ||
* This is a custom implementation of scala.collection.mutable.Map which stores the insertion | ||
* timestamp along with each key-value pair. If specified, the timestamp of each pair can be | ||
* updated every time it is accessed. Key-value pairs whose timestamp are older than a particular | ||
* threshold time can then be removed using the clearOldValues method. This is intended to | ||
* be a drop-in replacement of scala.collection.mutable.HashMap. | ||
* | ||
* @param updateTimeStampOnGet Whether timestamp of a pair will be updated when it is accessed | ||
*/ | ||
private[spark] class TimeStampedHashMap[A, B](updateTimeStampOnGet: Boolean = false) | ||
extends mutable.Map[A, B]() with Logging { | ||
|
||
// Note: this class supports Scala 2.13. A parallel source tree has a 2.12 implementation. | ||
|
||
private val internalMap = new ConcurrentHashMap[A, TimeStampedValue[B]]() | ||
|
||
def get(key: A): Option[B] = { | ||
val value = internalMap.get(key) | ||
if (value != null && updateTimeStampOnGet) { | ||
internalMap.replace(key, value, TimeStampedValue(value.value, currentTime)) | ||
} | ||
Option(value).map(_.value) | ||
} | ||
|
||
def iterator: Iterator[(A, B)] = { | ||
getEntrySet.iterator.asScala.map(kv => (kv.getKey, kv.getValue.value)) | ||
} | ||
|
||
def getEntrySet: Set[Entry[A, TimeStampedValue[B]]] = internalMap.entrySet | ||
|
||
override def + [B1 >: B](kv: (A, B1)): mutable.Map[A, B1] = { | ||
val newMap = new TimeStampedHashMap[A, B1] | ||
val oldInternalMap = this.internalMap.asInstanceOf[ConcurrentHashMap[A, TimeStampedValue[B1]]] | ||
newMap.internalMap.putAll(oldInternalMap) | ||
kv match { case (a, b) => newMap.internalMap.put(a, TimeStampedValue(b, currentTime)) } | ||
newMap | ||
} | ||
|
||
override def addOne(kv: (A, B)): this.type = { | ||
kv match { case (a, b) => internalMap.put(a, TimeStampedValue(b, currentTime)) } | ||
this | ||
} | ||
|
||
override def subtractOne(key: A): this.type = { | ||
internalMap.remove(key) | ||
this | ||
} | ||
|
||
override def update(key: A, value: B): Unit = { | ||
this += ((key, value)) | ||
} | ||
|
||
override def apply(key: A): B = { | ||
get(key).getOrElse { throw new NoSuchElementException() } | ||
} | ||
|
||
override def filter(p: ((A, B)) => Boolean): mutable.Map[A, B] = { | ||
internalMap.asScala.map { case (k, TimeStampedValue(v, t)) => (k, v) }.filter(p) | ||
} | ||
|
||
override def empty: mutable.Map[A, B] = new TimeStampedHashMap[A, B]() | ||
|
||
override def size: Int = internalMap.size | ||
|
||
override def foreach[U](f: ((A, B)) => U): Unit = { | ||
val it = getEntrySet.iterator | ||
while(it.hasNext) { | ||
val entry = it.next() | ||
val kv = (entry.getKey, entry.getValue.value) | ||
f(kv) | ||
} | ||
} | ||
|
||
def putIfAbsent(key: A, value: B): Option[B] = { | ||
val prev = internalMap.putIfAbsent(key, TimeStampedValue(value, currentTime)) | ||
Option(prev).map(_.value) | ||
} | ||
|
||
def putAll(map: Map[A, B]): Unit = { | ||
map.foreach { case (k, v) => update(k, v) } | ||
} | ||
|
||
def toMap: Map[A, B] = iterator.toMap | ||
|
||
def clearOldValues(threshTime: Long, f: (A, B) => Unit): Unit = { | ||
val it = getEntrySet.iterator | ||
while (it.hasNext) { | ||
val entry = it.next() | ||
if (entry.getValue.timestamp < threshTime) { | ||
f(entry.getKey, entry.getValue.value) | ||
logDebug("Removing key " + entry.getKey) | ||
it.remove() | ||
} | ||
} | ||
} | ||
|
||
/** Removes old key-value pairs that have timestamp earlier than `threshTime`. */ | ||
def clearOldValues(threshTime: Long): Unit = { | ||
clearOldValues(threshTime, (_, _) => ()) | ||
} | ||
|
||
private def currentTime: Long = System.currentTimeMillis | ||
|
||
// For testing | ||
|
||
def getTimeStampedValue(key: A): Option[TimeStampedValue[B]] = { | ||
Option(internalMap.get(key)) | ||
} | ||
|
||
def getTimestamp(key: A): Option[Long] = { | ||
getTimeStampedValue(key).map(_.timestamp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.