Skip to content

Commit

Permalink
Move read/writeBinaryPOD from hnswlib to knowhere utils. (#194)
Browse files Browse the repository at this point in the history
Signed-off-by: Buqian Zheng <zhengbuqian@gmail.com>
  • Loading branch information
zhengbuqian committed Nov 16, 2023
1 parent f7fd047 commit 6c321bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
12 changes: 12 additions & 0 deletions include/knowhere/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ ConvertIVFFlat(const BinarySet& binset, const MetricType metric_type, const uint
bool
UseDiskLoad(const std::string& index_type, const int32_t& /*version*/);

template <typename T, typename W>
static void
writeBinaryPOD(W& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T, typename R>
static void
readBinaryPOD(R& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

} // namespace knowhere
4 changes: 4 additions & 0 deletions thirdparty/hnswlib/hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "hnswlib.h"
#include "io/memory_io.h"
#include "knowhere/config.h"
#include "knowhere/utils.h"
#include "knowhere/heap.h"
#include "neighbor.h"
#include "visited_list_pool.h"
Expand Down Expand Up @@ -661,6 +662,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
loadIndex(const std::string& location, const knowhere::Config& config, size_t max_elements_i = 0) {
using knowhere::readBinaryPOD;
auto cfg = static_cast<const knowhere::BaseConfig&>(config);

auto input = knowhere::FileReader(location);
Expand Down Expand Up @@ -770,6 +772,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
saveIndex(knowhere::MemoryIOWriter& output) {
using knowhere::writeBinaryPOD;
// write l2/ip calculator
writeBinaryPOD(output, metric_type_);
writeBinaryPOD(output, data_size_);
Expand Down Expand Up @@ -807,6 +810,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
loadIndex(knowhere::MemoryIOReader& input, size_t max_elements_i = 0) {
using knowhere::readBinaryPOD;
// linxj: init with metrictype
size_t dim;
readBinaryPOD(input, metric_type_);
Expand Down
24 changes: 0 additions & 24 deletions thirdparty/hnswlib/hnswlib/hnswlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ class pairGreater {
}
};

template <typename T>
static void
writeBinaryPOD(std::ostream& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T>
static void
readBinaryPOD(std::istream& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

template <typename T, typename W>
static void
writeBinaryPOD(W& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T, typename R>
static void
readBinaryPOD(R& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

template <typename MTYPE>
using DISTFUNC = MTYPE (*)(const void*, const void*, const void*);

Expand Down

0 comments on commit 6c321bb

Please sign in to comment.