Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

FileRevisionCache.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.
 */
package com.github.jonathanxd.dracon.cache

import com.github.jonathanxd.dracon.config.PijulSettings
import com.github.jonathanxd.dracon.content.PijulContentRevision
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.i18n.DraconBundle
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.revision.*
import com.github.jonathanxd.dracon.util.pijul
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.progress.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.getProjectDataPath
import com.intellij.openapi.util.ThrowableComputable
import java.io.Serializable
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.CompletableFuture

@Service(Service.Level.PROJECT)
class FileRevisionCache(val project: Project)  {
    //override val cache = DataCache<String, Map<String, ByteArray>>(this.project, "path_revision")

    fun cachePath(): Path =
        this.project.getProjectDataPath("com.github.jonathanxd.dracon").resolve("revision-cache")

    fun loadAsync(rev: PijulVcsFileRevision): CompletableFuture<ByteArray> =
        this.loadAsync(rev.filePath, rev.revision)

    fun loadAsync(rev: PijulContentRevision): CompletableFuture<ByteArray> =
        this.loadAsync(rev.filePath, rev.revision)

    fun loadAsync(file: Path, rev: PijulRevisionNumber): CompletableFuture<ByteArray> {
        val root = project.service<PijulVcsContext>().root
        val cache = this.cachePath()
        project.pijul()
        Files.createDirectories(cache)

        val completableFuture = CompletableFuture<ByteArray>()

        ProgressManager.getInstance().run(object : Task.Backgroundable(
            project,
            DraconBundle.message("index.load.rev.for.text", rev.hash.substring(0, 10), file.fileName.toString()),
            true
        ) {
            override fun run(indicator: ProgressIndicator) {
                indicator.isIndeterminate = false
                try {
                    val revision = loadFileStateInRevision(
                        rev.hash,
                        project,
                        root,
                        cache,
                        file,
                        indicator,
                        PijulSettings.instance.isToCacheAllRevisions()
                    )
                    completableFuture.complete(revision.content)
                } catch (t: Throwable) {
                    t.printStackTrace()
                    completableFuture.completeExceptionally(t)
                }
            }
        })

        return completableFuture.handle { t, u ->
            u?.printStackTrace()
            t ?: ByteArray(0)
        }
    }

    private fun Path.filePathAsString() = this.toAbsolutePath().toString()

    fun invalidate() {
        deleteFilesInsideDirectory(this.cachePath())
    }

    /*fun loadAndPrecacheRevisions(amount: Int, i: ProgressIndicator) {
        this.cache.lock()
        try {
            i.isIndeterminate = false
            val root = project.service<PijulVcsContext>().root
            val revs = pijul(project).allRevisions(project, root).result?.take(amount).orEmpty()

            val states = i.withPushPop {
                i.text = DraconBundle.message("index.revision.loading.text")

                loadStateInEveryRevisionForAllFiles(
                    revs.map { it.hash },
                    project,
                    root,
                    i
                )
            }

            i.text2 = DraconBundle.message("index.item.description.finish.text")

            i.withPushPop {
                i.text = DraconBundle.message("index.revision.files.text")
                states.entries.forEachWithProgress(i) { (rev, stateMap), indic ->
                    this.cache.updateCache(rev) {
                        stateMap.mapKeys { (k, _) -> k.filePathAsString() }
                    }
                }
            }

            i.text2 = DraconBundle.message("index.item.description.finish.text")
            i.fraction = 1.0
        } finally {
            this.cache.unlock()
        }
    }*/
}