From 51c93e671c469ee2a3daf0ea8a0dc49e5bd16e47 Mon Sep 17 00:00:00 2001 From: mdrlzy Date: Sat, 16 Sep 2023 19:38:47 +0600 Subject: [PATCH 1/4] bump arklib, update_all, remove cast on Kotlin side --- arklib/Cargo.lock | 2 +- arklib/Cargo.toml | 2 +- arklib/src/lib.rs | 58 ++++++++++--------- lib/src/main/java/space/taran/arklib/Lib.kt | 2 +- .../taran/arklib/binding/BindingIndex.kt | 23 +++----- .../taran/arklib/data/index/RootIndex.kt | 6 +- 6 files changed, 44 insertions(+), 49 deletions(-) diff --git a/arklib/Cargo.lock b/arklib/Cargo.lock index 07b7fb7c..ca2c8f83 100644 --- a/arklib/Cargo.lock +++ b/arklib/Cargo.lock @@ -56,7 +56,7 @@ checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7" [[package]] name = "arklib" version = "0.1.0" -source = "git+https://github.com/ARK-builders/arklib?rev=03add5a#03add5a4af35e9e9895717af01afa4cfd97ab5c5" +source = "git+https://github.com/ARK-builders/arklib?rev=82ba6ac#82ba6ace5de2b84affd4dbe7c87a9b8d2401d2af" dependencies = [ "anyhow", "canonical-path", diff --git a/arklib/Cargo.toml b/arklib/Cargo.toml index 3b42dfb0..1a354e22 100644 --- a/arklib/Cargo.toml +++ b/arklib/Cargo.toml @@ -8,7 +8,7 @@ name = "arklib" crate-type = ["cdylib", "staticlib"] [dependencies] -arklib = { git = "https://github.com/ARK-builders/arklib", rev = "03add5a" } +arklib = { git = "https://github.com/ARK-builders/arklib", rev = "82ba6ac" } anyhow = "1.0.58" env_logger = "0.9.0" url = { version = "2.2.2", features = ["serde"] } diff --git a/arklib/src/lib.rs b/arklib/src/lib.rs index 0dbc946d..ab6689e8 100644 --- a/arklib/src/lib.rs +++ b/arklib/src/lib.rs @@ -422,7 +422,7 @@ pub mod android { } #[no_mangle] - pub extern "C" fn Java_dev_arkbuilders_arklib_binding_BindingIndex_updateNative( + pub extern "C" fn Java_dev_arkbuilders_arklib_binding_BindingIndex_updateAllNative( env: JNIEnv, _: JClass, jni_root: JString, @@ -430,52 +430,47 @@ pub mod android { let result = match provide_index(env, jni_root) { Ok(rwlock) => { let mut index = rwlock.write().unwrap(); - index.update().unwrap() + index.update_all().unwrap() } Err(err) => { panic!("couldn't provide index {}", err) } }; - let jni_deleted_list = env.new_object("java/util/ArrayList", "()V", &[]).unwrap(); + let jni_deleted_set = env.new_object("java/util/HashSet", "()V", &[]).unwrap(); let jni_added_map = env.new_object("java/util/HashMap", "()V", &[]).unwrap(); for id in &result.deleted { - let id = env.new_string(id.to_string()).unwrap().into(); + let jni_id = id_to_jni_id(env, id); - env.call_method(jni_deleted_list, "add", "(Ljava/lang/Object;)Z", &[id]) - .unwrap(); + env.call_method( + jni_deleted_set, + "add", + "(Ljava/lang/Object;)Z", + &[jni_id] + ).unwrap(); } for (path, id) in &result.added { - let id = env.new_string(id.to_string()).unwrap().into(); - let path = env.new_string(path.to_str().unwrap()).unwrap().into(); + let jni_id = id_to_jni_id(env, id); + let jni_path = env.new_string(path.to_str().unwrap()).unwrap().into(); + env.call_method( jni_added_map, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", - &[id, path], + &[jni_id, jni_path], ) .unwrap(); } - let jni_params_list = env.new_object("java/util/ArrayList", "()V", &[]).unwrap(); - env.call_method( - jni_params_list, - "add", - "(Ljava/lang/Object;)Z", - &[jni_deleted_list.into()], - ) - .unwrap(); - env.call_method( - jni_params_list, - "add", - "(Ljava/lang/Object;)Z", - &[jni_added_map.into()], - ) - .unwrap(); + let jni_raw_updates = env.new_object( + "dev/arkbuilders/arklib/binding/RawUpdates", + "(Ljava/util/HashSet;Ljava/util/HashMap;)V", + &[jni_deleted_set.into(), jni_added_map.into()] + ).unwrap(); - jni_params_list.into_inner() + jni_raw_updates.into_inner() } #[no_mangle] @@ -501,7 +496,7 @@ pub mod android { let jni_map = env.new_object("java/util/HashMap", "()V", &[]).unwrap(); for (id, path) in &index.id2path { - let id = env.new_string(id.to_string()).unwrap().into(); + let id = id_to_jni_id(env, id); let path = env.new_string(path.to_str().unwrap()).unwrap().into(); env.call_method( jni_map, @@ -515,6 +510,17 @@ pub mod android { jni_map.into_inner() } + fn id_to_jni_id<'a>(env: JNIEnv<'a>, id: &'a ResourceId) -> JValue<'a> { + let data_size: jlong = id.data_size as usize as i64; + let crc32: jlong = id.crc32 as usize as i64; + + env.new_object( + "dev/arkbuilders/arklib/ResourceId", + "(JJ)V", + &[JValue::from(data_size), JValue::from(crc32)] + ).unwrap().into() + } + fn provide_index(env: JNIEnv, jni_root: JString) -> Result>, Error> { let root_string: String = env.get_string(jni_root).unwrap().into(); trace!("providing index for root {}", &root_string); diff --git a/lib/src/main/java/space/taran/arklib/Lib.kt b/lib/src/main/java/space/taran/arklib/Lib.kt index 58a25ccd..c363f25a 100644 --- a/lib/src/main/java/space/taran/arklib/Lib.kt +++ b/lib/src/main/java/space/taran/arklib/Lib.kt @@ -42,7 +42,7 @@ data class ResourceId( fun fromString(str: String): ResourceId { val parts = str.split(KEY_VALUE_SEPARATOR) - return ResourceId( + return ResourceId( parts[0].toLong(), parts[1].toLong() ) diff --git a/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt b/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt index d4d9c5b6..b1e3b372 100644 --- a/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt +++ b/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt @@ -5,32 +5,23 @@ import java.nio.file.Path import kotlin.io.path.Path class RawUpdates( - val deleted: Set, - val added: Map + val deleted: HashSet, + val added: HashMap, ) object BindingIndex { private external fun loadNative(root: String): Boolean fun load(root: Path): Boolean = loadNative(root.toString()) - private external fun updateNative(root: String): List - fun update(root: Path): RawUpdates { - val list = updateNative(root.toString()) - val deleted = (list[0] as List).map { - ResourceId.fromString(it) - }.toSet() - val added = (list[1] as HashMap).map { (id, path) -> - ResourceId.fromString(id) to Path(path) - }.toMap() - return RawUpdates(deleted, added) - } + private external fun updateAllNative(root: String): RawUpdates + fun updateAll(root: Path) = updateAllNative(root.toString()) private external fun storeNative(root: String) fun store(root: Path) = storeNative(root.toString()) - private external fun id2pathNative(root: String): HashMap + private external fun id2pathNative(root: String): HashMap fun id2path(root: Path): Map = - id2pathNative(root.toString()).map { (idStr, pathStr) -> - ResourceId.fromString(idStr) to Path(pathStr) + id2pathNative(root.toString()).map { (id, pathStr) -> + id to Path(pathStr) }.toMap() } \ No newline at end of file diff --git a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt index d3f936e0..8ca9ade3 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt @@ -5,8 +5,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock -import kotlinx.coroutines.withContext import dev.arkbuilders.arklib.ResourceId import dev.arkbuilders.arklib.binding.BindingIndex import dev.arkbuilders.arklib.binding.RawUpdates @@ -91,7 +89,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { // we should not have subscribers yet // this update is not pushed into the flow // it is needed only to catch up - BindingIndex.update(path) + BindingIndex.updateAll(path) BindingIndex.store(path) // id2path should be used in order to filter-out duplicates @@ -120,7 +118,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { withContextAndLock(Dispatchers.IO, mutex) { Log.i(LOG_PREFIX, "Updating the index of root $path") - val raw: RawUpdates = BindingIndex.update(path) + val raw: RawUpdates = BindingIndex.updateAll(path) BindingIndex.store(path) val updates: ResourceUpdates = wrap(raw) From 31ecd62667d6827820f3c6d3943e5d25c2799b60 Mon Sep 17 00:00:00 2001 From: mdrlzy Date: Sun, 17 Sep 2023 13:08:56 +0600 Subject: [PATCH 2/4] update_one --- arklib/Cargo.lock | 1 + arklib/Cargo.toml | 1 + arklib/src/lib.rs | 132 ++++++++++++------ .../taran/arklib/binding/BindingIndex.kt | 11 +- .../arklib/data/index/IndexAggregation.kt | 6 + .../arklib/data/index/IndexProjection.kt | 3 + .../taran/arklib/data/index/ResourceIndex.kt | 7 + .../taran/arklib/data/index/RootIndex.kt | 42 ++++-- 8 files changed, 148 insertions(+), 55 deletions(-) diff --git a/arklib/Cargo.lock b/arklib/Cargo.lock index ca2c8f83..3f6348ff 100644 --- a/arklib/Cargo.lock +++ b/arklib/Cargo.lock @@ -92,6 +92,7 @@ dependencies = [ "android_logger", "anyhow", "arklib", + "canonical-path", "env_logger", "image", "jni", diff --git a/arklib/Cargo.toml b/arklib/Cargo.toml index 1a354e22..be011ae8 100644 --- a/arklib/Cargo.toml +++ b/arklib/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["cdylib", "staticlib"] arklib = { git = "https://github.com/ARK-builders/arklib", rev = "82ba6ac" } anyhow = "1.0.58" env_logger = "0.9.0" +canonical-path = "2.0.2" url = { version = "2.2.2", features = ["serde"] } openssl = { version = "0.10.42", features = ["vendored"] } image = "0.24.2" diff --git a/arklib/src/lib.rs b/arklib/src/lib.rs index ab6689e8..9a790a1b 100644 --- a/arklib/src/lib.rs +++ b/arklib/src/lib.rs @@ -10,9 +10,12 @@ pub mod android { use arklib::pdf::PDFQuality; use std::path::PathBuf; + use std::str::FromStr; use std::sync::{Arc, RwLock}; use std::{fs::File, path::Path}; + use canonical_path::CanonicalPathBuf; + use anyhow::Error; use image::EncodableLayout; use log::{debug, trace, Level}; @@ -49,31 +52,8 @@ pub mod android { let resourceId = ResourceId::compute(data_size.try_into().unwrap(), file_path).unwrap(); - let resource_id_cls = env.find_class("dev/arkbuilders/arklib/ResourceId").unwrap(); - - let create_resource_id_fn = env - .get_static_method_id( - resource_id_cls, - "create", - "(JJ)Ldev/arkbuilders/arklib/ResourceId;", - ) - .unwrap(); - - let data_size: jlong = resourceId.data_size as usize as i64; - let crc32: jlong = resourceId.crc32 as usize as i64; - - trace!("after uszie"); - let resource_id = env - .call_static_method_unchecked( - resource_id_cls, - create_resource_id_fn, - JavaType::Object(String::from("dev/arkbuilders/arklib/ResourceId")), - &[JValue::from(data_size), JValue::from(crc32)], - ) - .unwrap() - .l() - .unwrap(); - resource_id.into_inner() + let resource_id = id_to_jni_id(env, &resourceId); + resource_id.l().unwrap().into_inner() } #[no_mangle] @@ -443,12 +423,76 @@ pub mod android { for id in &result.deleted { let jni_id = id_to_jni_id(env, id); + env.call_method(jni_deleted_set, "add", "(Ljava/lang/Object;)Z", &[jni_id]) + .unwrap(); + } + + for (path, id) in &result.added { + let jni_id = id_to_jni_id(env, id); + let jni_path = env.new_string(path.to_str().unwrap()).unwrap().into(); + env.call_method( - jni_deleted_set, - "add", - "(Ljava/lang/Object;)Z", - &[jni_id] - ).unwrap(); + jni_added_map, + "put", + "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", + &[jni_id, jni_path], + ) + .unwrap(); + } + + let jni_raw_updates = env + .new_object( + "dev/arkbuilders/arklib/binding/RawUpdates", + "(Ljava/util/HashSet;Ljava/util/HashMap;)V", + &[jni_deleted_set.into(), jni_added_map.into()], + ) + .unwrap(); + + jni_raw_updates.into_inner() + } + + #[no_mangle] + pub extern "C" fn Java_dev_arkbuilders_arklib_binding_BindingIndex_updateOneNative( + env: JNIEnv, + _: JClass, + jni_root: JString, + jni_path: JString, + jni_old_id: JString, + ) -> jobject { + let path: String = env + .get_string(jni_path) + .expect("Failed to parse path") + .into(); + + let path: &Path = Path::new(&path); + + let old_id_str: String = env + .get_string(jni_old_id) + .expect("Failed to parse old id") + .into(); + + let old_id = ResourceId::from_str(&old_id_str).unwrap(); + + let result = match provide_index(env, jni_root) { + Ok(rwlock) => { + let mut index = rwlock.write().unwrap(); + index + .update_one(CanonicalPathBuf::canonicalize(path).unwrap(), old_id) + .unwrap() + } + Err(err) => { + panic!("couldn't provide index {}", err) + } + }; + + let jni_deleted_set = env.new_object("java/util/HashSet", "()V", &[]).unwrap(); + let jni_added_map = env.new_object("java/util/HashMap", "()V", &[]).unwrap(); + + for id in &result.deleted { + let jni_id = id_to_jni_id(env, id); + + env.call_method(jni_deleted_set, "add", "(Ljava/lang/Object;)Z", &[jni_id]) + .unwrap(); } for (path, id) in &result.added { @@ -464,11 +508,13 @@ pub mod android { .unwrap(); } - let jni_raw_updates = env.new_object( - "dev/arkbuilders/arklib/binding/RawUpdates", - "(Ljava/util/HashSet;Ljava/util/HashMap;)V", - &[jni_deleted_set.into(), jni_added_map.into()] - ).unwrap(); + let jni_raw_updates = env + .new_object( + "dev/arkbuilders/arklib/binding/RawUpdates", + "(Ljava/util/HashSet;Ljava/util/HashMap;)V", + &[jni_deleted_set.into(), jni_added_map.into()], + ) + .unwrap(); jni_raw_updates.into_inner() } @@ -496,13 +542,13 @@ pub mod android { let jni_map = env.new_object("java/util/HashMap", "()V", &[]).unwrap(); for (id, path) in &index.id2path { - let id = id_to_jni_id(env, id); - let path = env.new_string(path.to_str().unwrap()).unwrap().into(); + let jni_id = id_to_jni_id(env, id); + let jni_path = env.new_string(path.to_str().unwrap()).unwrap().into(); env.call_method( jni_map, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", - &[id, path], + &[jni_id, jni_path], ) .unwrap(); } @@ -515,10 +561,12 @@ pub mod android { let crc32: jlong = id.crc32 as usize as i64; env.new_object( - "dev/arkbuilders/arklib/ResourceId", - "(JJ)V", - &[JValue::from(data_size), JValue::from(crc32)] - ).unwrap().into() + "dev/arkbuilders/arklib/ResourceId", + "(JJ)V", + &[JValue::from(data_size), JValue::from(crc32)], + ) + .unwrap() + .into() } fn provide_index(env: JNIEnv, jni_root: JString) -> Result>, Error> { diff --git a/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt b/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt index b1e3b372..694b0c16 100644 --- a/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt +++ b/lib/src/main/java/space/taran/arklib/binding/BindingIndex.kt @@ -6,7 +6,7 @@ import kotlin.io.path.Path class RawUpdates( val deleted: HashSet, - val added: HashMap, + val added: HashMap, ) object BindingIndex { @@ -16,6 +16,15 @@ object BindingIndex { private external fun updateAllNative(root: String): RawUpdates fun updateAll(root: Path) = updateAllNative(root.toString()) + private external fun updateOneNative( + root: String, + path: String, + oldId: String + ): RawUpdates + + fun updateOne(root: Path, path: Path, oldId: ResourceId) = + updateOneNative(root.toString(), path.toString(), oldId.toString()) + private external fun storeNative(root: String) fun store(root: Path) = storeNative(root.toString()) diff --git a/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt b/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt index b02868ad..55c520b3 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt @@ -54,4 +54,10 @@ class IndexAggregation( override suspend fun updateAll() { shards.forEach { it.updateAll() } } + + override suspend fun updateOne(resourcePath: Path, oldId: ResourceId) { + shards.find { + resourcePath.startsWith(it.path) + }?.updateOne(resourcePath, oldId) + } } \ No newline at end of file diff --git a/lib/src/main/java/space/taran/arklib/data/index/IndexProjection.kt b/lib/src/main/java/space/taran/arklib/data/index/IndexProjection.kt index dc64a332..0c415da9 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/IndexProjection.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/IndexProjection.kt @@ -48,6 +48,9 @@ class IndexProjection( override suspend fun updateAll() = root.updateAll() + override suspend fun updateOne(resourcePath: Path, oldId: ResourceId) = + root.updateOne(resourcePath, oldId) + override fun allResources(): Map { val allPathsMap = root.allPaths() return root diff --git a/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt b/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt index b2bb11a4..9044e862 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt @@ -13,6 +13,13 @@ interface ResourceIndex { suspend fun updateAll() + suspend fun updateOne(resourcePath: Path, oldId: ResourceId) + + suspend fun updateOne(oldId: ResourceId) = updateOne( + allPaths()[oldId]!!, + oldId + ) + fun allResources(): Map fun getResource(id: ResourceId): Resource? diff --git a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt index 8ca9ade3..3a292927 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt @@ -11,6 +11,7 @@ import dev.arkbuilders.arklib.binding.RawUpdates import dev.arkbuilders.arklib.utils.withContextAndLock import java.nio.file.Path import java.util.concurrent.ConcurrentHashMap +import kotlin.io.path.Path /** * [RootIndex] is a type of index backed by storage file. @@ -53,7 +54,8 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { val added = update.added //we can't present empty resources - .mapNotNull { (id, path) -> + .mapNotNull { (id, _path) -> + val path = Path(_path) val resource: Resource = Resource.compute(id, path) .onFailure { error -> Log.e( @@ -120,19 +122,19 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { val raw: RawUpdates = BindingIndex.updateAll(path) BindingIndex.store(path) + handleRawUpdates(raw) + } - val updates: ResourceUpdates = wrap(raw) - - updates.deleted.forEach { (id, _) -> - resourceAndPathById.remove(id) - } - - updates.added.forEach { (id, added) -> - resourceAndPathById[id] = added.resource to added.path - } + override suspend fun updateOne(resourcePath: Path, oldId: ResourceId): Unit = + withContextAndLock(Dispatchers.IO, mutex) { + Log.i( + LOG_PREFIX, + "Updating one resource[$resourcePath] the index of root $path" + ) - _updates.emit(updates) - check() + val raw: RawUpdates = BindingIndex.updateOne(path, resourcePath, oldId) + BindingIndex.store(path) + handleRawUpdates(raw) } override fun allResources(): Map = @@ -154,6 +156,22 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { NewResource(path, resource) }.toSet() + private suspend fun handleRawUpdates(raw: RawUpdates) { + BindingIndex.store(path) + + val updates: ResourceUpdates = wrap(raw) + + updates.deleted.forEach { (id, _) -> + resourceAndPathById.remove(id) + } + + updates.added.forEach { (id, added) -> + resourceAndPathById[id] = added.resource to added.path + } + + _updates.emit(updates) + check() + } private fun check() { val resourceById = resourceAndPathById.mapValues { it.value.first } From 346e727feae359db4d261e4770a98925b32684bf Mon Sep 17 00:00:00 2001 From: mdrlzy Date: Mon, 18 Sep 2023 17:24:23 +0600 Subject: [PATCH 3/4] Index: return ResourceUpdates from updateOne --- .../taran/arklib/data/index/IndexAggregation.kt | 14 ++++++++++---- .../space/taran/arklib/data/index/ResourceIndex.kt | 4 ++-- .../space/taran/arklib/data/index/RootIndex.kt | 7 ++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt b/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt index 55c520b3..22c2f683 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/IndexAggregation.kt @@ -55,9 +55,15 @@ class IndexAggregation( shards.forEach { it.updateAll() } } - override suspend fun updateOne(resourcePath: Path, oldId: ResourceId) { - shards.find { - resourcePath.startsWith(it.path) - }?.updateOne(resourcePath, oldId) + override suspend fun updateOne( + resourcePath: Path, + oldId: ResourceId + ): ResourceUpdates { + return shards.find { resourcePath.startsWith(it.path) } + ?.updateOne(resourcePath, oldId) + ?: error( + "At least one shard must contain the passed path" + + "shards: ${shards.map { it.path }} path: $resourcePath" + ) } } \ No newline at end of file diff --git a/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt b/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt index 9044e862..23547389 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/ResourceIndex.kt @@ -13,9 +13,9 @@ interface ResourceIndex { suspend fun updateAll() - suspend fun updateOne(resourcePath: Path, oldId: ResourceId) + suspend fun updateOne(resourcePath: Path, oldId: ResourceId): ResourceUpdates - suspend fun updateOne(oldId: ResourceId) = updateOne( + suspend fun updateOne(oldId: ResourceId): ResourceUpdates = updateOne( allPaths()[oldId]!!, oldId ) diff --git a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt index 3a292927..6fa77434 100644 --- a/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt +++ b/lib/src/main/java/space/taran/arklib/data/index/RootIndex.kt @@ -125,7 +125,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { handleRawUpdates(raw) } - override suspend fun updateOne(resourcePath: Path, oldId: ResourceId): Unit = + override suspend fun updateOne(resourcePath: Path, oldId: ResourceId): ResourceUpdates = withContextAndLock(Dispatchers.IO, mutex) { Log.i( LOG_PREFIX, @@ -134,7 +134,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { val raw: RawUpdates = BindingIndex.updateOne(path, resourcePath, oldId) BindingIndex.store(path) - handleRawUpdates(raw) + return@withContextAndLock handleRawUpdates(raw) } override fun allResources(): Map = @@ -156,7 +156,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { NewResource(path, resource) }.toSet() - private suspend fun handleRawUpdates(raw: RawUpdates) { + private suspend fun handleRawUpdates(raw: RawUpdates): ResourceUpdates { BindingIndex.store(path) val updates: ResourceUpdates = wrap(raw) @@ -171,6 +171,7 @@ class RootIndex private constructor(val path: Path) : ResourceIndex { _updates.emit(updates) check() + return updates } private fun check() { From 89d6da7d6e4397af847a6df19ec80c8793236fcf Mon Sep 17 00:00:00 2001 From: rizary Date: Mon, 18 Dec 2023 21:20:18 +0700 Subject: [PATCH 4/4] update to the latest arklib --- arklib/Cargo.lock | 3 ++- arklib/Cargo.toml | 2 +- arklib/src/lib.rs | 11 +++++------ lib/build.gradle | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arklib/Cargo.lock b/arklib/Cargo.lock index 3f6348ff..10028312 100644 --- a/arklib/Cargo.lock +++ b/arklib/Cargo.lock @@ -56,7 +56,7 @@ checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7" [[package]] name = "arklib" version = "0.1.0" -source = "git+https://github.com/ARK-builders/arklib?rev=82ba6ac#82ba6ace5de2b84affd4dbe7c87a9b8d2401d2af" +source = "git+https://github.com/ARK-builders/arklib?rev=d1fab20#d1fab2083a8bd0ae91faeeebcfd016cfbce8a215" dependencies = [ "anyhow", "canonical-path", @@ -78,6 +78,7 @@ dependencies = [ "serde_json", "tar", "target-lexicon", + "thiserror", "tokio", "ureq", "url", diff --git a/arklib/Cargo.toml b/arklib/Cargo.toml index be011ae8..27088bd7 100644 --- a/arklib/Cargo.toml +++ b/arklib/Cargo.toml @@ -8,7 +8,7 @@ name = "arklib" crate-type = ["cdylib", "staticlib"] [dependencies] -arklib = { git = "https://github.com/ARK-builders/arklib", rev = "82ba6ac" } +arklib = { git = "https://github.com/ARK-builders/arklib", rev = "d1fab20" } anyhow = "1.0.58" env_logger = "0.9.0" canonical-path = "2.0.2" diff --git a/arklib/src/lib.rs b/arklib/src/lib.rs index 9a790a1b..0dd8f3f6 100644 --- a/arklib/src/lib.rs +++ b/arklib/src/lib.rs @@ -13,10 +13,7 @@ pub mod android { use std::str::FromStr; use std::sync::{Arc, RwLock}; use std::{fs::File, path::Path}; - - use canonical_path::CanonicalPathBuf; - - use anyhow::Error; + use image::EncodableLayout; use log::{debug, trace, Level}; use url::Url; @@ -27,6 +24,8 @@ pub mod android { use jni::JNIEnv; extern crate android_logger; use android_logger::Config; + + pub type Result = std::result::Result; #[no_mangle] pub extern "C" fn Java_dev_arkbuilders_arklib_LibKt_initRustLogger(_: JNIEnv, _: JClass) { @@ -477,7 +476,7 @@ pub mod android { Ok(rwlock) => { let mut index = rwlock.write().unwrap(); index - .update_one(CanonicalPathBuf::canonicalize(path).unwrap(), old_id) + .update_one(&path, old_id) .unwrap() } Err(err) => { @@ -569,7 +568,7 @@ pub mod android { .into() } - fn provide_index(env: JNIEnv, jni_root: JString) -> Result>, Error> { + fn provide_index(env: JNIEnv, jni_root: JString) -> Result>> { let root_string: String = env.get_string(jni_root).unwrap().into(); trace!("providing index for root {}", &root_string); diff --git a/lib/build.gradle b/lib/build.gradle index 2e17f591..bf6dfbe4 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -48,7 +48,7 @@ cargo { prebuiltToolchains = true // Set to "debug" to enable logging. Or "release" to make a production build. // https://github.com/mozilla/rust-android-gradle/issues/38 - profile = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")} ? "debug" : "release" + profile = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")} ? "debug" : "release" } dependencies {