Skip to content

Commit

Permalink
Avoid warning when using other dbdir
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Mar 24, 2024
1 parent 81bdeb6 commit 2cab803
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ rapi_lock <- function(dual) {
.Call(`_duckdb_rapi_lock`, dual)
}

rapi_unlock <- function(dual) {
invisible(.Call(`_duckdb_rapi_unlock`, dual))
}

rapi_is_locked <- function(dual) {
.Call(`_duckdb_rapi_is_locked`, dual)
}
Expand Down
1 change: 1 addition & 0 deletions R/dbConnect__duckdb_driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dbConnect__duckdb_driver <- function(

# aha, a late comer. let's make a new instance.
if (dbdir != drv@dbdir || !rapi_lock(drv@database_ref)) {
rapi_unlock(drv@database_ref)
drv <- duckdb(dbdir, read_only, bigint, config)
}

Expand Down
9 changes: 9 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ extern "C" SEXP _duckdb_rapi_lock(SEXP dual) {
END_CPP11
}
// database.cpp
void rapi_unlock(duckdb::db_eptr_t dual);
extern "C" SEXP _duckdb_rapi_unlock(SEXP dual) {
BEGIN_CPP11
rapi_unlock(cpp11::as_cpp<cpp11::decay_t<duckdb::db_eptr_t>>(dual));
return R_NilValue;
END_CPP11
}
// database.cpp
bool rapi_is_locked(duckdb::db_eptr_t dual);
extern "C" SEXP _duckdb_rapi_is_locked(SEXP dual) {
BEGIN_CPP11
Expand Down Expand Up @@ -455,6 +463,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_duckdb_rapi_release", (DL_FUNC) &_duckdb_rapi_release, 1},
{"_duckdb_rapi_shutdown", (DL_FUNC) &_duckdb_rapi_shutdown, 1},
{"_duckdb_rapi_startup", (DL_FUNC) &_duckdb_rapi_startup, 3},
{"_duckdb_rapi_unlock", (DL_FUNC) &_duckdb_rapi_unlock, 1},
{"_duckdb_rapi_unregister_arrow", (DL_FUNC) &_duckdb_rapi_unregister_arrow, 2},
{"_duckdb_rapi_unregister_df", (DL_FUNC) &_duckdb_rapi_unregister_df, 2},
{NULL, NULL, 0}
Expand Down
7 changes: 7 additions & 0 deletions src/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ static bool CastRstringToVarchar(Vector &source, Vector &result, idx_t count, Ca
return dual->has();
}

[[cpp11::register]] void rapi_unlock(duckdb::db_eptr_t dual) {
if (!dual || !dual.get()) {
cpp11::stop("rapi_unlock: Invalid database reference");
}
dual->unlock();
}

[[cpp11::register]] bool rapi_is_locked(duckdb::db_eptr_t dual) {
if (!dual || !dual.get()) {
cpp11::stop("rapi_is_locked: Invalid database reference");
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ test_that("no warning after dbDisconnect() for driver stored in variable", {
expect_warning(gc(), NA)
})

test_that("no warning on dbConnect() with other dbdir", {
con <- dbConnect(duckdb(), tempfile(fileext = ".duckdb"))
dbDisconnect(con)

# The warning won't occur here anyway, this is to keep testthat happy
expect_warning(gc(), NA)
})

test_that("can connect to the same in-memory database via the same driver object", {
drv <- duckdb()

Expand Down

0 comments on commit 2cab803

Please sign in to comment.