-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6c40e9
commit 219ef5a
Showing
7 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#' Marshalling of 'bigmemory' objects | ||
#' | ||
#' @param x A [bigmemory::big.matrix] object. | ||
#' | ||
#' @param \dots Not used. | ||
#' | ||
#' @return | ||
#' A `marshalled` object as described in [marshal()]. | ||
#' | ||
#' @details | ||
#' [bigmemory::write.big.matrix()] is used to produce a marshalled version | ||
#' of the original object. | ||
#' [bigmemory::read.big.matrix()] is used to reconstruct a version of the | ||
#' original object from the marshalled object. | ||
#' | ||
#' @example incl/marshal.bigmemory.R | ||
#' | ||
#' @rdname marshal.bigmemory | ||
#' @aliases marshal.bigmemory | ||
#' @export | ||
marshal.big.matrix <- function(x, ...) { | ||
marshal_bigmemory(x, ...) | ||
} | ||
|
||
marshal_bigmemory <- function(x, ...) { | ||
tf <- tempfile(fileext = ".bigmemory") | ||
on.exit(if (!is.null(tf)) file.remove(tf)) | ||
bigmemory::write.big.matrix(x, filename = tf) | ||
raw <- readBin(tf, what = "raw", n = file.size(tf)) | ||
file.remove(tf) | ||
tf <- NULL | ||
|
||
res <- list( | ||
marshalled = list( | ||
typeof = bigmemory::typeof(x), | ||
raw = raw | ||
) | ||
) | ||
class(res) <- marshal_class(x) | ||
rm(list = "raw") | ||
|
||
res[["unmarshal"]] <- unmarshal_bigmemory | ||
|
||
assert_no_references(res) | ||
res | ||
} | ||
|
||
unmarshal_bigmemory <- function(x, ...) { | ||
object <- x[["marshalled"]] | ||
raw <- object[["raw"]] | ||
typeof <- object[["typeof"]] | ||
|
||
tf <- tempfile(fileext = ".bigmemory") | ||
on.exit(if (!is.null(tf)) file.remove(tf)) | ||
writeBin(raw, con = tf) | ||
|
||
res <- bigmemory::read.big.matrix(tf, type = typeof) | ||
file.remove(tf) | ||
tf <- NULL | ||
|
||
res | ||
} | ||
|
||
|
||
#' @rdname marshal.bigmemory | ||
#' @aliases marshallable.big.memory | ||
#' @export | ||
marshallable.big.memory <- function(...) { | ||
TRUE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if (requireNamespace("bigmemory", quietly = TRUE)) { | ||
library(bigmemory) | ||
|
||
x <- big.matrix(nrow = 3, ncol = 2, type = "double") | ||
x[] <- seq_along(x) | ||
|
||
## Marshal | ||
x_ <- marshal(x) | ||
|
||
## Unmarshal | ||
x2 <- unmarshal(x_) | ||
|
||
stopifnot(identical(x2[], x[])) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
library(marshal) | ||
|
||
if (requireNamespace("bigmemory", quietly = TRUE)) { | ||
library(bigmemory) | ||
|
||
x <- big.matrix(nrow = 3, ncol = 2, type = "double") | ||
x[] <- seq_along(x) | ||
|
||
## Marshal | ||
x_ <- marshal(x) | ||
|
||
## Unmarshal | ||
x2 <- unmarshal(x_) | ||
|
||
stopifnot(identical(x2[], x[])) | ||
} |