Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
MetaIndexes_c.cpp
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD+Patents license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 // Copyright 2004-present Facebook. All Rights Reserved.
10 // -*- c++ -*-
11 
12 #include "MetaIndexes_c.h"
13 #include "MetaIndexes.h"
14 #include "macros_impl.h"
15 
16 using faiss::Index;
17 using faiss::IndexIDMap;
18 using faiss::IndexIDMap2;
19 using faiss::IndexShards;
20 
21 DEFINE_GETTER(IndexIDMap, int, own_fields)
22 DEFINE_SETTER(IndexIDMap, int, own_fields)
23 
24 int faiss_IndexIDMap_new(FaissIndexIDMap** p_index, FaissIndex* index) {
25  try {
26  auto out = new IndexIDMap(reinterpret_cast<Index*>(index));
27  *p_index = reinterpret_cast<FaissIndexIDMap*>(out);
28  } CATCH_AND_HANDLE
29 }
30 
31 void faiss_IndexIDMap_id_map(FaissIndexIDMap* index, long** p_id_map, size_t* p_size) {
32  auto idx = reinterpret_cast<IndexIDMap*>(index);
33  if (p_id_map)
34  *p_id_map = idx->id_map.data();
35  if (p_size)
36  *p_size = idx->id_map.size();
37 }
38 
39 int faiss_IndexIDMap2_new(FaissIndexIDMap2** p_index, FaissIndex* index) {
40  try {
41  auto out = new IndexIDMap2(reinterpret_cast<Index*>(index));
42  *p_index = reinterpret_cast<FaissIndexIDMap2*>(out);
43  } CATCH_AND_HANDLE
44 }
45 
46 int faiss_IndexIDMap2_construct_rev_map(FaissIndexIDMap2* index) {
47  try {
48  reinterpret_cast<IndexIDMap2*>(index)->construct_rev_map();
49  } CATCH_AND_HANDLE
50 }
51 
52 DEFINE_GETTER(IndexShards, int, own_fields)
53 DEFINE_SETTER(IndexShards, int, own_fields)
54 
55 DEFINE_GETTER(IndexShards, int, threaded)
56 DEFINE_SETTER(IndexShards, int, threaded)
57 
58 DEFINE_GETTER(IndexShards, int, successive_ids)
59 DEFINE_SETTER(IndexShards, int, successive_ids)
60 
61 int faiss_IndexShards_new(FaissIndexShards** p_index, idx_t d) {
62  try {
63  auto out = new IndexShards(d);
64  *p_index = reinterpret_cast<FaissIndexShards*>(out);
65  } CATCH_AND_HANDLE
66 }
67 
68 int faiss_IndexShards_new_with_options(FaissIndexShards** p_index, idx_t d, int threaded, int successive_ids) {
69  try {
70  auto out = new IndexShards(d, static_cast<bool>(threaded), static_cast<bool>(successive_ids));
71  *p_index = reinterpret_cast<FaissIndexShards*>(out);
72  } CATCH_AND_HANDLE
73 }
74 
75 /** get a pointer to the index' shards (the `shard_indexes` field). The
76  * outputs of this function become invalid after any operation that can modify the index.
77  *
78  * @param index opaque pointer to index object
79  * @param p_shard_indexes output, the pointer to the beginning of `shard_indexes`.
80  * @param p_size output, the current length of `shard_indexes`.
81  */
82 void faiss_IndexShards_shard_indexes(FaissIndexShards* index, FaissIndex** p_shard_indexes, size_t* p_size) {
83  auto idx = reinterpret_cast<IndexShards*>(index);
84  if (p_shard_indexes)
85  *p_shard_indexes = reinterpret_cast<FaissIndex*>(idx->shard_indexes.data());
86  if (p_size)
87  *p_size = idx->shard_indexes.size();
88 }
89 
90 int faiss_IndexShards_add_shard(FaissIndexShards* index, FaissIndex* shard) {
91  try {
92  reinterpret_cast<IndexShards*>(index)->add_shard(
93  reinterpret_cast<Index*>(shard));
94  } CATCH_AND_HANDLE
95 }
96 
97 int faiss_IndexShards_sync_with_shard_indexes(FaissIndexShards* index) {
98  try {
99  reinterpret_cast<IndexShards*>(index)->sync_with_shard_indexes();
100  } CATCH_AND_HANDLE
101 }
102 
103 FaissIndex* faiss_IndexShards_at(FaissIndexShards* index, int i) {
104  auto shard = reinterpret_cast<IndexShards*>(index)->at(i);
105  return reinterpret_cast<FaissIndex*>(shard);
106 }