Skip to content

Commit

Permalink
Factor out make_readable_file
Browse files Browse the repository at this point in the history
  • Loading branch information
nealrichardson committed Sep 6, 2019
1 parent 3bee2ea commit 6989f73
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 57 deletions.
9 changes: 1 addition & 8 deletions r/R/RecordBatchReader.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ RecordBatchFileReader <- R6Class("RecordBatchFileReader", inherit = Object,
)

RecordBatchFileReader$create <- function(file) {
if (inherits(file, c("raw", "Buffer"))) {
file <- BufferReader$create(file)
} else if (is.character(file)) {
assert_that(length(file) == 1L)
file <- ReadableFile$create(file)
}
assert_that(inherits(file, "RandomAccessFile"))

file <- make_readable_file(file)
shared_ptr(RecordBatchFileReader, ipc___RecordBatchFileReader__Open(file))
}
17 changes: 6 additions & 11 deletions r/R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' `parse_options`, `convert_options`, or `read_options` arguments, or you can
#' call [csv_table_reader()] directly for lower-level access.
#'
#' @param file A character path to a local file, or an Arrow input stream
#' @inheritParams make_readable_file
#' @param delim Single character used to separate fields within a record.
#' @param quote Single character used to quote strings.
#' @param escape_double Does the file escape quotes by doubling them?
Expand Down Expand Up @@ -202,16 +202,11 @@ CsvTableReader$create <- function(file,
parse_options = csv_parse_options(),
convert_options = csv_convert_options(),
...) {
if (is.character(file)) {
file <- mmap_open(file)
}
if (inherits(file, "InputStream")) {
file <- shared_ptr(CsvTableReader,
csv___TableReader__Make(file, read_options, parse_options, convert_options)
)
}
assert_that(inherits(file, c("CsvTableReader", "TableReader")))
file
file <- make_readable_file(file)
shared_ptr(
CsvTableReader,
csv___TableReader__Make(file, read_options, parse_options, convert_options)
)
}

#' Arrow CSV and JSON table readers
Expand Down
16 changes: 3 additions & 13 deletions r/R/feather.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,7 @@ FeatherTableReader <- R6Class("FeatherTableReader", inherit = Object,
)
)

FeatherTableReader$create <- function(stream, mmap = TRUE, ...) {
if (is.character(stream)) {
if (isTRUE(mmap)) {
stream <- mmap_open(stream, ...)
} else {
stream <- ReadableFile$create(stream, ...)
}
} else if (is.raw(stream)) {
stream <- BufferReader$create(stream)
}

assert_that(inherits(stream, "InputStream"))
unique_ptr(FeatherTableReader, ipc___feather___TableReader__Open(stream))
FeatherTableReader$create <- function(file, mmap = TRUE, ...) {
file <- make_readable_file(file, mmap)
unique_ptr(FeatherTableReader, ipc___feather___TableReader__Open(file))
}
20 changes: 20 additions & 0 deletions r/R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,23 @@ mmap_open <- function(path, mode = c("read", "write", "readwrite")) {
path <- normalizePath(path)
shared_ptr(MemoryMappedFile, io___MemoryMappedFile__Open(path, mode))
}

#' Handle a range of possible input sources
#' @param file A character file name, raw vector, or an Arrow input stream
#' @param mmap Logical: whether to memory-map the file (default `TRUE`)
#' @return An `InputStream` or a subclass of one.
#' @keywords internal
make_readable_file <- function(file, mmap = TRUE) {
if (is.character(file)) {
assert_that(length(file) == 1L)
if (isTRUE(mmap)) {
file <- mmap_open(file)
} else {
file <- ReadableFile$create(file)
}
} else if (inherits(file, c("raw", "Buffer"))) {
file <- BufferReader$create(file)
}
assert_that(inherits(file, "InputStream"))
file
}
15 changes: 5 additions & 10 deletions r/R/json.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,11 @@ JsonTableReader$create <- function(file,
parse_options = json_parse_options(),
...) {

if (is.character(file)) {
file <- mmap_open(file)
}
if (inherits(file, "InputStream")) {
file <- shared_ptr(JsonTableReader,
json___TableReader__Make(file, read_options, parse_options)
)
}
assert_that(inherits(file, c("JsonTableReader", "TableReader")))
file
file <- make_readable_file(file)
shared_ptr(
JsonTableReader,
json___TableReader__Make(file, read_options, parse_options)
)
}

#' @rdname csv_table_reader
Expand Down
11 changes: 1 addition & 10 deletions r/R/parquet.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,7 @@ ParquetFileReader$create <- function(file,
props = ParquetReaderProperties$create(),
mmap = TRUE,
...) {
if (is.character(file)) {
if (isTRUE(mmap)) {
file <- mmap_open(file)
} else {
file <- ReadableFile$create(file)
}
} else if (is.raw(file)) {
file <- BufferReader$create(file)
}
assert_that(inherits(file, "RandomAccessFile"))
file <- make_readable_file(file, mmap)
assert_that(inherits(props, "ParquetReaderProperties"))

unique_ptr(ParquetFileReader, parquet___arrow___FileReader__OpenFile(file, props))
Expand Down
4 changes: 2 additions & 2 deletions r/R/read_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ read_table.character <- function(stream) {
}

#' @export
`read_table.raw` <- function(stream) {
read_table.raw <- function(stream) {
stream <- BufferReader$create(stream)
on.exit(stream$close())
batch_reader <- RecordBatchStreamReader$create(stream)
Expand All @@ -78,6 +78,6 @@ read_table.character <- function(stream) {

#' @rdname read_table
#' @export
read_arrow <- function(stream){
read_arrow <- function(stream) {
as.data.frame(read_table(stream))
}
20 changes: 20 additions & 0 deletions r/man/make_readable_file.Rd

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

2 changes: 1 addition & 1 deletion r/man/read_delim_arrow.Rd

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

2 changes: 1 addition & 1 deletion r/man/read_json_arrow.Rd

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

2 changes: 1 addition & 1 deletion r/man/read_parquet.Rd

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

0 comments on commit 6989f73

Please sign in to comment.