| 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 | |
| 15 | use lancedb::index::{scalar::BTreeIndexBuilder, Index}; |
| 16 | use neon::{ |
| 17 | context::{Context, FunctionContext}, |
| 18 | result::JsResult, |
| 19 | types::{JsBoolean, JsBox, JsPromise, JsString}, |
| 20 | }; |
| 21 | |
| 22 | use crate::{error::ResultExt, runtime, table::JsTable}; |
| 23 | |
| 24 | pub fn table_create_scalar_index(mut cx: FunctionContext) -> JsResult<JsPromise> { |
| 25 | let js_table = cx.this().downcast_or_throw::<JsBox<JsTable>, _>(&mut cx)?; |
| 26 | let column = cx.argument::<JsString>(0)?.value(&mut cx); |
| 27 | let replace = cx.argument::<JsBoolean>(1)?.value(&mut cx); |
| 28 | |
| 29 | let rt = runtime(&mut cx)?; |
| 30 | |
| 31 | let (deferred, promise) = cx.promise(); |
| 32 | let channel = cx.channel(); |
| 33 | let table = js_table.table.clone(); |
| 34 | |
| 35 | rt.spawn(async move { |
| 36 | let idx_result = table |
| 37 | .create_index(&[column], Index::BTree(BTreeIndexBuilder::default())) |
| 38 | .replace(replace) |
| 39 | .execute() |
| 40 | .await; |
| 41 | |
| 42 | deferred.settle_with(&channel, move |mut cx| { |
| 43 | idx_result.or_throw(&mut cx)?; |
| 44 | Ok(cx.undefined()) |
| 45 | }); |
| 46 | }); |
| 47 | Ok(promise) |
| 48 | } |