-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File Monitoring #107
Closed
+605
−120
Closed
File Monitoring #107
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/main/java/space/taran/arknavigator/mvp/model/IndexCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package space.taran.arknavigator.mvp.model | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import space.taran.arknavigator.mvp.model.dao.ResourceId | ||
import space.taran.arknavigator.mvp.model.repo.AggregatedResourcesIndex | ||
import space.taran.arknavigator.mvp.model.repo.Difference | ||
import space.taran.arknavigator.mvp.model.repo.PlainResourcesIndex | ||
import java.nio.file.Path | ||
|
||
class IndexCache { | ||
private var indexByRoot = mutableMapOf<Path, PlainResourcesIndex>() | ||
private var aggregatedIndex = AggregatedResourcesIndex(listOf()) | ||
private var flowByRootAndFav = mutableMapOf<RootAndFav, MutableStateFlow<Set<ResourceId>?>>() | ||
private var allRootsFlow = MutableStateFlow<Set<ResourceId>?>(null) | ||
|
||
suspend fun onIndexChange(root: Path, index: PlainResourcesIndex) { | ||
indexByRoot[root] = index | ||
emitChangesToAffectedRootAndFav(root, index) | ||
} | ||
|
||
suspend fun onResourceCreated(root: Path, resourcePath: Path) { | ||
val index = indexByRoot[root]!! | ||
index.reindexRoot(Difference(emptyList(), emptyList(), listOf(resourcePath))) | ||
emitChangesToAffectedRootAndFav(root, index) | ||
} | ||
|
||
suspend fun onResourceDeleted(root: Path, resourcePath: Path): ResourceId { | ||
val index = indexByRoot[root]!! | ||
val id = index.metaByPath[resourcePath]!!.id | ||
index.remove(id) | ||
emitChangesToAffectedRootAndFav(root, indexByRoot[root]!!) | ||
return id | ||
} | ||
|
||
suspend fun onResourceModified(root: Path, resourcePath: Path): PlainResourcesIndex { | ||
val index = indexByRoot[root]!! | ||
index.reindexRoot(Difference(emptyList(), listOf(resourcePath), emptyList())) | ||
emitChangesToAffectedRootAndFav(root, index) | ||
return index | ||
} | ||
|
||
suspend fun onReindexFinish() { | ||
allRootsFlow.emit(aggregatedIndex.listAllIds()) | ||
} | ||
|
||
fun getIndexByRoot(root: Path) = indexByRoot[root]!! | ||
|
||
fun listenResourcesChanges(rootAndFav: RootAndFav): StateFlow<Set<ResourceId>?> { | ||
return if (rootAndFav.isAllRoots()) { | ||
allRootsFlow | ||
} else { | ||
if (flowByRootAndFav[rootAndFav] == null) { | ||
indexByRoot[rootAndFav.root]?.let { index -> | ||
flowByRootAndFav[rootAndFav] = MutableStateFlow(index.listIds(rootAndFav.fav)) | ||
} ?: run { | ||
flowByRootAndFav[rootAndFav] = MutableStateFlow(null) | ||
} | ||
} | ||
flowByRootAndFav[rootAndFav]!! | ||
} | ||
} | ||
|
||
fun listIds(rootAndFav: RootAndFav): Set<ResourceId> { | ||
return if (rootAndFav.isAllRoots()) | ||
aggregatedIndex.listIds(rootAndFav.fav) | ||
else | ||
indexByRoot[rootAndFav.root]?.listIds(rootAndFav.fav) ?: emptySet() | ||
} | ||
|
||
fun getPath(resourceId: ResourceId): Path? { | ||
return aggregatedIndex.getPath(resourceId) | ||
} | ||
|
||
private suspend fun emitChangesToAffectedRootAndFav(root: Path, index: PlainResourcesIndex) { | ||
aggregatedIndex = AggregatedResourcesIndex(indexByRoot.values) | ||
if (allRootsFlow.value != null) | ||
allRootsFlow.emit(aggregatedIndex.listAllIds()) | ||
|
||
val affectedRootAndFavs = flowByRootAndFav.keys.filter { it.root == root } | ||
affectedRootAndFavs.forEach { | ||
if (flowByRootAndFav[it]!!.value != index.listIds(it.fav)) | ||
flowByRootAndFav[it]!!.emit(index.listIds(it.fav)) | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/space/taran/arknavigator/mvp/model/IndexingEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package space.taran.arknavigator.mvp.model | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import space.taran.arknavigator.mvp.model.fsmonitoring.FSMonitoring | ||
import space.taran.arknavigator.mvp.model.repo.FoldersRepo | ||
import space.taran.arknavigator.mvp.model.repo.ResourcesIndexFactory | ||
import java.nio.file.Path | ||
|
||
class IndexingEngine( | ||
private val indexCache: IndexCache, | ||
private val tagsCache: TagsCache, | ||
private val foldersRepo: FoldersRepo, | ||
private val resourcesIndexFactory: ResourcesIndexFactory, | ||
private val fsMonitoring: FSMonitoring | ||
) { | ||
suspend fun reindex() = withContext(Dispatchers.Default) { | ||
val roots = foldersRepo.query().succeeded.keys | ||
roots.forEach { | ||
val index = resourcesIndexFactory.loadFromDatabase(it) | ||
tagsCache.onIndexChanged(it, index) | ||
indexCache.onIndexChange(it, index) | ||
fsMonitoring.startWatchingRoot(it.toString()) | ||
} | ||
indexCache.onReindexFinish() | ||
tagsCache.onReindexFinish() | ||
} | ||
|
||
suspend fun index(root: Path) = withContext(Dispatchers.Default) { | ||
val index = resourcesIndexFactory.buildFromFilesystem(root) | ||
indexCache.onIndexChange(root, index) | ||
tagsCache.onIndexChanged(root, index) | ||
fsMonitoring.startWatchingRoot(root.toString()) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/space/taran/arknavigator/mvp/model/RootAndFav.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package space.taran.arknavigator.mvp.model | ||
|
||
import java.nio.file.Path | ||
|
||
data class RootAndFav ( | ||
val root: Path?, | ||
val fav: Path? | ||
) { | ||
init { | ||
if (root == null && fav != null) | ||
throw AssertionError("Combination null root and not null fav isn't allowed") | ||
} | ||
|
||
fun isAllRoots(): Boolean { | ||
return root == null | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
app/src/main/java/space/taran/arknavigator/mvp/model/TagsCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package space.taran.arknavigator.mvp.model | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import space.taran.arknavigator.mvp.model.dao.ResourceId | ||
import space.taran.arknavigator.mvp.model.repo.AggregatedTagsStorage | ||
import space.taran.arknavigator.mvp.model.repo.PlainResourcesIndex | ||
import space.taran.arknavigator.mvp.model.repo.PlainTagsStorage | ||
import space.taran.arknavigator.utils.Tags | ||
import java.nio.file.Path | ||
|
||
class TagsCache(val indexCache: IndexCache) { | ||
private var storageByRoot = mutableMapOf<Path, PlainTagsStorage>() | ||
private var aggregatedTagsStorage = AggregatedTagsStorage(listOf()) | ||
private var flowByRootAndFav = mutableMapOf<RootAndFav, MutableStateFlow<Tags?>>() | ||
private var allRootsFlow = MutableStateFlow<Tags?>(null) | ||
|
||
suspend fun onIndexChanged(root: Path, index: PlainResourcesIndex) { | ||
val storage = PlainTagsStorage.provide(root, index) | ||
storageByRoot[root] = storage | ||
val allIds = index.listAllIds() | ||
storage.cleanup(allIds) | ||
emitChangesToAffectedRootAndFav(root, storage, index) | ||
} | ||
|
||
suspend fun onReindexFinish() { | ||
allRootsFlow.emit(aggregatedTagsStorage.getTags(indexCache.listIds(RootAndFav(null, null)))) | ||
} | ||
|
||
fun listenTagsChanges(rootAndFav: RootAndFav): StateFlow<Tags?> { | ||
return if (rootAndFav.isAllRoots()) { | ||
allRootsFlow | ||
} else { | ||
if (flowByRootAndFav[rootAndFav] == null) { | ||
storageByRoot[rootAndFav.root]?.let { storage -> | ||
val ids = indexCache.listIds(rootAndFav) | ||
flowByRootAndFav[rootAndFav] = | ||
MutableStateFlow(storage.getTags(ids)) | ||
} ?: run { | ||
flowByRootAndFav[rootAndFav] = MutableStateFlow(null) | ||
} | ||
} | ||
|
||
flowByRootAndFav[rootAndFav]!! | ||
} | ||
} | ||
|
||
fun listUntagged(rootAndFav: RootAndFav): Set<ResourceId>? { | ||
val underPrefix = indexCache.listIds(rootAndFav) | ||
return if (rootAndFav.isAllRoots()) | ||
aggregatedTagsStorage.listUntaggedResources().intersect(underPrefix) | ||
else | ||
storageByRoot[rootAndFav.root]?.listUntaggedResources()?.intersect(underPrefix) | ||
} | ||
|
||
fun groupTagsByResources(ids: Iterable<ResourceId>): Map<ResourceId, Tags> = | ||
ids.map {it to getTags(it)}.toMap() | ||
|
||
fun getTags(id: ResourceId): Tags { | ||
return aggregatedTagsStorage.getTags(id) | ||
} | ||
|
||
fun getTags(ids: Iterable<ResourceId>): Tags { | ||
return aggregatedTagsStorage.getTags(ids) | ||
} | ||
|
||
fun getTags(rootAndFav: RootAndFav): Tags { | ||
return if (rootAndFav.isAllRoots()) { | ||
aggregatedTagsStorage.getTags(indexCache.listIds(rootAndFav)) | ||
} else { | ||
storageByRoot[rootAndFav.root]?.getTags(indexCache.listIds(rootAndFav)) ?: emptySet() | ||
} | ||
} | ||
|
||
suspend fun remove(resourceId: ResourceId) { | ||
aggregatedTagsStorage.remove(resourceId) | ||
} | ||
|
||
suspend fun setTags(rootAndFav: RootAndFav, id: ResourceId, tags: Tags) { | ||
if (rootAndFav.isAllRoots()) { | ||
aggregatedTagsStorage.setTags(id, tags) | ||
emitChangesToAllRootsFlow() | ||
} else { | ||
val root = rootAndFav.root!! | ||
val storage = storageByRoot[root]!! | ||
storage.setTags(id, tags) | ||
emitChangesToAffectedRootAndFav(root, storage, indexCache.getIndexByRoot(root)) | ||
} | ||
} | ||
|
||
private suspend fun emitChangesToAllRootsFlow() { | ||
aggregatedTagsStorage = AggregatedTagsStorage(storageByRoot.values) | ||
if (allRootsFlow.value != null) | ||
allRootsFlow.emit(aggregatedTagsStorage.getTags(indexCache.listIds(RootAndFav(null, null)))) | ||
} | ||
|
||
private suspend fun emitChangesToAffectedRootAndFav(root: Path, storage: PlainTagsStorage, index: PlainResourcesIndex) { | ||
emitChangesToAllRootsFlow() | ||
|
||
val affectedRootAndFavs = flowByRootAndFav.keys.filter { it.root == root } | ||
affectedRootAndFavs.forEach { | ||
val tags = storage.getTags(index.listIds(it.fav)) | ||
if (flowByRootAndFav[it]!!.value != tags) | ||
flowByRootAndFav[it]!!.emit(tags) | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/space/taran/arknavigator/mvp/model/fsmonitoring/DirectoryObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package space.taran.arknavigator.mvp.model.fsmonitoring | ||
|
||
import android.os.Build | ||
import android.os.FileObserver | ||
import androidx.annotation.RequiresApi | ||
import java.io.File | ||
|
||
class DirectoryObserver: FileObserver { | ||
@RequiresApi(Build.VERSION_CODES.Q) | ||
constructor(directory: File): super(directory) | ||
constructor(directory: String): super(directory) | ||
|
||
private lateinit var recursiveObs: RecursiveDirectoryObserver | ||
private lateinit var directory: String | ||
|
||
override fun onEvent(event: Int, path: String?) { | ||
val eventCode = event and ALL_EVENTS | ||
val eventPath = path?.let { "$directory/$path" } | ||
recursiveObs.onEvent(directory, eventCode, eventPath) | ||
} | ||
|
||
companion object { | ||
fun create(recursiveObs: RecursiveDirectoryObserver, directory: String): DirectoryObserver { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
DirectoryObserver(File(directory)).also { | ||
it.recursiveObs = recursiveObs | ||
it.directory = directory | ||
} | ||
} else { | ||
DirectoryObserver(directory).also { | ||
it.recursiveObs = recursiveObs | ||
it.directory = directory | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
resolve
:https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS40LjMwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsImpzQ29kZSI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiaW1wb3J0IGphdmEubmlvLmZpbGUuUGF0aFxuaW1wb3J0IGphdmEubmlvLmZpbGUuUGF0aHNcblxuZnVuIG1haW4oKSB7XG4gICB2YWwgcGFyZW50OiBQYXRoID0gUGF0aHMuZ2V0KFwiL2EvYi9jXCIpXG4gICB2YWwgY2hpbGQ6IFBhdGggPSBQYXRocy5nZXQoXCJkXCIpXG4gICB2YWwgcGF0aCA9IHBhcmVudC5yZXNvbHZlKGNoaWxkKVxuICAgcHJpbnRsbihwYXRoKVxufSJ9