From ae82b6519f6c2f4097db4d6c23d26ff8832b2f50 Mon Sep 17 00:00:00 2001 From: Arman Ghazaryan <91345263+Arman-Ghazaryan@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:18:57 +0400 Subject: [PATCH 1/4] Add: impl. of rename and remove funcs --- java/cloud/unum/usearch/Index.java | 12 ++++++++++++ .../unum/usearch/cloud_unum_usearch_Index.cpp | 9 +++++++++ .../unum/usearch/cloud_unum_usearch_Index.h | 16 ++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/java/cloud/unum/usearch/Index.java b/java/cloud/unum/usearch/Index.java index 3b234720..ff76c306 100644 --- a/java/cloud/unum/usearch/Index.java +++ b/java/cloud/unum/usearch/Index.java @@ -69,6 +69,14 @@ public void view(String path) { c_view(c_ptr, path); } + public boolean remove(int key) { + return c_remove(c_ptr, key); + } + + public boolean rename(int from, int to) { + return c_rename(c_ptr, from, to); + } + public static class Config { private String _metric = "ip"; private String _quantization = "f32"; @@ -163,4 +171,8 @@ private static native long c_create(// private static native void c_load(long ptr, String path); private static native void c_view(long ptr, String path); + + private static native boolean c_remove(long ptr, int key); + + private static native boolean c_rename(long ptr, int from, int to); } \ No newline at end of file diff --git a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp index 3f8d4d3b..12662beb 100644 --- a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp +++ b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp @@ -164,3 +164,12 @@ JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search( // std::free(matches_data); return matches; } + +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove(JNIEnv*, jclass, jlong c_ptr, jlong key) { + return reinterpret_cast(c_ptr)->remove(static_cast(key)).completed; +} + +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1rename(JNIEnv*, jclass, jlong c_ptr, jlong from, jlong to) { + using key_t = typename index_dense_t::key_t; + return reinterpret_cast(c_ptr)->rename(static_cast(from),static_cast(to)).completed; +} \ No newline at end of file diff --git a/java/cloud/unum/usearch/cloud_unum_usearch_Index.h b/java/cloud/unum/usearch/cloud_unum_usearch_Index.h index 9b8b6482..a79a87f2 100644 --- a/java/cloud/unum/usearch/cloud_unum_usearch_Index.h +++ b/java/cloud/unum/usearch/cloud_unum_usearch_Index.h @@ -103,6 +103,22 @@ JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1load JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1view (JNIEnv *, jclass, jlong, jstring); +/* + * Class: cloud_unum_usearch_Index + * Method: c_remove + * Signature: (JLjava/lang/String;)V + */ +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove + (JNIEnv *, jclass, jlong); + +/* + * Class: cloud_unum_usearch_Index + * Method: c_rename + * Signature: (JLjava/lang/String;)V + */ +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1rename + (JNIEnv *, jclass, jlong, jlong); + #ifdef __cplusplus } #endif From 619d9f19d55245ede90c18546bcc1f00d4c78787 Mon Sep 17 00:00:00 2001 From: Arman Ghazaryan <91345263+Arman-Ghazaryan@users.noreply.github.com> Date: Mon, 14 Aug 2023 12:30:34 +0400 Subject: [PATCH 2/4] Add: Error handling for remove and rename --- .../unum/usearch/cloud_unum_usearch_Index.cpp | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp index 12662beb..071513a1 100644 --- a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp +++ b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp @@ -165,11 +165,26 @@ JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search( // return matches; } -JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove(JNIEnv*, jclass, jlong c_ptr, jlong key) { - return reinterpret_cast(c_ptr)->remove(static_cast(key)).completed; +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove(JNIEnv* env, jclass, jlong c_ptr, jlong key) { + using key_t = typename index_dense_t::key_t; + using labeling_result_t = typename index_dense_t::labeling_result_t; + labeling_result_t result = reinterpret_cast(c_ptr)->remove(static_cast(key)); + if (!result) { + jclass jc = (*env).FindClass("java/lang/Error"); + if (jc) + (*env).ThrowNew(jc, "Failed to remove in vector index!"); + } + return result.completed; } -JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1rename(JNIEnv*, jclass, jlong c_ptr, jlong from, jlong to) { +JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1rename(JNIEnv* env, jclass, jlong c_ptr, jlong from, jlong to) { using key_t = typename index_dense_t::key_t; - return reinterpret_cast(c_ptr)->rename(static_cast(from),static_cast(to)).completed; + using labeling_result_t = typename index_dense_t::labeling_result_t; + labeling_result_t result = reinterpret_cast(c_ptr)->rename(static_cast(from), static_cast(to)); + if (!result) { + jclass jc = (*env).FindClass("java/lang/Error"); + if (jc) + (*env).ThrowNew(jc, "Failed to rename in vector index!"); + } + return result.completed; } \ No newline at end of file From 7fbf72fde49c61f9bf2302607b5653de91c459c0 Mon Sep 17 00:00:00 2001 From: Ash Vardanian <1983160+ashvardanian@users.noreply.github.com> Date: Mon, 14 Aug 2023 09:38:17 +0100 Subject: [PATCH 3/4] Fix: `stream_length` to include levels length --- include/usearch/index.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/usearch/index.hpp b/include/usearch/index.hpp index e0685418..cf4dc3cf 100644 --- a/include/usearch/index.hpp +++ b/include/usearch/index.hpp @@ -2443,7 +2443,7 @@ class index_gt { std::size_t stream_length() const noexcept { std::size_t neighbors_length = 0; for (std::size_t i = 0; i != size(); ++i) - neighbors_length += node_bytes_(node_at_(i).level()); + neighbors_length += node_bytes_(node_at_(i).level()) + sizeof(level_t); return sizeof(index_serialized_header_t) + neighbors_length; } From 552e1b73ae90aeced98cdafe212e3986c074aea8 Mon Sep 17 00:00:00 2001 From: Arman Ghazaryan <91345263+Arman-Ghazaryan@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:02:51 +0400 Subject: [PATCH 4/4] Refactor: use result error as error message --- .../unum/usearch/cloud_unum_usearch_Index.cpp | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp index 071513a1..0439a828 100644 --- a/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp +++ b/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp @@ -58,30 +58,33 @@ JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1create( // JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1save(JNIEnv* env, jclass, jlong c_ptr, jstring path) { char const* path_cstr = (*env).GetStringUTFChars(path, 0); - if (!reinterpret_cast(c_ptr)->save(path_cstr)) { + serialization_result_t result = reinterpret_cast(c_ptr)->save(path_cstr); + if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to dump vector index to path!"); + (*env).ThrowNew(jc, result.error.release()); } (*env).ReleaseStringUTFChars(path, path_cstr); } JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1load(JNIEnv* env, jclass, jlong c_ptr, jstring path) { char const* path_cstr = (*env).GetStringUTFChars(path, 0); - if (!reinterpret_cast(c_ptr)->load(path_cstr)) { + serialization_result_t result = reinterpret_cast(c_ptr)->load(path_cstr); + if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to load vector index from path!"); + (*env).ThrowNew(jc, result.error.release()); } (*env).ReleaseStringUTFChars(path, path_cstr); } JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1view(JNIEnv* env, jclass, jlong c_ptr, jstring path) { char const* path_cstr = (*env).GetStringUTFChars(path, 0); - if (!reinterpret_cast(c_ptr)->view(path_cstr)) { + serialization_result_t result = reinterpret_cast(c_ptr)->view(path_cstr); + if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to view vector index from path!"); + (*env).ThrowNew(jc, result.error.release()); } (*env).ReleaseStringUTFChars(path, path_cstr); } @@ -122,10 +125,13 @@ JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1add( // float_span_t vector_span = float_span_t{vector_data, static_cast(vector_dims)}; using key_t = typename index_dense_t::key_t; - if (!reinterpret_cast(c_ptr)->add(static_cast(key), vector_span)) { + using add_result_t = typename index_dense_t::add_result_t; + + add_result_t result = reinterpret_cast(c_ptr)->add(static_cast(key), vector_span); + if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to insert a new point in vector index!"); + (*env).ThrowNew(jc, result.error.release()); } (*env).ReleaseFloatArrayElements(vector, vector_data, 0); } @@ -157,7 +163,7 @@ JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search( // } else { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to find in vector index!"); + (*env).ThrowNew(jc, result.error.release()); } (*env).ReleaseFloatArrayElements(vector, vector_data, 0); @@ -172,7 +178,7 @@ JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove(JNIEnv* env, jcla if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to remove in vector index!"); + (*env).ThrowNew(jc, result.error.release()); } return result.completed; } @@ -180,11 +186,12 @@ JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1remove(JNIEnv* env, jcla JNIEXPORT bool JNICALL Java_cloud_unum_usearch_Index_c_1rename(JNIEnv* env, jclass, jlong c_ptr, jlong from, jlong to) { using key_t = typename index_dense_t::key_t; using labeling_result_t = typename index_dense_t::labeling_result_t; - labeling_result_t result = reinterpret_cast(c_ptr)->rename(static_cast(from), static_cast(to)); + labeling_result_t result = + reinterpret_cast(c_ptr)->rename(static_cast(from), static_cast(to)); if (!result) { jclass jc = (*env).FindClass("java/lang/Error"); if (jc) - (*env).ThrowNew(jc, "Failed to rename in vector index!"); + (*env).ThrowNew(jc, result.error.release()); } return result.completed; } \ No newline at end of file