Skip to content

Commit

Permalink
Add iterator support to pyknowhere interface.
Browse files Browse the repository at this point in the history
Signed-off-by: Buqian Zheng <[email protected]>
  • Loading branch information
zhengbuqian committed Nov 13, 2023
1 parent f7fd047 commit 5afdc61
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
41 changes: 40 additions & 1 deletion python/knowhere/knowhere.i
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ import_array();
%include <std_pair.i>
%include <std_map.i>
%include <std_shared_ptr.i>
%include <std_vector.i>
%include <exception.i>
%shared_ptr(knowhere::DataSet)
%shared_ptr(knowhere::BinarySet)
%template(DataSetPtr) std::shared_ptr<knowhere::DataSet>;
%template(BinarySetPtr) std::shared_ptr<knowhere::BinarySet>;
%template(int64_float_pair) std::pair<long long int, float>;
%include <knowhere/expected.h>
%include <knowhere/dataset.h>
%include <knowhere/binaryset.h>
Expand Down Expand Up @@ -104,7 +106,7 @@ del Enum
%inline %{

class GILReleaser {
public:
public:
GILReleaser() : save(PyEval_SaveThread()) {
}
~GILReleaser() {
Expand All @@ -113,6 +115,25 @@ public:
PyThreadState* save;
};

class AnnIterator {
public:
AnnIterator(std::shared_ptr<IndexNode::iterator> it = nullptr) : it_(it) {
}
~AnnIterator() {
}

bool HasNext() {
return it_ && it_->HasNext();
}

std::pair<int64_t, float> Next() {
return it_->Next();
}

private:
std::shared_ptr<IndexNode::iterator> it_;
};

class IndexWrap {
public:
IndexWrap(const std::string& name, const int32_t& version) {
Expand Down Expand Up @@ -157,6 +178,22 @@ class IndexWrap {
}
}

std::vector<AnnIterator>
GetAnnIterator(knowhere::DataSetPtr dataset, const std::string& json, const knowhere::BitsetView& bitset, knowhere::Status& status) {
GILReleaser rel;
auto res = idx.AnnIterator(*dataset, knowhere::Json::parse(json), bitset);
if (!res.has_value()) {
status = res.error();
return std::vector<AnnIterator>();
}
status = knowhere::Status::success;
std::vector<AnnIterator> result;
for (auto it : res.value()) {
result.emplace_back(it);
}
return result;
}

knowhere::DataSetPtr
RangeSearch(knowhere::DataSetPtr dataset, const std::string& json, const knowhere::BitsetView& bitset, knowhere::Status& status){
GILReleaser rel;
Expand Down Expand Up @@ -468,3 +505,5 @@ SetSimdType(const std::string type) {
}

%}

%template(AnnIteratorVector) std::vector<AnnIterator>;
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_readme():
description=(
"A library for efficient similarity search and clustering of vectors."
),
url="https://github.com/milvus-io/knowhere",
url="https://github.com/zilliztech/knowhere",
author="Milvus Team",
author_email="[email protected]",
license='Apache License 2.0',
Expand Down
1 change: 1 addition & 0 deletions src/common/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static const std::unordered_set<std::string> ext_legal_json_keys = {"metric_type
"M", // HNSW param
"efConstruction", // HNSW param
"ef", // HNSW param
"seed_ef", // HNSW iterator param
"level",
"index_type",
"index_mode",
Expand Down

0 comments on commit 5afdc61

Please sign in to comment.