Skip to content

Commit

Permalink
move finalizers to private
Browse files Browse the repository at this point in the history
  • Loading branch information
mllg committed Oct 31, 2023
1 parent f0e0355 commit f74c68f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 52 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -21,7 +21,7 @@ Depends:
mlr3 (>= 0.13.0),
R (>= 3.1.0)
Imports:
R6,
R6 (>= 2.4.0),
backports,
checkmate,
data.table,
Expand Down
20 changes: 10 additions & 10 deletions R/DataBackendDplyr.R
Expand Up @@ -120,16 +120,6 @@ DataBackendDplyr = R6Class("DataBackendDplyr", inherit = DataBackend, cloneable
self$connector = assert_function(connector, args = character(), null.ok = TRUE)
},

#' @description
#' Finalizer which disconnects from the database.
#' This is called during garbage collection of the instance.
#' @return `logical(1)`, the return value of [DBI::dbDisconnect()].
finalize = function() {
if (isTRUE(self$valid)) {
DBI::dbDisconnect(private$.data$src$con)
}
},

#' @description
#' Returns a slice of the data.
#' Calls [dplyr::filter()] and [dplyr::select()] on the table and converts it to a [data.table::data.table()].
Expand Down Expand Up @@ -271,6 +261,16 @@ DataBackendDplyr = R6Class("DataBackendDplyr", inherit = DataBackend, cloneable
),

private = list(
# @description
# Finalizer which disconnects from the database.
# This is called during garbage collection of the instance.
# @return `logical(1)`, the return value of [DBI::dbDisconnect()].
finalize = function() {
if (isTRUE(self$valid)) {
DBI::dbDisconnect(private$.data$src$con)
}
},

.calculate_hash = function() {
private$.reconnect()
calculate_hash(private$.data)
Expand Down
20 changes: 10 additions & 10 deletions R/DataBackendDuckDB.R
Expand Up @@ -73,16 +73,6 @@ DataBackendDuckDB = R6Class("DataBackendDuckDB", inherit = DataBackend, cloneabl

},

#' @description
#' Finalizer which disconnects from the database.
#' This is called during garbage collection of the instance.
#' @return `logical(1)`, the return value of [DBI::dbDisconnect()].
finalize = function() {
if (isTRUE(self$valid)) {
DBI::dbDisconnect(private$.data, shutdown = TRUE)
}
},

#' @description
#' Returns a slice of the data.
#'
Expand Down Expand Up @@ -243,6 +233,16 @@ DataBackendDuckDB = R6Class("DataBackendDuckDB", inherit = DataBackend, cloneabl
),

private = list(
# @description
# Finalizer which disconnects from the database.
# This is called during garbage collection of the instance.
# @return `logical(1)`, the return value of [DBI::dbDisconnect()].
finalize = function() {
if (isTRUE(self$valid)) {
DBI::dbDisconnect(private$.data, shutdown = TRUE)
}
},

.calculate_hash = function() {
private$.reconnect()
calculate_hash(private$.data@driver@dbdir)
Expand Down
15 changes: 0 additions & 15 deletions man/DataBackendDplyr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions man/DataBackendDuckDB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Expand Up @@ -29,7 +29,7 @@ disconnect.SQLiteConnection = function(x) {
registerS3method("disconnect", "SQLiteConnection", disconnect.SQLiteConnection)

disconnect.DataBackend = function(x) {
x$finalize()
mlr3misc::get_private(x)$finalize()
}
registerS3method("disconnect", "DataBackend", disconnect.DataBackend)

Expand Down

0 comments on commit f74c68f

Please sign in to comment.