Skip to content

Commit

Permalink
Add the ability to clone and read binary indexes to the C API. (#3318)
Browse files Browse the repository at this point in the history
Summary:
I noticed we have a pretty decent C API for binary indexes and please correct me if I'm wrong but we seem to be missing a couple of functions, like the ability to clone and read binary indexes. This PR provides those functions.

Pull Request resolved: #3318

Reviewed By: algoriddle

Differential Revision: D55469615

Pulled By: mdouze

fbshipit-source-id: 42e6f827d8b5ad6bc3efe989e47ede3aa06c1810
  • Loading branch information
aalekhpatel07 authored and facebook-github-bot committed Mar 29, 2024
1 parent d99f07e commit 4e6b6f8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions c_api/clone_index_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "macros_impl.h"

using faiss::Index;
using faiss::IndexBinary;

int faiss_clone_index(const FaissIndex* idx, FaissIndex** p_out) {
try {
Expand All @@ -22,3 +23,14 @@ int faiss_clone_index(const FaissIndex* idx, FaissIndex** p_out) {
}
CATCH_AND_HANDLE
}

int faiss_clone_index_binary(
const FaissIndexBinary* idx,
FaissIndexBinary** p_out) {
try {
auto out = faiss::clone_binary_index(
reinterpret_cast<const IndexBinary*>(idx));
*p_out = reinterpret_cast<FaissIndexBinary*>(out);
}
CATCH_AND_HANDLE
}
4 changes: 4 additions & 0 deletions c_api/clone_index_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define FAISS_CLONE_INDEX_C_H

#include <stdio.h>
#include "IndexBinary_c.h"
#include "Index_c.h"
#include "faiss_c.h"

Expand All @@ -25,6 +26,9 @@ extern "C" {
/** Clone an index. This is equivalent to `faiss::clone_index` */
int faiss_clone_index(const FaissIndex*, FaissIndex** p_out);

/** Clone a binary index. This is equivalent to `faiss::clone_index_binary` */
int faiss_clone_index_binary(const FaissIndexBinary*, FaissIndexBinary** p_out);

#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 15 additions & 1 deletion c_api/index_factory_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using faiss::Index;

/** Build and index with the sequence of processing steps described in
/** Build an index with the sequence of processing steps described in
* the string.
*/
int faiss_index_factory(
Expand All @@ -29,3 +29,17 @@ int faiss_index_factory(
}
CATCH_AND_HANDLE
}

/** Build an index with the sequence of processing steps described in
* the string.
*/
int faiss_index_binary_factory(
FaissIndexBinary** p_index,
int d,
const char* description) {
try {
*p_index = reinterpret_cast<FaissIndexBinary*>(
faiss::index_binary_factory(d, description));
}
CATCH_AND_HANDLE
}
11 changes: 10 additions & 1 deletion c_api/index_factory_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#ifndef FAISS_INDEX_FACTORY_C_H
#define FAISS_INDEX_FACTORY_C_H

#include "IndexBinary_c.h"
#include "Index_c.h"
#include "faiss_c.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Build and index with the sequence of processing steps described in
/** Build an index with the sequence of processing steps described in
* the string.
*/
int faiss_index_factory(
Expand All @@ -27,6 +28,14 @@ int faiss_index_factory(
const char* description,
FaissMetricType metric);

/** Build a binary index with the sequence of processing steps described in
* the string.
*/
int faiss_index_binary_factory(
FaissIndexBinary** p_index,
int d,
const char* description);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 4e6b6f8

Please sign in to comment.