-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a small
Cache
abstraction and replaced TrieMap
-based cache …
…implementation with `ConcurrentHashMap`. This change introduces potential minor performance improvements and compatibility with [GraalVM](https://www.graalvm.org/) `native-image`.
- Loading branch information
1 parent
f432c24
commit 6179740
Showing
15 changed files
with
254 additions
and
64 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sangria.util | ||
|
||
import scala.collection.{Map, Set} | ||
|
||
trait Cache[Key, Value] { | ||
def size: Int | ||
|
||
def contains(key: Key): Boolean | ||
def apply(key: Key): Value | ||
def get(key: Key): Option[Value] | ||
def getOrElse(key: Key, default: ⇒ Value): Value | ||
def update(key: Key, value: Value): Unit | ||
def remove(key: Key): Unit | ||
def clear(): Unit | ||
|
||
// NOTE: that `getOrElseUpdate` allows a race condition between value retrieval and cache update. | ||
// It is an explicit decision to avoid any kind of synchronization (it is preferred to recompute value multiple times than to synchronize) | ||
def getOrElseUpdate(key: Key, fn: ⇒ Value): Value | ||
def find(fn: (Key, Value) ⇒ Boolean): Option[(Key, Value)] | ||
def mapToSet[R](fn: (Key, Value) ⇒ R): Set[R] | ||
def mapValues[R](fn: Value ⇒ R): Map[Key, R] | ||
def keyExists(fn: Key ⇒ Boolean): Boolean | ||
def forEachValue(fn: Value ⇒ Unit): Unit | ||
def removeKeys(fn: Key ⇒ Boolean): Unit | ||
} | ||
|
||
object Cache { | ||
def empty[Key, Value]: Cache[Key, Value] = emptyConcurrentHashMap[Key, Value] | ||
|
||
def emptyTrieMap[Key, Value] = new TrieMapCache[Key, Value] | ||
def emptyConcurrentHashMap[Key, Value] = new ConcurrentHashMapCache[Key, Value] | ||
|
||
def apply[Key, Value](elems: (Key, Value)*) = { | ||
val c = empty[Key, Value] | ||
elems.foreach {case (key, value) ⇒ c(key) = value} | ||
c | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/scala/sangria/util/ConcurrentHashMapCache.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,117 @@ | ||
package sangria.util | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class ConcurrentHashMapCache[Key, Value] extends Cache[Key, Value] { | ||
private val cache = new ConcurrentHashMap[Key, Value]() | ||
|
||
def size = cache.size | ||
|
||
def contains(key: Key) = cache.containsKey(key) | ||
def apply(key: Key) = | ||
cache.get(key) match { | ||
case null ⇒ throw new NoSuchElementException | ||
case v ⇒ v | ||
} | ||
|
||
def get(key: Key) = Option(cache.get(key)) | ||
def getOrElse(key: Key, default: ⇒ Value) = cache.get(key) match { | ||
case null ⇒ default | ||
case v ⇒ v | ||
} | ||
|
||
def update(key: Key, value: Value) = cache.put(key, value) | ||
def remove(key: Key) = cache.remove(key) | ||
def clear() = cache.clear() | ||
|
||
def getOrElseUpdate(key: Key, fn: ⇒ Value) = cache.get(key) match { | ||
case null ⇒ | ||
val res = fn | ||
cache.put(key, res) | ||
res | ||
|
||
case v ⇒ v | ||
} | ||
|
||
def find(fn: (Key, Value) ⇒ Boolean) = { | ||
val it = cache.entrySet().iterator() | ||
var res: Option[(Key, Value)] = None | ||
|
||
while (it.hasNext && res.isEmpty) { | ||
val elem = it.next() | ||
|
||
if (fn(elem.getKey, elem.getValue)) | ||
res = Some(elem.getKey → elem.getValue) | ||
} | ||
|
||
res | ||
} | ||
|
||
def mapToSet[R](fn: (Key, Value) ⇒ R) = { | ||
val it = cache.entrySet().iterator() | ||
val res = scala.collection.mutable.Set[R]() | ||
|
||
while (it.hasNext) { | ||
val elem = it.next() | ||
|
||
res += fn(elem.getKey, elem.getValue) | ||
} | ||
|
||
res | ||
} | ||
|
||
def mapValues[R](fn: Value ⇒ R) = { | ||
val it = cache.entrySet().iterator() | ||
val res = scala.collection.mutable.Map[Key, R]() | ||
|
||
while (it.hasNext) { | ||
val elem = it.next() | ||
|
||
res(elem.getKey) = fn(elem.getValue) | ||
} | ||
|
||
res | ||
} | ||
|
||
def keyExists(fn: Key ⇒ Boolean): Boolean = { | ||
val it = cache.entrySet().iterator() | ||
|
||
while (it.hasNext) { | ||
val elem = it.next() | ||
|
||
if (fn(elem.getKey)) return true | ||
} | ||
|
||
false | ||
} | ||
|
||
def forEachValue(fn: Value ⇒ Unit) = { | ||
val it = cache.values().iterator() | ||
|
||
while (it.hasNext) { | ||
val elem = it.next() | ||
|
||
fn(elem) | ||
} | ||
} | ||
|
||
def removeKeys(fn: Key ⇒ Boolean) = { | ||
val it = cache.keySet().iterator() | ||
|
||
while (it.hasNext) { | ||
val elem = it.next() | ||
|
||
if (fn(elem)) it.remove() | ||
} | ||
} | ||
|
||
def canEqual(other: Any): Boolean = other.isInstanceOf[ConcurrentHashMapCache[_, _]] | ||
|
||
override def equals(other: Any): Boolean = other match { | ||
case that: ConcurrentHashMapCache[_, _] ⇒ (that canEqual this) && cache == that.cache | ||
case _ ⇒ false | ||
} | ||
|
||
override def hashCode(): Int = | ||
31 * cache.hashCode() | ||
} |
Oops, something went wrong.