Skip to content

Commit

Permalink
replace 'memory' with 'lookup' in databases
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Nov 10, 2024
1 parent c5bd721 commit 4c7acf5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
31 changes: 17 additions & 14 deletions R/class_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ database_init <- function(
repository = tar_options$get_repository_meta(),
resources = tar_options$get_resources()
) {
memory <- memory_init()
lookup <- lookup_new()
key <- file.path(
resources[[repository]]$prefix %|||% path_store_default(),
subkey
)
switch(
repository,
local = database_local_new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand All @@ -30,7 +30,7 @@ database_init <- function(
resources = resources
),
aws = database_aws_new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand All @@ -42,7 +42,7 @@ database_init <- function(
resources = resources
),
gcp = database_gcp_new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand All @@ -67,7 +67,7 @@ database_class <- R6::R6Class(
portable = FALSE,
cloneable = FALSE,
public = list(
memory = NULL,
lookup = NULL,
path = NULL,
key = NULL,
header = NULL,
Expand All @@ -80,7 +80,7 @@ database_class <- R6::R6Class(
buffer = NULL,
staged = NULL,
initialize = function(
memory = NULL,
lookup = NULL,
path = NULL,
key = NULL,
header = NULL,
Expand All @@ -92,7 +92,7 @@ database_class <- R6::R6Class(
resources = NULL,
buffer = NULL
) {
self$memory <- memory
self$lookup <- lookup
self$path <- path
self$key <- key
self$header <- header
Expand All @@ -105,14 +105,17 @@ database_class <- R6::R6Class(
self$buffer <- buffer
},
get_row = function(name) {
memory_get_object(self$memory, name)
lookup_get(.subset2(self, "lookup"), name)
},
set_row = function(row) {
name <- as.character(row$name)
memory_set_object(self$memory, name = name, object = as.list(row))
lookup_set(
.subset2(self, "lookup"),
name = as.character(.subset2(row, "name")),
value = as.list(row)
)
},
del_rows = function(names) {
memory_del_objects(self$memory, names)
lookup_remove(.subset2(self, "lookup"), names)
},
get_data = function() {
rows <- self$list_rows()
Expand All @@ -136,10 +139,10 @@ database_class <- R6::R6Class(
map(seq_along(list$name), ~self$set_row(lapply(list, `[[`, i = .x)))
},
exists_row = function(name) {
memory_exists_object(self$memory, name)
lookup_exists(.subset2(self, "lookup"), name)
},
list_rows = function() {
self$memory$names
lookup_list(.subset2(self, "lookup"))
},
condense_data = function(data) {
data[!duplicated(data$name, fromLast = TRUE), ]
Expand Down Expand Up @@ -464,7 +467,7 @@ database_class <- R6::R6Class(
)
},
validate = function() {
memory_validate(self$memory)
lookup_validate(self$lookup)
self$validate_file()
tar_assert_chr(self$path)
tar_assert_scalar(self$path)
Expand Down
4 changes: 2 additions & 2 deletions R/class_database_aws.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Covered in semi-automated cloud tests.
# nocov start
database_aws_new <- function(
memory = NULL,
lookup = NULL,
path = NULL,
key = NULL,
header = NULL,
Expand All @@ -14,7 +14,7 @@ database_aws_new <- function(
resources = NULL
) {
database_aws_class$new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand Down
4 changes: 2 additions & 2 deletions R/class_database_gcp.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Covered in semi-automated cloud tests.
# nocov start
database_gcp_new <- function(
memory = NULL,
lookup = NULL,
path = NULL,
key = NULL,
header = NULL,
Expand All @@ -14,7 +14,7 @@ database_gcp_new <- function(
resources = NULL
) {
database_gcp_class$new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand Down
4 changes: 2 additions & 2 deletions R/class_database_local.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
database_local_new <- function(
memory = NULL,
lookup = NULL,
path = NULL,
key = NULL,
header = NULL,
Expand All @@ -12,7 +12,7 @@ database_local_new <- function(
buffer = NULL
) {
database_local_class$new(
memory = memory,
lookup = lookup,
path = path,
key = key,
header = header,
Expand Down
2 changes: 1 addition & 1 deletion R/class_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ meta_class <- R6::R6Class(
tar_runtime$meta <- self
},
ensure_preprocessed = function(write = FALSE) {
if (identical(self$database$memory$count, 0L)) {
if (length(lookup_list(self$database$lookup)) == 0L) {
self$preprocess(write = write)
}
},
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-class_database.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ tar_test("database$exists_row()", {

tar_test("database$set_row()", {
out <- database_init()
expect_equal(out$memory$names, character(0))
expect_equal(lookup_list(out$lookup), character(0))
row <- list(name = "abc", string = "run(xyz)", children = list(letters))
out$set_row(row)
expect_equal(out$memory$names, "abc")
expect_equal(memory_get_object(out$memory, "abc"), row)
expect_equal(lookup_list(out$lookup), "abc")
expect_equal(lookup_get(out$lookup, "abc"), row)
})

tar_test("database$get_data()", {
db <- database_init()
expect_equal(db$memory$names, character(0))
expect_equal(lookup_list(db$lookup), character(0))
row1 <- list(name = "abc", string = "123")
row2 <- list(name = "xyz", string = "456")
db$set_row(row1)
Expand Down Expand Up @@ -216,7 +216,7 @@ tar_test("database$set_data()", {
)
db <- database_init()
db$set_data(data)
expect_equal(sort(db$memory$names), sort(c("e", "f", "x")))
expect_equal(sort(lookup_list(db$lookup)), sort(c("e", "f", "x")))
exp <- list(
name = "e",
col2 = "e22",
Expand All @@ -236,7 +236,7 @@ tar_test("database$preprocess() on empty data", {
list_columns = "col3"
)
db$preprocess(write = TRUE)
expect_equal(db$memory$names, character(0))
expect_equal(lookup_list(db$lookup), character(0))
expect_equal(readLines(db$path), "name|col3")
})

Expand Down Expand Up @@ -264,7 +264,7 @@ tar_test("database$preprocess() on different column types", {
db$preprocess(write = TRUE)
out <- readLines(path)
expect_equal(out, lines[-3])
expect_equal(sort(db$memory$names), sort(c("e", "f", "x")))
expect_equal(sort(lookup_list(db$lookup)), sort(c("e", "f", "x")))
exp <- list(
name = "e",
col2 = "e22",
Expand Down Expand Up @@ -320,7 +320,7 @@ tar_test("database$insert_row()", {
out <- readLines(db$path)
exp <- c("name|col2|col3", "x|1|a*b", "x|1|a*b")
expect_equal(out, exp)
expect_equal(db$memory$names, "x")
expect_equal(lookup_list(db$lookup), "x")
expect_equal(db$get_row("x"), row)
})

Expand Down Expand Up @@ -389,12 +389,12 @@ tar_test("fail to validate incompatible header", {
tar_test("database buffer", {
db <- database_init()
expect_null(db$buffer)
expect_equal(db$memory$names, character(0L))
expect_equal(lookup_list(db$lookup), character(0L))
db$buffer_row(list(name = "x"))
expect_equal(db$memory$names, "x")
expect_equal(lookup_list(db$lookup), "x")
db$buffer_row(list(name = "y"))
expect_equal(sort(db$buffer), sort(c("x", "y")))
expect_equal(sort(db$memory$names), sort(c("x", "y")))
expect_equal(sort(lookup_list(db$lookup)), sort(c("x", "y")))
expect_false(file.exists(db$path))
db$flush_rows()
lines <- readLines(db$path)
Expand Down

0 comments on commit 4c7acf5

Please sign in to comment.