Skip to content

Commit

Permalink
Adopt ARK storage for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Dec 28, 2023
1 parent 0e7b176 commit 42f9cf3
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.arkbuilders.arkmemo.models

import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arkmemo.repo.versions.Version

data class VersionsResult (
val versions: List<Version>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.arkFolder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import dev.arkbuilders.arkmemo.models.Version
import dev.arkbuilders.arkmemo.models.VersionsResult
import space.taran.arkmemo.utils.arkVersions
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.FileTime
import kotlin.io.path.writeLines

class PlainVersionStorage(private val root: Path): VersionStorage {
class RootVersionStorage(private val root: Path): VersionStorage {

private val storageFile = root.arkFolder().arkVersions()
private var lastModified = FileTime.fromMillis(0L)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.arkbuilders.arkmemo.repo.versions

import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.arkFolder
import dev.arkbuilders.arklib.data.storage.FileStorage
import kotlinx.coroutines.CoroutineScope
import space.taran.arkmemo.utils.arkVersions
import java.nio.file.Path

class RootVersionsStorage(
private val scope: CoroutineScope,
private val root: Path
):
FileStorage<Versions>("versions", scope, root.arkFolder().arkVersions(), VersionsMonoid),
VersionsStorage {

override fun valueFromString(raw: String): Versions =
raw.split(",").filter { it.isNotEmpty() }.map {
ResourceId.fromString(it)
}.toSet()

override fun valueToString(value: Versions): String = value.joinToString(",")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.arkbuilders.arkmemo.models
package dev.arkbuilders.arkmemo.repo.versions

import dev.arkbuilders.arklib.ResourceId

typealias Version2 = ResourceId

data class Version(
val parent: ResourceId,
val child: ResourceId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dev.arkbuilders.arkmemo.repo.versions

import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arkmemo.models.Version
import dev.arkbuilders.arklib.data.storage.Storage

interface VersionStorage: Storage<Version2> {

interface VersionStorage {
suspend fun add(version: Version)

suspend fun forget(id: ResourceId)
Expand All @@ -19,6 +20,4 @@ interface VersionStorage {
fun childrenNotParents(): List<ResourceId>

fun isLatestResourceVersion(id: ResourceId): Boolean

suspend fun persist()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class VersionStorageRepo @Inject constructor(
private val memoPreferences: MemoPreferences
) {

private val storageByRoot = mutableMapOf<Path, PlainVersionStorage>()
private val storageByRoot = mutableMapOf<Path, RootVersionStorage>()

suspend fun provide(): VersionStorage {
val root = memoPreferences.getNotesStorage()
if (storageByRoot[root] == null) {
val versionStorage = PlainVersionStorage(root)
val versionStorage = RootVersionStorage(root)
versionStorage.init()
storageByRoot[root] = versionStorage
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.arkbuilders.arkmemo.repo.versions

import android.util.Log
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.data.storage.Monoid

typealias Versions = Set<ResourceId>

object VersionsMonoid : Monoid<Versions> {

override val neutral: Versions = setOf()

override fun combine(a: Versions, b: Versions): Versions {
val result = a.union(b)
Log.d(LOG_PREFIX, "merging $a and $b into $result")
return result
}
}

internal val LOG_PREFIX: String = "[versions]"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.arkbuilders.arkmemo.repo.versions

import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.data.storage.Storage

interface VersionsStorage: Storage<Versions> {

fun setParents(child: ResourceId, parents: Versions) = setValue(child, parents)

fun getParents(child: ResourceId): Versions = getValue(child)

fun removeParents(child: ResourceId) {
remove(child)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.arkbuilders.arkmemo.repo.versions

class VersionsStorageRepo {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.arkbuilders.arkmemo.ui.viewmodels

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -10,7 +9,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import dev.arkbuilders.arkmemo.models.Version
import dev.arkbuilders.arkmemo.repo.versions.Version
import dev.arkbuilders.arkmemo.repo.versions.VersionStorage
import dev.arkbuilders.arkmemo.repo.versions.VersionStorageRepo
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package space.taran.arkmemo

import space.taran.arklib.ResourceId
import dev.arkbuilders.arkmemo.models.Version
import dev.arkbuilders.arkmemo.repo.versions.PlainVersionStorage
import dev.arkbuilders.arkmemo.repo.versions.Version
import dev.arkbuilders.arkmemo.repo.versions.RootVersionStorage
import org.junit.Test
import org.junit.Assert.*
import java.io.File


class VersionStorageTest {
private val path = File(ROOT_PATH).toPath()
private val versionStorage = PlainVersionStorage(path)
private val versionStorage = RootVersionStorage(path)

@Test
fun versions_size_is_correct() {
Expand Down

0 comments on commit 42f9cf3

Please sign in to comment.