| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c++ -*- |
| 9 |
// I/O code for indexes |
| 10 |
|
| 11 |
#include "index_io_c.h" |
| 12 |
#include <faiss/index_io.h> |
| 13 |
#include "macros_impl.h" |
| 14 |
|
| 15 |
using faiss::Index; |
| 16 |
using faiss::IndexBinary; |
| 17 |
using faiss::IOReader; |
| 18 |
using faiss::IOWriter; |
| 19 |
using faiss::VectorTransform; |
| 20 |
|
| 21 |
int faiss_write_index(const FaissIndex* idx, FILE* f) { |
| 22 |
try { |
| 23 |
faiss::write_index(reinterpret_cast<const Index*>(idx), f); |
| 24 |
} |
| 25 |
CATCH_AND_HANDLE |
| 26 |
} |
| 27 |
|
| 28 |
int faiss_write_index_fname(const FaissIndex* idx, const char* fname) { |
| 29 |
try { |
| 30 |
faiss::write_index(reinterpret_cast<const Index*>(idx), fname); |
| 31 |
} |
| 32 |
CATCH_AND_HANDLE |
| 33 |
} |
| 34 |
|
| 35 |
int faiss_write_index_custom( |
| 36 |
const FaissIndex* idx, |
| 37 |
FaissIOWriter* io_writer, |
| 38 |
int io_flags) { |
| 39 |
try { |
| 40 |
faiss::write_index( |
| 41 |
reinterpret_cast<const Index*>(idx), |
| 42 |
reinterpret_cast<IOWriter*>(io_writer), |
| 43 |
io_flags); |
| 44 |
} |
| 45 |
CATCH_AND_HANDLE |
| 46 |
} |
| 47 |
|
| 48 |
int faiss_read_index(FILE* f, int io_flags, FaissIndex** p_out) { |
| 49 |
try { |
| 50 |
auto out = faiss::read_index(f, io_flags); |
| 51 |
*p_out = reinterpret_cast<FaissIndex*>(out); |
| 52 |
} |
| 53 |
CATCH_AND_HANDLE |
| 54 |
} |
| 55 |
|
| 56 |
int faiss_read_index_fname( |
| 57 |
const char* fname, |
| 58 |
int io_flags, |
| 59 |
FaissIndex** p_out) { |
| 60 |
try { |
| 61 |
auto out = faiss::read_index(fname, io_flags); |
| 62 |
*p_out = reinterpret_cast<FaissIndex*>(out); |
| 63 |
} |
| 64 |
CATCH_AND_HANDLE |
| 65 |
} |
| 66 |
|
| 67 |
int faiss_read_index_custom( |
| 68 |
FaissIOReader* io_reader, |
| 69 |
int io_flags, |
| 70 |
FaissIndex** p_out) { |
| 71 |
try { |
| 72 |
auto out = faiss::read_index( |
| 73 |
reinterpret_cast<IOReader*>(io_reader), io_flags); |
| 74 |
*p_out = reinterpret_cast<FaissIndex*>(out); |
| 75 |
} |
| 76 |
CATCH_AND_HANDLE |
| 77 |
} |
| 78 |
|
| 79 |
int faiss_write_index_binary(const FaissIndexBinary* idx, FILE* f) { |
| 80 |
try { |
| 81 |
faiss::write_index_binary(reinterpret_cast<const IndexBinary*>(idx), f); |
| 82 |
} |
| 83 |
CATCH_AND_HANDLE |
| 84 |
} |
| 85 |
|
| 86 |
int faiss_write_index_binary_fname( |
| 87 |
const FaissIndexBinary* idx, |
| 88 |
const char* fname) { |
| 89 |
try { |
| 90 |
faiss::write_index_binary( |
| 91 |
reinterpret_cast<const IndexBinary*>(idx), fname); |
| 92 |
} |
| 93 |
CATCH_AND_HANDLE |
| 94 |
} |
| 95 |
|
| 96 |
int faiss_write_index_binary_custom( |
| 97 |
const FaissIndexBinary* idx, |
| 98 |
FaissIOWriter* io_writer) { |
| 99 |
try { |
| 100 |
faiss::write_index_binary( |
| 101 |
reinterpret_cast<const IndexBinary*>(idx), |
| 102 |
reinterpret_cast<IOWriter*>(io_writer)); |
| 103 |
} |
| 104 |
CATCH_AND_HANDLE |
| 105 |
} |
| 106 |
|
| 107 |
int faiss_read_index_binary(FILE* f, int io_flags, FaissIndexBinary** p_out) { |
| 108 |
try { |
| 109 |
auto out = faiss::read_index_binary(f, io_flags); |
| 110 |
*p_out = reinterpret_cast<FaissIndexBinary*>(out); |
| 111 |
} |
| 112 |
CATCH_AND_HANDLE |
| 113 |
} |
| 114 |
|
| 115 |
int faiss_read_index_binary_fname( |
| 116 |
const char* fname, |
| 117 |
int io_flags, |
| 118 |
FaissIndexBinary** p_out) { |
| 119 |
try { |
| 120 |
auto out = faiss::read_index_binary(fname, io_flags); |
| 121 |
*p_out = reinterpret_cast<FaissIndexBinary*>(out); |
| 122 |
} |
| 123 |
CATCH_AND_HANDLE |
| 124 |
} |
| 125 |
|
| 126 |
int faiss_read_index_binary_custom( |
| 127 |
FaissIOReader* io_reader, |
| 128 |
int io_flags, |
| 129 |
FaissIndexBinary** p_out) { |
| 130 |
try { |
| 131 |
auto out = faiss::read_index_binary( |
| 132 |
reinterpret_cast<IOReader*>(io_reader), io_flags); |
| 133 |
*p_out = reinterpret_cast<FaissIndexBinary*>(out); |
| 134 |
} |
| 135 |
CATCH_AND_HANDLE |
| 136 |
} |
| 137 |
|
| 138 |
int faiss_read_VectorTransform_fname( |
| 139 |
const char* fname, |
| 140 |
FaissVectorTransform** p_out) { |
| 141 |
try { |
| 142 |
auto out = faiss::read_VectorTransform(fname); |
| 143 |
*p_out = reinterpret_cast<FaissVectorTransform*>(out); |
| 144 |
} |
| 145 |
CATCH_AND_HANDLE |
| 146 |
} |