Skip to content
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

Added generic serial / deserialization interfaces #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Header-only C++ HNSW implementation with python bindings, insertions and updates

**NEWS:**

**version 0.8.1**

* Added generic serialization / deserialization interfaces for HierarchicalNSW and BruteforceSearch that take `std::ostream` / `std::istream` arguments

**version 0.8.0**

* Multi-vector document search and epsilon search (for now, only in C++)
Expand Down
25 changes: 17 additions & 8 deletions hnswlib/bruteforce.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mutex>
#include <algorithm>
#include <assert.h>
#include <iostream>

namespace hnswlib {
template<typename dist_t>
Expand Down Expand Up @@ -135,24 +136,25 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
}


void saveIndex(const std::string &location) {
std::ofstream output(location, std::ios::binary);
std::streampos position;

void saveIndex(std::ostream &output) {
writeBinaryPOD(output, maxelements_);
writeBinaryPOD(output, size_per_element_);
writeBinaryPOD(output, cur_element_count);

output.write(data_, maxelements_ * size_per_element_);
}


void saveIndex(const std::string &location) {
std::ofstream output(location, std::ios::binary);

saveIndex(output);

output.close();
}


void loadIndex(const std::string &location, SpaceInterface<dist_t> *s) {
std::ifstream input(location, std::ios::binary);
std::streampos position;

void loadIndex(std::istream &input, SpaceInterface<dist_t> *s) {
readBinaryPOD(input, maxelements_);
readBinaryPOD(input, size_per_element_);
readBinaryPOD(input, cur_element_count);
Expand All @@ -166,6 +168,13 @@ class BruteforceSearch : public AlgorithmInterface<dist_t> {
throw std::runtime_error("Not enough memory: loadIndex failed to allocate data");

input.read(data_, maxelements_ * size_per_element_);
}


void loadIndex(const std::string &location, SpaceInterface<dist_t> *s) {
std::ifstream input(location, std::ios::binary);

loadIndex(input, s);

input.close();
}
Expand Down
26 changes: 18 additions & 8 deletions hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unordered_set>
#include <list>
#include <memory>
#include <iostream>

namespace hnswlib {
typedef unsigned int tableint;
Expand Down Expand Up @@ -682,10 +683,8 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
return size;
}

void saveIndex(const std::string &location) {
std::ofstream output(location, std::ios::binary);
std::streampos position;

void saveIndex(std::ostream &output) {
writeBinaryPOD(output, offsetLevel0_);
writeBinaryPOD(output, max_elements_);
writeBinaryPOD(output, cur_element_count);
Expand All @@ -709,16 +708,17 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
if (linkListSize)
output.write(linkLists_[i], linkListSize);
}
output.close();
}


void loadIndex(const std::string &location, SpaceInterface<dist_t> *s, size_t max_elements_i = 0) {
std::ifstream input(location, std::ios::binary);
void saveIndex(const std::string &location) {
std::ofstream output(location, std::ios::binary);
saveIndex(output);
output.close();
}

if (!input.is_open())
throw std::runtime_error("Cannot open file");

void loadIndex(std::istream &input, SpaceInterface<dist_t> *s, size_t max_elements_i = 0) {
clear();
// get file size:
input.seekg(0, input.end);
Expand Down Expand Up @@ -815,6 +815,16 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
if (allow_replace_deleted_) deleted_elements.insert(i);
}
}
}


void loadIndex(const std::string &location, SpaceInterface<dist_t> *s, size_t max_elements_i = 0) {
std::ifstream input(location, std::ios::binary);

if (!input.is_open())
throw std::runtime_error("Cannot open file");

loadIndex(input, s, max_elements_i);

input.close();

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext

__version__ = '0.8.0'
__version__ = '0.8.1'


include_dirs = [
Expand Down