Elffuss BraidHub

kikocis/lancedb public

main · 3.1 KiBRawPermalink
1// Copyright 2023 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 lancedb::index::vector::IvfPqIndexBuilder;
16use lancedb::index::Index;
17use lancedb::DistanceType;
18use neon::context::FunctionContext;
19use neon::prelude::*;
20use std::convert::TryFrom;
21
22use crate::error::ResultExt;
23use crate::neon_ext::js_object_ext::JsObjectExt;
24use crate::runtime;
25use crate::table::JsTable;
26
27pub fn table_create_vector_index(mut cx: FunctionContext) -> JsResult<JsPromise> {
28 let js_table = cx.this().downcast_or_throw::<JsBox<JsTable>, _>(&mut cx)?;
29 let index_params = cx.argument::<JsObject>(0)?;
30
31 let rt = runtime(&mut cx)?;
32
33 let (deferred, promise) = cx.promise();
34 let channel = cx.channel();
35 let table = js_table.table.clone();
36
37 let column_name = index_params
38 .get_opt::<JsString, _, _>(&mut cx, "column")?
39 .map(|s| s.value(&mut cx))
40 .unwrap_or("vector".to_string()); // Backward compatibility
41
42 let replace = index_params
43 .get_opt::<JsBoolean, _, _>(&mut cx, "replace")?
44 .map(|r| r.value(&mut cx));
45
46 let tbl = table.clone();
47 let ivf_pq_builder = get_index_params_builder(&mut cx, index_params).or_throw(&mut cx)?;
48
49 let mut index_builder = tbl.create_index(&[column_name], Index::IvfPq(ivf_pq_builder));
50 if let Some(replace) = replace {
51 index_builder = index_builder.replace(replace);
52 }
53
54 rt.spawn(async move {
55 let idx_result = index_builder.execute().await;
56 deferred.settle_with(&channel, move |mut cx| {
57 idx_result.or_throw(&mut cx)?;
58 Ok(cx.boxed(JsTable::from(table)))
59 });
60 });
61 Ok(promise)
62}
63
64fn get_index_params_builder(
65 cx: &mut FunctionContext,
66 obj: Handle<JsObject>,
67) -> crate::error::Result<IvfPqIndexBuilder> {
68 if obj.get_opt::<JsString, _, _>(cx, "index_name")?.is_some() {
69 return Err(crate::error::Error::LanceDB {
70 message: "Setting the index_name is no longer supported".to_string(),
71 });
72 }
73 let mut builder = IvfPqIndexBuilder::default();
74 if let Some(metric_type) = obj.get_opt::<JsString, _, _>(cx, "metric_type")? {
75 let distance_type = DistanceType::try_from(metric_type.value(cx).as_str())?;
76 builder = builder.distance_type(distance_type);
77 }
78 if let Some(np) = obj.get_opt_u32(cx, "num_partitions")? {
79 builder = builder.num_partitions(np);
80 }
81 if let Some(ns) = obj.get_opt_u32(cx, "num_sub_vectors")? {
82 builder = builder.num_sub_vectors(ns);
83 }
84 if let Some(max_iters) = obj.get_opt_u32(cx, "max_iters")? {
85 builder = builder.max_iterations(max_iters);
86 }
87 Ok(builder)
88}