Skip to content

Commit e9856c4

Browse files
Use java.util.Collections.synchronizedMap on a Java HashMap.
1 parent 88dd0c8 commit e9856c4

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

sql/core/src/main/scala/org/apache/spark/sql/SQLConf.scala

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package org.apache.spark.sql
1919

2020
import java.util.Properties
2121

22-
import scala.collection.mutable
22+
import scala.collection.JavaConverters._
2323

2424
/**
2525
* SQLConf holds mutable config parameters and hints. These can be set and
@@ -29,43 +29,48 @@ import scala.collection.mutable
2929
*/
3030
class SQLConf {
3131

32-
protected val settings = new mutable.HashMap[String, String]()
32+
private val settings = java.util.Collections.synchronizedMap(
33+
new java.util.HashMap[String, String]())
3334

3435
private[spark] def clear() {
3536
settings.clear()
3637
}
3738

3839
def this(props: Properties) = {
3940
this()
40-
import scala.collection.JavaConversions._ // implicits for java.util.Properties
41-
props.foreach { case (k, v) => this.settings(k) = v }
41+
props.asScala.foreach { case (k, v) => this.settings.put(k, v) }
4242
}
4343

4444
def set(key: String, value: String): SQLConf = {
4545
require(key != null, "key cannot be null")
4646
require(value != null, s"value cannot be null for ${key}")
47-
settings(key) = value
47+
settings.put(key, value)
4848
this
4949
}
5050

5151
def get(key: String): String = {
52-
settings.getOrElse(key, throw new NoSuchElementException(key))
52+
if (!settings.containsKey(key)) {
53+
throw new NoSuchElementException(key)
54+
}
55+
settings.get(key)
5356
}
5457

5558
def get(key: String, defaultValue: String): String = {
56-
settings.getOrElse(key, defaultValue)
59+
if (!settings.containsKey(key)) defaultValue else settings.get(key)
5760
}
5861

59-
def getAll: Array[(String, String)] = settings.clone().toArray
62+
def getAll: Array[(String, String)] = settings.asScala.toArray
6063

6164
def getOption(key: String): Option[String] = {
62-
settings.get(key)
65+
if (!settings.containsKey(key)) None else Some(settings.get(key))
6366
}
6467

65-
def contains(key: String): Boolean = settings.contains(key)
68+
def contains(key: String): Boolean = settings.containsKey(key)
6669

6770
def toDebugString: String = {
68-
settings.toArray.sorted.map{ case (k, v) => s"$k=$v" }.mkString("\n")
71+
settings.synchronized {
72+
settings.asScala.toArray.sorted.map{ case (k, v) => s"$k=$v" }.mkString("\n")
73+
}
6974
}
7075

7176
}

0 commit comments

Comments
 (0)