-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into topic/lru-cache
- Loading branch information
Showing
4 changed files
with
182 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2018-2024 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package skunk.data | ||
|
||
|
||
import scala.collection.immutable.SortedMap | ||
|
||
final case class Cache[K, V] private ( | ||
max: Int, | ||
entries: Map[K, V] | ||
)(accesses: SortedMap[Long, K], | ||
accessesInverted: Map[K, Long], | ||
counter: Long | ||
) { | ||
|
||
private def accessesWithoutKey(k: K): SortedMap[Long, K] = | ||
accessesInverted.get(k).fold(accesses)(oldCounter => accesses - oldCounter) | ||
|
||
def get(k: K): Option[V] = | ||
lookup(k).map(_._2) | ||
|
||
def lookup(k: K): Option[(Cache[K, V], V)] = | ||
entries.get(k) match { | ||
case Some(v) => | ||
val newAccesses = accessesWithoutKey(k) + (counter -> k) | ||
val newCache = new Cache(max, entries)(newAccesses, accessesInverted + (k -> counter), counter + 1) | ||
Some(newCache -> v) | ||
case None => | ||
None | ||
} | ||
|
||
def put(k: K, v: V): Cache[K, V] = | ||
insert(k, v)._1 | ||
|
||
def insert(k: K, v: V): (Cache[K, V], Option[(K, V)]) = | ||
if (max <= 0) (this, Some((k, v))) | ||
else if (entries.size >= max && !containsKey(k)) { | ||
val (counterToEvict, keyToEvict) = accesses.head | ||
val newCache = new Cache(max, entries - keyToEvict + (k -> v))(accessesWithoutKey(k) - counterToEvict + (counter -> k), accessesInverted + (k -> counter), counter + 1) | ||
(newCache, Some((keyToEvict, entries(keyToEvict)))) | ||
} else { | ||
val newCache = new Cache(max, entries + (k -> v))(accessesWithoutKey(k) + (counter -> k), accessesInverted + (k -> counter), counter + 1) | ||
(newCache, entries.get(k).filter(_ != v).map(k -> _)) | ||
} | ||
|
||
def containsKey(k: K): Boolean = entries.contains(k) | ||
def values: Iterable[V] = entries.values | ||
|
||
override def toString: String = | ||
accesses.map { case (_, k) => s"$k -> ${entries(k)}" }.mkString("Cache(", ", ", ")") | ||
} | ||
|
||
object Cache { | ||
def empty[K, V](max: Int): Cache[K, V] = | ||
new Cache(max max 0, Map.empty)(SortedMap.empty, Map.empty, 0L) | ||
} | ||
|
||
|
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,79 @@ | ||
// Copyright (c) 2018-2024 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package skunk.data | ||
|
||
import munit.ScalaCheckSuite | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop._ | ||
|
||
class CacheTest extends ScalaCheckSuite { | ||
|
||
val genEmpty: Gen[Cache[Int, String]] = | ||
Gen.choose(-1, 10).map(Cache.empty) | ||
|
||
test("insert on empty cache results in eviction") { | ||
val cache = Cache.empty(0).put("one", 1) | ||
assertEquals(cache.values.toList, Nil) | ||
assert(!cache.containsKey("one")) | ||
} | ||
|
||
test("max is never negative") { | ||
forAll(genEmpty) { c => | ||
assert(c.max >= 0) | ||
} | ||
} | ||
|
||
test("insert should allow lookup") { | ||
forAll(genEmpty) { c => | ||
val cʹ = c.put(1, "x") | ||
assertEquals(cʹ.lookup(1), if (c.max == 0) None else Some((cʹ, "x"))) | ||
} | ||
} | ||
|
||
test("eviction") { | ||
forAll(genEmpty) { c => | ||
val max = c.max | ||
|
||
// Load up the cache such that one more insert will cause it to overflow | ||
val cʹ = (0 until max).foldLeft(c) { case (c, n) => c.insert(n, "x")._1 } | ||
assertEquals(cʹ.values.size, max) | ||
|
||
// Overflow the cache | ||
val (cʹʹ, evicted) = cʹ.insert(max, "x") | ||
assertEquals(cʹʹ.values.size, max) | ||
assertEquals(evicted, Some(0 -> "x")) | ||
|
||
if (max > 2) { | ||
// Access oldest element | ||
val cʹʹʹ = cʹʹ.lookup(1).get._1 | ||
|
||
// Insert another element and make sure oldest element is not the element evicted | ||
val (cʹʹʹʹ, evictedʹ) = cʹʹʹ.insert(max + 1, "x") | ||
assertEquals(evictedʹ, Some(2 -> "x")) | ||
} | ||
} | ||
} | ||
|
||
test("eviction 2") { | ||
val c1 = Cache.empty(2).put("one", 1) | ||
assertEquals(c1.values.toList, List(1)) | ||
assertEquals(c1.get("one"), Some(1)) | ||
|
||
val (c2, evicted2) = c1.insert("two", 2) | ||
assert(c2.containsKey("one")) | ||
assert(c2.containsKey("two")) | ||
assertEquals(evicted2, None) | ||
|
||
val (c3, evicted3) = c2.insert("one", 1) | ||
assert(c3.containsKey("one")) | ||
assert(c3.containsKey("two")) | ||
assertEquals(evicted3, None) | ||
|
||
val (c4, evicted4) = c2.insert("one", 0) | ||
assert(c4.containsKey("one")) | ||
assert(c4.containsKey("two")) | ||
assertEquals(evicted4, Some("one" -> 1)) | ||
} | ||
} |
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