Skip to content

Commit 1c56719

Browse files
author
Dmitry Yashunin
committed
Index deserialization from old pickle and fixes
1 parent 82c0fd1 commit 1c56719

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

hnswlib/hnswalg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ namespace hnswlib {
768768
* @param internalId
769769
*/
770770
void markDeletedInternal(tableint internalId) {
771-
assert(internalId < max_elements_);
771+
assert(internalId < cur_element_count);
772772
if (!isMarkedDeleted(internalId))
773773
{
774774
unsigned char *ll_cur = ((unsigned char *)get_linklist0(internalId))+2;
@@ -800,7 +800,7 @@ namespace hnswlib {
800800
* @param internalId
801801
*/
802802
void unmarkDeletedInternal(tableint internalId) {
803-
assert(internalId < max_elements_);
803+
assert(internalId < cur_element_count);
804804
if (isMarkedDeleted(internalId))
805805
{
806806
unsigned char *ll_cur = ((unsigned char *)get_linklist0(internalId))+2;

python_bindings/bindings.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ inline void ParallelFor(size_t start, size_t end, size_t numThreads, Function fn
7070
std::rethrow_exception(lastException);
7171
}
7272
}
73-
74-
7573
}
7674

7775

@@ -108,7 +106,7 @@ class Index {
108106
default_ef=10;
109107
}
110108

111-
static const int ser_version = 1; // serialization version
109+
static const int ser_version = 2; // serialization version
112110

113111
std::string space_name;
114112
int dim;
@@ -477,7 +475,6 @@ class Index {
477475
assert_true(appr_alg->ef_construction_ == d["ef_construction"].cast<size_t>(), "Invalid value of ef_construction_ ");
478476

479477
appr_alg->ef_ = d["ef"].cast<size_t>();
480-
appr_alg->num_deleted_=d["num_deleted"].cast<size_t>();
481478

482479
assert_true(appr_alg->size_links_per_element_ == d["size_links_per_element"].cast<size_t>(), "Invalid value of size_links_per_element_ ");
483480

@@ -523,6 +520,26 @@ class Index {
523520

524521
}
525522
}
523+
524+
// set num_deleted
525+
if (d.contains("has_deletions"))
526+
{
527+
// create index from outdated pickle that doesn't contain num_deleted
528+
appr_alg->num_deleted_ = 0;
529+
std::cout << "Warning: creating index from outdated pickle" << std::endl;
530+
bool has_deletions = d["has_deletions"].cast<bool>();
531+
if (has_deletions)
532+
{
533+
for (size_t i = 0; i < appr_alg->cur_element_count; i++) {
534+
if(appr_alg->isMarkedDeleted(i))
535+
appr_alg->num_deleted_ += 1;
536+
}
537+
}
538+
}
539+
else
540+
{
541+
appr_alg->num_deleted_ = d["num_deleted"].cast<size_t>();
542+
}
526543
}
527544

528545
py::object knnQuery_return_numpy(py::object input, size_t k = 1, int num_threads = -1) {

0 commit comments

Comments
 (0)