Skip to content

Commit

Permalink
refactor: use common binary states
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Oct 22, 2023
1 parent 30146fa commit eae26c5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
25 changes: 20 additions & 5 deletions R/Rush.R
Original file line number Diff line number Diff line change
Expand Up @@ -826,16 +826,24 @@ Rush = R6::R6Class("Rush",
#' Keys of the hashes.
#' If `NULL` new keys are generated.
#' @param state (`character(1)`)\cr
#' Status of the hashes.
#' State of the hashes.
#'
#' @return (`character()`)\cr
#' Keys of the hashes.
write_hashes = function(..., .values = list(), keys = NULL, state = NA) {
write_hashes = function(..., .values = list(), keys = NULL, state = NA_character_) {
values = discard(c(list(...), .values), function(l) !length(l))
assert_list(values, names = "unique", types = "list", min.len = 1)
fields = names(values)
keys = assert_character(keys %??% uuid::UUIDgenerate(n = length(values[[1]])), len = length(values[[1]]), .var.name = "keys")
bin_state = redux::object_to_bin(list(state = state))
assert_string(state, na.ok = TRUE)
bin_state = switch(state,
"queued" = queued_state,
"running" = running_state,
"failed" = failed_state,
"finished" = finished_state,
`NA_character_` = na_state,
redux::object_to_bin(list(state = state))
)

lg$debug("Writting %i hash(es) with %i field(s)", length(keys), length(fields))

Expand All @@ -844,8 +852,7 @@ Rush = R6::R6Class("Rush",
# serialize lists
bin_values = map(list(...), redux::object_to_bin)

lg$debug("Serialzing %i value(s) to %s",
length(bin_values), format(Reduce(`+`, map(bin_values, object.size))))
lg$debug("Serialzing %i value(s) to %s", length(bin_values), format(Reduce(`+`, map(bin_values, object.size))))

# merge fields and values alternatively
# c and rbind are fastest option in R
Expand Down Expand Up @@ -1119,3 +1126,11 @@ Rush = R6::R6Class("Rush",
}
)
)

# common state for all tasks
# used in $write_hashes()
queued_state = redux::object_to_bin(list(state = "queued"))
running_state = redux::object_to_bin(list(state = "running"))
failed_state = redux::object_to_bin(list(state = "failed"))
finished_state = redux::object_to_bin(list(state = "finished"))
na_state = redux::object_to_bin(list(state = NA_character_))
4 changes: 2 additions & 2 deletions man/Rush.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-RushWorker.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ test_that("writting a hash with a state works", {
expect_equal(rush$read_hashes(keys, c("xs", "state")), list(list(x1 = 1, x2 = 2, state = "queued"), list(x1 = 1, x2 = 3, state = "queued")))
})

test_that("writting a hash with a NA state works", {
# skip_on_cran()

config = start_flush_redis()
rush = RushWorker$new(network_id = "test-rush", config = config, host = "local")

keys = rush$write_hashes(xs = list(list(x1 = 1, x2 = 2)), state = NA_character_)
expect_equal(rush$read_hashes(keys, c("xs", "state")), list(list(x1 = 1, x2 = 2, state = NA_character_)))
})


test_that("writting a hash with a arbitrary state works", {
# skip_on_cran()

config = start_flush_redis()
rush = RushWorker$new(network_id = "test-rush", config = config, host = "local")

keys = rush$write_hashes(xs = list(list(x1 = 1, x2 = 2)), state = "test")
expect_equal(rush$read_hashes(keys, c("xs", "state")), list(list(x1 = 1, x2 = 2, state = "test")))
})

test_that("pushing a task to the queue works", {
# skip_on_cran()

Expand Down

0 comments on commit eae26c5

Please sign in to comment.