| 1 | use crate::ffi::JNIEnvExt; |
| 2 | use crate::traits::IntoJava; |
| 3 | use crate::{Error, RT}; |
| 4 | use jni::objects::{JObject, JString, JValue}; |
| 5 | use jni::JNIEnv; |
| 6 | pub const NATIVE_CONNECTION: &str = "nativeConnectionHandle"; |
| 7 | use crate::Result; |
| 8 | use lancedb::connection::{connect, Connection}; |
| 9 | |
| 10 | #[derive(Clone)] |
| 11 | pub struct BlockingConnection { |
| 12 | pub(crate) inner: Connection, |
| 13 | } |
| 14 | |
| 15 | impl BlockingConnection { |
| 16 | pub fn create(dataset_uri: &str) -> Result<Self> { |
| 17 | let inner = RT.block_on(connect(dataset_uri).execute())?; |
| 18 | Ok(Self { inner }) |
| 19 | } |
| 20 | |
| 21 | pub fn table_names( |
| 22 | &self, |
| 23 | start_after: Option<String>, |
| 24 | limit: Option<i32>, |
| 25 | ) -> Result<Vec<String>> { |
| 26 | let mut op = self.inner.table_names(); |
| 27 | if let Some(start_after) = start_after { |
| 28 | op = op.start_after(start_after); |
| 29 | } |
| 30 | if let Some(limit) = limit { |
| 31 | op = op.limit(limit as u32); |
| 32 | } |
| 33 | Ok(RT.block_on(op.execute())?) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl IntoJava for BlockingConnection { |
| 38 | fn into_java<'a>(self, env: &mut JNIEnv<'a>) -> JObject<'a> { |
| 39 | attach_native_connection(env, self) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn attach_native_connection<'local>( |
| 44 | env: &mut JNIEnv<'local>, |
| 45 | connection: BlockingConnection, |
| 46 | ) -> JObject<'local> { |
| 47 | let j_connection = create_java_connection_object(env); |
| 48 | // This block sets a native Rust object (Connection) as a field in the Java object (j_Connection). |
| 49 | // Caution: This creates a potential for memory leaks. The Rust object (Connection) is not |
| 50 | // automatically garbage-collected by Java, and its memory will not be freed unless |
| 51 | // explicitly handled. |
| 52 | // |
| 53 | // To prevent memory leaks, ensure the following: |
| 54 | // 1. The Java object (`j_Connection`) should implement the `java.io.Closeable` interface. |
| 55 | // 2. Users of this Java object should be instructed to always use it within a try-with-resources |
| 56 | // statement (or manually call the `close()` method) to ensure that `self.close()` is invoked. |
| 57 | match unsafe { env.set_rust_field(&j_connection, NATIVE_CONNECTION, connection) } { |
| 58 | Ok(_) => j_connection, |
| 59 | Err(err) => { |
| 60 | env.throw_new( |
| 61 | "java/lang/RuntimeException", |
| 62 | format!("Failed to set native handle for Connection: {}", err), |
| 63 | ) |
| 64 | .expect("Error throwing exception"); |
| 65 | JObject::null() |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn create_java_connection_object<'a>(env: &mut JNIEnv<'a>) -> JObject<'a> { |
| 71 | env.new_object("com/lancedb/lancedb/Connection", "()V", &[]) |
| 72 | .expect("Failed to create Java Lance Connection instance") |
| 73 | } |
| 74 | |
| 75 | #[no_mangle] |
| 76 | pub extern "system" fn Java_com_lancedb_lancedb_Connection_releaseNativeConnection( |
| 77 | mut env: JNIEnv, |
| 78 | j_connection: JObject, |
| 79 | ) { |
| 80 | let _: BlockingConnection = unsafe { |
| 81 | env.take_rust_field(j_connection, NATIVE_CONNECTION) |
| 82 | .expect("Failed to take native Connection handle") |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | #[no_mangle] |
| 87 | pub extern "system" fn Java_com_lancedb_lancedb_Connection_connect<'local>( |
| 88 | mut env: JNIEnv<'local>, |
| 89 | _obj: JObject, |
| 90 | dataset_uri_object: JString, |
| 91 | ) -> JObject<'local> { |
| 92 | let dataset_uri: String = ok_or_throw!(env, env.get_string(&dataset_uri_object)).into(); |
| 93 | let blocking_connection = ok_or_throw!(env, BlockingConnection::create(&dataset_uri)); |
| 94 | blocking_connection.into_java(&mut env) |
| 95 | } |
| 96 | |
| 97 | #[no_mangle] |
| 98 | pub extern "system" fn Java_com_lancedb_lancedb_Connection_tableNames<'local>( |
| 99 | mut env: JNIEnv<'local>, |
| 100 | j_connection: JObject, |
| 101 | start_after_obj: JObject, // Optional<String> |
| 102 | limit_obj: JObject, // Optional<Integer> |
| 103 | ) -> JObject<'local> { |
| 104 | ok_or_throw!( |
| 105 | env, |
| 106 | inner_table_names(&mut env, j_connection, start_after_obj, limit_obj) |
| 107 | ) |
| 108 | } |
| 109 | |
| 110 | fn inner_table_names<'local>( |
| 111 | env: &mut JNIEnv<'local>, |
| 112 | j_connection: JObject, |
| 113 | start_after_obj: JObject, // Optional<String> |
| 114 | limit_obj: JObject, // Optional<Integer> |
| 115 | ) -> Result<JObject<'local>> { |
| 116 | let start_after = env.get_string_opt(&start_after_obj)?; |
| 117 | let limit = env.get_int_opt(&limit_obj)?; |
| 118 | let conn = |
| 119 | unsafe { env.get_rust_field::<_, _, BlockingConnection>(j_connection, NATIVE_CONNECTION) }?; |
| 120 | let table_names = conn.table_names(start_after, limit)?; |
| 121 | drop(conn); |
| 122 | let j_names = env.new_object("java/util/ArrayList", "()V", &[])?; |
| 123 | for item in table_names { |
| 124 | let jstr_item = env.new_string(item)?; |
| 125 | let item_jobj = JObject::from(jstr_item); |
| 126 | let item_gen = JValue::Object(&item_jobj); |
| 127 | env.call_method(&j_names, "add", "(Ljava/lang/Object;)Z", &[item_gen])?; |
| 128 | } |
| 129 | Ok(j_names) |
| 130 | } |