[IC] Add unit tests for resetting in-memory caches wrappers in a transaction

#KT-56052 In Progress
This commit is contained in:
Alexander.Likhachev
2023-01-30 21:48:05 +01:00
committed by Space Team
parent 04a5c7761c
commit ca8d0bd100
3 changed files with 66 additions and 3 deletions
@@ -32,7 +32,7 @@ abstract class BasicMap<K : Comparable<K>, V>(
) {
protected val storage: LazyStorage<K, V> = CachingLazyStorage(storageFile, keyDescriptor, valueExternalizer).let {
if (icContext.keepIncrementalCompilationCachesInMemory) {
InMemoryStorageWrapper(it).also { wrapper ->
DefaultInMemoryStorageWrapper(it).also { wrapper ->
icContext.transaction.registerInMemoryStorageWrapper(wrapper)
}
} else {
@@ -8,12 +8,16 @@ package org.jetbrains.kotlin.incremental.storage
import java.util.*
import kotlin.collections.LinkedHashMap
interface InMemoryStorageWrapper<K, V> : LazyStorage<K, V> {
fun resetInMemoryChanges()
}
/**
* An in-memory wrapper for [origin] that keeps all the write operations in-memory.
* Flushes all the changes to the [origin] on [flush] invocation.
* [resetInMemoryChanges] should be called to reset in-memory changes of this wrapper.
*/
class InMemoryStorageWrapper<K, V>(private val origin: LazyStorage<K, V>) : LazyStorage<K, V> {
class DefaultInMemoryStorageWrapper<K, V>(private val origin: LazyStorage<K, V>) : InMemoryStorageWrapper<K, V> {
private val inMemoryStorage = LinkedHashMap<K, ValueWrapper<V>>()
private val removedKeys = hashSetOf<K>()
private var isCleanRequested = false
@@ -21,7 +25,7 @@ class InMemoryStorageWrapper<K, V>(private val origin: LazyStorage<K, V>) : Lazy
override val keys: Collection<K>
get() = if (isCleanRequested) inMemoryStorage.keys else (origin.keys - removedKeys) + inMemoryStorage.keys
fun resetInMemoryChanges() {
override fun resetInMemoryChanges() {
isCleanRequested = false
inMemoryStorage.clear()
removedKeys.clear()