This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex.cpp
234 lines (211 loc) 路 8.95 KB
/
com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex.h"
#include "init.h"
#include "index.h"
#include "params.h"
#include "knnquery.h"
#include "knnqueue.h"
#include "methodfactory.h"
#include "spacefactory.h"
#include "space.h"
using std::vector;
using similarity::initLibrary;
using similarity::AnyParams;
using similarity::Index;
using similarity::MethodFactoryRegistry;
using similarity::SpaceFactoryRegistry;
using similarity::AnyParams;
using similarity::Space;
using similarity::ObjectVector;
using similarity::Object;
using similarity::KNNQuery;
using similarity::KNNQueue;
extern "C"
struct IndexWrapper {
IndexWrapper(string spaceType) {
space.reset(SpaceFactoryRegistry<float>::Instance().CreateSpace(spaceType, AnyParams()));
index.reset(MethodFactoryRegistry<float>::Instance().CreateMethod(false, "hnsw", spaceType, *space, data));
}
std::unique_ptr<Space<float>> space;
std::unique_ptr<Index<float>> index;
// Index gets constructed with a reference to data (see above) but is otherwise unused
ObjectVector data;
};
struct JavaException {
JavaException(JNIEnv* env, const char* type = "", const char* message = "")
{
jclass newExcCls = env->FindClass(type);
if (newExcCls != NULL)
env->ThrowNew(newExcCls, message);
}
};
inline void has_exception_in_stack(JNIEnv* env)
{
if (env->ExceptionCheck() == JNI_TRUE)
throw std::runtime_error("Exception Occured");
}
void catch_cpp_exception_and_throw_java(JNIEnv* env)
{
try {
throw;
}
catch (const std::bad_alloc& rhs) {
JavaException(env, "java/io/IOException", rhs.what());
}
catch (const std::runtime_error& re) {
JavaException(env, "java/lang/Exception", re.what());
}
catch (const std::exception& e) {
JavaException(env, "java/lang/Exception", e.what());
}
catch (...) {
JavaException(env, "java/lang/Exception", "Unknown exception occured");
}
}
JNIEXPORT void JNICALL Java_com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex_saveIndex(JNIEnv* env, jclass cls, jintArray ids, jobjectArray vectors, jstring indexPath, jobjectArray algoParams, jstring spaceType)
{
Space<float>* space = NULL;
ObjectVector dataset;
Index<float>* index = NULL;
int* object_ids = NULL;
try {
const char *spaceTypeCStr = env->GetStringUTFChars(spaceType, 0);
string spaceTypeString(spaceTypeCStr);
env->ReleaseStringUTFChars(spaceType, spaceTypeCStr);
has_exception_in_stack(env);
space = SpaceFactoryRegistry<float>::Instance().CreateSpace(spaceTypeString, AnyParams());
object_ids = env->GetIntArrayElements(ids, 0);
for (int i = 0; i < env->GetArrayLength(vectors); i++) {
jfloatArray vectorArray = (jfloatArray)env->GetObjectArrayElement(vectors, i);
float* vector = env->GetFloatArrayElements(vectorArray, 0);
dataset.push_back(new Object(object_ids[i], -1, env->GetArrayLength(vectorArray)*sizeof(float), vector));
env->ReleaseFloatArrayElements(vectorArray, vector, JNI_ABORT);
}
// free up memory
env->ReleaseIntArrayElements(ids, object_ids, JNI_ABORT);
index = MethodFactoryRegistry<float>::Instance().CreateMethod(false, "hnsw", spaceTypeString, *space, dataset);
int paramsCount = env->GetArrayLength(algoParams);
vector<string> paramsList;
for (int i=0; i<paramsCount; i++) {
jstring param = (jstring) (env->GetObjectArrayElement(algoParams, i));
const char *rawString = env->GetStringUTFChars(param, 0);
paramsList.push_back(rawString);
env->ReleaseStringUTFChars(param, rawString);
}
index->CreateIndex(AnyParams(paramsList));
has_exception_in_stack(env);
const char *indexString = env->GetStringUTFChars(indexPath, 0);
index->SaveIndex(indexString);
env->ReleaseStringUTFChars(indexPath, indexString);
has_exception_in_stack(env);
// Free each object in the dataset. No need to clear the vector because it goes out of scope
// immediately
for (auto it = dataset.begin(); it != dataset.end(); it++) {
delete *it;
}
delete index;
delete space;
}
catch (...) {
if (object_ids) { env->ReleaseIntArrayElements(ids, object_ids, JNI_ABORT); }
for (auto it = dataset.begin(); it != dataset.end(); it++) {
delete *it;
}
if (index) { delete index; }
if (space) { delete space; }
catch_cpp_exception_and_throw_java(env);
}
}
JNIEXPORT jobjectArray JNICALL Java_com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex_queryIndex(JNIEnv* env, jclass cls, jlong indexPointer, jfloatArray queryVector, jint k)
{
try {
IndexWrapper *indexWrapper = reinterpret_cast<IndexWrapper*>(indexPointer);
float* rawQueryvector = env->GetFloatArrayElements(queryVector, 0);
std::unique_ptr<const Object> queryObject(new Object(-1, -1, env->GetArrayLength(queryVector)*sizeof(float), rawQueryvector));
env->ReleaseFloatArrayElements(queryVector, rawQueryvector, JNI_ABORT);
has_exception_in_stack(env);
KNNQuery<float> knnQuery(*(indexWrapper->space), queryObject.get(), k);
indexWrapper->index->Search(&knnQuery);
std::unique_ptr<KNNQueue<float>> result(knnQuery.Result()->Clone());
has_exception_in_stack(env);
int resultSize = result->Size();
jclass resultClass = env->FindClass("com/amazon/opendistroforelasticsearch/knn/index/KNNQueryResult");
jmethodID allArgs = env->GetMethodID(resultClass, "<init>", "(IF)V");
jobjectArray results = env->NewObjectArray(resultSize, resultClass, NULL);
for (int i = 0; i < resultSize; i++) {
float distance = result->TopDistance();
long id = result->Pop()->id();
env->SetObjectArrayElement(results, i, env->NewObject(resultClass, allArgs, id, distance));
}
has_exception_in_stack(env);
return results;
} catch(...) {
catch_cpp_exception_and_throw_java(env);
}
return NULL;
}
JNIEXPORT jlong JNICALL Java_com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex_init(JNIEnv* env, jclass cls, jstring indexPath, jobjectArray algoParams, jstring spaceType)
{
IndexWrapper *indexWrapper = NULL;
try {
const char *indexPathCStr = env->GetStringUTFChars(indexPath, 0);
string indexPathString(indexPathCStr);
env->ReleaseStringUTFChars(indexPath, indexPathCStr);
has_exception_in_stack(env);
// Load index from file (may throw)
const char *spaceTypeCStr = env->GetStringUTFChars(spaceType, 0);
string spaceTypeString(spaceTypeCStr);
env->ReleaseStringUTFChars(spaceType, spaceTypeCStr);
has_exception_in_stack(env);
IndexWrapper *indexWrapper = new IndexWrapper(spaceTypeString);
indexWrapper->index->LoadIndex(indexPathString);
// Parse and set query params
int paramsCount = env->GetArrayLength(algoParams);
vector<string> paramsList;
for (int i=0; i<paramsCount; i++) {
jstring param = (jstring) (env->GetObjectArrayElement(algoParams, i));
const char *rawString = env->GetStringUTFChars(param, 0);
paramsList.push_back(rawString);
env->ReleaseStringUTFChars(param, rawString);
}
indexWrapper->index->SetQueryTimeParams(AnyParams(paramsList));
has_exception_in_stack(env);
return (jlong) indexWrapper;
}
// nmslib seems to throw std::runtime_error if the index cannot be read (which
// is the only known failure mode for init()).
catch (...) {
if (indexWrapper) delete indexWrapper;
catch_cpp_exception_and_throw_java(env);
}
return NULL;
}
JNIEXPORT void JNICALL Java_com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex_gc(JNIEnv* env, jclass cls, jlong indexPointer)
{
try {
IndexWrapper *indexWrapper = reinterpret_cast<IndexWrapper*>(indexPointer);
has_exception_in_stack(env);
delete indexWrapper;
has_exception_in_stack(env);
}
catch (...) {
catch_cpp_exception_and_throw_java(env);
}
}
JNIEXPORT void JNICALL Java_com_amazon_opendistroforelasticsearch_knn_index_v2011_KNNIndex_initLibrary(JNIEnv *, jclass)
{
initLibrary();
}