From f74c68fc297e333d5567546945aa486a3f9d26a3 Mon Sep 17 00:00:00 2001 From: Michel Lang Date: Tue, 31 Oct 2023 18:31:17 +0100 Subject: [PATCH] move finalizers to private --- DESCRIPTION | 2 +- R/DataBackendDplyr.R | 20 ++++++++++---------- R/DataBackendDuckDB.R | 20 ++++++++++---------- man/DataBackendDplyr.Rd | 15 --------------- man/DataBackendDuckDB.Rd | 15 --------------- tests/testthat/helper.R | 2 +- 6 files changed, 22 insertions(+), 52 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3b59d1a..dac75d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,7 +21,7 @@ Depends: mlr3 (>= 0.13.0), R (>= 3.1.0) Imports: - R6, + R6 (>= 2.4.0), backports, checkmate, data.table, diff --git a/R/DataBackendDplyr.R b/R/DataBackendDplyr.R index 03a21ce..d7bc52d 100644 --- a/R/DataBackendDplyr.R +++ b/R/DataBackendDplyr.R @@ -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()]. @@ -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) diff --git a/R/DataBackendDuckDB.R b/R/DataBackendDuckDB.R index 03d0dd3..a043a6f 100644 --- a/R/DataBackendDuckDB.R +++ b/R/DataBackendDuckDB.R @@ -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. #' @@ -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) diff --git a/man/DataBackendDplyr.Rd b/man/DataBackendDplyr.Rd index f972d15..5db4c26 100644 --- a/man/DataBackendDplyr.Rd +++ b/man/DataBackendDplyr.Rd @@ -94,7 +94,6 @@ Returns the result of \code{\link[DBI:dbIsValid]{DBI::dbIsValid()}} otherwise.} \subsection{Public methods}{ \itemize{ \item \href{#method-DataBackendDplyr-new}{\code{DataBackendDplyr$new()}} -\item \href{#method-DataBackendDplyr-finalize}{\code{DataBackendDplyr$finalize()}} \item \href{#method-DataBackendDplyr-data}{\code{DataBackendDplyr$data()}} \item \href{#method-DataBackendDplyr-head}{\code{DataBackendDplyr$head()}} \item \href{#method-DataBackendDplyr-distinct}{\code{DataBackendDplyr$distinct()}} @@ -160,20 +159,6 @@ To protect your credentials, it is recommended to use the \CRANpkg{secret} packa } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-DataBackendDplyr-finalize}{}}} -\subsection{Method \code{finalize()}}{ -Finalizer which disconnects from the database. -This is called during garbage collection of the instance. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{DataBackendDplyr$finalize()}\if{html}{\out{
}} -} - -\subsection{Returns}{ -\code{logical(1)}, the return value of \code{\link[DBI:dbDisconnect]{DBI::dbDisconnect()}}. -} -} -\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-DataBackendDplyr-data}{}}} \subsection{Method \code{data()}}{ diff --git a/man/DataBackendDuckDB.Rd b/man/DataBackendDuckDB.Rd index 98c32de..bee586b 100644 --- a/man/DataBackendDuckDB.Rd +++ b/man/DataBackendDuckDB.Rd @@ -56,7 +56,6 @@ Returns the result of \code{\link[DBI:dbIsValid]{DBI::dbIsValid()}} otherwise.} \subsection{Public methods}{ \itemize{ \item \href{#method-DataBackendDuckDB-new}{\code{DataBackendDuckDB$new()}} -\item \href{#method-DataBackendDuckDB-finalize}{\code{DataBackendDuckDB$finalize()}} \item \href{#method-DataBackendDuckDB-data}{\code{DataBackendDuckDB$data()}} \item \href{#method-DataBackendDuckDB-head}{\code{DataBackendDuckDB$head()}} \item \href{#method-DataBackendDuckDB-distinct}{\code{DataBackendDuckDB$distinct()}} @@ -121,20 +120,6 @@ To protect your credentials, it is recommended to use the \CRANpkg{secret} packa } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-DataBackendDuckDB-finalize}{}}} -\subsection{Method \code{finalize()}}{ -Finalizer which disconnects from the database. -This is called during garbage collection of the instance. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{DataBackendDuckDB$finalize()}\if{html}{\out{
}} -} - -\subsection{Returns}{ -\code{logical(1)}, the return value of \code{\link[DBI:dbDisconnect]{DBI::dbDisconnect()}}. -} -} -\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-DataBackendDuckDB-data}{}}} \subsection{Method \code{data()}}{ diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 726f67e..bf387cd 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -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)