Skip to content

Commit

Permalink
Merge pull request #13 from traversc/master
Browse files Browse the repository at this point in the history
Msgpack_read/write and some minor improvements
  • Loading branch information
eddelbuettel committed Jun 21, 2018
2 parents 6de851a + f361aff commit 9504c6e
Show file tree
Hide file tree
Showing 8 changed files with 795 additions and 596 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,8 +1,8 @@
Package: RcppMsgPack
Type: Package
Title: 'MsgPack' C++ Header Files and Interface Functions for R
Version: 0.2.2
Date: 2018-05-06
Version: 0.2.3
Date: 2018-06-21
Author: Travers Ching and Dirk Eddelbuettel; the authors and contributors of MsgPack
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Description: 'MsgPack' header files are provided for use by R packages, along
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -3,4 +3,5 @@ importFrom("Rcpp", "evalCpp")
export("msgpack_format", "msgpack_map", "msgpack_pack", "msgpack_simplify", "msgpack_unpack",
"msgpackFormat", "msgpackMap", "msgpackPack", "msgpackSimplify", "msgpackUnpack",
"msgpack_timestamp_encode", "msgpackTimestampEncode", "msgpack_timestamp_decode", "msgpackTimestampDecode",
"msgpack_write", "msgpackWrite", "msgpack_read", "msgpackRead",
"arrayEx", "enumEx")
97 changes: 96 additions & 1 deletion R/functions.r
Expand Up @@ -201,4 +201,99 @@ msgpack_timestamp_decode <- function(x, posix=T, tz="UTC") {
}

#' @rdname msgpack_timestamp_decode
msgpackTimestampDecode <- msgpack_timestamp_decode
msgpackTimestampDecode <- msgpack_timestamp_decode

#' 'MsgPack' write
#' @description A helper function to serialize an object and write it to a file, or a connection.
#' @param ... Serializable R objects.
#' @param msg Message to write to file. If not NULL and a raw vector, write it instead of the R objects. Default: NULL.
#' @param file A connection, or a string describing the file or pipe to write to, depending on the mode.
#' @param mode One of "auto", "file", "gzip" or "pipe". If "auto", detects based on the file string (any space == pipe, ".gz" == gzip, file otherwise). Ignored if file is a connection.
#' @examples
#' tmp <- tempfile(fileext=".gz")
#' msgpack_write(1:10, file=tmp)
#' x <- msgpack_read(tmp, simplify=TRUE)
msgpack_write <- function(..., msg=NULL, file, mode="auto") {
if(is.null(msg)) {
msg <- msgpack_pack(...)
}
stopifnot("raw" %in% class(msg))
if("connection" %in% class(file)) {
con <- file
} else if(mode=="auto") {
if(grepl("\\s", file)) {
con <- pipe(file, open="wb")
} else if(grepl("\\.gz$", file)) {
con <- gzfile(file, open="wb")
} else {
con <- file(file, open="wb")
}
} else if(mode=="gzip") {
con <- gzfile(file, open="wb")
} else if(mode=="pipe") {
con <- pipe(file, open="wb")
} else {
con <- file(file, open="wb")
}
writeBin(msg, con=con, useBytes=T)
close(con)
invisible(NULL)
}

#' @rdname msgpack_write
msgpackWrite <- msgpack_write

#' 'MsgPack' read
#' @description A helper function to de-serialize an object read from a file or a connection.
#' @param file A connection, or a string describing the file or pipe to write to, depending on the mode.
#' @param simplify Passed to msgpack_unpack. Default: FALSE.
#' @param mode One of "auto", "file", "gzip" or "pipe". If "auto", detects based on the file string (any space == pipe, ".gz" == gzip, file otherwise). Ignored if file is a connection.
#' @param nbytes If reading from a pipe or gzip, how many bytes to read at a time. Default: 16777216
#' @examples
#' tmp <- tempfile(fileext=".gz")
#' msgpack_write(1:10, file=tmp)
#' x <- msgpack_read(tmp, simplify=TRUE)
msgpack_read <- function(file, simplify=F, mode="auto", nbytes=16777216) {
is_file <- F
if("connection" %in% class(file)) {
con <- file
} else if(mode=="auto") {
if(grepl("\\s", file)) {
con <- pipe(file, open="rb")
} else if(grepl("\\.gz$", file)) {
con <- gzfile(file, open="rb")
} else {
is_file <- T
con <- file(file, open="rb")
file_size <- file.info(file)$size
}
} else if(mode=="gzip") {
con <- gzfile(file, open="rb")
} else if(mode=="pipe") {
con <- pipe(file, open="rb")
} else {
is_file <- T
con <- file(file, open="rb")
file_size <- file.info(file)$size
}

if(is_file) {
bin <- readBin(con = con, what="raw", n=file_size)
} else {
bin_list <- list()
i <- 1
bin_list[[i]] <- readBin(con = con, what="raw", n=nbytes)
while(length(bin_list[[i]]) != 0) {
i <- i + 1
bin_list[[i]] <- readBin(con = con, what="raw", n=nbytes)
}
bin <- do.call(c, bin_list)
}
close(con)
msgpack_unpack(bin, simplify=simplify)
}

#' @rdname msgpack_read
msgpackRead <- msgpack_read


28 changes: 28 additions & 0 deletions man/msgpack_read.Rd

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

28 changes: 28 additions & 0 deletions man/msgpack_write.Rd

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

4 changes: 2 additions & 2 deletions src/RcppExports.cpp
Expand Up @@ -49,13 +49,13 @@ BEGIN_RCPP
END_RCPP
}
// c_timestamp_encode
RawVector c_timestamp_encode(double seconds, uint32_t nanoseconds);
RawVector c_timestamp_encode(double seconds, u_int32_t nanoseconds);
RcppExport SEXP _RcppMsgPack_c_timestamp_encode(SEXP secondsSEXP, SEXP nanosecondsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< double >::type seconds(secondsSEXP);
Rcpp::traits::input_parameter< uint32_t >::type nanoseconds(nanosecondsSEXP);
Rcpp::traits::input_parameter< u_int32_t >::type nanoseconds(nanosecondsSEXP);
rcpp_result_gen = Rcpp::wrap(c_timestamp_encode(seconds, nanoseconds));
return rcpp_result_gen;
END_RCPP
Expand Down

0 comments on commit 9504c6e

Please sign in to comment.