diff --git a/c_api/CMakeLists.txt b/c_api/CMakeLists.txt index 01789a3214..8e322bb72c 100644 --- a/c_api/CMakeLists.txt +++ b/c_api/CMakeLists.txt @@ -28,6 +28,7 @@ set(FAISS_C_SRC error_impl.cpp index_factory_c.cpp index_io_c.cpp + index_io_c_ex.cpp impl/AuxIndexStructures_c.cpp utils/distances_c.cpp ) diff --git a/c_api/index_io_c_ex.cpp b/c_api/index_io_c_ex.cpp new file mode 100644 index 0000000000..200f00dac0 --- /dev/null +++ b/c_api/index_io_c_ex.cpp @@ -0,0 +1,47 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// Copyright 2023-present Facebook. All Rights Reserved +// -*- c++ -*- +// More I/O code for indexes + +#include "index_io_c_ex.h" +#include +#include +#include "macros_impl.h" + +using faiss::Index; +using faiss::IndexBinary; + +int faiss_write_index_buf(const FaissIndex* idx, int* size, unsigned char** buf) { + try { + faiss::VectorIOWriter writer; + + faiss::write_index(reinterpret_cast(idx), &writer); + unsigned char* tempBuf = (unsigned char*)malloc((writer.data.size() + 1) * sizeof(uint8_t)); + std::copy(writer.data.begin(), writer.data.end(), tempBuf); + tempBuf[writer.data.size()] = 0; + + // return the serialized index content to the passed buf + // furthermore, return its size (perhaps useful for memory management) + *buf = tempBuf; + *size = writer.data.size(); + } + CATCH_AND_HANDLE +} + +int faiss_read_index_buf(const unsigned char* buf, int size, int io_flags, FaissIndex** p_out) { + try { + faiss::VectorIOReader reader; + reader.data.resize(size); + memcpy(reader.data.data(), buf, size); + + auto index = faiss::read_index(&reader, io_flags); + *p_out = reinterpret_cast(index); + } + CATCH_AND_HANDLE +} diff --git a/c_api/index_io_c_ex.h b/c_api/index_io_c_ex.h new file mode 100644 index 0000000000..4cb10fd292 --- /dev/null +++ b/c_api/index_io_c_ex.h @@ -0,0 +1,36 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// Copyright 2023-present Facebook. All Rights Reserved +// -*- c++ -*- +// More I/O code for indexes + +#ifndef FAISS_INDEX_IO_C_EX_H +#define FAISS_INDEX_IO_C_EX_H + +#include +#include "IndexBinary_c.h" +#include "Index_c.h" +#include "faiss_c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Write index to buffer + */ +int faiss_write_index_buf(const FaissIndex* idx, int* buf_size, unsigned char** buf); + +/** Read index from buffer + */ +int faiss_read_index_buf(const unsigned char* buf, int limit, int io_flags, + FaissIndex** p_out); + +#ifdef __cplusplus +} +#endif +#endif