Skip to content

Commit

Permalink
Merge branch 'main-dev' of https://github.com/ashvardanian/usearch in…
Browse files Browse the repository at this point in the history
…to main-dev
  • Loading branch information
ashvardanian committed Aug 14, 2023
2 parents 7e5f6a7 + 79f33d7 commit 0307e6e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/usearch/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2518,7 +2518,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;
}

Expand Down
12 changes: 12 additions & 0 deletions java/cloud/unum/usearch/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -164,4 +172,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);
}
49 changes: 40 additions & 9 deletions java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<index_dense_t*>(c_ptr)->save(path_cstr)) {
serialization_result_t result = reinterpret_cast<index_dense_t*>(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<index_dense_t*>(c_ptr)->load(path_cstr)) {
serialization_result_t result = reinterpret_cast<index_dense_t*>(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<index_dense_t*>(c_ptr)->view(path_cstr)) {
serialization_result_t result = reinterpret_cast<index_dense_t*>(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);
}
Expand Down Expand Up @@ -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<std::size_t>(vector_dims)};

using key_t = typename index_dense_t::key_t;
if (!reinterpret_cast<index_dense_t*>(c_ptr)->add(static_cast<key_t>(key), vector_span)) {
using add_result_t = typename index_dense_t::add_result_t;

add_result_t result = reinterpret_cast<index_dense_t*>(c_ptr)->add(static_cast<key_t>(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);
}
Expand Down Expand Up @@ -157,10 +163,35 @@ 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);
std::free(matches_data);
return matches;
}

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<index_dense_t*>(c_ptr)->remove(static_cast<key_t>(key));
if (!result) {
jclass jc = (*env).FindClass("java/lang/Error");
if (jc)
(*env).ThrowNew(jc, result.error.release());
}
return result.completed;
}

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<index_dense_t*>(c_ptr)->rename(static_cast<key_t>(from), static_cast<key_t>(to));
if (!result) {
jclass jc = (*env).FindClass("java/lang/Error");
if (jc)
(*env).ThrowNew(jc, result.error.release());
}
return result.completed;
}
16 changes: 16 additions & 0 deletions java/cloud/unum/usearch/cloud_unum_usearch_Index.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0307e6e

Please sign in to comment.