2024-10-23 00:46:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2018-05-02 19:39:59 +08:00
|
|
|
*
|
2019-06-12 21:46:08 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2018-05-02 19:39:59 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// -*- c++ -*-
|
|
|
|
// I/O code for indexes
|
|
|
|
|
|
|
|
#include "index_io_c.h"
|
2021-02-25 20:44:50 +08:00
|
|
|
#include <faiss/index_io.h>
|
2018-05-02 19:39:59 +08:00
|
|
|
#include "macros_impl.h"
|
|
|
|
|
|
|
|
using faiss::Index;
|
2022-03-16 18:28:46 +08:00
|
|
|
using faiss::IndexBinary;
|
2025-04-02 02:05:29 +08:00
|
|
|
using faiss::IOReader;
|
|
|
|
using faiss::IOWriter;
|
2024-10-31 03:01:15 +08:00
|
|
|
using faiss::VectorTransform;
|
2018-05-02 19:39:59 +08:00
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
int faiss_write_index(const FaissIndex* idx, FILE* f) {
|
2018-05-02 19:39:59 +08:00
|
|
|
try {
|
|
|
|
faiss::write_index(reinterpret_cast<const Index*>(idx), f);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-05-02 19:39:59 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
int faiss_write_index_fname(const FaissIndex* idx, const char* fname) {
|
2018-05-02 19:39:59 +08:00
|
|
|
try {
|
|
|
|
faiss::write_index(reinterpret_cast<const Index*>(idx), fname);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-05-02 19:39:59 +08:00
|
|
|
}
|
|
|
|
|
2025-04-02 02:05:29 +08:00
|
|
|
int faiss_write_index_custom(
|
|
|
|
const FaissIndex* idx,
|
|
|
|
FaissIOWriter* io_writer,
|
|
|
|
int io_flags) {
|
|
|
|
try {
|
|
|
|
faiss::write_index(
|
|
|
|
reinterpret_cast<const Index*>(idx),
|
|
|
|
reinterpret_cast<IOWriter*>(io_writer),
|
|
|
|
io_flags);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
int faiss_read_index(FILE* f, int io_flags, FaissIndex** p_out) {
|
2018-05-02 19:39:59 +08:00
|
|
|
try {
|
|
|
|
auto out = faiss::read_index(f, io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndex*>(out);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2018-05-02 19:39:59 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
int faiss_read_index_fname(
|
|
|
|
const char* fname,
|
|
|
|
int io_flags,
|
|
|
|
FaissIndex** p_out) {
|
2018-05-02 19:39:59 +08:00
|
|
|
try {
|
|
|
|
auto out = faiss::read_index(fname, io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndex*>(out);
|
2021-02-25 20:44:50 +08:00
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
2021-02-16 04:40:02 +08:00
|
|
|
}
|
2022-03-16 18:28:46 +08:00
|
|
|
|
2025-04-02 02:05:29 +08:00
|
|
|
int faiss_read_index_custom(
|
|
|
|
FaissIOReader* io_reader,
|
|
|
|
int io_flags,
|
|
|
|
FaissIndex** p_out) {
|
|
|
|
try {
|
|
|
|
auto out = faiss::read_index(
|
|
|
|
reinterpret_cast<IOReader*>(io_reader), io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndex*>(out);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:28:46 +08:00
|
|
|
int faiss_write_index_binary(const FaissIndexBinary* idx, FILE* f) {
|
|
|
|
try {
|
|
|
|
faiss::write_index_binary(reinterpret_cast<const IndexBinary*>(idx), f);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
|
|
|
int faiss_write_index_binary_fname(
|
|
|
|
const FaissIndexBinary* idx,
|
|
|
|
const char* fname) {
|
|
|
|
try {
|
|
|
|
faiss::write_index_binary(
|
|
|
|
reinterpret_cast<const IndexBinary*>(idx), fname);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
2025-04-02 02:05:29 +08:00
|
|
|
int faiss_write_index_binary_custom(
|
|
|
|
const FaissIndexBinary* idx,
|
|
|
|
FaissIOWriter* io_writer) {
|
|
|
|
try {
|
|
|
|
faiss::write_index_binary(
|
|
|
|
reinterpret_cast<const IndexBinary*>(idx),
|
|
|
|
reinterpret_cast<IOWriter*>(io_writer));
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
2022-03-16 18:28:46 +08:00
|
|
|
int faiss_read_index_binary(FILE* f, int io_flags, FaissIndexBinary** p_out) {
|
|
|
|
try {
|
|
|
|
auto out = faiss::read_index_binary(f, io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndexBinary*>(out);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
|
|
|
int faiss_read_index_binary_fname(
|
|
|
|
const char* fname,
|
|
|
|
int io_flags,
|
|
|
|
FaissIndexBinary** p_out) {
|
|
|
|
try {
|
|
|
|
auto out = faiss::read_index_binary(fname, io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndexBinary*>(out);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
2024-10-31 03:01:15 +08:00
|
|
|
|
2025-04-02 02:05:29 +08:00
|
|
|
int faiss_read_index_binary_custom(
|
|
|
|
FaissIOReader* io_reader,
|
|
|
|
int io_flags,
|
|
|
|
FaissIndexBinary** p_out) {
|
|
|
|
try {
|
|
|
|
auto out = faiss::read_index_binary(
|
|
|
|
reinterpret_cast<IOReader*>(io_reader), io_flags);
|
|
|
|
*p_out = reinterpret_cast<FaissIndexBinary*>(out);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|
|
|
|
|
2024-10-31 03:01:15 +08:00
|
|
|
int faiss_read_VectorTransform_fname(
|
|
|
|
const char* fname,
|
|
|
|
FaissVectorTransform** p_out) {
|
|
|
|
try {
|
|
|
|
auto out = faiss::read_VectorTransform(fname);
|
|
|
|
*p_out = reinterpret_cast<FaissVectorTransform*>(out);
|
|
|
|
}
|
|
|
|
CATCH_AND_HANDLE
|
|
|
|
}
|