Skip to content

support deletion #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include_directories("${PROJECT_BINARY_DIR}")

set(SOURCE_EXE main.cpp)

set(SOURCE_LIB sift_1b.cpp)
set(SOURCE_LIB sift_1b.cpp delete_test.cpp)

add_library(sift_test STATIC ${SOURCE_LIB})

Expand Down
79 changes: 79 additions & 0 deletions delete_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <set>
#include "hnswlib/hnswlib.h"

int testDelete() {
hnswlib::HierarchicalNSW<int>* appr_alg;
int dim = 128, size = 2000;
hnswlib::L2SpaceI l2space(dim);
appr_alg = new hnswlib::HierarchicalNSW<int>(&l2space, size + 10, 128, 200, 100);

srand(time(NULL));
int* buffer = new int[dim];
for (int i = 0; i < 200; i++) {
for (int j = 0; j < dim; j++) {
buffer[j] = rand() % 10000;
}
appr_alg->addPoint((void*) buffer, i + 1);
}

int* sample = new int[dim];
for (int j = 0; j < dim; j++) {
sample[j] = rand() % 10000;
}
std::set<hnswlib::labeltype> deletedSet;

for (int i = 0; i < 10; i++) {
std::priority_queue<std::pair<int, hnswlib::labeltype>> result = appr_alg->searchKnn((void*) sample, 20);
std::cout << "result size:" << result.size() << std::endl;
hnswlib::labeltype toDel;
while (!result.empty()) {
if (result.size() == 1) {
std::cout << ' ' << result.top().second << "(" << result.top().first << ") ";
if (deletedSet.find(result.top().second) != deletedSet.end()) {
std::cerr << "delete failed!" << std::endl;
return -1;
}
toDel = result.top().second;
}
result.pop();
}
std::cout << std::endl;
appr_alg->markDelete(toDel);
deletedSet.insert(toDel);
try {
appr_alg->template getDataByLabel<int>(toDel);
std::cerr << "delete failed!" << std::endl;
return -1;
} catch (std::runtime_error e) {
}
}

appr_alg->recycle_in_test();
std::cout << "reusable starts at " << appr_alg->reusable_entry <<
"enter point is at " << appr_alg->enterpoint_node_ <<
" enter point data is " << appr_alg->getExternalLabel(appr_alg->enterpoint_node_) <<
"\t" << appr_alg->isMarkedDeleted(appr_alg->enterpoint_node_) << std::endl;

for (int i = 0; i < 210; i++) {
for (int j = 0; j < dim; j++) {
buffer[j] = rand() % 10000;
}
appr_alg->addPoint((void*) buffer, i + 1 + size);

std::priority_queue<std::pair<int, hnswlib::labeltype>> result = appr_alg->searchKnn((void*) buffer, 5);
while (!result.empty()) {
std::cout << ' ' << result.top().second << "(" << result.top().first << ") ";
result.pop();
}
std::cout << std::endl;
}

delete appr_alg;
delete[] buffer;
delete[] sample;
return 0;
};

Loading