-
Notifications
You must be signed in to change notification settings - Fork 79
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
feat: Importing large objects from client side #472
base: main
Are you sure you want to change the base?
Changes from 4 commits
f432ec8
878d932
32fd13f
2476b0e
56ae5bd
7574866
5fae348
e71926b
490dcd1
f30f6ac
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 |
---|---|---|
|
@@ -135,3 +135,38 @@ postgresWaitForNotify <- function(conn, timeout = 1) { | |
postgresIsTransacting <- function(conn) { | ||
connection_is_transacting(conn@ptr) | ||
} | ||
|
||
|
||
#' Imports a large object from file | ||
#' | ||
#' Returns an object idenfier (Oid) for the imported large object | ||
#' | ||
#' @export | ||
#' @param conn a [PqConnection-class] object, produced by | ||
#' [DBI::dbConnect()] | ||
#' @param filepath a path to the large object to import | ||
#' @param oid the oid to write to. Defaults to 0 which assigns an unused oid | ||
#' @return the identifier of the large object, an integer | ||
#' @examples | ||
#' con <- postgresDefault() | ||
#' path_to_file <- 'my_image.png' | ||
#' dbWithTransaction(con, { | ||
#' oid <- postgresImportLargeObject(con, test_file_path) | ||
#' }) | ||
postgresImportLargeObject <- function(conn, filepath = NULL, oid = 0) { | ||
|
||
if (!postgresIsTransacting(conn)) { | ||
stopc("Cannot import a large object outside of a transaction") | ||
} | ||
|
||
if (is.null(filepath)) stopc("'filepath' cannot be NULL") | ||
if (oid < 0) stopc("'oid' cannot be negative") | ||
if (is.null(oid) | is.na(oid)) stopc("'oid' cannot be NULL/NA") | ||
if (file.access(filepath,4) == -1) stopc(paste0("Unable to read from filepath '",filepath,"'")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will the error look like if we let libpq do the check?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. libpq returns an InvalidOid (a zero). Letting libpq do the check has the downside that we cannot inform the user of the specific problem as libpq's So currently if the accessibility of the file has changed, an error is thrown with the message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see:
Would that work well enough? Also: how do we auto-assign the OID? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I was unaware of
(It's a bit clunky as the term "error" is repeated, but informative nonetheless.) pqlib auto-assigns the oid if the argument 'oid' is zero. From the docs:
I also changed the return- and argument types to 'Oid' from int as oid's are implemented as unsigned integers. |
||
|
||
|
||
out_oid = connection_import_lo_from_file(conn@ptr, filepath, oid) | ||
if (out_oid == 0) stopc("Import failed. Maybe you tried to write to an existing oid?") | ||
return(out_oid) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include <cpp11.hpp> | ||
#include <libpq-fe.h> | ||
#include <libpq/libpq-fs.h> | ||
|
||
#include <plogr.h> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
postgres |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
test_that("can import and read a large object", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object.txt') | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path) }) | ||
expect_gt(oid,0) | ||
lo_data <- unlist(dbGetQuery(con, "select lo_get($1) as lo_data", params=list(oid))$lo_data[1]) | ||
large_object_txt <- as.raw(c(0x70, 0x6f, 0x73, 0x74, 0x67, 0x72, 0x65, 0x73)) # the string 'postgres' | ||
expect_equal(lo_data, large_object_txt) | ||
}) | ||
|
||
|
||
test_that("importing to an existing oid throws error", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object.txt') | ||
oid <- 1234 | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path, oid) }) | ||
|
||
expect_error( | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path, oid) }) | ||
) | ||
dbExecute(con, "select lo_unlink($1) as lo_data", params=list(oid)) | ||
}) | ||
|
||
|
||
test_that("import from a non-existing path throws error", { | ||
con <- postgresDefault() | ||
on.exit(dbDisconnect(con)) | ||
test_file_path <- paste0(test_path(),'/data/large_object_that_does_not_exist.txt') | ||
expect_error( | ||
dbWithTransaction(con, { oid <- postgresImportLargeObject(con, test_file_path) }) | ||
) | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or perhaps this could be two checks?