hnswlib 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/build.yml +20 -0
- data/.gitignore +18 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +176 -0
- data/README.md +56 -0
- data/Rakefile +17 -0
- data/ext/hnswlib/extconf.rb +11 -0
- data/ext/hnswlib/hnswlibext.cpp +29 -0
- data/ext/hnswlib/hnswlibext.hpp +420 -0
- data/ext/hnswlib/src/LICENSE +201 -0
- data/ext/hnswlib/src/bruteforce.h +152 -0
- data/ext/hnswlib/src/hnswalg.h +1192 -0
- data/ext/hnswlib/src/hnswlib.h +108 -0
- data/ext/hnswlib/src/space_ip.h +282 -0
- data/ext/hnswlib/src/space_l2.h +281 -0
- data/ext/hnswlib/src/visited_list_pool.h +78 -0
- data/hnswlib.gemspec +35 -0
- data/lib/hnswlib.rb +154 -0
- data/lib/hnswlib/version.rb +9 -0
- metadata +69 -0
@@ -0,0 +1,420 @@
|
|
1
|
+
/**
|
2
|
+
* hnswlib.rb is a Ruby binding for the Hnswlib.
|
3
|
+
*
|
4
|
+
* Copyright (c) 2021 Atsushi Tatsuma
|
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
|
+
* http://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
|
+
#ifndef HNSWLIBEXT_HPP
|
20
|
+
#define HNSWLIBEXT_HPP 1
|
21
|
+
|
22
|
+
#include <ruby.h>
|
23
|
+
|
24
|
+
#include <hnswlib.h>
|
25
|
+
|
26
|
+
VALUE rb_cHnswlibL2Space;
|
27
|
+
VALUE rb_cHnswlibInnerProductSpace;
|
28
|
+
VALUE rb_cHnswlibHierarchicalNSW;
|
29
|
+
|
30
|
+
class RbHnswlibL2Space {
|
31
|
+
public:
|
32
|
+
static VALUE hnsw_l2space_alloc(VALUE self) {
|
33
|
+
hnswlib::L2Space* ptr = (hnswlib::L2Space*)ruby_xmalloc(sizeof(hnswlib::L2Space));
|
34
|
+
return TypedData_Wrap_Struct(self, &hnsw_l2space_type, ptr);
|
35
|
+
};
|
36
|
+
|
37
|
+
static void hnsw_l2space_free(void* ptr) {
|
38
|
+
((hnswlib::L2Space*)ptr)->~L2Space();
|
39
|
+
ruby_xfree(ptr);
|
40
|
+
};
|
41
|
+
|
42
|
+
static size_t hnsw_l2space_size(const void* ptr) {
|
43
|
+
return sizeof(*((hnswlib::L2Space*)ptr));
|
44
|
+
};
|
45
|
+
|
46
|
+
static hnswlib::L2Space* get_hnsw_l2space(VALUE self) {
|
47
|
+
hnswlib::L2Space* ptr;
|
48
|
+
TypedData_Get_Struct(self, hnswlib::L2Space, &hnsw_l2space_type, ptr);
|
49
|
+
return ptr;
|
50
|
+
};
|
51
|
+
|
52
|
+
static VALUE define_class(VALUE rb_mHnswlib) {
|
53
|
+
rb_cHnswlibL2Space = rb_define_class_under(rb_mHnswlib, "L2Space", rb_cObject);
|
54
|
+
rb_define_alloc_func(rb_cHnswlibL2Space, hnsw_l2space_alloc);
|
55
|
+
rb_define_method(rb_cHnswlibL2Space, "initialize", RUBY_METHOD_FUNC(_hnsw_l2space_init), 1);
|
56
|
+
rb_define_method(rb_cHnswlibL2Space, "distance", RUBY_METHOD_FUNC(_hnsw_l2space_distance), 2);
|
57
|
+
rb_define_attr(rb_cHnswlibL2Space, "dim", 1, 0);
|
58
|
+
return rb_cHnswlibL2Space;
|
59
|
+
};
|
60
|
+
|
61
|
+
private:
|
62
|
+
static const rb_data_type_t hnsw_l2space_type;
|
63
|
+
|
64
|
+
static VALUE _hnsw_l2space_init(VALUE self, VALUE dim) {
|
65
|
+
rb_iv_set(self, "@dim", dim);
|
66
|
+
hnswlib::L2Space* ptr = get_hnsw_l2space(self);
|
67
|
+
new (ptr) hnswlib::L2Space(NUM2INT(rb_iv_get(self, "@dim")));
|
68
|
+
return Qnil;
|
69
|
+
};
|
70
|
+
|
71
|
+
static VALUE _hnsw_l2space_distance(VALUE self, VALUE arr_a, VALUE arr_b) {
|
72
|
+
const int dim = NUM2INT(rb_iv_get(self, "@dim"));
|
73
|
+
if (dim != RARRAY_LEN(arr_a) || dim != RARRAY_LEN(arr_b)) {
|
74
|
+
rb_raise(rb_eArgError, "Array size does not match to space dimensionality.");
|
75
|
+
return Qnil;
|
76
|
+
}
|
77
|
+
float* vec_a = (float*)ruby_xmalloc(dim * sizeof(float));
|
78
|
+
for (int i = 0; i < dim; i++) {
|
79
|
+
vec_a[i] = (float)NUM2DBL(rb_ary_entry(arr_a, i));
|
80
|
+
}
|
81
|
+
float* vec_b = (float*)ruby_xmalloc(dim * sizeof(float));
|
82
|
+
for (int i = 0; i < dim; i++) {
|
83
|
+
vec_b[i] = (float)NUM2DBL(rb_ary_entry(arr_b, i));
|
84
|
+
}
|
85
|
+
hnswlib::DISTFUNC<float> dist_func = get_hnsw_l2space(self)->get_dist_func();
|
86
|
+
const float dist = dist_func(vec_a, vec_b, get_hnsw_l2space(self)->get_dist_func_param());
|
87
|
+
ruby_xfree(vec_a);
|
88
|
+
ruby_xfree(vec_b);
|
89
|
+
return DBL2NUM((double)dist);
|
90
|
+
};
|
91
|
+
};
|
92
|
+
|
93
|
+
const rb_data_type_t RbHnswlibL2Space::hnsw_l2space_type = {
|
94
|
+
"RbHnswlibL2Space",
|
95
|
+
{
|
96
|
+
NULL,
|
97
|
+
RbHnswlibL2Space::hnsw_l2space_free,
|
98
|
+
RbHnswlibL2Space::hnsw_l2space_size
|
99
|
+
},
|
100
|
+
NULL,
|
101
|
+
NULL,
|
102
|
+
RUBY_TYPED_FREE_IMMEDIATELY
|
103
|
+
};
|
104
|
+
|
105
|
+
class RbHnswlibInnerProductSpace {
|
106
|
+
public:
|
107
|
+
static VALUE hnsw_ipspace_alloc(VALUE self) {
|
108
|
+
hnswlib::InnerProductSpace* ptr = (hnswlib::InnerProductSpace*)ruby_xmalloc(sizeof(hnswlib::InnerProductSpace));
|
109
|
+
return TypedData_Wrap_Struct(self, &hnsw_ipspace_type, ptr);
|
110
|
+
};
|
111
|
+
|
112
|
+
static void hnsw_ipspace_free(void* ptr) {
|
113
|
+
((hnswlib::InnerProductSpace*)ptr)->~InnerProductSpace();
|
114
|
+
ruby_xfree(ptr);
|
115
|
+
};
|
116
|
+
|
117
|
+
static size_t hnsw_ipspace_size(const void* ptr) {
|
118
|
+
return sizeof(*((hnswlib::InnerProductSpace*)ptr));
|
119
|
+
};
|
120
|
+
|
121
|
+
static hnswlib::InnerProductSpace* get_hnsw_ipspace(VALUE self) {
|
122
|
+
hnswlib::InnerProductSpace* ptr;
|
123
|
+
TypedData_Get_Struct(self, hnswlib::InnerProductSpace, &hnsw_ipspace_type, ptr);
|
124
|
+
return ptr;
|
125
|
+
};
|
126
|
+
|
127
|
+
static VALUE define_class(VALUE rb_mHnswlib) {
|
128
|
+
rb_cHnswlibInnerProductSpace = rb_define_class_under(rb_mHnswlib, "InnerProductSpace", rb_cObject);
|
129
|
+
rb_define_alloc_func(rb_cHnswlibInnerProductSpace, hnsw_ipspace_alloc);
|
130
|
+
rb_define_method(rb_cHnswlibInnerProductSpace, "initialize", RUBY_METHOD_FUNC(_hnsw_ipspace_init), 1);
|
131
|
+
rb_define_method(rb_cHnswlibInnerProductSpace, "distance", RUBY_METHOD_FUNC(_hnsw_ipspace_distance), 2);
|
132
|
+
rb_define_attr(rb_cHnswlibInnerProductSpace, "dim", 1, 0);
|
133
|
+
return rb_cHnswlibInnerProductSpace;
|
134
|
+
};
|
135
|
+
|
136
|
+
private:
|
137
|
+
static const rb_data_type_t hnsw_ipspace_type;
|
138
|
+
|
139
|
+
static VALUE _hnsw_ipspace_init(VALUE self, VALUE dim) {
|
140
|
+
rb_iv_set(self, "@dim", dim);
|
141
|
+
hnswlib::InnerProductSpace* ptr = get_hnsw_ipspace(self);
|
142
|
+
new (ptr) hnswlib::InnerProductSpace(NUM2INT(rb_iv_get(self, "@dim")));
|
143
|
+
return Qnil;
|
144
|
+
};
|
145
|
+
|
146
|
+
static VALUE _hnsw_ipspace_distance(VALUE self, VALUE arr_a, VALUE arr_b) {
|
147
|
+
const int dim = NUM2INT(rb_iv_get(self, "@dim"));
|
148
|
+
if (dim != RARRAY_LEN(arr_a) || dim != RARRAY_LEN(arr_b)) {
|
149
|
+
rb_raise(rb_eArgError, "Array size does not match to space dimensionality.");
|
150
|
+
return Qnil;
|
151
|
+
}
|
152
|
+
float* vec_a = (float*)ruby_xmalloc(dim * sizeof(float));
|
153
|
+
for (int i = 0; i < dim; i++) {
|
154
|
+
vec_a[i] = (float)NUM2DBL(rb_ary_entry(arr_a, i));
|
155
|
+
}
|
156
|
+
float* vec_b = (float*)ruby_xmalloc(dim * sizeof(float));
|
157
|
+
for (int i = 0; i < dim; i++) {
|
158
|
+
vec_b[i] = (float)NUM2DBL(rb_ary_entry(arr_b, i));
|
159
|
+
}
|
160
|
+
hnswlib::DISTFUNC<float> dist_func = get_hnsw_ipspace(self)->get_dist_func();
|
161
|
+
const float dist = dist_func(vec_a, vec_b, get_hnsw_ipspace(self)->get_dist_func_param());
|
162
|
+
ruby_xfree(vec_a);
|
163
|
+
ruby_xfree(vec_b);
|
164
|
+
return DBL2NUM((double)dist);
|
165
|
+
};
|
166
|
+
};
|
167
|
+
|
168
|
+
const rb_data_type_t RbHnswlibInnerProductSpace::hnsw_ipspace_type = {
|
169
|
+
"RbHnswlibInnerProductSpace",
|
170
|
+
{
|
171
|
+
NULL,
|
172
|
+
RbHnswlibInnerProductSpace::hnsw_ipspace_free,
|
173
|
+
RbHnswlibInnerProductSpace::hnsw_ipspace_size
|
174
|
+
},
|
175
|
+
NULL,
|
176
|
+
NULL,
|
177
|
+
RUBY_TYPED_FREE_IMMEDIATELY
|
178
|
+
};
|
179
|
+
|
180
|
+
class RbHnswlibHierarchicalNSW {
|
181
|
+
public:
|
182
|
+
static VALUE hnsw_hierarchicalnsw_alloc(VALUE self) {
|
183
|
+
hnswlib::HierarchicalNSW<float>* ptr = (hnswlib::HierarchicalNSW<float>*)ruby_xmalloc(sizeof(hnswlib::HierarchicalNSW<float>));
|
184
|
+
return TypedData_Wrap_Struct(self, &hnsw_hierarchicalnsw_type, ptr);
|
185
|
+
};
|
186
|
+
|
187
|
+
static void hnsw_hierarchicalnsw_free(void* ptr) {
|
188
|
+
((hnswlib::HierarchicalNSW<float>*)ptr)->~HierarchicalNSW();
|
189
|
+
ruby_xfree(ptr);
|
190
|
+
};
|
191
|
+
|
192
|
+
static size_t hnsw_hierarchicalnsw_size(const void* ptr) {
|
193
|
+
return sizeof(*((hnswlib::HierarchicalNSW<float>*)ptr));
|
194
|
+
};
|
195
|
+
|
196
|
+
static hnswlib::HierarchicalNSW<float>* get_hnsw_hierarchicalnsw(VALUE self) {
|
197
|
+
hnswlib::HierarchicalNSW<float>* ptr;
|
198
|
+
TypedData_Get_Struct(self, hnswlib::HierarchicalNSW<float>, &hnsw_hierarchicalnsw_type, ptr);
|
199
|
+
return ptr;
|
200
|
+
};
|
201
|
+
|
202
|
+
static VALUE define_class(VALUE rb_mHnswlib) {
|
203
|
+
rb_cHnswlibHierarchicalNSW = rb_define_class_under(rb_mHnswlib, "HierarchicalNSW", rb_cObject);
|
204
|
+
rb_define_alloc_func(rb_cHnswlibHierarchicalNSW, hnsw_hierarchicalnsw_alloc);
|
205
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "initialize", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_init), -1);
|
206
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "add_point", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_add_point), 2);
|
207
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "search_knn", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_search_knn), 2);
|
208
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "save_index", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_save_index), 1);
|
209
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "load_index", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_load_index), 1);
|
210
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "get_point", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_get_point), 1);
|
211
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "get_ids", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_get_ids), 0);
|
212
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "mark_deleted", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_mark_deleted), 1);
|
213
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "resize_index", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_resize_index), 1);
|
214
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "set_ef", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_set_ef), 1);
|
215
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "max_elements", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_max_elements), 0);
|
216
|
+
rb_define_method(rb_cHnswlibHierarchicalNSW, "current_count", RUBY_METHOD_FUNC(_hnsw_hierarchicalnsw_current_count), 0);
|
217
|
+
rb_define_attr(rb_cHnswlibHierarchicalNSW, "space", 1, 0);
|
218
|
+
return rb_cHnswlibHierarchicalNSW;
|
219
|
+
};
|
220
|
+
|
221
|
+
private:
|
222
|
+
static const rb_data_type_t hnsw_hierarchicalnsw_type;
|
223
|
+
|
224
|
+
static VALUE _hnsw_hierarchicalnsw_init(int argc, VALUE* argv, VALUE self) {
|
225
|
+
VALUE kw_args = Qnil;
|
226
|
+
ID kw_table[5] = {
|
227
|
+
rb_intern("space"),
|
228
|
+
rb_intern("max_elements"),
|
229
|
+
rb_intern("m"),
|
230
|
+
rb_intern("ef_construction"),
|
231
|
+
rb_intern("random_seed")
|
232
|
+
};
|
233
|
+
VALUE kw_values[5] = {
|
234
|
+
Qundef, Qundef, Qundef, Qundef, Qundef
|
235
|
+
};
|
236
|
+
rb_scan_args(argc, argv, ":", &kw_args);
|
237
|
+
rb_get_kwargs(kw_args, kw_table, 2, 3, kw_values);
|
238
|
+
if (kw_values[2] == Qundef) kw_values[2] = INT2NUM(16);
|
239
|
+
if (kw_values[3] == Qundef) kw_values[3] = INT2NUM(200);
|
240
|
+
if (kw_values[4] == Qundef) kw_values[4] = INT2NUM(100);
|
241
|
+
|
242
|
+
rb_iv_set(self, "@space", kw_values[0]);
|
243
|
+
hnswlib::SpaceInterface<float>* space;
|
244
|
+
if (CLASS_OF(kw_values[0]) == rb_cHnswlibL2Space) {
|
245
|
+
space = RbHnswlibL2Space::get_hnsw_l2space(kw_values[0]);
|
246
|
+
} else {
|
247
|
+
space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(kw_values[0]);
|
248
|
+
}
|
249
|
+
const size_t max_elements = (size_t)NUM2INT(kw_values[1]);
|
250
|
+
const size_t m = (size_t)NUM2INT(kw_values[2]);
|
251
|
+
const size_t ef_construction = (size_t)NUM2INT(kw_values[3]);
|
252
|
+
const size_t random_seed = (size_t)NUM2INT(kw_values[4]);
|
253
|
+
|
254
|
+
hnswlib::HierarchicalNSW<float>* ptr = get_hnsw_hierarchicalnsw(self);
|
255
|
+
new (ptr) hnswlib::HierarchicalNSW<float>(space, max_elements, m, ef_construction, random_seed);
|
256
|
+
|
257
|
+
return Qnil;
|
258
|
+
};
|
259
|
+
|
260
|
+
static VALUE _hnsw_hierarchicalnsw_add_point(VALUE self, VALUE arr, VALUE idx) {
|
261
|
+
const int dim = NUM2INT(rb_iv_get(rb_iv_get(self, "@space"), "@dim"));
|
262
|
+
|
263
|
+
if (!RB_TYPE_P(arr, T_ARRAY)) {
|
264
|
+
rb_raise(rb_eArgError, "Expect point vector to be Ruby Array.");
|
265
|
+
return Qfalse;
|
266
|
+
}
|
267
|
+
|
268
|
+
if (!RB_INTEGER_TYPE_P(idx)) {
|
269
|
+
rb_raise(rb_eArgError, "Expect index to be Ruby Integer.");
|
270
|
+
return Qfalse;
|
271
|
+
}
|
272
|
+
|
273
|
+
if (dim != RARRAY_LEN(arr)) {
|
274
|
+
rb_raise(rb_eArgError, "Array size does not match to index dimensionality.");
|
275
|
+
return Qfalse;
|
276
|
+
}
|
277
|
+
|
278
|
+
float* vec = (float*)ruby_xmalloc(dim * sizeof(float));
|
279
|
+
for (int i = 0; i < dim; i++) {
|
280
|
+
vec[i] = (float)NUM2DBL(rb_ary_entry(arr, i));
|
281
|
+
}
|
282
|
+
|
283
|
+
get_hnsw_hierarchicalnsw(self)->addPoint((void *)vec, (size_t)NUM2INT(idx));
|
284
|
+
|
285
|
+
ruby_xfree(vec);
|
286
|
+
return Qtrue;
|
287
|
+
};
|
288
|
+
|
289
|
+
static VALUE _hnsw_hierarchicalnsw_search_knn(VALUE self, VALUE arr, VALUE k) {
|
290
|
+
const int dim = NUM2INT(rb_iv_get(rb_iv_get(self, "@space"), "@dim"));
|
291
|
+
|
292
|
+
if (!RB_TYPE_P(arr, T_ARRAY)) {
|
293
|
+
rb_raise(rb_eArgError, "Expect query vector to be Ruby Array.");
|
294
|
+
return Qnil;
|
295
|
+
}
|
296
|
+
|
297
|
+
if (!RB_INTEGER_TYPE_P(k)) {
|
298
|
+
rb_raise(rb_eArgError, "Expect the number of nearest neighbors to be Ruby Integer.");
|
299
|
+
return Qnil;
|
300
|
+
}
|
301
|
+
|
302
|
+
if (dim != RARRAY_LEN(arr)) {
|
303
|
+
rb_raise(rb_eArgError, "Array size does not match to index dimensionality.");
|
304
|
+
return Qnil;
|
305
|
+
}
|
306
|
+
|
307
|
+
float* vec = (float*)ruby_xmalloc(dim * sizeof(float));
|
308
|
+
for (int i = 0; i < dim; i++) {
|
309
|
+
vec[i] = (float)NUM2DBL(rb_ary_entry(arr, i));
|
310
|
+
}
|
311
|
+
|
312
|
+
std::priority_queue<std::pair<float, size_t>> result =
|
313
|
+
get_hnsw_hierarchicalnsw(self)->searchKnn((void *)vec, (size_t)NUM2INT(k));
|
314
|
+
|
315
|
+
ruby_xfree(vec);
|
316
|
+
|
317
|
+
if (result.size() != (size_t)NUM2INT(k)) {
|
318
|
+
rb_raise(rb_eRuntimeError, "Cannot return the results in a contigious 2D array. Probably ef or M is too small.");
|
319
|
+
return Qnil;
|
320
|
+
}
|
321
|
+
|
322
|
+
VALUE distances_arr = rb_ary_new2(result.size());
|
323
|
+
VALUE neighbors_arr = rb_ary_new2(result.size());
|
324
|
+
|
325
|
+
for (int i = NUM2INT(k) - 1; i >= 0; i--) {
|
326
|
+
const std::pair<float, size_t>& result_tuple = result.top();
|
327
|
+
rb_ary_store(distances_arr, i, DBL2NUM((double)result_tuple.first));
|
328
|
+
rb_ary_store(neighbors_arr, i, INT2NUM((int)result_tuple.second));
|
329
|
+
result.pop();
|
330
|
+
}
|
331
|
+
|
332
|
+
VALUE ret = rb_ary_new2(2);
|
333
|
+
rb_ary_store(ret, 0, neighbors_arr);
|
334
|
+
rb_ary_store(ret, 1, distances_arr);
|
335
|
+
return ret;
|
336
|
+
};
|
337
|
+
|
338
|
+
static VALUE _hnsw_hierarchicalnsw_save_index(VALUE self, VALUE _filename) {
|
339
|
+
std::string filename(StringValuePtr(_filename));
|
340
|
+
get_hnsw_hierarchicalnsw(self)->saveIndex(filename);
|
341
|
+
RB_GC_GUARD(_filename);
|
342
|
+
return Qnil;
|
343
|
+
};
|
344
|
+
|
345
|
+
static VALUE _hnsw_hierarchicalnsw_load_index(VALUE self, VALUE _filename) {
|
346
|
+
std::string filename(StringValuePtr(_filename));
|
347
|
+
hnswlib::SpaceInterface<float>* space;
|
348
|
+
if (CLASS_OF(rb_iv_get(self, "@space")) == rb_cHnswlibL2Space) {
|
349
|
+
space = RbHnswlibL2Space::get_hnsw_l2space(rb_iv_get(self, "@space"));
|
350
|
+
} else {
|
351
|
+
space = RbHnswlibInnerProductSpace::get_hnsw_ipspace(rb_iv_get(self, "@space"));
|
352
|
+
}
|
353
|
+
get_hnsw_hierarchicalnsw(self)->loadIndex(filename, space);
|
354
|
+
RB_GC_GUARD(_filename);
|
355
|
+
return Qnil;
|
356
|
+
};
|
357
|
+
|
358
|
+
static VALUE _hnsw_hierarchicalnsw_get_point(VALUE self, VALUE idx) {
|
359
|
+
VALUE ret = Qnil;
|
360
|
+
try {
|
361
|
+
std::vector<float> vec = get_hnsw_hierarchicalnsw(self)->template getDataByLabel<float>((size_t)NUM2INT(idx));
|
362
|
+
ret = rb_ary_new2(vec.size());
|
363
|
+
for (size_t i = 0; i < vec.size(); i++) {
|
364
|
+
rb_ary_store(ret, i, DBL2NUM((double)vec[i]));
|
365
|
+
}
|
366
|
+
} catch(std::runtime_error const& e) {
|
367
|
+
rb_raise(rb_eRuntimeError, "%s", e.what());
|
368
|
+
}
|
369
|
+
return ret;
|
370
|
+
};
|
371
|
+
|
372
|
+
static VALUE _hnsw_hierarchicalnsw_get_ids(VALUE self) {
|
373
|
+
VALUE ret = rb_ary_new();
|
374
|
+
for (auto kv : get_hnsw_hierarchicalnsw(self)->label_lookup_) {
|
375
|
+
rb_ary_push(ret, INT2NUM((int)kv.first));
|
376
|
+
}
|
377
|
+
return ret;
|
378
|
+
};
|
379
|
+
|
380
|
+
static VALUE _hnsw_hierarchicalnsw_mark_deleted(VALUE self, VALUE idx) {
|
381
|
+
get_hnsw_hierarchicalnsw(self)->markDelete((size_t)NUM2INT(idx));
|
382
|
+
return Qnil;
|
383
|
+
};
|
384
|
+
|
385
|
+
static VALUE _hnsw_hierarchicalnsw_resize_index(VALUE self, VALUE new_max_elements) {
|
386
|
+
if ((size_t)NUM2INT(new_max_elements) < get_hnsw_hierarchicalnsw(self)->cur_element_count) {
|
387
|
+
rb_raise(rb_eArgError, "Cannot resize, max element is less than the current number of elements.");
|
388
|
+
return Qnil;
|
389
|
+
}
|
390
|
+
get_hnsw_hierarchicalnsw(self)->resizeIndex((size_t)NUM2INT(new_max_elements));
|
391
|
+
return Qnil;
|
392
|
+
};
|
393
|
+
|
394
|
+
static VALUE _hnsw_hierarchicalnsw_set_ef(VALUE self, VALUE ef) {
|
395
|
+
get_hnsw_hierarchicalnsw(self)->ef_ = (size_t)NUM2INT(ef);
|
396
|
+
return Qnil;
|
397
|
+
};
|
398
|
+
|
399
|
+
static VALUE _hnsw_hierarchicalnsw_max_elements(VALUE self) {
|
400
|
+
return INT2NUM((int)(get_hnsw_hierarchicalnsw(self)->max_elements_));
|
401
|
+
};
|
402
|
+
|
403
|
+
static VALUE _hnsw_hierarchicalnsw_current_count(VALUE self) {
|
404
|
+
return INT2NUM((int)(get_hnsw_hierarchicalnsw(self)->cur_element_count));
|
405
|
+
};
|
406
|
+
};
|
407
|
+
|
408
|
+
const rb_data_type_t RbHnswlibHierarchicalNSW::hnsw_hierarchicalnsw_type = {
|
409
|
+
"RbHnswlibHierarchicalNSW",
|
410
|
+
{
|
411
|
+
NULL,
|
412
|
+
RbHnswlibHierarchicalNSW::hnsw_hierarchicalnsw_free,
|
413
|
+
RbHnswlibHierarchicalNSW::hnsw_hierarchicalnsw_size
|
414
|
+
},
|
415
|
+
NULL,
|
416
|
+
NULL,
|
417
|
+
RUBY_TYPED_FREE_IMMEDIATELY
|
418
|
+
};
|
419
|
+
|
420
|
+
#endif /* HNSWLIBEXT_HPP */
|
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|