| 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 neon::prelude::*; |
| 16 | use once_cell::sync::OnceCell; |
| 17 | use tokio::runtime::Runtime; |
| 18 | |
| 19 | use lancedb::connect; |
| 20 | use lancedb::connection::Connection; |
| 21 | |
| 22 | use crate::error::ResultExt; |
| 23 | use crate::query::JsQuery; |
| 24 | use crate::table::JsTable; |
| 25 | |
| 26 | mod arrow; |
| 27 | mod convert; |
| 28 | mod error; |
| 29 | mod index; |
| 30 | mod neon_ext; |
| 31 | mod query; |
| 32 | mod table; |
| 33 | |
| 34 | struct JsDatabase { |
| 35 | database: Connection, |
| 36 | } |
| 37 | |
| 38 | impl Finalize for JsDatabase {} |
| 39 | |
| 40 | fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> { |
| 41 | static RUNTIME: OnceCell<Runtime> = OnceCell::new(); |
| 42 | static LOG: OnceCell<()> = OnceCell::new(); |
| 43 | |
| 44 | LOG.get_or_init(env_logger::init); |
| 45 | |
| 46 | RUNTIME.get_or_try_init(|| Runtime::new().or_throw(cx)) |
| 47 | } |
| 48 | |
| 49 | fn database_new(mut cx: FunctionContext) -> JsResult<JsPromise> { |
| 50 | let path = cx.argument::<JsString>(0)?.value(&mut cx); |
| 51 | let read_consistency_interval = cx |
| 52 | .argument_opt(2) |
| 53 | .and_then(|arg| arg.downcast::<JsNumber, _>(&mut cx).ok()) |
| 54 | .map(|v| v.value(&mut cx)) |
| 55 | .map(std::time::Duration::from_secs_f64); |
| 56 | |
| 57 | let storage_options_js = cx.argument::<JsArray>(1)?.to_vec(&mut cx)?; |
| 58 | let mut storage_options: Vec<(String, String)> = Vec::with_capacity(storage_options_js.len()); |
| 59 | for handle in storage_options_js { |
| 60 | let obj = handle.downcast::<JsArray, _>(&mut cx).unwrap(); |
| 61 | let key = obj.get::<JsString, _, _>(&mut cx, 0)?.value(&mut cx); |
| 62 | let value = obj.get::<JsString, _, _>(&mut cx, 1)?.value(&mut cx); |
| 63 | |
| 64 | storage_options.push((key, value)); |
| 65 | } |
| 66 | |
| 67 | let rt = runtime(&mut cx)?; |
| 68 | let channel = cx.channel(); |
| 69 | let (deferred, promise) = cx.promise(); |
| 70 | |
| 71 | let mut conn_builder = connect(&path).storage_options(storage_options); |
| 72 | |
| 73 | if let Some(interval) = read_consistency_interval { |
| 74 | conn_builder = conn_builder.read_consistency_interval(interval); |
| 75 | } |
| 76 | rt.spawn(async move { |
| 77 | let database = conn_builder.execute().await; |
| 78 | |
| 79 | deferred.settle_with(&channel, move |mut cx| { |
| 80 | let db = JsDatabase { |
| 81 | database: database.or_throw(&mut cx)?, |
| 82 | }; |
| 83 | Ok(cx.boxed(db)) |
| 84 | }); |
| 85 | }); |
| 86 | Ok(promise) |
| 87 | } |
| 88 | |
| 89 | fn database_table_names(mut cx: FunctionContext) -> JsResult<JsPromise> { |
| 90 | let db = cx |
| 91 | .this() |
| 92 | .downcast_or_throw::<JsBox<JsDatabase>, _>(&mut cx)?; |
| 93 | |
| 94 | let rt = runtime(&mut cx)?; |
| 95 | let (deferred, promise) = cx.promise(); |
| 96 | let channel = cx.channel(); |
| 97 | let database = db.database.clone(); |
| 98 | |
| 99 | rt.spawn(async move { |
| 100 | let tables_rst = database.table_names().execute().await; |
| 101 | |
| 102 | deferred.settle_with(&channel, move |mut cx| { |
| 103 | let tables = tables_rst.or_throw(&mut cx)?; |
| 104 | let table_names = convert::vec_str_to_array(&tables, &mut cx); |
| 105 | table_names |
| 106 | }); |
| 107 | }); |
| 108 | Ok(promise) |
| 109 | } |
| 110 | |
| 111 | fn database_open_table(mut cx: FunctionContext) -> JsResult<JsPromise> { |
| 112 | let db = cx |
| 113 | .this() |
| 114 | .downcast_or_throw::<JsBox<JsDatabase>, _>(&mut cx)?; |
| 115 | let table_name = cx.argument::<JsString>(0)?.value(&mut cx); |
| 116 | |
| 117 | let rt = runtime(&mut cx)?; |
| 118 | let channel = cx.channel(); |
| 119 | let database = db.database.clone(); |
| 120 | |
| 121 | let (deferred, promise) = cx.promise(); |
| 122 | rt.spawn(async move { |
| 123 | let table_rst = database.open_table(&table_name).execute().await; |
| 124 | |
| 125 | deferred.settle_with(&channel, move |mut cx| { |
| 126 | let js_table = JsTable::from(table_rst.or_throw(&mut cx)?); |
| 127 | Ok(cx.boxed(js_table)) |
| 128 | }); |
| 129 | }); |
| 130 | Ok(promise) |
| 131 | } |
| 132 | |
| 133 | fn database_drop_table(mut cx: FunctionContext) -> JsResult<JsPromise> { |
| 134 | let db = cx |
| 135 | .this() |
| 136 | .downcast_or_throw::<JsBox<JsDatabase>, _>(&mut cx)?; |
| 137 | let table_name = cx.argument::<JsString>(0)?.value(&mut cx); |
| 138 | |
| 139 | let rt = runtime(&mut cx)?; |
| 140 | let channel = cx.channel(); |
| 141 | let database = db.database.clone(); |
| 142 | |
| 143 | let (deferred, promise) = cx.promise(); |
| 144 | rt.spawn(async move { |
| 145 | let result = database.drop_table(&table_name).await; |
| 146 | deferred.settle_with(&channel, move |mut cx| { |
| 147 | result.or_throw(&mut cx)?; |
| 148 | Ok(cx.null()) |
| 149 | }); |
| 150 | }); |
| 151 | Ok(promise) |
| 152 | } |
| 153 | |
| 154 | #[neon::main] |
| 155 | fn main(mut cx: ModuleContext) -> NeonResult<()> { |
| 156 | cx.export_function("databaseNew", database_new)?; |
| 157 | cx.export_function("databaseTableNames", database_table_names)?; |
| 158 | cx.export_function("databaseOpenTable", database_open_table)?; |
| 159 | cx.export_function("databaseDropTable", database_drop_table)?; |
| 160 | cx.export_function("tableSearch", JsQuery::js_search)?; |
| 161 | cx.export_function("tableCreate", JsTable::js_create)?; |
| 162 | cx.export_function("tableAdd", JsTable::js_add)?; |
| 163 | cx.export_function("tableCountRows", JsTable::js_count_rows)?; |
| 164 | cx.export_function("tableDelete", JsTable::js_delete)?; |
| 165 | cx.export_function("tableUpdate", JsTable::js_update)?; |
| 166 | cx.export_function("tableMergeInsert", JsTable::js_merge_insert)?; |
| 167 | cx.export_function("tableCleanupOldVersions", JsTable::js_cleanup)?; |
| 168 | cx.export_function("tableCompactFiles", JsTable::js_compact)?; |
| 169 | cx.export_function("tableListIndices", JsTable::js_list_indices)?; |
| 170 | cx.export_function("tableIndexStats", JsTable::js_index_stats)?; |
| 171 | cx.export_function( |
| 172 | "tableCreateScalarIndex", |
| 173 | index::scalar::table_create_scalar_index, |
| 174 | )?; |
| 175 | cx.export_function( |
| 176 | "tableCreateVectorIndex", |
| 177 | index::vector::table_create_vector_index, |
| 178 | )?; |
| 179 | cx.export_function("tableSchema", JsTable::js_schema)?; |
| 180 | cx.export_function("tableAddColumns", JsTable::js_add_columns)?; |
| 181 | cx.export_function("tableAlterColumns", JsTable::js_alter_columns)?; |
| 182 | cx.export_function("tableDropColumns", JsTable::js_drop_columns)?; |
| 183 | Ok(()) |
| 184 | } |