| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use std::sync::Mutex; |
| 16 | |
| 17 | use lancedb::{ |
| 18 | index::{scalar::BTreeIndexBuilder, vector::IvfPqIndexBuilder, Index as LanceDbIndex}, |
| 19 | DistanceType, |
| 20 | }; |
| 21 | use pyo3::{ |
| 22 | exceptions::{PyRuntimeError, PyValueError}, |
| 23 | pyclass, pymethods, PyResult, |
| 24 | }; |
| 25 | |
| 26 | #[pyclass] |
| 27 | pub struct Index { |
| 28 | inner: Mutex<Option<LanceDbIndex>>, |
| 29 | } |
| 30 | |
| 31 | impl Index { |
| 32 | pub fn consume(&self) -> PyResult<LanceDbIndex> { |
| 33 | self.inner |
| 34 | .lock() |
| 35 | .unwrap() |
| 36 | .take() |
| 37 | .ok_or_else(|| PyRuntimeError::new_err("cannot use an Index more than once")) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[pymethods] |
| 42 | impl Index { |
| 43 | #[staticmethod] |
| 44 | pub fn ivf_pq( |
| 45 | distance_type: Option<String>, |
| 46 | num_partitions: Option<u32>, |
| 47 | num_sub_vectors: Option<u32>, |
| 48 | max_iterations: Option<u32>, |
| 49 | sample_rate: Option<u32>, |
| 50 | ) -> PyResult<Self> { |
| 51 | let mut ivf_pq_builder = IvfPqIndexBuilder::default(); |
| 52 | if let Some(distance_type) = distance_type { |
| 53 | let distance_type = match distance_type.as_str() { |
| 54 | "l2" => Ok(DistanceType::L2), |
| 55 | "cosine" => Ok(DistanceType::Cosine), |
| 56 | "dot" => Ok(DistanceType::Dot), |
| 57 | _ => Err(PyValueError::new_err(format!( |
| 58 | "Invalid distance type '{}'. Must be one of l2, cosine, or dot", |
| 59 | distance_type |
| 60 | ))), |
| 61 | }?; |
| 62 | ivf_pq_builder = ivf_pq_builder.distance_type(distance_type); |
| 63 | } |
| 64 | if let Some(num_partitions) = num_partitions { |
| 65 | ivf_pq_builder = ivf_pq_builder.num_partitions(num_partitions); |
| 66 | } |
| 67 | if let Some(num_sub_vectors) = num_sub_vectors { |
| 68 | ivf_pq_builder = ivf_pq_builder.num_sub_vectors(num_sub_vectors); |
| 69 | } |
| 70 | if let Some(max_iterations) = max_iterations { |
| 71 | ivf_pq_builder = ivf_pq_builder.max_iterations(max_iterations); |
| 72 | } |
| 73 | if let Some(sample_rate) = sample_rate { |
| 74 | ivf_pq_builder = ivf_pq_builder.sample_rate(sample_rate); |
| 75 | } |
| 76 | Ok(Self { |
| 77 | inner: Mutex::new(Some(LanceDbIndex::IvfPq(ivf_pq_builder))), |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | #[staticmethod] |
| 82 | pub fn btree() -> PyResult<Self> { |
| 83 | Ok(Self { |
| 84 | inner: Mutex::new(Some(LanceDbIndex::BTree(BTreeIndexBuilder::default()))), |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | #[staticmethod] |
| 89 | pub fn bitmap() -> PyResult<Self> { |
| 90 | Ok(Self { |
| 91 | inner: Mutex::new(Some(LanceDbIndex::Bitmap(Default::default()))), |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | #[staticmethod] |
| 96 | pub fn label_list() -> PyResult<Self> { |
| 97 | Ok(Self { |
| 98 | inner: Mutex::new(Some(LanceDbIndex::LabelList(Default::default()))), |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | #[staticmethod] |
| 103 | pub fn fts() -> PyResult<Self> { |
| 104 | Ok(Self { |
| 105 | inner: Mutex::new(Some(LanceDbIndex::FTS(Default::default()))), |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[pyclass(get_all)] |
| 111 | /// A description of an index currently configured on a column |
| 112 | pub struct IndexConfig { |
| 113 | /// The type of the index |
| 114 | pub index_type: String, |
| 115 | /// The columns in the index |
| 116 | /// |
| 117 | /// Currently this is always a list of size 1. In the future there may |
| 118 | /// be more columns to represent composite indices. |
| 119 | pub columns: Vec<String>, |
| 120 | } |
| 121 | |
| 122 | #[pymethods] |
| 123 | impl IndexConfig { |
| 124 | pub fn __repr__(&self) -> String { |
| 125 | format!("Index({}, columns={:?})", self.index_type, self.columns) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl From<lancedb::index::IndexConfig> for IndexConfig { |
| 130 | fn from(value: lancedb::index::IndexConfig) -> Self { |
| 131 | let index_type = format!("{:?}", value.index_type); |
| 132 | Self { |
| 133 | index_type, |
| 134 | columns: value.columns, |
| 135 | } |
| 136 | } |
| 137 | } |