usearch 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +9 -0
- package/.github/workflows/release.yml +22 -0
- package/.vscode/settings.json +2 -1
- package/{CMakeListsInstructuons.md → CMakeListsInstructions.md} +28 -1
- package/Cargo.toml +1 -1
- package/README.md +11 -3
- package/VERSION +1 -1
- package/build.gradle +127 -0
- package/include/usearch/usearch.hpp +19 -11
- package/java/cloud/unum/usearch/Index.java +166 -0
- package/java/cloud/unum/usearch/cloud_unum_usearch_Index.cpp +173 -0
- package/java/cloud/unum/usearch/cloud_unum_usearch_Index.h +109 -0
- package/java/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/java/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/java/gradlew +244 -0
- package/java/test/IndexTest.java +25 -0
- package/package.json +1 -1
- package/python/lib.cpp +2 -24
- package/rust/lib.cpp +1 -1
- package/src/advanced.hpp +24 -7
- package/wolfram/lib.cpp +21 -0
package/.gitattributes
ADDED
|
@@ -108,6 +108,28 @@ jobs:
|
|
|
108
108
|
with:
|
|
109
109
|
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
|
|
110
110
|
|
|
111
|
+
publish_java:
|
|
112
|
+
name: Publish Java
|
|
113
|
+
runs-on: ubuntu-latest
|
|
114
|
+
permissions:
|
|
115
|
+
contents: read
|
|
116
|
+
packages: write
|
|
117
|
+
steps:
|
|
118
|
+
- uses: actions/checkout@v3
|
|
119
|
+
- run: git submodule update --init --recursive
|
|
120
|
+
- uses: actions/setup-java@v3
|
|
121
|
+
with:
|
|
122
|
+
java-version: '11'
|
|
123
|
+
distribution: 'adopt'
|
|
124
|
+
- name: Setup Gradle
|
|
125
|
+
uses: gradle/gradle-build-action@v2
|
|
126
|
+
- name: Execute Gradle build
|
|
127
|
+
run: gradle clean build
|
|
128
|
+
- name: Publish package
|
|
129
|
+
run: gradle publish
|
|
130
|
+
env:
|
|
131
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
132
|
+
|
|
111
133
|
deploy_docs:
|
|
112
134
|
name: Deploy Docs
|
|
113
135
|
environment:
|
package/.vscode/settings.json
CHANGED
|
@@ -75,13 +75,40 @@ cargo publish
|
|
|
75
75
|
### Java
|
|
76
76
|
|
|
77
77
|
```sh
|
|
78
|
-
|
|
78
|
+
gradle clean build
|
|
79
|
+
java -cp . -Djava.library.path=/Users/av/github/usearch/build/libs/usearch/shared java/cloud/unum/usearch/Index.java
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Or step by-step:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
cs java/unum/cloud/usearch
|
|
79
86
|
javac -h . Index.java
|
|
80
87
|
|
|
88
|
+
# Ubuntu:
|
|
89
|
+
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux cloud_unum_usearch_Index.cpp -o cloud_unum_usearch_Index.o
|
|
90
|
+
g++ -shared -fPIC -o libusearch.so cloud_unum_usearch_Index.o -lc
|
|
91
|
+
|
|
92
|
+
# Windows
|
|
93
|
+
g++ -c -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 cloud_unum_usearch_Index.cpp -o cloud_unum_usearch_Index.o
|
|
94
|
+
g++ -shared -o USearchJNI.dll cloud_unum_usearch_Index.o -Wl,--add-stdcall-alias
|
|
95
|
+
|
|
96
|
+
# MacOS
|
|
97
|
+
g++ -std=c++11 -c -fPIC \
|
|
98
|
+
-I../../../../include -I../../../../src -I../../../../fp16/include -I../../../../simsimd/include \
|
|
99
|
+
-I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin cloud_unum_usearch_Index.cpp -o cloud_unum_usearch_Index.o
|
|
100
|
+
g++ -dynamiclib -o libusearch.dylib cloud_unum_usearch_Index.o -lc
|
|
101
|
+
# Run linking to that directory
|
|
102
|
+
java -cp . -Djava.library.path=/Users/av/github/usearch/java/cloud/unum/usearch/ Index.java
|
|
103
|
+
java -cp . -Djava.library.path=/Users/av/github/usearch/java cloud.unum.usearch.Index
|
|
81
104
|
```
|
|
82
105
|
|
|
83
106
|
### Wolfram
|
|
84
107
|
|
|
108
|
+
```sh
|
|
109
|
+
brew install --cask wolfram-engine
|
|
110
|
+
```
|
|
111
|
+
|
|
85
112
|
## Benchmarks
|
|
86
113
|
|
|
87
114
|
### Datasets
|
package/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "usearch"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.7"
|
|
4
4
|
authors = ["Ashot Vardanian <1983160+ashvardanian@users.noreply.github.com>"]
|
|
5
5
|
description = "The Mighty Tiny Vector Search Engine with Automatic Quantization and Hardware Acceleration"
|
|
6
6
|
edition = "2021"
|
package/README.md
CHANGED
|
@@ -29,9 +29,11 @@ Compact, yet Powerful<br/>
|
|
|
29
29
|
- [x] Don't copy vectors if not needed.
|
|
30
30
|
- [x] Bring your threads.
|
|
31
31
|
- [x] Multiple vectors per label.
|
|
32
|
-
- [x] Python bindings: `pip install usearch
|
|
33
|
-
- [x] JavaScript bindings: `npm install usearch
|
|
34
|
-
- [x] Rust bindings: `cargo add usearch
|
|
32
|
+
- [x] Python bindings: `pip install usearch`.
|
|
33
|
+
- [x] JavaScript bindings: `npm install usearch`.
|
|
34
|
+
- [x] Rust bindings: `cargo add usearch`.
|
|
35
|
+
- [x] Java bindings: `cloud.unum:usearch` on GitHub.
|
|
36
|
+
- [ ] GoLang bindings.
|
|
35
37
|
- [ ] Wolfram language bindings.
|
|
36
38
|
- [x] For Linux: GCC, Clang.
|
|
37
39
|
- [x] For MacOS: Apple Clang.
|
|
@@ -55,6 +57,10 @@ index.add(/* label: */ 42, /* vector: */ {&vec, 3});
|
|
|
55
57
|
index.search(
|
|
56
58
|
/* query: */ {&vec, 3}, 10 /* results */,
|
|
57
59
|
/* callback: */ [](std::size_t label, float distance) { });
|
|
60
|
+
|
|
61
|
+
index.save("index.usearch"); // Serializing to disk
|
|
62
|
+
index.load("index.usearch"); // Reconstructing from disk
|
|
63
|
+
index.view("index.usearch"); // Memory-mapping from disk
|
|
58
64
|
```
|
|
59
65
|
|
|
60
66
|
Alternatively fetch it with CMake:
|
|
@@ -64,6 +70,8 @@ FetchContent_Declare(usearch GIT_REPOSITORY https://github.com/unum-cloud/usearc
|
|
|
64
70
|
FetchContent_MakeAvailable(usearch)
|
|
65
71
|
```
|
|
66
72
|
|
|
73
|
+
|
|
74
|
+
|
|
67
75
|
## Features
|
|
68
76
|
|
|
69
77
|
### Bring your Threads
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.7
|
package/build.gradle
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import org.gradle.internal.jvm.Jvm
|
|
2
|
+
|
|
3
|
+
plugins {
|
|
4
|
+
id 'java-library'
|
|
5
|
+
id 'cpp'
|
|
6
|
+
id 'maven-publish'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
group "cloud.unum.usearch"
|
|
10
|
+
version = file("VERSION").text.trim()
|
|
11
|
+
|
|
12
|
+
sourceCompatibility = 1.8
|
|
13
|
+
targetCompatibility = 1.8
|
|
14
|
+
|
|
15
|
+
repositories {
|
|
16
|
+
mavenCentral()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
sourceSets {
|
|
20
|
+
main {
|
|
21
|
+
java {
|
|
22
|
+
srcDir "java/"
|
|
23
|
+
exclude "test/"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
test {
|
|
27
|
+
java {
|
|
28
|
+
srcDir "java/test"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
dependencies {
|
|
34
|
+
testImplementation('junit:junit:4.13.2')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
model {
|
|
38
|
+
|
|
39
|
+
components {
|
|
40
|
+
usearch(NativeLibrarySpec) {
|
|
41
|
+
sources {
|
|
42
|
+
cpp {
|
|
43
|
+
source {
|
|
44
|
+
srcDirs "java/cloud/unum/usearch"
|
|
45
|
+
include "**/*.cpp"
|
|
46
|
+
}
|
|
47
|
+
exportedHeaders {
|
|
48
|
+
srcDirs "include", "src", "fp16/include", "simsimd/include", "${Jvm.current().javaHome}/include"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
binaries.withType(StaticLibraryBinarySpec) {
|
|
53
|
+
buildable = false
|
|
54
|
+
}
|
|
55
|
+
binaries.withType(SharedLibraryBinarySpec) {
|
|
56
|
+
if (targetPlatform.operatingSystem.linux) {
|
|
57
|
+
cppCompiler.args '-I', "${Jvm.current().javaHome}/include/linux"
|
|
58
|
+
cppCompiler.args '-D_FILE_OFFSET_BITS=64'
|
|
59
|
+
} else if (targetPlatform.operatingSystem.macOsX) {
|
|
60
|
+
cppCompiler.args '-I', "${Jvm.current().javaHome}/include/darwin"
|
|
61
|
+
cppCompiler.args '-D_FILE_OFFSET_BITS=64'
|
|
62
|
+
}
|
|
63
|
+
cppCompiler.args '-std=c++11'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
test {
|
|
70
|
+
forkEvery 1
|
|
71
|
+
|
|
72
|
+
dependsOn jar
|
|
73
|
+
|
|
74
|
+
// Rearrange test classpath, add compiled JAR instead of main classes
|
|
75
|
+
classpath = project.sourceSets.test.output + configurations.testRuntimeClasspath + files(jar.archiveFile)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
jar {
|
|
79
|
+
dependsOn("usearchSharedLibrary")
|
|
80
|
+
into("usearch") {
|
|
81
|
+
from("$buildDir/libs/usearch/shared/libusearch.dylib")
|
|
82
|
+
from("$buildDir/libs/usearch/shared/libusearch.so")
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
publishing {
|
|
87
|
+
publications {
|
|
88
|
+
library(MavenPublication) {
|
|
89
|
+
from components.java
|
|
90
|
+
|
|
91
|
+
pom {
|
|
92
|
+
name = "USearch"
|
|
93
|
+
description = "The Mighty Tiny Vector Search Engine with Automatic Quantization and Hardware Acceleration"
|
|
94
|
+
url = "https://github.com/unum-cloud/usearch"
|
|
95
|
+
licenses {
|
|
96
|
+
license {
|
|
97
|
+
name = 'The Apache License, Version 2.0'
|
|
98
|
+
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Publishing to GitHub is much easier ;)
|
|
106
|
+
// repositories {
|
|
107
|
+
// maven {
|
|
108
|
+
// name = "OSSRH"
|
|
109
|
+
// url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
|
|
110
|
+
// credentials {
|
|
111
|
+
// username = System.getenv("MAVEN_USERNAME")
|
|
112
|
+
// password = System.getenv("MAVEN_PASSWORD")
|
|
113
|
+
// }
|
|
114
|
+
// }
|
|
115
|
+
// }
|
|
116
|
+
|
|
117
|
+
repositories {
|
|
118
|
+
maven {
|
|
119
|
+
name = "GitHubPackages"
|
|
120
|
+
url = "https://maven.pkg.github.com/unum-cloud/usearch"
|
|
121
|
+
credentials {
|
|
122
|
+
username = System.getenv("GITHUB_ACTOR")
|
|
123
|
+
password = System.getenv("GITHUB_TOKEN")
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -7,21 +7,25 @@
|
|
|
7
7
|
* @copyright Copyright (c) 2023
|
|
8
8
|
*/
|
|
9
9
|
#pragma once
|
|
10
|
-
#include <
|
|
11
|
-
#include <
|
|
12
|
-
#include <
|
|
13
|
-
#include <
|
|
14
|
-
#include <
|
|
15
|
-
#include <
|
|
16
|
-
#include <
|
|
17
|
-
#include <
|
|
18
|
-
#include <
|
|
19
|
-
|
|
10
|
+
#include <algorithm> // `std::sort_heap`
|
|
11
|
+
#include <atomic> // `std::atomic`
|
|
12
|
+
#include <bitset> // `std::bitset`
|
|
13
|
+
#include <climits> // `CHAR_BIT`
|
|
14
|
+
#include <cmath> // `std::sqrt`
|
|
15
|
+
#include <cstring> // `std::memset`
|
|
16
|
+
#include <mutex> // `std::unique_lock` - replacement candidate
|
|
17
|
+
#include <random> // `std::default_random_engine` - replacement candidate
|
|
18
|
+
#include <unistd.h> // `open`, `close`
|
|
19
|
+
#include <utility> // `std::exchange`
|
|
20
|
+
#include <vector> // `std::vector`
|
|
21
|
+
|
|
22
|
+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
|
23
|
+
#else
|
|
20
24
|
#include <fcntl.h> // `fallocate`
|
|
21
25
|
#include <stdlib.h> // `posix_memalign`
|
|
22
26
|
#include <sys/mman.h> // `mmap`
|
|
23
27
|
#include <sys/stat.h> // `fstat` for file size
|
|
24
|
-
#
|
|
28
|
+
#endif
|
|
25
29
|
|
|
26
30
|
#if defined(__GNUC__)
|
|
27
31
|
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
|
|
@@ -965,6 +969,9 @@ class index_gt {
|
|
|
965
969
|
* Compatibility: Linux, MacOS.
|
|
966
970
|
*/
|
|
967
971
|
void view(char const* file_path) noexcept(false) {
|
|
972
|
+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
|
973
|
+
throw std::logic_error("Memory-mapping is not yet available for Windows");
|
|
974
|
+
#else
|
|
968
975
|
state_t state;
|
|
969
976
|
int open_flags = O_RDONLY;
|
|
970
977
|
#if __linux__
|
|
@@ -1012,6 +1019,7 @@ class index_gt {
|
|
|
1012
1019
|
}
|
|
1013
1020
|
|
|
1014
1021
|
viewed_file_descriptor_ = descriptor;
|
|
1022
|
+
#endif
|
|
1015
1023
|
}
|
|
1016
1024
|
|
|
1017
1025
|
#pragma endregion
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
package cloud.unum.usearch;
|
|
2
|
+
|
|
3
|
+
import java.io.File;
|
|
4
|
+
import java.nio.file.Files;
|
|
5
|
+
import java.nio.file.StandardCopyOption;
|
|
6
|
+
import java.io.InputStream;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @brief Java bindings for Unum USearch.
|
|
10
|
+
*
|
|
11
|
+
* @see Tutorials:
|
|
12
|
+
* https://nachtimwald.com/2017/06/06/wrapping-a-c-library-in-java/
|
|
13
|
+
* https://www3.ntu.edu.sg/home/ehchua/programming/java/javanativeinterface.html
|
|
14
|
+
*/
|
|
15
|
+
public class Index {
|
|
16
|
+
|
|
17
|
+
private long c_ptr = 0;
|
|
18
|
+
|
|
19
|
+
public Index(String metric, String accuracy, //
|
|
20
|
+
long dimensions, long capacity, long connectivity, //
|
|
21
|
+
long expansion_add, long expansion_search) {
|
|
22
|
+
c_ptr = c_create(
|
|
23
|
+
metric,
|
|
24
|
+
accuracy,
|
|
25
|
+
dimensions,
|
|
26
|
+
capacity,
|
|
27
|
+
connectivity,
|
|
28
|
+
expansion_add,
|
|
29
|
+
expansion_search);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public long size() {
|
|
33
|
+
return c_size(c_ptr);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public long connectivity() {
|
|
37
|
+
return c_connectivity(c_ptr);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public long dimensions() {
|
|
41
|
+
return c_dimensions(c_ptr);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public long capacity() {
|
|
45
|
+
return c_capacity(c_ptr);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public void reserve(long capacity) {
|
|
49
|
+
c_reserve(c_ptr, capacity);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public void add(int label, float vector[]) {
|
|
53
|
+
c_add(c_ptr, label, vector);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public int[] search(float vector[], long count) {
|
|
57
|
+
return c_search(c_ptr, vector, count);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public void save(String path) {
|
|
61
|
+
c_save(c_ptr, path);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public void load(String path) {
|
|
65
|
+
c_load(c_ptr, path);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public void view(String path) {
|
|
69
|
+
c_view(c_ptr, path);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public static class Config {
|
|
73
|
+
private String _metric = "ip";
|
|
74
|
+
private String _accuracy = "f32";
|
|
75
|
+
private long _dimensions = 0;
|
|
76
|
+
private long _capacity = 0;
|
|
77
|
+
private long _connectivity = 0;
|
|
78
|
+
private long _expansion_add = 0;
|
|
79
|
+
private long _expansion_search = 0;
|
|
80
|
+
|
|
81
|
+
public Config() {
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public Index build() {
|
|
85
|
+
return new Index(
|
|
86
|
+
_metric,
|
|
87
|
+
_accuracy,
|
|
88
|
+
_dimensions,
|
|
89
|
+
_capacity,
|
|
90
|
+
_connectivity,
|
|
91
|
+
_expansion_add,
|
|
92
|
+
_expansion_search);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public Config metric(String _metric) {
|
|
96
|
+
this._metric = _metric;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public Config accuracy(String _accuracy) {
|
|
101
|
+
this._accuracy = _accuracy;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public Config dimensions(long _dimensions) {
|
|
106
|
+
this._dimensions = _dimensions;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public Config capacity(long _capacity) {
|
|
111
|
+
this._capacity = _capacity;
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public Config connectivity(long _connectivity) {
|
|
116
|
+
this._connectivity = _connectivity;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public Config expansion_add(long _expansion_add) {
|
|
121
|
+
this._expansion_add = _expansion_add;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public Config expansion_search(long _expansion_search) {
|
|
126
|
+
this._expansion_search = _expansion_search;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static {
|
|
132
|
+
System.loadLibrary("usearch");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public static void main(String[] args) {
|
|
136
|
+
Index index = new Index.Config().metric("cos").build();
|
|
137
|
+
index.size();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private static native long c_create(//
|
|
141
|
+
String metric, String accuracy, //
|
|
142
|
+
long dimensions, long capacity, long connectivity, //
|
|
143
|
+
long expansion_add, long expansion_search);
|
|
144
|
+
|
|
145
|
+
private static native void c_destroy(long ptr);
|
|
146
|
+
|
|
147
|
+
private static native long c_size(long ptr);
|
|
148
|
+
|
|
149
|
+
private static native long c_connectivity(long ptr);
|
|
150
|
+
|
|
151
|
+
private static native long c_dimensions(long ptr);
|
|
152
|
+
|
|
153
|
+
private static native long c_capacity(long ptr);
|
|
154
|
+
|
|
155
|
+
private static native void c_reserve(long ptr, long capacity);
|
|
156
|
+
|
|
157
|
+
private static native void c_add(long ptr, int label, float vector[]);
|
|
158
|
+
|
|
159
|
+
private static native int[] c_search(long ptr, float vector[], long count);
|
|
160
|
+
|
|
161
|
+
private static native void c_save(long ptr, String path);
|
|
162
|
+
|
|
163
|
+
private static native void c_load(long ptr, String path);
|
|
164
|
+
|
|
165
|
+
private static native void c_view(long ptr, String path);
|
|
166
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#include <jni.h>
|
|
2
|
+
|
|
3
|
+
#include "cloud_unum_usearch_Index.h"
|
|
4
|
+
|
|
5
|
+
#include <thread>
|
|
6
|
+
|
|
7
|
+
#include "advanced.hpp"
|
|
8
|
+
|
|
9
|
+
using namespace unum::usearch;
|
|
10
|
+
using namespace unum;
|
|
11
|
+
|
|
12
|
+
using label_t = jint;
|
|
13
|
+
using distance_t = punned_distance_t;
|
|
14
|
+
using native_index_t = auto_index_gt<label_t>;
|
|
15
|
+
using span_t = span_gt<float>;
|
|
16
|
+
|
|
17
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1create( //
|
|
18
|
+
JNIEnv* env, jclass, //
|
|
19
|
+
jstring metric, jstring accuracy, //
|
|
20
|
+
jlong dimensions, jlong capacity, jlong connectivity, //
|
|
21
|
+
jlong expansion_add, jlong expansion_search) {
|
|
22
|
+
|
|
23
|
+
jlong result{};
|
|
24
|
+
char const* metric_cstr{};
|
|
25
|
+
char const* accuracy_cstr{};
|
|
26
|
+
try {
|
|
27
|
+
|
|
28
|
+
config_t config;
|
|
29
|
+
config.expansion_add = static_cast<std::size_t>(expansion_add);
|
|
30
|
+
config.expansion_search = static_cast<std::size_t>(expansion_search);
|
|
31
|
+
config.connectivity = static_cast<std::size_t>(connectivity);
|
|
32
|
+
config.max_elements = static_cast<std::size_t>(capacity);
|
|
33
|
+
config.max_threads_add = std::thread::hardware_concurrency();
|
|
34
|
+
config.max_threads_search = std::thread::hardware_concurrency();
|
|
35
|
+
|
|
36
|
+
metric_cstr = (*env).GetStringUTFChars(metric, 0);
|
|
37
|
+
std::size_t metric_length = (*env).GetStringUTFLength(metric);
|
|
38
|
+
accuracy_cstr = (*env).GetStringUTFChars(accuracy, 0);
|
|
39
|
+
std::size_t accuracy_length = (*env).GetStringUTFLength(accuracy);
|
|
40
|
+
|
|
41
|
+
accuracy_t accuracy = accuracy_from_name(accuracy_cstr, accuracy_length);
|
|
42
|
+
native_index_t index = index_from_name<native_index_t>( //
|
|
43
|
+
metric_cstr, metric_length, static_cast<std::size_t>(dimensions), accuracy, config);
|
|
44
|
+
|
|
45
|
+
native_index_t* result_ptr = new native_index_t(std::move(index));
|
|
46
|
+
std::memcpy(&result, &result_ptr, sizeof(jlong));
|
|
47
|
+
} catch (...) {
|
|
48
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
49
|
+
if (jc)
|
|
50
|
+
(*env).ThrowNew(jc, "Failed to initialize the vector index!");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
(*env).ReleaseStringUTFChars(metric, metric_cstr);
|
|
54
|
+
(*env).ReleaseStringUTFChars(accuracy, accuracy_cstr);
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1save(JNIEnv* env, jclass, jlong c_ptr, jstring path) {
|
|
59
|
+
char const* path_cstr{};
|
|
60
|
+
try {
|
|
61
|
+
path_cstr = (*env).GetStringUTFChars(path, 0);
|
|
62
|
+
reinterpret_cast<native_index_t*>(c_ptr)->save(path_cstr);
|
|
63
|
+
} catch (...) {
|
|
64
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
65
|
+
if (jc)
|
|
66
|
+
(*env).ThrowNew(jc, "Failed to dump vector index to path!");
|
|
67
|
+
}
|
|
68
|
+
(*env).ReleaseStringUTFChars(path, path_cstr);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1load(JNIEnv* env, jclass, jlong c_ptr, jstring path) {
|
|
72
|
+
char const* path_cstr{};
|
|
73
|
+
try {
|
|
74
|
+
path_cstr = (*env).GetStringUTFChars(path, 0);
|
|
75
|
+
reinterpret_cast<native_index_t*>(c_ptr)->load(path_cstr);
|
|
76
|
+
} catch (...) {
|
|
77
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
78
|
+
if (jc)
|
|
79
|
+
(*env).ThrowNew(jc, "Failed to load vector index from path!");
|
|
80
|
+
}
|
|
81
|
+
(*env).ReleaseStringUTFChars(path, path_cstr);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1view(JNIEnv* env, jclass, jlong c_ptr, jstring path) {
|
|
85
|
+
char const* path_cstr{};
|
|
86
|
+
try {
|
|
87
|
+
path_cstr = (*env).GetStringUTFChars(path, 0);
|
|
88
|
+
reinterpret_cast<native_index_t*>(c_ptr)->view(path_cstr);
|
|
89
|
+
} catch (...) {
|
|
90
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
91
|
+
if (jc)
|
|
92
|
+
(*env).ThrowNew(jc, "Failed to view vector index from path!");
|
|
93
|
+
}
|
|
94
|
+
(*env).ReleaseStringUTFChars(path, path_cstr);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1destroy(JNIEnv*, jclass, jlong c_ptr) {
|
|
98
|
+
delete reinterpret_cast<native_index_t*>(c_ptr);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1size(JNIEnv*, jclass, jlong c_ptr) {
|
|
102
|
+
return reinterpret_cast<native_index_t*>(c_ptr)->size();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1connectivity(JNIEnv*, jclass, jlong c_ptr) {
|
|
106
|
+
return reinterpret_cast<native_index_t*>(c_ptr)->connectivity();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1dimensions(JNIEnv*, jclass, jlong c_ptr) {
|
|
110
|
+
return reinterpret_cast<native_index_t*>(c_ptr)->dimensions();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1capacity(JNIEnv*, jclass, jlong c_ptr) {
|
|
114
|
+
return reinterpret_cast<native_index_t*>(c_ptr)->capacity();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1reserve(JNIEnv* env, jclass, jlong c_ptr, jlong capacity) {
|
|
118
|
+
try {
|
|
119
|
+
return reinterpret_cast<native_index_t*>(c_ptr)->reserve(static_cast<std::size_t>(capacity));
|
|
120
|
+
} catch (...) {
|
|
121
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
122
|
+
if (jc)
|
|
123
|
+
(*env).ThrowNew(jc, "Failed to grow vector index!");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1add( //
|
|
128
|
+
JNIEnv* env, jclass, jlong c_ptr, jint label, jfloatArray vector) {
|
|
129
|
+
|
|
130
|
+
jfloat* vector_data{};
|
|
131
|
+
try {
|
|
132
|
+
vector_data = (*env).GetFloatArrayElements(vector, 0);
|
|
133
|
+
jsize vector_dims = (*env).GetArrayLength(vector);
|
|
134
|
+
span_t vector_span = span_t{vector_data, static_cast<std::size_t>(vector_dims)};
|
|
135
|
+
reinterpret_cast<native_index_t*>(c_ptr)->add(label, vector_span);
|
|
136
|
+
} catch (...) {
|
|
137
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
138
|
+
if (jc)
|
|
139
|
+
(*env).ThrowNew(jc, "Failed to insert a new point in vector index!");
|
|
140
|
+
}
|
|
141
|
+
(*env).ReleaseFloatArrayElements(vector, vector_data, 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search( //
|
|
145
|
+
JNIEnv* env, jclass, jlong c_ptr, jfloatArray vector, jlong wanted) {
|
|
146
|
+
|
|
147
|
+
jintArray matches;
|
|
148
|
+
matches = (*env).NewIntArray(wanted);
|
|
149
|
+
if (matches == NULL)
|
|
150
|
+
return NULL;
|
|
151
|
+
|
|
152
|
+
jint* matches_data = (jint*)std::malloc(sizeof(jint) * wanted);
|
|
153
|
+
if (matches_data == NULL)
|
|
154
|
+
return NULL;
|
|
155
|
+
|
|
156
|
+
jfloat* vector_data{};
|
|
157
|
+
std::size_t found{};
|
|
158
|
+
try {
|
|
159
|
+
vector_data = (*env).GetFloatArrayElements(vector, 0);
|
|
160
|
+
jsize vector_dims = (*env).GetArrayLength(vector);
|
|
161
|
+
span_t vector_span = span_t{vector_data, static_cast<std::size_t>(vector_dims)};
|
|
162
|
+
found = reinterpret_cast<native_index_t*>(c_ptr)->search( //
|
|
163
|
+
vector_span, static_cast<std::size_t>(wanted), matches_data, NULL);
|
|
164
|
+
(*env).SetIntArrayRegion(matches, 0, found, matches_data);
|
|
165
|
+
} catch (...) {
|
|
166
|
+
jclass jc = (*env).FindClass("java/lang/Error");
|
|
167
|
+
if (jc)
|
|
168
|
+
(*env).ThrowNew(jc, "Failed to find in vector index!");
|
|
169
|
+
}
|
|
170
|
+
(*env).ReleaseFloatArrayElements(vector, vector_data, 0);
|
|
171
|
+
std::free(matches_data);
|
|
172
|
+
return matches;
|
|
173
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/* DO NOT EDIT THIS FILE - it is machine generated */
|
|
2
|
+
#include <jni.h>
|
|
3
|
+
/* Header for class cloud_unum_usearch_Index */
|
|
4
|
+
|
|
5
|
+
#ifndef _Included_cloud_unum_usearch_Index
|
|
6
|
+
#define _Included_cloud_unum_usearch_Index
|
|
7
|
+
#ifdef __cplusplus
|
|
8
|
+
extern "C" {
|
|
9
|
+
#endif
|
|
10
|
+
/*
|
|
11
|
+
* Class: cloud_unum_usearch_Index
|
|
12
|
+
* Method: c_create
|
|
13
|
+
* Signature: (Ljava/lang/String;Ljava/lang/String;JJJJJ)J
|
|
14
|
+
*/
|
|
15
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1create
|
|
16
|
+
(JNIEnv *, jclass, jstring, jstring, jlong, jlong, jlong, jlong, jlong);
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* Class: cloud_unum_usearch_Index
|
|
20
|
+
* Method: c_destroy
|
|
21
|
+
* Signature: (J)V
|
|
22
|
+
*/
|
|
23
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1destroy
|
|
24
|
+
(JNIEnv *, jclass, jlong);
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
* Class: cloud_unum_usearch_Index
|
|
28
|
+
* Method: c_size
|
|
29
|
+
* Signature: (J)J
|
|
30
|
+
*/
|
|
31
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1size
|
|
32
|
+
(JNIEnv *, jclass, jlong);
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* Class: cloud_unum_usearch_Index
|
|
36
|
+
* Method: c_connectivity
|
|
37
|
+
* Signature: (J)J
|
|
38
|
+
*/
|
|
39
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1connectivity
|
|
40
|
+
(JNIEnv *, jclass, jlong);
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* Class: cloud_unum_usearch_Index
|
|
44
|
+
* Method: c_dimensions
|
|
45
|
+
* Signature: (J)J
|
|
46
|
+
*/
|
|
47
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1dimensions
|
|
48
|
+
(JNIEnv *, jclass, jlong);
|
|
49
|
+
|
|
50
|
+
/*
|
|
51
|
+
* Class: cloud_unum_usearch_Index
|
|
52
|
+
* Method: c_capacity
|
|
53
|
+
* Signature: (J)J
|
|
54
|
+
*/
|
|
55
|
+
JNIEXPORT jlong JNICALL Java_cloud_unum_usearch_Index_c_1capacity
|
|
56
|
+
(JNIEnv *, jclass, jlong);
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
* Class: cloud_unum_usearch_Index
|
|
60
|
+
* Method: c_reserve
|
|
61
|
+
* Signature: (JJ)V
|
|
62
|
+
*/
|
|
63
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1reserve
|
|
64
|
+
(JNIEnv *, jclass, jlong, jlong);
|
|
65
|
+
|
|
66
|
+
/*
|
|
67
|
+
* Class: cloud_unum_usearch_Index
|
|
68
|
+
* Method: c_add
|
|
69
|
+
* Signature: (JI[F)V
|
|
70
|
+
*/
|
|
71
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1add
|
|
72
|
+
(JNIEnv *, jclass, jlong, jint, jfloatArray);
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Class: cloud_unum_usearch_Index
|
|
76
|
+
* Method: c_search
|
|
77
|
+
* Signature: (J[FJ)[I
|
|
78
|
+
*/
|
|
79
|
+
JNIEXPORT jintArray JNICALL Java_cloud_unum_usearch_Index_c_1search
|
|
80
|
+
(JNIEnv *, jclass, jlong, jfloatArray, jlong);
|
|
81
|
+
|
|
82
|
+
/*
|
|
83
|
+
* Class: cloud_unum_usearch_Index
|
|
84
|
+
* Method: c_save
|
|
85
|
+
* Signature: (JLjava/lang/String;)V
|
|
86
|
+
*/
|
|
87
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1save
|
|
88
|
+
(JNIEnv *, jclass, jlong, jstring);
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* Class: cloud_unum_usearch_Index
|
|
92
|
+
* Method: c_load
|
|
93
|
+
* Signature: (JLjava/lang/String;)V
|
|
94
|
+
*/
|
|
95
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1load
|
|
96
|
+
(JNIEnv *, jclass, jlong, jstring);
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* Class: cloud_unum_usearch_Index
|
|
100
|
+
* Method: c_view
|
|
101
|
+
* Signature: (JLjava/lang/String;)V
|
|
102
|
+
*/
|
|
103
|
+
JNIEXPORT void JNICALL Java_cloud_unum_usearch_Index_c_1view
|
|
104
|
+
(JNIEnv *, jclass, jlong, jstring);
|
|
105
|
+
|
|
106
|
+
#ifdef __cplusplus
|
|
107
|
+
}
|
|
108
|
+
#endif
|
|
109
|
+
#endif
|
|
Binary file
|
package/java/gradlew
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Copyright © 2015-2021 the original authors.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# You may obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
# See the License for the specific language governing permissions and
|
|
16
|
+
# limitations under the License.
|
|
17
|
+
#
|
|
18
|
+
|
|
19
|
+
##############################################################################
|
|
20
|
+
#
|
|
21
|
+
# Gradle start up script for POSIX generated by Gradle.
|
|
22
|
+
#
|
|
23
|
+
# Important for running:
|
|
24
|
+
#
|
|
25
|
+
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
|
26
|
+
# noncompliant, but you have some other compliant shell such as ksh or
|
|
27
|
+
# bash, then to run this script, type that shell name before the whole
|
|
28
|
+
# command line, like:
|
|
29
|
+
#
|
|
30
|
+
# ksh Gradle
|
|
31
|
+
#
|
|
32
|
+
# Busybox and similar reduced shells will NOT work, because this script
|
|
33
|
+
# requires all of these POSIX shell features:
|
|
34
|
+
# * functions;
|
|
35
|
+
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
|
36
|
+
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
|
37
|
+
# * compound commands having a testable exit status, especially «case»;
|
|
38
|
+
# * various built-in commands including «command», «set», and «ulimit».
|
|
39
|
+
#
|
|
40
|
+
# Important for patching:
|
|
41
|
+
#
|
|
42
|
+
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
|
43
|
+
# by Bash, Ksh, etc; in particular arrays are avoided.
|
|
44
|
+
#
|
|
45
|
+
# The "traditional" practice of packing multiple parameters into a
|
|
46
|
+
# space-separated string is a well documented source of bugs and security
|
|
47
|
+
# problems, so this is (mostly) avoided, by progressively accumulating
|
|
48
|
+
# options in "$@", and eventually passing that to Java.
|
|
49
|
+
#
|
|
50
|
+
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
|
51
|
+
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
|
52
|
+
# see the in-line comments for details.
|
|
53
|
+
#
|
|
54
|
+
# There are tweaks for specific operating systems such as AIX, CygWin,
|
|
55
|
+
# Darwin, MinGW, and NonStop.
|
|
56
|
+
#
|
|
57
|
+
# (3) This script is generated from the Groovy template
|
|
58
|
+
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
|
59
|
+
# within the Gradle project.
|
|
60
|
+
#
|
|
61
|
+
# You can find Gradle at https://github.com/gradle/gradle/.
|
|
62
|
+
#
|
|
63
|
+
##############################################################################
|
|
64
|
+
|
|
65
|
+
# Attempt to set APP_HOME
|
|
66
|
+
|
|
67
|
+
# Resolve links: $0 may be a link
|
|
68
|
+
app_path=$0
|
|
69
|
+
|
|
70
|
+
# Need this for daisy-chained symlinks.
|
|
71
|
+
while
|
|
72
|
+
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
|
73
|
+
[ -h "$app_path" ]
|
|
74
|
+
do
|
|
75
|
+
ls=$( ls -ld "$app_path" )
|
|
76
|
+
link=${ls#*' -> '}
|
|
77
|
+
case $link in #(
|
|
78
|
+
/*) app_path=$link ;; #(
|
|
79
|
+
*) app_path=$APP_HOME$link ;;
|
|
80
|
+
esac
|
|
81
|
+
done
|
|
82
|
+
|
|
83
|
+
# This is normally unused
|
|
84
|
+
# shellcheck disable=SC2034
|
|
85
|
+
APP_BASE_NAME=${0##*/}
|
|
86
|
+
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
|
87
|
+
|
|
88
|
+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
89
|
+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
90
|
+
|
|
91
|
+
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
|
92
|
+
MAX_FD=maximum
|
|
93
|
+
|
|
94
|
+
warn () {
|
|
95
|
+
echo "$*"
|
|
96
|
+
} >&2
|
|
97
|
+
|
|
98
|
+
die () {
|
|
99
|
+
echo
|
|
100
|
+
echo "$*"
|
|
101
|
+
echo
|
|
102
|
+
exit 1
|
|
103
|
+
} >&2
|
|
104
|
+
|
|
105
|
+
# OS specific support (must be 'true' or 'false').
|
|
106
|
+
cygwin=false
|
|
107
|
+
msys=false
|
|
108
|
+
darwin=false
|
|
109
|
+
nonstop=false
|
|
110
|
+
case "$( uname )" in #(
|
|
111
|
+
CYGWIN* ) cygwin=true ;; #(
|
|
112
|
+
Darwin* ) darwin=true ;; #(
|
|
113
|
+
MSYS* | MINGW* ) msys=true ;; #(
|
|
114
|
+
NONSTOP* ) nonstop=true ;;
|
|
115
|
+
esac
|
|
116
|
+
|
|
117
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# Determine the Java command to use to start the JVM.
|
|
121
|
+
if [ -n "$JAVA_HOME" ] ; then
|
|
122
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
|
123
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
|
124
|
+
JAVACMD=$JAVA_HOME/jre/sh/java
|
|
125
|
+
else
|
|
126
|
+
JAVACMD=$JAVA_HOME/bin/java
|
|
127
|
+
fi
|
|
128
|
+
if [ ! -x "$JAVACMD" ] ; then
|
|
129
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
|
130
|
+
|
|
131
|
+
Please set the JAVA_HOME variable in your environment to match the
|
|
132
|
+
location of your Java installation."
|
|
133
|
+
fi
|
|
134
|
+
else
|
|
135
|
+
JAVACMD=java
|
|
136
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
137
|
+
|
|
138
|
+
Please set the JAVA_HOME variable in your environment to match the
|
|
139
|
+
location of your Java installation."
|
|
140
|
+
fi
|
|
141
|
+
|
|
142
|
+
# Increase the maximum file descriptors if we can.
|
|
143
|
+
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
|
144
|
+
case $MAX_FD in #(
|
|
145
|
+
max*)
|
|
146
|
+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
|
147
|
+
# shellcheck disable=SC3045
|
|
148
|
+
MAX_FD=$( ulimit -H -n ) ||
|
|
149
|
+
warn "Could not query maximum file descriptor limit"
|
|
150
|
+
esac
|
|
151
|
+
case $MAX_FD in #(
|
|
152
|
+
'' | soft) :;; #(
|
|
153
|
+
*)
|
|
154
|
+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
|
155
|
+
# shellcheck disable=SC3045
|
|
156
|
+
ulimit -n "$MAX_FD" ||
|
|
157
|
+
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
|
158
|
+
esac
|
|
159
|
+
fi
|
|
160
|
+
|
|
161
|
+
# Collect all arguments for the java command, stacking in reverse order:
|
|
162
|
+
# * args from the command line
|
|
163
|
+
# * the main class name
|
|
164
|
+
# * -classpath
|
|
165
|
+
# * -D...appname settings
|
|
166
|
+
# * --module-path (only if needed)
|
|
167
|
+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
|
168
|
+
|
|
169
|
+
# For Cygwin or MSYS, switch paths to Windows format before running java
|
|
170
|
+
if "$cygwin" || "$msys" ; then
|
|
171
|
+
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
|
172
|
+
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
|
173
|
+
|
|
174
|
+
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
|
175
|
+
|
|
176
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
|
177
|
+
for arg do
|
|
178
|
+
if
|
|
179
|
+
case $arg in #(
|
|
180
|
+
-*) false ;; # don't mess with options #(
|
|
181
|
+
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
|
182
|
+
[ -e "$t" ] ;; #(
|
|
183
|
+
*) false ;;
|
|
184
|
+
esac
|
|
185
|
+
then
|
|
186
|
+
arg=$( cygpath --path --ignore --mixed "$arg" )
|
|
187
|
+
fi
|
|
188
|
+
# Roll the args list around exactly as many times as the number of
|
|
189
|
+
# args, so each arg winds up back in the position where it started, but
|
|
190
|
+
# possibly modified.
|
|
191
|
+
#
|
|
192
|
+
# NB: a `for` loop captures its iteration list before it begins, so
|
|
193
|
+
# changing the positional parameters here affects neither the number of
|
|
194
|
+
# iterations, nor the values presented in `arg`.
|
|
195
|
+
shift # remove old arg
|
|
196
|
+
set -- "$@" "$arg" # push replacement arg
|
|
197
|
+
done
|
|
198
|
+
fi
|
|
199
|
+
|
|
200
|
+
# Collect all arguments for the java command;
|
|
201
|
+
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
|
202
|
+
# shell script including quotes and variable substitutions, so put them in
|
|
203
|
+
# double quotes to make sure that they get re-expanded; and
|
|
204
|
+
# * put everything else in single quotes, so that it's not re-expanded.
|
|
205
|
+
|
|
206
|
+
set -- \
|
|
207
|
+
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
|
208
|
+
-classpath "$CLASSPATH" \
|
|
209
|
+
org.gradle.wrapper.GradleWrapperMain \
|
|
210
|
+
"$@"
|
|
211
|
+
|
|
212
|
+
# Stop when "xargs" is not available.
|
|
213
|
+
if ! command -v xargs >/dev/null 2>&1
|
|
214
|
+
then
|
|
215
|
+
die "xargs is not available"
|
|
216
|
+
fi
|
|
217
|
+
|
|
218
|
+
# Use "xargs" to parse quoted args.
|
|
219
|
+
#
|
|
220
|
+
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
|
221
|
+
#
|
|
222
|
+
# In Bash we could simply go:
|
|
223
|
+
#
|
|
224
|
+
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
|
225
|
+
# set -- "${ARGS[@]}" "$@"
|
|
226
|
+
#
|
|
227
|
+
# but POSIX shell has neither arrays nor command substitution, so instead we
|
|
228
|
+
# post-process each arg (as a line of input to sed) to backslash-escape any
|
|
229
|
+
# character that might be a shell metacharacter, then use eval to reverse
|
|
230
|
+
# that process (while maintaining the separation between arguments), and wrap
|
|
231
|
+
# the whole thing up as a single "set" statement.
|
|
232
|
+
#
|
|
233
|
+
# This will of course break if any of these variables contains a newline or
|
|
234
|
+
# an unmatched quote.
|
|
235
|
+
#
|
|
236
|
+
|
|
237
|
+
eval "set -- $(
|
|
238
|
+
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
|
239
|
+
xargs -n1 |
|
|
240
|
+
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
|
241
|
+
tr '\n' ' '
|
|
242
|
+
)" '"$@"'
|
|
243
|
+
|
|
244
|
+
exec "$JAVACMD" "$@"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import java.io.File;
|
|
2
|
+
import java.util.Arrays;
|
|
3
|
+
|
|
4
|
+
import org.junit.Test;
|
|
5
|
+
|
|
6
|
+
import cloud.unum.usearch.Index;
|
|
7
|
+
|
|
8
|
+
public class IndexTest {
|
|
9
|
+
public static void deleteDirectoryFiles(String path) {
|
|
10
|
+
File directory = new File(path);
|
|
11
|
+
if (!directory.isDirectory())
|
|
12
|
+
return;
|
|
13
|
+
|
|
14
|
+
for (File f : directory.listFiles())
|
|
15
|
+
f.delete();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Test
|
|
19
|
+
public void test() {
|
|
20
|
+
String path = "./tmp/";
|
|
21
|
+
deleteDirectoryFiles(path);
|
|
22
|
+
new Index("cos", "f32", 1, 10, 1, 1, 1);
|
|
23
|
+
System.out.println("Java Tests Passed!");
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json
CHANGED
package/python/lib.cpp
CHANGED
|
@@ -47,30 +47,8 @@ static native_index_t make_index( //
|
|
|
47
47
|
config.max_threads_add = std::thread::hardware_concurrency();
|
|
48
48
|
config.max_threads_search = std::thread::hardware_concurrency();
|
|
49
49
|
|
|
50
|
-
accuracy_t accuracy;
|
|
51
|
-
|
|
52
|
-
accuracy = accuracy_t::f32_k;
|
|
53
|
-
else if (scalar_type == "f64")
|
|
54
|
-
accuracy = accuracy_t::f64_k;
|
|
55
|
-
else if (scalar_type == "f16")
|
|
56
|
-
accuracy = accuracy_t::f16_k;
|
|
57
|
-
else if (scalar_type == "i8q100")
|
|
58
|
-
accuracy = accuracy_t::i8q100_k;
|
|
59
|
-
else
|
|
60
|
-
throw std::runtime_error("Unknown type, choose: f32, f16, f64, i8q100");
|
|
61
|
-
|
|
62
|
-
if (metric == "l2_sq" || metric == "euclidean_sq")
|
|
63
|
-
return native_index_t::l2(dimensions, accuracy, config);
|
|
64
|
-
else if (metric == "ip" || metric == "inner" || metric == "dot")
|
|
65
|
-
return native_index_t::ip(dimensions, accuracy, config);
|
|
66
|
-
else if (metric == "cos" || metric == "angular")
|
|
67
|
-
return native_index_t::cos(dimensions, accuracy, config);
|
|
68
|
-
else if (metric == "haversine")
|
|
69
|
-
return native_index_t::haversine(accuracy, config);
|
|
70
|
-
else
|
|
71
|
-
throw std::runtime_error("Unknown distance, choose: l2_sq, ip, cos, hamming, jaccard");
|
|
72
|
-
|
|
73
|
-
return {};
|
|
50
|
+
accuracy_t accuracy = accuracy_from_name(scalar_type.c_str(), scalar_type.size());
|
|
51
|
+
return index_from_name<native_index_t>(metric.c_str(), metric.size(), dimensions, accuracy, config);
|
|
74
52
|
}
|
|
75
53
|
|
|
76
54
|
static void add_to_index(native_index_t& index, py::buffer labels, py::buffer vectors, bool copy) {
|
package/rust/lib.cpp
CHANGED
|
@@ -40,7 +40,7 @@ void Index::save(rust::Str path) const { index_->save(std::string(path).c_str())
|
|
|
40
40
|
void Index::load(rust::Str path) const { index_->load(std::string(path).c_str()); }
|
|
41
41
|
void Index::view(rust::Str path) const { index_->view(std::string(path).c_str()); }
|
|
42
42
|
|
|
43
|
-
accuracy_t accuracy(rust::Str quant) { return
|
|
43
|
+
accuracy_t accuracy(rust::Str quant) { return accuracy_from_name(quant.data(), quant.size()); }
|
|
44
44
|
|
|
45
45
|
std::unique_ptr<Index> wrap(native_index_t&& index) {
|
|
46
46
|
std::shared_ptr<native_index_t> native = std::make_shared<native_index_t>(std::move(index));
|
package/src/advanced.hpp
CHANGED
|
@@ -172,26 +172,43 @@ enum class accuracy_t {
|
|
|
172
172
|
i8q100_k,
|
|
173
173
|
};
|
|
174
174
|
|
|
175
|
-
inline bool str_equals(char const* begin, size_t len, char const* other_begin) noexcept {
|
|
176
|
-
size_t other_len = strlen(other_begin);
|
|
175
|
+
inline bool str_equals(char const* begin, std::size_t len, char const* other_begin) noexcept {
|
|
176
|
+
std::size_t other_len = strlen(other_begin);
|
|
177
177
|
return len == other_len && strncmp(begin, other_begin, len) == 0;
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
-
inline accuracy_t
|
|
180
|
+
inline accuracy_t accuracy_from_name(char const* name, std::size_t len) {
|
|
181
181
|
accuracy_t accuracy;
|
|
182
|
-
if (str_equals(
|
|
182
|
+
if (str_equals(name, len, "f32"))
|
|
183
183
|
accuracy = accuracy_t::f32_k;
|
|
184
|
-
else if (str_equals(
|
|
184
|
+
else if (str_equals(name, len, "f64"))
|
|
185
185
|
accuracy = accuracy_t::f64_k;
|
|
186
|
-
else if (str_equals(
|
|
186
|
+
else if (str_equals(name, len, "f16"))
|
|
187
187
|
accuracy = accuracy_t::f16_k;
|
|
188
|
-
else if (str_equals(
|
|
188
|
+
else if (str_equals(name, len, "i8q100"))
|
|
189
189
|
accuracy = accuracy_t::i8q100_k;
|
|
190
190
|
else
|
|
191
191
|
throw std::runtime_error("Unknown type, choose: f32, f16, f64, i8q100");
|
|
192
192
|
return accuracy;
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
template <typename index_at>
|
|
196
|
+
inline index_at index_from_name( //
|
|
197
|
+
char const* name, std::size_t len, std::size_t dimensions, accuracy_t accuracy, config_t const& config) {
|
|
198
|
+
|
|
199
|
+
if (str_equals(name, len, "l2_sq") || str_equals(name, len, "euclidean_sq"))
|
|
200
|
+
return index_at::l2(dimensions, accuracy, config);
|
|
201
|
+
else if (str_equals(name, len, "ip") || str_equals(name, len, "inner") || str_equals(name, len, "dot"))
|
|
202
|
+
return index_at::ip(dimensions, accuracy, config);
|
|
203
|
+
else if (str_equals(name, len, "cos") || str_equals(name, len, "angular"))
|
|
204
|
+
return index_at::cos(dimensions, accuracy, config);
|
|
205
|
+
else if (str_equals(name, len, "haversine"))
|
|
206
|
+
return index_at::haversine(accuracy, config);
|
|
207
|
+
else
|
|
208
|
+
throw std::runtime_error("Unknown distance, choose: l2_sq, ip, cos, hamming, jaccard");
|
|
209
|
+
return {};
|
|
210
|
+
}
|
|
211
|
+
|
|
195
212
|
enum class isa_t {
|
|
196
213
|
auto_k,
|
|
197
214
|
neon_k,
|
package/wolfram/lib.cpp
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#include "WolframLibrary.h"
|
|
2
|
+
|
|
3
|
+
EXTERN_C DLLEXPORT mint WolframLibrary_getVersion() { return WolframLibraryVersion; }
|
|
4
|
+
EXTERN_C DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) { return 0; }
|
|
5
|
+
EXTERN_C DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) { return; }
|
|
6
|
+
|
|
7
|
+
EXTERN_C DLLEXPORT int IndexInit(WolframLibraryData libData, mint Argc, MArgument* Args, MArgument Res) {
|
|
8
|
+
return LIBRARY_NO_ERROR;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
EXTERN_C DLLEXPORT int IndexReserve(WolframLibraryData libData, mint Argc, MArgument* Args, MArgument Res) {
|
|
12
|
+
return LIBRARY_NO_ERROR;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
EXTERN_C DLLEXPORT int IndexAdd(WolframLibraryData libData, mint Argc, MArgument* Args, MArgument Res) {
|
|
16
|
+
return LIBRARY_NO_ERROR;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
EXTERN_C DLLEXPORT int IndexSearch(WolframLibraryData libData, mint Argc, MArgument* Args, MArgument Res) {
|
|
20
|
+
return LIBRARY_NO_ERROR;
|
|
21
|
+
}
|