PijulCache.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
@file:Suppress("UNCHECKED_CAST")
package com.github.jonathanxd.dracon.cache
import com.github.jonathanxd.dracon.pijul.PijulExecution
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
/**
* Some changes are so big that it takes a long time to retrieve, refreshing the view would cause
* the entire change tree to be retrieved again from Pijul, taking a long time again, this cache tries to alleviate
* this by caching the result of `Pijul` command.
*
* It in fact, does not cache the result of `pijul` command, it could cache anything, however an update function
* must be provided when querying the cache. The update function must be a pure function and the function identity
* must be tied to the `key`, in other words, the `key` is the identity of the function. This constraint must never be broken,
* because [PijulCache] does no update stored functions, the function is stored only when there is no cache data for the `key`,
* subsequent calls will ignore the provided function.
*/
interface PijulCache {
/**
* Retrieves cached value, or computes if absent.
*/
fun <K: Any, V: Any> cache(key: K, updateFn: () -> V): V
/**
* Request the cache to be updated, it does not mean that the cache will be updated in the time this is invoked.
*/
fun <K: Any> requestUpdate(key: K)
}
class HashMapCache : PijulCache {
private val cache = ConcurrentHashMap<Any, Cache<*>>()
private val updateExecutor = Executors.newCachedThreadPool()
override fun <K: Any, V: Any> cache(key: K, updateFn: () -> V): V {
return (this.cache.computeIfAbsent(key) {
val update = updateFn()
Cache(update, updateFn)
} as Cache<V>).value
}
override fun <K : Any> requestUpdate(key: K) {
val fn = this.cache[key]?.updateFn
if (fn != null) {
updateExecutor.submit {
val updated = fn()
this.cache[key] = Cache(updated, fn)
}
}
}
}
data class HashKey(val hash: String)
data class Cache<V: Any>(var value: V, var updateFn: (() -> V)?)
data class CommandResult(val stdOut: String, val stdErr: String)