Skip to content

Commit e50d698

Browse files
authored
Add: Java get API (#407)
1 parent 1f93994 commit e50d698

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ model {
8686
}
8787

8888
toolChains {
89+
clang(Clang)
8990
gcc(Gcc) {
9091
target("linux_aarch64") {
9192
cppCompiler.withArguments { args ->

java/cloud/unum/usearch/Index.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ public int[] search(float vector[], long count) {
122122
return c_search(c_ptr, vector, count);
123123
}
124124

125+
/**
126+
* Return the contents of the vector at key.
127+
*
128+
* @param key key to lookup.
129+
* @return the contents of the vector.
130+
* @throws {@link IllegalArgumentException} is key is not available.
131+
*/
132+
public float[] get(int key) {
133+
return c_get(c_ptr, key);
134+
}
135+
125136
/**
126137
* Saves the index to a file.
127138
*
@@ -296,7 +307,11 @@ public Config expansion_search(long _expansion_search) {
296307
System.loadLibrary("usearch"); // used for tests. This library in classpath only
297308
} catch (UnsatisfiedLinkError e) {
298309
try {
299-
NativeUtils.loadLibraryFromJar("/usearch/libusearch.so");
310+
if (System.getProperty("os.name").equals("Mac OS X")) {
311+
NativeUtils.loadLibraryFromJar("/usearch/libusearch.dylib");
312+
} else {
313+
NativeUtils.loadLibraryFromJar("/usearch/libusearch.so");
314+
}
300315
} catch (IOException e1) {
301316
throw new RuntimeException(e1);
302317
}
@@ -339,6 +354,8 @@ private static native long c_create(
339354

340355
private static native int[] c_search(long ptr, float vector[], long count);
341356

357+
private static native float[] c_get(long ptr, int key);
358+
342359
private static native void c_save(long ptr, String path);
343360

344361
private static native void c_load(long ptr, String path);

java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1add( //
141141
(*env).ReleaseFloatArrayElements(vector, vector_data, 0);
142142
}
143143

144+
JNIEXPORT jfloatArray JNICALL Java_cloud_unum_usearch_Index_c_1get(
145+
JNIEnv *env, jclass, jlong c_ptr, jint key) {
146+
147+
auto index = reinterpret_cast<index_dense_t*>(c_ptr);
148+
size_t dim = index->dimensions();
149+
std::unique_ptr<jfloat[]> vector(new jfloat[dim]);
150+
if (index->get(key, vector.get()) == 0) {
151+
jclass jc = env->FindClass("java/lang/IllegalArgumentException");
152+
if (jc) {
153+
env->ThrowNew(jc, "key not found");
154+
}
155+
}
156+
jfloatArray jvector = env->NewFloatArray(dim);
157+
if (jvector == nullptr) { // out of memory
158+
return nullptr;
159+
}
160+
env->SetFloatArrayRegion(jvector, 0, dim, vector.get());
161+
return jvector;
162+
}
163+
144164
JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search( //
145165
JNIEnv* env, jclass, jlong c_ptr, jfloatArray vector, jlong wanted) {
146166

java/cloud/unum/usearch/cloud_unum_usearch_Index.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/test/IndexTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import static org.junit.Assert.assertArrayEquals;
2+
import static org.junit.Assert.assertThrows;
3+
14
import java.io.File;
2-
import java.util.Arrays;
35

46
import org.junit.Test;
57

@@ -28,4 +30,22 @@ public void test() {
2830

2931
System.out.println("Java Tests Passed!");
3032
}
33+
34+
public void testGetSuccess() {
35+
Index index = new Index.Config().metric("cos").dimensions(2).build();
36+
float vec[] = { 10, 20 };
37+
index.reserve(10);
38+
index.add(42, vec);
39+
40+
assertArrayEquals(vec, index.get(42), 0.01f);
41+
}
42+
43+
public void testGetFailed() {
44+
Index index = new Index.Config().metric("cos").dimensions(2).build();
45+
float vec[] = { 10, 20 };
46+
index.reserve(10);
47+
index.add(42, vec);
48+
49+
assertThrows(IllegalArgumentException.class, () -> index.get(41));
50+
}
3151
}

0 commit comments

Comments
 (0)