Elffuss BraidHub

kikocis/lancedb public

e67de45d10f7 · 4.3 KiBRawPermalink
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
15use std::sync::Mutex;
16
17use lancedb::index::scalar::FtsIndexBuilder;
18use lancedb::{
19 index::{scalar::BTreeIndexBuilder, vector::IvfPqIndexBuilder, Index as LanceDbIndex},
20 DistanceType,
21};
22use pyo3::{
23 exceptions::{PyRuntimeError, PyValueError},
24 pyclass, pymethods, PyResult,
25};
26
27#[pyclass]
28pub struct Index {
29 inner: Mutex<Option<LanceDbIndex>>,
30}
31
32impl Index {
33 pub fn consume(&self) -> PyResult<LanceDbIndex> {
34 self.inner
35 .lock()
36 .unwrap()
37 .take()
38 .ok_or_else(|| PyRuntimeError::new_err("cannot use an Index more than once"))
39 }
40}
41
42#[pymethods]
43impl Index {
44 #[staticmethod]
45 pub fn ivf_pq(
46 distance_type: Option<String>,
47 num_partitions: Option<u32>,
48 num_sub_vectors: Option<u32>,
49 max_iterations: Option<u32>,
50 sample_rate: Option<u32>,
51 ) -> PyResult<Self> {
52 let mut ivf_pq_builder = IvfPqIndexBuilder::default();
53 if let Some(distance_type) = distance_type {
54 let distance_type = match distance_type.as_str() {
55 "l2" => Ok(DistanceType::L2),
56 "cosine" => Ok(DistanceType::Cosine),
57 "dot" => Ok(DistanceType::Dot),
58 _ => Err(PyValueError::new_err(format!(
59 "Invalid distance type '{}'. Must be one of l2, cosine, or dot",
60 distance_type
61 ))),
62 }?;
63 ivf_pq_builder = ivf_pq_builder.distance_type(distance_type);
64 }
65 if let Some(num_partitions) = num_partitions {
66 ivf_pq_builder = ivf_pq_builder.num_partitions(num_partitions);
67 }
68 if let Some(num_sub_vectors) = num_sub_vectors {
69 ivf_pq_builder = ivf_pq_builder.num_sub_vectors(num_sub_vectors);
70 }
71 if let Some(max_iterations) = max_iterations {
72 ivf_pq_builder = ivf_pq_builder.max_iterations(max_iterations);
73 }
74 if let Some(sample_rate) = sample_rate {
75 ivf_pq_builder = ivf_pq_builder.sample_rate(sample_rate);
76 }
77 Ok(Self {
78 inner: Mutex::new(Some(LanceDbIndex::IvfPq(ivf_pq_builder))),
79 })
80 }
81
82 #[staticmethod]
83 pub fn btree() -> PyResult<Self> {
84 Ok(Self {
85 inner: Mutex::new(Some(LanceDbIndex::BTree(BTreeIndexBuilder::default()))),
86 })
87 }
88
89 #[staticmethod]
90 pub fn bitmap() -> PyResult<Self> {
91 Ok(Self {
92 inner: Mutex::new(Some(LanceDbIndex::Bitmap(Default::default()))),
93 })
94 }
95
96 #[staticmethod]
97 pub fn label_list() -> PyResult<Self> {
98 Ok(Self {
99 inner: Mutex::new(Some(LanceDbIndex::LabelList(Default::default()))),
100 })
101 }
102
103 #[staticmethod]
104 pub fn fts(with_position: Option<bool>) -> Self {
105 let mut opts = FtsIndexBuilder::default();
106 if let Some(with_position) = with_position {
107 opts = opts.with_position(with_position);
108 }
109 Self {
110 inner: Mutex::new(Some(LanceDbIndex::FTS(opts))),
111 }
112 }
113}
114
115#[pyclass(get_all)]
116/// A description of an index currently configured on a column
117pub struct IndexConfig {
118 /// The type of the index
119 pub index_type: String,
120 /// The columns in the index
121 ///
122 /// Currently this is always a list of size 1. In the future there may
123 /// be more columns to represent composite indices.
124 pub columns: Vec<String>,
125}
126
127#[pymethods]
128impl IndexConfig {
129 pub fn __repr__(&self) -> String {
130 format!("Index({}, columns={:?})", self.index_type, self.columns)
131 }
132}
133
134impl From<lancedb::index::IndexConfig> for IndexConfig {
135 fn from(value: lancedb::index::IndexConfig) -> Self {
136 let index_type = format!("{:?}", value.index_type);
137 Self {
138 index_type,
139 columns: value.columns,
140 }
141 }
142}