diff --git a/cpp/DBHostObject.cpp b/cpp/DBHostObject.cpp index d95f285..991ed89 100644 --- a/cpp/DBHostObject.cpp +++ b/cpp/DBHostObject.cpp @@ -161,13 +161,12 @@ DBHostObject::DBHostObject(jsi::Runtime &rt, DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path, std::shared_ptr invoker, - std::shared_ptr thread_pool, std::string &db_name, std::string &path, std::string &crsqlite_path, std::string &sqlite_vec_path, std::string &encryption_key) - : base_path(base_path), invoker(std::move(invoker)), - thread_pool(std::move(thread_pool)), db_name(db_name), rt(rt) { + : base_path(base_path), invoker(std::move(invoker)), db_name(db_name), rt(rt) { + _thread_pool = std::make_shared(); #ifdef OP_SQLITE_USE_SQLCIPHER BridgeResult result = opsqlite_open(db_name, path, crsqlite_path, @@ -331,7 +330,7 @@ void DBHostObject::create_jsi_functions() { } }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); @@ -410,7 +409,7 @@ void DBHostObject::create_jsi_functions() { } }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); @@ -478,7 +477,7 @@ void DBHostObject::create_jsi_functions() { } }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); @@ -553,7 +552,7 @@ void DBHostObject::create_jsi_functions() { }); } }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); @@ -615,7 +614,7 @@ void DBHostObject::create_jsi_functions() { }); } }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); @@ -775,7 +774,7 @@ void DBHostObject::create_jsi_functions() { #endif auto preparedStatementHostObject = std::make_shared(db, db_name, statement, - invoker, thread_pool); + invoker, _thread_pool); return jsi::Object::createFromHostObject(rt, preparedStatementHostObject); }); @@ -813,7 +812,7 @@ void DBHostObject::create_jsi_functions() { flush_pending_reactive_queries(resolve); }; - thread_pool->queueWork(task); + _thread_pool->queueWork(task); return {}; })); diff --git a/cpp/DBHostObject.h b/cpp/DBHostObject.h index 28c4afd..9940beb 100644 --- a/cpp/DBHostObject.h +++ b/cpp/DBHostObject.h @@ -36,9 +36,9 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject { // Constructor for local databases DBHostObject(jsi::Runtime &rt, std::string &base_path, std::shared_ptr invoker, - std::shared_ptr thread_pool, std::string &db_name, - std::string &path, std::string &crsqlite_path, - std::string &sqlite_vec_path, std::string &encryption_key); + std::string &db_name, std::string &path, + std::string &crsqlite_path, std::string &sqlite_vec_path, + std::string &encryption_key); #ifdef OP_SQLITE_USE_LIBSQL // Constructor for remoteOpen, purely for remote databases @@ -48,9 +48,8 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject { // Constructor for a local database with remote sync DBHostObject(jsi::Runtime &rt, std::shared_ptr invoker, - std::shared_ptr thread_pool, std::string &db_name, - std::string &path, std::string &url, std::string &auth_token, - int sync_interval); + std::string &db_name, std::string &path, std::string &url, + std::string &auth_token, int sync_interval); #endif std::vector getPropertyNames(jsi::Runtime &rt); @@ -70,7 +69,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject { std::string base_path; std::shared_ptr update_hook; std::shared_ptr invoker; - std::shared_ptr thread_pool; + std::shared_ptr _thread_pool; std::string db_name; std::shared_ptr update_hook_callback; std::shared_ptr commit_hook_callback; @@ -80,7 +79,12 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject { std::vector pending_reactive_invocations; bool is_update_hook_registered = false; bool invalidated = false; +#ifdef OP_SQLITE_USE_LIBSQL + libsql_database_t db; + libsql_connection_t c; +#else sqlite3 *db; +#endif }; } // namespace opsqlite diff --git a/cpp/ThreadPool.cpp b/cpp/ThreadPool.cpp index 40838d4..e1103da 100644 --- a/cpp/ThreadPool.cpp +++ b/cpp/ThreadPool.cpp @@ -6,11 +6,12 @@ ThreadPool::ThreadPool() : done(false) { // This returns the number of threads supported by the system. If the // function can't figure out this information, it returns 0. 0 is not good, // so we create at least 1 - auto numberOfThreads = std::thread::hardware_concurrency(); - if (numberOfThreads == 0) { - numberOfThreads = 1; - } +// auto numberOfThreads = std::thread::hardware_concurrency(); +// if (numberOfThreads == 0) { +// numberOfThreads = 1; +// } + auto numberOfThreads = 1; for (unsigned i = 0; i < numberOfThreads; ++i) { // The threads will execute the private member `doWork`. Note that we need // to pass a reference to the function (namespaced with the class name) as diff --git a/cpp/bindings.cpp b/cpp/bindings.cpp index 4bf4330..85696ea 100644 --- a/cpp/bindings.cpp +++ b/cpp/bindings.cpp @@ -22,7 +22,6 @@ namespace jsi = facebook::jsi; std::string _base_path; std::string _crsqlite_path; std::string _sqlite_vec_path; -std::shared_ptr thread_pool = std::make_shared(); std::vector> dbs; // React native will try to clean the module on JS context invalidation @@ -37,7 +36,7 @@ void clearState() { invalidated = true; // We then join all the threads before the context gets invalidated - thread_pool->restartPool(); +// thread_pool->restartPool(); } void install(jsi::Runtime &rt, @@ -83,7 +82,7 @@ void install(jsi::Runtime &rt, } std::shared_ptr db = std::make_shared( - rt, path, invoker, thread_pool, name, path, _crsqlite_path, + rt, path, invoker, name, path, _crsqlite_path, _sqlite_vec_path, encryptionKey); dbs.emplace_back(db); return jsi::Object::createFromHostObject(rt, db); @@ -146,7 +145,7 @@ void install(jsi::Runtime &rt, } std::shared_ptr db = std::make_shared( - rt, invoker, thread_pool, name, path, url, auth_token, sync_interval); + rt, invoker, name, path, url, auth_token, sync_interval); return jsi::Object::createFromHostObject(rt, db); }); #endif diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index 9b6c5d6..a11bfd4 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -94,7 +94,7 @@ sqlite3 *opsqlite_open(std::string const &name, std::string const &path, } #ifdef OP_SQLITE_USE_SQLCIPHER - opsqlite_execute(name, "PRAGMA key = '" + encryption_key + "'", nullptr); + opsqlite_execute(db, "PRAGMA key = '" + encryption_key + "'", nullptr); #endif sqlite3_enable_load_extension(db, 1);