All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.incremental.storage.NonCachingLazyStorage.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * 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.IOUtil
import com.intellij.util.io.KeyDescriptor
import com.intellij.util.io.PersistentHashMap
import java.io.File
import java.io.IOException


class NonCachingLazyStorage(
    private val storageFile: File,
    private val keyDescriptor: KeyDescriptor,
    private val valueExternalizer: DataExternalizer
) : LazyStorage {
    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()
        } finally {
            storage = null
            if (!IOUtil.deleteAllFilesStartingWith(storageFile)) {
                throw IOException("Could not delete internal storage: ${storageFile.absolutePath}")
            }
        }
    }

    @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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy