-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
299 additions
and
52 deletions.
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
27 changes: 12 additions & 15 deletions
27
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 |
---|---|---|
@@ -1,38 +1,35 @@ | ||
package space.taran.arknavigator.mvp.model | ||
|
||
import space.taran.arknavigator.mvp.model.dao.ResourceId | ||
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.Files | ||
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 resourcesIndexFactory: ResourcesIndexFactory, | ||
private val fsMonitoring: FSMonitoring | ||
) { | ||
suspend fun reindex() { | ||
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(path: Path) { | ||
val index = resourcesIndexFactory.buildFromFilesystem(path) | ||
indexCache.onIndexChange(path, index) | ||
tagsCache.onIndexChanged(path, index) | ||
} | ||
|
||
suspend fun remove(resourceId: ResourceId): Path? { | ||
val path = indexCache.remove(resourceId) | ||
tagsCache.remove(resourceId) | ||
Files.delete(path) | ||
return path | ||
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()) | ||
} | ||
} |
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
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 | ||
} | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/space/taran/arknavigator/mvp/model/fsmonitoring/FSMonitoring.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,56 @@ | ||
package space.taran.arknavigator.mvp.model.fsmonitoring | ||
|
||
import android.os.FileObserver | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import space.taran.arknavigator.mvp.model.IndexCache | ||
import space.taran.arknavigator.mvp.model.TagsCache | ||
import space.taran.arknavigator.utils.isTagsStorage | ||
import java.nio.file.Path | ||
|
||
class FSMonitoring( | ||
val indexCache: IndexCache, | ||
val tagsCache: TagsCache, | ||
val appScope: CoroutineScope | ||
) { | ||
private val rootObservers = mutableListOf<RecursiveDirectoryObserver>() | ||
|
||
fun startWatchingRoot(root: String) { | ||
val obs = RecursiveDirectoryObserver(this, root) | ||
rootObservers.add(obs) | ||
obs.startWatching() | ||
} | ||
|
||
fun onEvent(root: Path, directory: Path, event: Int, eventPath: Path?) { | ||
when (event) { | ||
FileObserver.CREATE -> {} | ||
FileObserver.CLOSE_WRITE -> onCloseWrite(root, directory, eventPath!!) | ||
FileObserver.DELETE -> onDelete(root, directory, eventPath!!) | ||
FileObserver.MOVE_SELF -> {} | ||
FileObserver.MOVED_FROM -> onMovedFrom(root, directory, eventPath!!) | ||
FileObserver.MOVED_TO -> onMovedTo(root, directory, eventPath!!) | ||
FileObserver.DELETE_SELF -> {} | ||
} | ||
} | ||
|
||
private fun onCloseWrite(root: Path, directory: Path, eventPath: Path) = appScope.launch(Dispatchers.Default) { | ||
if (!isTagsStorage(eventPath)) { | ||
val index = indexCache.onResourceModified(root, eventPath) | ||
tagsCache.onIndexChanged(root, index) | ||
} | ||
} | ||
|
||
private fun onDelete(root: Path, directory: Path, eventPath: Path) = appScope.launch(Dispatchers.Default) { | ||
val id = indexCache.onResourceDeleted(root, eventPath) | ||
tagsCache.remove(id) | ||
} | ||
|
||
private fun onMovedFrom(root: Path, directory: Path, eventPath: Path) = appScope.launch(Dispatchers.Default) { | ||
indexCache.onResourceDeleted(root, eventPath) | ||
} | ||
|
||
private fun onMovedTo(root: Path, directory: Path, eventPath: Path) = appScope.launch(Dispatchers.Default) { | ||
indexCache.onResourceCreated(root, eventPath) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...c/main/java/space/taran/arknavigator/mvp/model/fsmonitoring/RecursiveDirectoryObserver.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,38 @@ | ||
package space.taran.arknavigator.mvp.model.fsmonitoring | ||
|
||
import space.taran.arknavigator.utils.FSEventLogger | ||
import java.io.File | ||
import java.util.* | ||
import kotlin.io.path.Path | ||
|
||
class RecursiveDirectoryObserver( | ||
private val fsMonitoring: FSMonitoring, | ||
private val root: String | ||
) { | ||
var directoryObservers: MutableList<DirectoryObserver> = mutableListOf() | ||
|
||
fun startWatching() { | ||
directoryObservers = mutableListOf() | ||
val stack: Stack<String> = Stack() | ||
stack.push(root) | ||
while (!stack.empty()) { | ||
val parent: String = stack.pop() | ||
directoryObservers.add(DirectoryObserver.create(this, parent)) | ||
val path = File(parent) | ||
val files: Array<File> = path.listFiles() ?: continue | ||
for (i in files.indices) { | ||
if (files[i].isDirectory && !files[i].name.equals(".") | ||
&& !files[i].name.equals("..") | ||
) { | ||
stack.push(files[i].path) | ||
} | ||
} | ||
} | ||
for (i in directoryObservers.indices) directoryObservers[i].startWatching() | ||
} | ||
|
||
fun onEvent(directory: String, event: Int, eventPath: String?) { | ||
FSEventLogger.log(directory, event, eventPath) | ||
fsMonitoring.onEvent(Path(root), Path(directory), event, eventPath?.let { Path(it) }) | ||
} | ||
} |
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
Oops, something went wrong.