diff --git a/bindings/java/rs_src/limbo_connection.rs b/bindings/java/rs_src/limbo_connection.rs index 5441acee5..dd54e9087 100644 --- a/bindings/java/rs_src/limbo_connection.rs +++ b/bindings/java/rs_src/limbo_connection.rs @@ -9,16 +9,19 @@ use jni::sys::jlong; use jni::JNIEnv; use limbo_core::Connection; use std::rc::Rc; +use std::sync::Arc; #[derive(Clone)] #[allow(dead_code)] pub struct LimboConnection { + // Because java's LimboConnection is 1:1 mapped to limbo connection, we can use Rc pub(crate) conn: Rc, - pub(crate) io: Rc, + // Because io is shared across multiple `LimboConnection`s, wrap it with Arc + pub(crate) io: Arc, } impl LimboConnection { - pub fn new(conn: Rc, io: Rc) -> Self { + pub fn new(conn: Rc, io: Arc) -> Self { LimboConnection { conn, io } } diff --git a/bindings/java/rs_src/limbo_db.rs b/bindings/java/rs_src/limbo_db.rs index 09d8afa75..16cb3d66b 100644 --- a/bindings/java/rs_src/limbo_db.rs +++ b/bindings/java/rs_src/limbo_db.rs @@ -5,16 +5,16 @@ use jni::objects::{JByteArray, JObject}; use jni::sys::{jint, jlong}; use jni::JNIEnv; use limbo_core::Database; -use std::rc::Rc; use std::sync::Arc; struct LimboDB { db: Arc, + io: Arc, } impl LimboDB { - pub fn new(db: Arc) -> Self { - LimboDB { db } + pub fn new(db: Arc, io: Arc) -> Self { + LimboDB { db, io } } pub fn to_ptr(self) -> jlong { @@ -76,14 +76,13 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_openUtf8<'loca } }; - LimboDB::new(db).to_ptr() + LimboDB::new(db, io).to_ptr() } #[no_mangle] pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'local>( mut env: JNIEnv<'local>, obj: JObject<'local>, - file_path_byte_arr: JByteArray<'local>, db_pointer: jlong, ) -> jlong { let db = match to_limbo_db(db_pointer) { @@ -94,41 +93,7 @@ pub extern "system" fn Java_org_github_tursodatabase_core_LimboDB_connect0<'loca } }; - let path = match env - .convert_byte_array(file_path_byte_arr) - .map_err(|e| e.to_string()) - { - Ok(bytes) => match String::from_utf8(bytes) { - Ok(s) => s, - Err(e) => { - set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string()); - return 0; - } - }, - Err(e) => { - set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string()); - return 0; - } - }; - - let io: Rc = match path.as_str() { - ":memory:" => match limbo_core::MemoryIO::new() { - Ok(io) => Rc::new(io), - Err(e) => { - set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string()); - return 0; - } - }, - _ => match limbo_core::PlatformIO::new() { - Ok(io) => Rc::new(io), - Err(e) => { - set_err_msg_and_throw_exception(&mut env, obj, LIMBO_ETC, e.to_string()); - return 0; - } - }, - }; - let conn = LimboConnection::new(db.db.connect(), io); - + let conn = LimboConnection::new(db.db.connect(), db.io.clone()); conn.to_ptr() } diff --git a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java index 89d13b8cf..ad6ee68a0 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/core/LimboDB.java @@ -1,5 +1,9 @@ package org.github.tursodatabase.core; +import static org.github.tursodatabase.utils.ByteArrayUtils.stringToUtf8ByteArray; + +import java.sql.SQLException; +import java.util.concurrent.locks.ReentrantLock; import org.github.tursodatabase.LimboErrorCode; import org.github.tursodatabase.annotations.NativeInvocation; @@ -8,12 +12,6 @@ import org.github.tursodatabase.utils.LimboExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; -import java.util.concurrent.locks.ReentrantLock; - -import static org.github.tursodatabase.utils.ByteArrayUtils.stringToUtf8ByteArray; - /** * This class provides a thin JNI layer over the SQLite3 C API. */ @@ -39,7 +37,7 @@ public final class LimboDB extends AbstractDB { * Loads the SQLite interface backend. */ public static void load() { - if (isLoaded) return; + if (isLoaded) {return;} try { System.loadLibrary("_limbo_java"); @@ -49,7 +47,7 @@ public final class LimboDB extends AbstractDB { } /** - * @param url e.g. "jdbc:sqlite:fileName + * @param url e.g. "jdbc:sqlite:fileName * @param filePath e.g. path to file */ public static LimboDB create(String url, String filePath) throws SQLException { @@ -86,7 +84,9 @@ public final class LimboDB extends AbstractDB { byte[] filePathBytes = stringToUtf8ByteArray(filePath); if (filePathBytes == null) { - throw LimboExceptionUtils.buildLimboException(LimboErrorCode.LIMBO_ETC.code, "File path cannot be converted to byteArray. File name: " + filePath); + throw LimboExceptionUtils.buildLimboException( + LimboErrorCode.LIMBO_ETC.code, + "File path cannot be converted to byteArray. File name: " + filePath); } dbPointer = openUtf8(filePathBytes, openFlags); @@ -95,14 +95,10 @@ public final class LimboDB extends AbstractDB { @Override public long connect() throws SQLException { - byte[] filePathBytes = stringToUtf8ByteArray(filePath); - if (filePathBytes == null) { - throw LimboExceptionUtils.buildLimboException(LimboErrorCode.LIMBO_ETC.code, "File path cannot be converted to byteArray. File name: " + filePath); - } - return connect0(filePathBytes, dbPointer); + return connect0(dbPointer); } - private native long connect0(byte[] path, long databasePtr) throws SQLException; + private native long connect0(long databasePtr) throws SQLException; @VisibleForTesting native void throwJavaException(int errorCode) throws SQLException; @@ -110,7 +106,7 @@ public final class LimboDB extends AbstractDB { /** * Throws formatted SQLException with error code and message. * - * @param errorCode Error code. + * @param errorCode Error code. * @param errorMessageBytes Error message. */ @NativeInvocation(invokedFrom = "limbo_db.rs")