Elffuss BraidHub

kikocis/lancedb public

main · 6.0 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::{BTreeIndexBuilder, FtsIndexBuilder};
18use lancedb::index::vector::{IvfHnswPqIndexBuilder, IvfHnswSqIndexBuilder, IvfPqIndexBuilder};
19use lancedb::index::Index as LanceDbIndex;
20use napi_derive::napi;
21
22use crate::util::parse_distance_type;
23
24#[napi]
25pub struct Index {
26 inner: Mutex<Option<LanceDbIndex>>,
27}
28
29impl Index {
30 pub fn consume(&self) -> napi::Result<LanceDbIndex> {
31 self.inner
32 .lock()
33 .unwrap()
34 .take()
35 .ok_or(napi::Error::from_reason(
36 "attempt to use an index more than once",
37 ))
38 }
39}
40
41#[napi]
42impl Index {
43 #[napi(factory)]
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 ) -> napi::Result<Self> {
51 let mut ivf_pq_builder = IvfPqIndexBuilder::default();
52 if let Some(distance_type) = distance_type {
53 let distance_type = parse_distance_type(distance_type)?;
54 ivf_pq_builder = ivf_pq_builder.distance_type(distance_type);
55 }
56 if let Some(num_partitions) = num_partitions {
57 ivf_pq_builder = ivf_pq_builder.num_partitions(num_partitions);
58 }
59 if let Some(num_sub_vectors) = num_sub_vectors {
60 ivf_pq_builder = ivf_pq_builder.num_sub_vectors(num_sub_vectors);
61 }
62 if let Some(max_iterations) = max_iterations {
63 ivf_pq_builder = ivf_pq_builder.max_iterations(max_iterations);
64 }
65 if let Some(sample_rate) = sample_rate {
66 ivf_pq_builder = ivf_pq_builder.sample_rate(sample_rate);
67 }
68 Ok(Self {
69 inner: Mutex::new(Some(LanceDbIndex::IvfPq(ivf_pq_builder))),
70 })
71 }
72
73 #[napi(factory)]
74 pub fn btree() -> Self {
75 Self {
76 inner: Mutex::new(Some(LanceDbIndex::BTree(BTreeIndexBuilder::default()))),
77 }
78 }
79
80 #[napi(factory)]
81 pub fn bitmap() -> Self {
82 Self {
83 inner: Mutex::new(Some(LanceDbIndex::Bitmap(Default::default()))),
84 }
85 }
86
87 #[napi(factory)]
88 pub fn label_list() -> Self {
89 Self {
90 inner: Mutex::new(Some(LanceDbIndex::LabelList(Default::default()))),
91 }
92 }
93
94 #[napi(factory)]
95 pub fn fts(with_position: Option<bool>) -> Self {
96 let mut opts = FtsIndexBuilder::default();
97 if let Some(with_position) = with_position {
98 opts = opts.with_position(with_position);
99 }
100 Self {
101 inner: Mutex::new(Some(LanceDbIndex::FTS(opts))),
102 }
103 }
104
105 #[napi(factory)]
106 pub fn hnsw_pq(
107 distance_type: Option<String>,
108 num_partitions: Option<u32>,
109 num_sub_vectors: Option<u32>,
110 max_iterations: Option<u32>,
111 sample_rate: Option<u32>,
112 m: Option<u32>,
113 ef_construction: Option<u32>,
114 ) -> napi::Result<Self> {
115 let mut hnsw_pq_builder = IvfHnswPqIndexBuilder::default();
116 if let Some(distance_type) = distance_type {
117 let distance_type = parse_distance_type(distance_type)?;
118 hnsw_pq_builder = hnsw_pq_builder.distance_type(distance_type);
119 }
120 if let Some(num_partitions) = num_partitions {
121 hnsw_pq_builder = hnsw_pq_builder.num_partitions(num_partitions);
122 }
123 if let Some(num_sub_vectors) = num_sub_vectors {
124 hnsw_pq_builder = hnsw_pq_builder.num_sub_vectors(num_sub_vectors);
125 }
126 if let Some(max_iterations) = max_iterations {
127 hnsw_pq_builder = hnsw_pq_builder.max_iterations(max_iterations);
128 }
129 if let Some(sample_rate) = sample_rate {
130 hnsw_pq_builder = hnsw_pq_builder.sample_rate(sample_rate);
131 }
132 if let Some(m) = m {
133 hnsw_pq_builder = hnsw_pq_builder.num_edges(m);
134 }
135 if let Some(ef_construction) = ef_construction {
136 hnsw_pq_builder = hnsw_pq_builder.ef_construction(ef_construction);
137 }
138 Ok(Self {
139 inner: Mutex::new(Some(LanceDbIndex::IvfHnswPq(hnsw_pq_builder))),
140 })
141 }
142
143 #[napi(factory)]
144 pub fn hnsw_sq(
145 distance_type: Option<String>,
146 num_partitions: Option<u32>,
147 max_iterations: Option<u32>,
148 sample_rate: Option<u32>,
149 m: Option<u32>,
150 ef_construction: Option<u32>,
151 ) -> napi::Result<Self> {
152 let mut hnsw_sq_builder = IvfHnswSqIndexBuilder::default();
153 if let Some(distance_type) = distance_type {
154 let distance_type = parse_distance_type(distance_type)?;
155 hnsw_sq_builder = hnsw_sq_builder.distance_type(distance_type);
156 }
157 if let Some(num_partitions) = num_partitions {
158 hnsw_sq_builder = hnsw_sq_builder.num_partitions(num_partitions);
159 }
160 if let Some(max_iterations) = max_iterations {
161 hnsw_sq_builder = hnsw_sq_builder.max_iterations(max_iterations);
162 }
163 if let Some(sample_rate) = sample_rate {
164 hnsw_sq_builder = hnsw_sq_builder.sample_rate(sample_rate);
165 }
166 if let Some(m) = m {
167 hnsw_sq_builder = hnsw_sq_builder.num_edges(m);
168 }
169 if let Some(ef_construction) = ef_construction {
170 hnsw_sq_builder = hnsw_sq_builder.ef_construction(ef_construction);
171 }
172 Ok(Self {
173 inner: Mutex::new(Some(LanceDbIndex::IvfHnswSq(hnsw_sq_builder))),
174 })
175 }
176}