faiss_next/index/
id_map2.rs1use std::ptr;
2
3use faiss_next_sys::{self, FaissIndex, FaissIndexIDMap2};
4
5use crate::error::{check_return_code, Error, Result};
6use crate::index::native::InnerPtr;
7use crate::index::traits::Index;
8
9pub struct IndexIDMap2 {
10 inner: InnerPtr<FaissIndexIDMap2>,
11}
12
13impl IndexIDMap2 {
14 pub fn new(index: super::IndexImpl) -> Result<Self> {
15 unsafe {
16 let mut inner = ptr::null_mut();
17 check_return_code(faiss_next_sys::faiss_IndexIDMap2_new(
18 &mut inner,
19 index.inner_ptr(),
20 ))?;
21 std::mem::forget(index);
22 Ok(Self {
23 inner: InnerPtr::new(inner)?,
24 })
25 }
26 }
27
28 pub fn from_index(index: super::IndexImpl) -> Result<Self> {
29 unsafe {
30 let idmap_ptr = faiss_next_sys::faiss_IndexIDMap2_cast(index.inner_ptr());
31 if idmap_ptr.is_null() {
32 return Err(Error::invalid_cast(
33 "IndexIDMap2",
34 "index is not an IndexIDMap2",
35 ));
36 }
37 std::mem::forget(index);
38 Ok(Self {
39 inner: InnerPtr::new(idmap_ptr)?,
40 })
41 }
42 }
43
44 pub fn sub_index(&self) -> *mut FaissIndex {
45 unsafe { faiss_next_sys::faiss_IndexIDMap2_sub_index(self.inner.as_ptr()) }
46 }
47
48 pub fn construct_rev_map(&mut self) {
49 unsafe {
50 faiss_next_sys::faiss_IndexIDMap2_construct_rev_map(self.inner.as_ptr());
51 }
52 }
53}
54
55impl Index for IndexIDMap2 {
56 fn inner_ptr(&self) -> *mut FaissIndex {
57 self.inner.as_ptr() as *mut FaissIndex
58 }
59}
60
61impl Drop for IndexIDMap2 {
62 fn drop(&mut self) {
63 tracing::trace!("dropping IndexIDMap2");
64 unsafe {
65 faiss_next_sys::faiss_Index_free(self.inner.as_ptr() as *mut FaissIndex);
66 }
67 }
68}
69
70unsafe impl Send for IndexIDMap2 {}
71unsafe impl Sync for IndexIDMap2 {}