diff --git a/build-common/src/com/intellij/util/io/JpsPersistentHashMap.java.201 b/build-common/src/com/intellij/util/io/JpsPersistentHashMap.java.201 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/build-common/src/org/jetbrains/kotlin/incremental/storage/NonCachingLazyStorage.kt.201 b/build-common/src/org/jetbrains/kotlin/incremental/storage/NonCachingLazyStorage.kt.201 new file mode 100644 index 00000000000..614426e0b1e --- /dev/null +++ b/build-common/src/org/jetbrains/kotlin/incremental/storage/NonCachingLazyStorage.kt.201 @@ -0,0 +1,106 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed 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.jetbrains.kotlin.incremental.storage + +import com.intellij.util.io.DataExternalizer +import com.intellij.util.io.KeyDescriptor +import com.intellij.util.io.PersistentHashMap +import java.io.File + + +class NonCachingLazyStorage( + private val storageFile: File, + private val keyDescriptor: KeyDescriptor, + private val valueExternalizer: DataExternalizer +) : LazyStorage { + @Volatile + private var storage: PersistentHashMap? = null + + @Synchronized + private fun getStorageIfExists(): PersistentHashMap? { + if (storage != null) return storage + + if (storageFile.exists()) { + storage = createMap() + return storage + } + + return null + } + + @Synchronized + private fun getStorageOrCreateNew(): PersistentHashMap { + if (storage == null) { + storage = createMap() + } + + return storage!! + } + + override val keys: Collection + get() = getStorageIfExists()?.allKeysWithExistingMapping ?: listOf() + + override operator fun contains(key: K): Boolean = + getStorageIfExists()?.containsMapping(key) ?: false + + override operator fun get(key: K): V? = + getStorageIfExists()?.get(key) + + override operator fun set(key: K, value: V) { + getStorageOrCreateNew().put(key, value) + } + + override fun remove(key: K) { + getStorageIfExists()?.remove(key) + } + + override fun append(key: K, value: V) { + getStorageOrCreateNew().appendData(key) { dataOutput -> valueExternalizer.save(dataOutput, value) } + } + + @Synchronized + override fun clean() { + try { + storage?.close() + } catch (ignored: Throwable) { + } + + PersistentHashMap.deleteFilesStartingWith(storageFile) + storage = null + } + + @Synchronized + override fun flush(memoryCachesOnly: Boolean) { + val existingStorage = storage ?: return + + if (memoryCachesOnly) { + if (existingStorage.isDirty) { + existingStorage.dropMemoryCaches() + } + } else { + existingStorage.force() + } + } + + @Synchronized + override fun close() { + storage?.close() + } + + private fun createMap(): PersistentHashMap = + PersistentHashMap(storageFile, keyDescriptor, valueExternalizer) +}