-
Notifications
You must be signed in to change notification settings - Fork 2
/
hnsw_wrapper.cc
144 lines (123 loc) · 4.79 KB
/
hnsw_wrapper.cc
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
//hnsw_wrapper.cpp
#include <vector>
#include <iostream>
#include "hnswlib/hnswlib.h"
#include "hnsw_wrapper.h"
#include <thread>
#include <atomic>
HNSW initHNSW(int dim, unsigned long int max_elements, int M, int ef_construction, int rand_seed, char stype) {
hnswlib::SpaceInterface<float> *space;
if (stype == 'i') {
space = new hnswlib::InnerProductSpace(dim);
} else {
space = new hnswlib::L2Space(dim);
}
hnswlib::HierarchicalNSW<float> *appr_alg = new hnswlib::HierarchicalNSW<float>(space, max_elements, M,
ef_construction, rand_seed);
return (void *) appr_alg;
}
HNSW loadHNSW(char *location, int dim, char stype) {
hnswlib::SpaceInterface<float> *space;
if (stype == 'i') {
space = new hnswlib::InnerProductSpace(dim);
} else {
space = new hnswlib::L2Space(dim);
}
hnswlib::HierarchicalNSW<float> *appr_alg = new hnswlib::HierarchicalNSW<float>(space, std::string(location), false,
0);
return (void *) appr_alg;
}
HNSW saveHNSW(HNSW index, char *location) {
((hnswlib::HierarchicalNSW<float> *) index)->saveIndex(location);
return 0;
}
void addPoint(HNSW index, float *vec, unsigned long int label) {
((hnswlib::HierarchicalNSW<float> *) index)->addPoint(vec, label);
}
int searchKnn(HNSW index, float *vec, int N, unsigned long int *label, float *dist) {
std::priority_queue <std::pair<float, hnswlib::labeltype>> gt;
try {
gt = ((hnswlib::HierarchicalNSW<float> *) index)->searchKnn(vec, N);
} catch (const std::exception &e) {
return 0;
}
int n = gt.size();
std::pair<float, hnswlib::labeltype> pair;
for (int i = n - 1; i >= 0; i--) {
pair = gt.top();
*(dist + i) = pair.first;
*(label + i) = pair.second;
gt.pop();
}
return n;
}
void setEf(HNSW index, int ef) {
((hnswlib::HierarchicalNSW<float> *) index)->ef_ = ef;
}
bool resizeIndex(HNSW index, unsigned long int new_max_elements) {
if (new_max_elements < ((hnswlib::HierarchicalNSW<float> *) index)->getCurrentElementCount()) {
return false;
}
try {
((hnswlib::HierarchicalNSW<float> *) index)->resizeIndex(new_max_elements);
} catch (const std::exception &e) {
return false;
}
return true;
}
bool markDelete(HNSW index, unsigned long int label) {
try {
((hnswlib::HierarchicalNSW<float> *) index)->markDelete(label);
return true;
} catch (const std::exception &e) {
return false;
}
}
bool unmarkDelete(HNSW index, unsigned long int label) {
try {
((hnswlib::HierarchicalNSW<float> *) index)->unmarkDelete(label);
return true;
} catch (const std::exception &e) {
return false;
}
}
bool isMarkedDeleted(HNSW index, unsigned long int label) {
std::unique_lock <std::mutex> lock_table(((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_lock);
auto search = ((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_.find(label);
if (search != ((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_.end()) {
bool res = ((hnswlib::HierarchicalNSW<float> *) index)->isMarkedDeleted(search->second);
lock_table.unlock();
return res;
}
return false;
}
bool updatePoint(HNSW index, float *vec, unsigned long int label, float updateNeighborProbability) {
std::unique_lock <std::mutex> lock_table(((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_lock);
auto search = ((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_.find(label);
if (search != ((hnswlib::HierarchicalNSW<float> *) index)->label_lookup_.end()) {
hnswlib::tableint existingInternalId = search->second;
lock_table.unlock();
// const void *dataPoint, tableint internalId, float updateNeighborProbability
((hnswlib::HierarchicalNSW<float> *) index)->updatePoint(vec, existingInternalId, updateNeighborProbability);
return true;
}
return false;
}
void getDataByLabel(HNSW index, unsigned long int label, float* out_data) {
auto data = ((hnswlib::HierarchicalNSW<float>*)index)->getDataByLabel<float>(label);
std::vector<float>* vec = new std::vector<float>(data.begin(), data.end());
size_t size = vec->size();
for (size_t i = 0; i < size; i++) {
out_data[i] = (*vec)[i];
}
delete vec;
}
int getMaxElements(HNSW index) {
return ((hnswlib::HierarchicalNSW<float> *) index)->getMaxElements();
}
int getCurrentElementCount(HNSW index) {
return ((hnswlib::HierarchicalNSW<float> *) index)->getCurrentElementCount();
}
int getDeleteCount(HNSW index) {
return ((hnswlib::HierarchicalNSW<float> *) index)->getDeletedCount();
}