Skip to content

Commit b08b4c6

Browse files
Added wrapper for vfs_copy_dir() function (#804)
I noticed this function was missing when writing up a tutorial on VFS, so this is my attempt at wrapping it.
1 parent b461048 commit b08b4c6

File tree

11 files changed

+156
-27
lines changed

11 files changed

+156
-27
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: tiledb
22
Type: Package
3-
Version: 0.31.0.1
3+
Version: 0.31.0.2
44
Title: Modern Database Engine for Complex Data Based on Multi-Dimensional Arrays
55
Authors@R: c(
66
person("TileDB, Inc.", role = c("aut", "cph")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export(tiledb_subarray_to_query)
298298
export(tiledb_version)
299299
export(tiledb_vfs)
300300
export(tiledb_vfs_close)
301+
export(tiledb_vfs_copy_dir)
301302
export(tiledb_vfs_copy_file)
302303
export(tiledb_vfs_create_bucket)
303304
export(tiledb_vfs_create_dir)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
* Allow `parse_query_condition()` to work on dimensions when an array is passed
4+
* Add `tiledb_vfs_copy_dir()`, a wrapper for the `vfs_copy_dir()` function
45

56
# tiledb 0.31.0
67

R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,10 @@ libtiledb_vfs_copy_file <- function(vfs, old_uri, new_uri) {
953953
.Call(`_tiledb_libtiledb_vfs_copy_file`, vfs, old_uri, new_uri)
954954
}
955955

956+
libtiledb_vfs_copy_dir <- function(vfs, old_uri, new_uri) {
957+
.Call(`_tiledb_libtiledb_vfs_copy_dir`, vfs, old_uri, new_uri)
958+
}
959+
956960
libtiledb_vfs_fh_free <- function(fhxp) {
957961
invisible(.Call(`_tiledb_libtiledb_vfs_fh_free`, fhxp))
958962
}

R/VFS.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ tiledb_vfs_serialize <- function(obj, uri, vfs = tiledb_get_vfs()) {
505505
#' @param file Character variable with a local file path
506506
#' @param uri Character variable with a URI describing a file path
507507
#' @param vfs A TileDB VFS object; default is to use a cached value.
508-
#' @return The uri value of the removed file
508+
#' @return The uri value of the copied file
509509
#' @export
510510
tiledb_vfs_copy_file <- function(file, uri, vfs = tiledb_get_vfs()) {
511511
stopifnot(
@@ -517,6 +517,24 @@ tiledb_vfs_copy_file <- function(file, uri, vfs = tiledb_get_vfs()) {
517517
libtiledb_vfs_copy_file(vfs@ptr, file, uri)
518518
}
519519

520+
#' Copy a directory recursively to VFS
521+
#'
522+
#' @param dir Character variable with a local directory path
523+
#' @param uri Character variable with a URI describing a directory path
524+
#' @param vfs A TileDB VFS object; default is to use a cached value.
525+
#' @return The uri value of the copied directory
526+
#' @export
527+
tiledb_vfs_copy_dir <- function(dir, uri, vfs = tiledb_get_vfs()) {
528+
stopifnot(
529+
"Argument 'vfs' must be a tiledb_vfs object" = is(vfs, "tiledb_vfs"),
530+
"Argument 'uri' must be character" = is.character(uri),
531+
"Argument 'dir' must be character and point to a directory" =
532+
is.character(uri) && dir.exists(dir),
533+
"Directory with 'uri' already exists" = !dir.exists(uri)
534+
)
535+
libtiledb_vfs_copy_dir(vfs@ptr, dir, uri)
536+
}
537+
520538
#' Recursively list objects from given URI
521539
#'
522540
#' This functionality is currently limited to S3 URIs.

inst/tinytest/test_vfs.R

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,98 @@ expect_true(is(vfs, "tiledb_vfs"))
1111
## create/remove/is/is_empty/empty bucket hard to test without credentials
1212

1313
#test_that("tiledb_vfs create / test / remove directory", {
14+
15+
# Create/copy/move/remove directory
1416
vfs <- tiledb_vfs()
15-
uri <- tempfile()
16-
17-
expect_equal(tiledb_vfs_create_dir(uri), uri)
18-
## check directly
19-
expect_true(dir.exists(uri))
20-
## check via VFS
21-
expect_true(tiledb_vfs_is_dir(uri))
22-
23-
newuri <- tempfile()
24-
expect_equal(tiledb_vfs_move_dir(uri, newuri), newuri)
25-
expect_equal(tiledb_vfs_remove_dir(newuri), newuri)
26-
expect_false(dir.exists(newuri))
17+
base_path <- tempdir()
18+
dir_uri <- file.path(base_path, "dir_uri")
19+
20+
## Create directory
21+
expect_equal(tiledb_vfs_create_dir(dir_uri), dir_uri)
22+
### check directly
23+
expect_true(dir.exists(dir_uri))
24+
### check via VFS
25+
expect_true(tiledb_vfs_is_dir(dir_uri))
26+
27+
## Check dir size
28+
expect_equal(tiledb_vfs_dir_size(dir_uri), 0)
29+
30+
## Create a file in dir_uri to make sure move_dir and copy_dir work
31+
file_name <- "test.txt"
32+
file_uri <- file.path(dir_uri, file_name)
33+
expect_equal(tiledb_vfs_touch(file_uri), file_uri)
34+
35+
# Windows doesn't support copying directories yet
36+
if (tolower(Sys.info()["sysname"]) != "windows") {
37+
## Copy directory
38+
copy_dir_uri <- file.path(base_path, "copy_dir_uri")
39+
expect_equal(tiledb_vfs_copy_dir(dir_uri, copy_dir_uri), copy_dir_uri)
40+
41+
### Check both directories exist
42+
expect_true(dir.exists(dir_uri))
43+
expect_true(dir.exists(copy_dir_uri))
44+
45+
### Check both files exist
46+
copy_file_uri <- file.path(copy_dir_uri, file_name)
47+
expect_true(file.exists(file_uri))
48+
expect_true(file.exists(copy_file_uri))
49+
}
50+
51+
## Move directory
52+
move_dir_uri <- file.path(base_path, "move_dir_uri")
53+
expect_equal(tiledb_vfs_move_dir(dir_uri, move_dir_uri), move_dir_uri)
54+
55+
### Check only new directory exists
56+
expect_false(dir.exists(dir_uri))
57+
58+
### Check only new file exists
59+
move_file_uri <- file.path(move_dir_uri, file_name)
60+
expect_false(file.exists(file_uri))
61+
expect_true(file.exists(move_file_uri))
62+
63+
## Remove directory
64+
expect_equal(tiledb_vfs_remove_dir(move_dir_uri), move_dir_uri)
65+
66+
### Check the moved file and directory no longer exist
67+
expect_false(dir.exists(move_dir_uri))
68+
expect_false(file.exists(move_file_uri))
69+
2770
#})
2871

72+
# Create/copy/move/remove file
73+
2974
#test_that("tiledb_vfs create / test / remove file", {
3075
vfs <- tiledb_vfs()
31-
uri <- tempfile()
76+
file_uri <- tempfile()
77+
78+
## Touch file
79+
expect_equal(tiledb_vfs_touch(file_uri), file_uri)
80+
expect_true(file.exists(file_uri))
81+
expect_true(tiledb_vfs_is_file(file_uri))
3282

33-
expect_equal(tiledb_vfs_touch(uri), uri)
34-
expect_true(tiledb_vfs_is_file(uri))
83+
### Check file exists via VFS
84+
expect_true(tiledb_vfs_is_file(file_uri))
3585

36-
## check via VFS
37-
expect_true(tiledb_vfs_is_file(uri))
86+
### check file size via VFS
87+
expect_equal(tiledb_vfs_file_size(file_uri), 0)
3888

39-
## check via VFS
40-
expect_equal(tiledb_vfs_file_size(uri), 0)
89+
if (tolower(Sys.info()["sysname"]) != "windows") {
90+
## Copy file
91+
copy_file_uri <- tempfile()
92+
expect_equal(tiledb_vfs_copy_file(file_uri, copy_file_uri), copy_file_uri)
93+
expect_true(file.exists(file_uri))
94+
expect_true(file.exists(copy_file_uri))
95+
}
96+
97+
## Move file
98+
move_file_uri <- tempfile()
99+
expect_equal(tiledb_vfs_move_file(file_uri, move_file_uri), move_file_uri)
100+
expect_false(file.exists(file_uri))
101+
expect_true(file.exists(move_file_uri))
41102

42-
newuri <- tempfile()
43-
expect_equal(tiledb_vfs_move_file(uri, newuri), newuri)
44-
expect_equal(tiledb_vfs_remove_file(newuri), newuri)
45-
expect_false(file.exists(newuri))
103+
## Remove file
104+
expect_equal(tiledb_vfs_remove_file(move_file_uri), move_file_uri)
105+
expect_false(file.exists(move_file_uri))
46106

47107
#})
48108

@@ -62,6 +122,9 @@ if (requireNamespace("palmerpenguins", quietly=TRUE)) {
62122
uriser <- tempfile()
63123
expect_equal(tiledb_vfs_serialize(pp, uriser), uriser)
64124

125+
## Check file has contents
126+
expect_false(tiledb_vfs_file_size(uriser) == 0)
127+
65128
expect_equal(pp, tiledb_vfs_unserialize(uriser))
66129
}
67130

man/tiledb-package.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tiledb_vfs_copy_dir.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tiledb_vfs_copy_file.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,19 @@ BEGIN_RCPP
28512851
return rcpp_result_gen;
28522852
END_RCPP
28532853
}
2854+
// libtiledb_vfs_copy_dir
2855+
std::string libtiledb_vfs_copy_dir(XPtr<tiledb::VFS> vfs, std::string old_uri, std::string new_uri);
2856+
RcppExport SEXP _tiledb_libtiledb_vfs_copy_dir(SEXP vfsSEXP, SEXP old_uriSEXP, SEXP new_uriSEXP) {
2857+
BEGIN_RCPP
2858+
Rcpp::RObject rcpp_result_gen;
2859+
Rcpp::RNGScope rcpp_rngScope_gen;
2860+
Rcpp::traits::input_parameter< XPtr<tiledb::VFS> >::type vfs(vfsSEXP);
2861+
Rcpp::traits::input_parameter< std::string >::type old_uri(old_uriSEXP);
2862+
Rcpp::traits::input_parameter< std::string >::type new_uri(new_uriSEXP);
2863+
rcpp_result_gen = Rcpp::wrap(libtiledb_vfs_copy_dir(vfs, old_uri, new_uri));
2864+
return rcpp_result_gen;
2865+
END_RCPP
2866+
}
28542867
// libtiledb_vfs_fh_free
28552868
void libtiledb_vfs_fh_free(XPtr<vfs_fh_t> fhxp);
28562869
RcppExport SEXP _tiledb_libtiledb_vfs_fh_free(SEXP fhxpSEXP) {
@@ -3976,6 +3989,7 @@ static const R_CallMethodDef CallEntries[] = {
39763989
{"_tiledb_libtiledb_vfs_dir_size", (DL_FUNC) &_tiledb_libtiledb_vfs_dir_size, 2},
39773990
{"_tiledb_libtiledb_vfs_ls", (DL_FUNC) &_tiledb_libtiledb_vfs_ls, 2},
39783991
{"_tiledb_libtiledb_vfs_copy_file", (DL_FUNC) &_tiledb_libtiledb_vfs_copy_file, 3},
3992+
{"_tiledb_libtiledb_vfs_copy_dir", (DL_FUNC) &_tiledb_libtiledb_vfs_copy_dir, 3},
39793993
{"_tiledb_libtiledb_vfs_fh_free", (DL_FUNC) &_tiledb_libtiledb_vfs_fh_free, 1},
39803994
{"_tiledb_libtiledb_vfs_ls_recursive", (DL_FUNC) &_tiledb_libtiledb_vfs_ls_recursive, 3},
39813995
{"_tiledb_libtiledb_stats_enable", (DL_FUNC) &_tiledb_libtiledb_stats_enable, 0},

0 commit comments

Comments
 (0)