Skip to content

Commit 3e158d9

Browse files
author
Uri Goren
authored
Update LazyIndex.py
Added a short description
1 parent a4490a0 commit 3e158d9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

python_bindings/LazyIndex.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import hnswlib
2-
2+
"""
3+
A python wrapper for lazy indexing, preserves the same api as hnswlib.Index but initializes the index only after adding items for the first time with `add_items`.
4+
"""
35
class LazyIndex(hnswlib.Index):
46
def __init__(self, space, dim,max_elements=1024, ef_construction=200, M=16):
57
super().__init__(space, dim)
68
self.init_max_elements=max_elements
79
self.init_ef_construction=ef_construction
810
self.init_M=M
9-
def init(self, max_elements=0):
11+
def init_index(self, max_elements=0,M=None,ef_construction=None):
1012
if max_elements==0:
1113
max_elements=self.init_max_elements
14+
if ef_construction:
15+
self.init_ef_construction=ef_construction
16+
if M:
17+
self.init_M=M
1218
super().init_index(max_elements, self.init_M, self.init_ef_construction)
1319
def add_items(self, data, ids=None, num_threads=-1):
1420
if self.max_elements==0:
15-
self.init()
21+
self.init_index()
1622
return super().add_items(data,ids, num_threads)
1723
def get_items(self, ids=None):
1824
if self.max_elements==0:
@@ -24,7 +30,7 @@ def knn_query(self, data,k=1, num_threads=-1):
2430
return super().knn_query(data, k, num_threads)
2531
def resize_index(self, size):
2632
if self.max_elements==0:
27-
return self.init(size)
33+
return self.init_index(size)
2834
else:
2935
return super().resize_index(size)
3036
def set_ef(self, ef):

0 commit comments

Comments
 (0)