Skip to main content

faiss_next/index/
lsh.rs

1use std::ptr;
2
3use faiss_next_sys::{self, FaissIndex, FaissIndexLSH};
4
5use crate::error::{check_return_code, Error, Result};
6use crate::index::native::InnerPtr;
7use crate::index::traits::Index;
8
9pub struct IndexLSH {
10    inner: InnerPtr<FaissIndexLSH>,
11}
12
13impl IndexLSH {
14    pub fn new(d: u32, nbits: u32) -> Result<Self> {
15        unsafe {
16            let mut inner = ptr::null_mut();
17            check_return_code(faiss_next_sys::faiss_IndexLSH_new(
18                &mut inner,
19                d as i64,
20                nbits as i32,
21            ))?;
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 lsh_ptr = faiss_next_sys::faiss_IndexLSH_cast(index.inner_ptr());
31            if lsh_ptr.is_null() {
32                return Err(Error::invalid_cast("IndexLSH", "index is not an LSH index"));
33            }
34            std::mem::forget(index);
35            Ok(Self {
36                inner: InnerPtr::new(lsh_ptr)?,
37            })
38        }
39    }
40
41    pub fn nbits(&self) -> u32 {
42        unsafe { faiss_next_sys::faiss_IndexLSH_nbits(self.inner.as_ptr()) as u32 }
43    }
44
45    pub fn code_size(&self) -> u32 {
46        unsafe { faiss_next_sys::faiss_IndexLSH_code_size(self.inner.as_ptr()) as u32 }
47    }
48}
49
50impl Index for IndexLSH {
51    fn inner_ptr(&self) -> *mut FaissIndex {
52        self.inner.as_ptr() as *mut FaissIndex
53    }
54}
55
56impl Drop for IndexLSH {
57    fn drop(&mut self) {
58        tracing::trace!("dropping IndexLSH");
59        unsafe {
60            faiss_next_sys::faiss_IndexLSH_free(self.inner.as_ptr());
61        }
62    }
63}
64
65unsafe impl Send for IndexLSH {}
66unsafe impl Sync for IndexLSH {}