Skip to content

Commit

Permalink
Make each connection have it's own thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Dec 16, 2024
1 parent c7ed807 commit 0fa9f73
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
19 changes: 9 additions & 10 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ DBHostObject::DBHostObject(jsi::Runtime &rt,

DBHostObject::DBHostObject(jsi::Runtime &rt, std::string &base_path,
std::shared_ptr<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> 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<ThreadPool>();

#ifdef OP_SQLITE_USE_SQLCIPHER
BridgeResult result = opsqlite_open(db_name, path, crsqlite_path,
Expand Down Expand Up @@ -331,7 +330,7 @@ void DBHostObject::create_jsi_functions() {
}
};

thread_pool->queueWork(task);
_thread_pool->queueWork(task);

return {};
}));
Expand Down Expand Up @@ -410,7 +409,7 @@ void DBHostObject::create_jsi_functions() {
}
};

thread_pool->queueWork(task);
_thread_pool->queueWork(task);

return {};
}));
Expand Down Expand Up @@ -478,7 +477,7 @@ void DBHostObject::create_jsi_functions() {
}
};

thread_pool->queueWork(task);
_thread_pool->queueWork(task);

return {};
}));
Expand Down Expand Up @@ -553,7 +552,7 @@ void DBHostObject::create_jsi_functions() {
});
}
};
thread_pool->queueWork(task);
_thread_pool->queueWork(task);

return {};
}));
Expand Down Expand Up @@ -615,7 +614,7 @@ void DBHostObject::create_jsi_functions() {
});
}
};
thread_pool->queueWork(task);
_thread_pool->queueWork(task);
return {};
}));

Expand Down Expand Up @@ -775,7 +774,7 @@ void DBHostObject::create_jsi_functions() {
#endif
auto preparedStatementHostObject =
std::make_shared<PreparedStatementHostObject>(db, db_name, statement,
invoker, thread_pool);
invoker, _thread_pool);

return jsi::Object::createFromHostObject(rt, preparedStatementHostObject);
});
Expand Down Expand Up @@ -813,7 +812,7 @@ void DBHostObject::create_jsi_functions() {
flush_pending_reactive_queries(resolve);
};

thread_pool->queueWork(task);
_thread_pool->queueWork(task);

return {};
}));
Expand Down
18 changes: 11 additions & 7 deletions cpp/DBHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> 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
Expand All @@ -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<react::CallInvoker> invoker,
std::shared_ptr<ThreadPool> 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<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt);
Expand All @@ -70,7 +69,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
std::string base_path;
std::shared_ptr<jsi::Value> update_hook;
std::shared_ptr<react::CallInvoker> invoker;
std::shared_ptr<ThreadPool> thread_pool;
std::shared_ptr<ThreadPool> _thread_pool;
std::string db_name;
std::shared_ptr<jsi::Value> update_hook_callback;
std::shared_ptr<jsi::Value> commit_hook_callback;
Expand All @@ -80,7 +79,12 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
std::vector<PendingReactiveInvocation> 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
9 changes: 5 additions & 4 deletions cpp/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace jsi = facebook::jsi;
std::string _base_path;
std::string _crsqlite_path;
std::string _sqlite_vec_path;
std::shared_ptr<ThreadPool> thread_pool = std::make_shared<ThreadPool>();
std::vector<std::shared_ptr<DBHostObject>> dbs;

// React native will try to clean the module on JS context invalidation
Expand All @@ -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,
Expand Down Expand Up @@ -83,7 +82,7 @@ void install(jsi::Runtime &rt,
}

std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
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);
Expand Down Expand Up @@ -146,7 +145,7 @@ void install(jsi::Runtime &rt,
}

std::shared_ptr<DBHostObject> db = std::make_shared<DBHostObject>(
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
Expand Down
2 changes: 1 addition & 1 deletion cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0fa9f73

Please sign in to comment.