-
Notifications
You must be signed in to change notification settings - Fork 1k
add fread file connection support #7422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
00dc7bb
78bce0e
0afd468
fa79c8c
9590e22
58b3386
995d2dc
3866b6d
8294c6f
1b7cec7
9b3c387
3da8943
f6f9ed3
5a98e62
d76c3a5
d520cd4
c3f7cf6
4235a5c
e37b0ee
2bcfc6c
2e67cc2
4383ae2
5e91780
5182c0c
441c557
63fa168
a609fda
daabbb7
1a98f38
5eef830
0c6eff5
236bc5c
6f4d90f
fea7506
2474161
7b2d3b1
8c164c3
c19cbc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,16 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") | |
| input = text | ||
| } | ||
| } | ||
| else if (is.null(cmd)) { | ||
| # Check if input is a connection and read it into memory | ||
| input_is_con = FALSE | ||
| if (!missing(input) && inherits(input, "connection")) { | ||
| input_is_con = TRUE | ||
| } else if (!is.null(file) && inherits(file, "connection")) { | ||
| input = file | ||
| input_is_con = TRUE | ||
| file = NULL | ||
| } | ||
| if (!input_is_con && is.null(cmd) && is.null(text)) { | ||
| if (!is.character(input) || length(input)!=1L) { | ||
| stopf("input= must be a single character string containing a file name, a system command containing at least one space, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or, the input data itself containing at least one \\n or \\r") | ||
| } | ||
|
|
@@ -81,6 +90,59 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") | |
| } | ||
| file = tmpFile | ||
| } | ||
| connection_spill_info = NULL | ||
| if (input_is_con) { | ||
| if (verbose) { | ||
| catf("[00] Spill connection to tempfile\n Connection class: %s\n Reading connection into RAM buffer... ", toString(class(input))) | ||
| flush.console() | ||
| } | ||
| spill_started.at = proc.time() | ||
| con_summary = summary(input) | ||
| con_desc = con_summary$description | ||
| con_class = class1(input) | ||
| con_open = isOpen(input) | ||
|
|
||
| needs_reopen = FALSE | ||
| if (con_open) { | ||
| binary_modes = c("rb", "r+b", "wb", "w+b", "ab", "a+b") | ||
|
||
| if (!con_summary$mode %chin% binary_modes) needs_reopen = TRUE | ||
| } | ||
|
|
||
| close_con = NULL | ||
|
|
||
| if (needs_reopen) { | ||
| close(input) | ||
| input = switch(con_class, | ||
ben-schwen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "file" = file(con_desc, "rb"), | ||
| "gzfile" = gzfile(con_desc, "rb"), | ||
| "bzfile" = bzfile(con_desc, "rb"), | ||
| "url" = url(con_desc, "rb"), | ||
| "pipe" = pipe(con_desc, "rb"), | ||
| stopf("Unsupported connection type: %s", con_class)) | ||
ben-schwen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| close_con = input | ||
| } else if (!con_open) { | ||
| open(input, "rb") | ||
| close_con = input | ||
| } | ||
| tmpFile = tempfile(tmpdir=tmpdir) | ||
| on.exit(unlink(tmpFile), add=TRUE) | ||
| bytes_copied = .Call(CspillConnectionToFile, input, tmpFile, as.numeric(nrows)) | ||
| spill_elapsed = (proc.time() - spill_started.at)[["elapsed"]] | ||
|
|
||
| if (bytes_copied == 0) { | ||
| warningf("Connection has size 0. Returning a NULL %s.", if (data.table) 'data.table' else 'data.frame') | ||
ben-schwen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return(if (data.table) data.table(NULL) else data.frame(NULL)) | ||
| } | ||
|
|
||
| if (verbose) { | ||
| catf("done in %s\n", timetaken(spill_started.at)) | ||
| flush.console() | ||
| } | ||
| connection_spill_info = c(spill_elapsed, bytes_copied) | ||
| input = tmpFile | ||
| file = tmpFile | ||
| if (!is.null(close_con)) close(close_con) | ||
| } | ||
| if (!is.null(file)) { | ||
| if (!is.character(file) || length(file)!=1L) | ||
| stopf("file= must be a single character string containing a filename, or URL starting 'http[s]://', 'ftp[s]://' or 'file://'") | ||
|
|
@@ -293,7 +355,7 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") | |
| tz="UTC" | ||
| } | ||
| ans = .Call(CfreadR,input,identical(input,file),sep,dec,quote,header,nrows,skip,na.strings,strip.white,blank.lines.skip,comment.char, | ||
| fill,showProgress,nThread,verbose,warnings2errors,logical01,logicalYN,select,drop,colClasses,integer64,encoding,keepLeadingZeros,tz=="UTC") | ||
| fill,showProgress,nThread,verbose,warnings2errors,logical01,logicalYN,select,drop,colClasses,integer64,encoding,keepLeadingZeros,tz=="UTC",connection_spill_info) | ||
| if (!length(ans)) return(null.data.table()) # test 1743.308 drops all columns | ||
| nr = length(ans[[1L]]) | ||
| require_bit64_if_needed(ans) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.