Skip to content
Merged
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
1 change: 1 addition & 0 deletions c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
47 changes: 47 additions & 0 deletions c_api/index_io_c_ex.cpp
Original file line number Diff line number Diff line change
@@ -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 <faiss/index_io.h>
#include <faiss/impl/io.h>
#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<const Index*>(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<FaissIndex*>(index);
}
CATCH_AND_HANDLE
}
36 changes: 36 additions & 0 deletions c_api/index_io_c_ex.h
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#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