Skip to content

Commit

Permalink
Add a useful print method
Browse files Browse the repository at this point in the history
Fixes #140
  • Loading branch information
hadley committed Jan 6, 2023
1 parent c8a8673 commit 332eb68
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 26 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ exportMethods(onValidate)
exportMethods(poolCheckout)
exportMethods(poolClose)
exportMethods(poolReturn)
exportMethods(show)
exportMethods(sqlAppendTable)
exportMethods(sqlCreateTable)
exportMethods(sqlData)
Expand Down
2 changes: 2 additions & 0 deletions R/DBI.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ NULL
#' @examples
#' # You use a dbPool in the same way as a standard DBI connection
#' pool <- dbPool(RSQLite::SQLite())
#' pool
#'
#' DBI::dbWriteTable(pool, "mtcars", mtcars)
#' dbGetQuery(pool, "SELECT * FROM mtcars LIMIT 4")
#'
Expand Down
11 changes: 0 additions & 11 deletions R/pool-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,6 @@ setMethod("poolClose", "Pool", function(pool) {
pool$close()
})

#' Show method
#' @param object A Pool object.
#' @export
setMethod("show", "Pool", function(object) {
pooledObj <- poolCheckout(object)
on.exit(poolReturn(pooledObj))
cat("<Pool>\n", " pooled object class: ",
is(pooledObj)[1], sep = "")
})


#' Return an object back to the pool
#'
#' Should be called by the end user if they previously fetched
Expand Down
16 changes: 16 additions & 0 deletions R/pool.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Pool <- R6::R6Class("Pool",
self$validationInterval <- validationInterval
self$state <- state

private$objClass <- NULL

private$freeObjects <- new.env(parent = emptyenv())

for (i in seq_len(self$minSize)) {
Expand Down Expand Up @@ -141,6 +143,15 @@ Pool <- R6::R6Class("Pool",
"- they will be destroyed the next time the ",
"garbage collector runs).", call. = FALSE)
}
},

print = function(...) {
cat("<Pool> of ", private$objClass, " objects\n", sep = "")
cat(" Objects checked out: ", self$counters$taken, "\n", sep = "")
cat(" Available in pool: ", self$counters$free, "\n", sep = "")
cat(" Max size: ", self$maxSize, "\n", sep = "")
cat(" Valid: ", self$valid, "\n", sep = "")
invisible(self)
}
),

Expand All @@ -149,6 +160,7 @@ Pool <- R6::R6Class("Pool",
freeObjects = NULL,
factory = NULL,
idCounter = NULL,
objClass = NULL,

## creates an object, assigns it to the
## free environment and returns it
Expand All @@ -164,6 +176,10 @@ Pool <- R6::R6Class("Pool",
"returns the object to be pooled.")
}

if (is.null(private$objClass)) {
private$objClass <- class(object)
}

## attach metadata about the object
pool_metadata <- new.env(parent = emptyenv())
attr(object, "pool_metadata") <- pool_metadata
Expand Down
2 changes: 2 additions & 0 deletions man/dbPool.Rd

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

14 changes: 0 additions & 14 deletions man/show-Pool-method.Rd

This file was deleted.

32 changes: 32 additions & 0 deletions tests/testthat/_snaps/pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# pool has useful print method

Code
pool
Output
<Pool> of numeric objects
Objects checked out: 0
Available in pool: 1
Max size: Inf
Valid: TRUE
Code
x1 <- poolCheckout(pool)
x2 <- poolCheckout(pool)
pool
Output
<Pool> of numeric objects
Objects checked out: 2
Available in pool: 0
Max size: Inf
Valid: TRUE
Code
poolReturn(x1)
pool
Output
<Pool> of numeric objects
Objects checked out: 1
Available in pool: 1
Max size: Inf
Valid: TRUE
Code
poolReturn(x2)

16 changes: 16 additions & 0 deletions tests/testthat/test-pool.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ describe("pool", {
})
})

test_that("pool has useful print method", {
pool <- poolCreate(function() 10)
on.exit(poolClose(pool))

expect_snapshot({
pool

x1 <- poolCheckout(pool)
x2 <- poolCheckout(pool)
pool

poolReturn(x1)
pool

poolReturn(x2)
})
})

0 comments on commit 332eb68

Please sign in to comment.