Skip to content

Commit

Permalink
Correct filehandle use in VFS serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel committed Nov 11, 2023
1 parent 2365e53 commit 0ede752
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,10 @@ libtiledb_vfs_copy_file <- function(vfs, old_uri, new_uri) {
.Call(`_tiledb_libtiledb_vfs_copy_file`, vfs, old_uri, new_uri)
}

libtiledb_vfs_fh_free <- function(fhxp) {
invisible(.Call(`_tiledb_libtiledb_vfs_fh_free`, fhxp))
}

libtiledb_stats_enable <- function() {
invisible(.Call(`_tiledb_libtiledb_stats_enable`))
}
Expand Down
23 changes: 13 additions & 10 deletions R/VFS.R
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,10 @@ tiledb_vfs_unserialize <- function(uri, vfs = tiledb_get_vfs()) {
stopifnot("Argument 'vfs' must a tiledb_vfs object" = is(vfs, "tiledb_vfs"),
"Argument 'uri' must be character" = is.character(uri))
n <- tiledb_vfs_file_size(uri)
file <- tiledb_vfs_open(uri, "READ")
vec <- tiledb_vfs_read(file, 0, n)
tiledb_vfs_close(file)
fh <- tiledb_vfs_open(uri, "READ")
vec <- tiledb_vfs_read(fh, 0, n)
tiledb_vfs_close(fh)
libtiledb_vfs_fh_free(fh)
## The gzcon(rawConnection()) idea is from https://stackoverflow.com/a/58136567/508431
## The packBits(intToBits()) part on the int vector read is from a friend via slack
obj <- unserialize(gzcon(rawConnection(packBits(intToBits(vec)))))
Expand All @@ -420,15 +421,17 @@ tiledb_vfs_serialize <- function(obj, uri, vfs = tiledb_get_vfs()) {
saveRDS(obj, tf)

## Read local file
file <- tiledb_vfs_open(tf, "READ")
vec <- tiledb_vfs_read(file, 0, tiledb_vfs_file_size(tf))
tiledb_vfs_close(file)
fh <- tiledb_vfs_open(tf, "READ")
vec <- tiledb_vfs_read(fh, 0, tiledb_vfs_file_size(tf))
tiledb_vfs_close(fh)
libtiledb_vfs_fh_free(fh)

## Now write 'vec' to the target URI
file <- tiledb_vfs_open(uri, "WRITE")
tiledb_vfs_write(file, vec)
tiledb_vfs_sync(file)
tiledb_vfs_close(file)
fh <- tiledb_vfs_open(uri, "WRITE")
tiledb_vfs_write(fh, vec)
tiledb_vfs_sync(fh)
tiledb_vfs_close(fh)
libtiledb_vfs_fh_free(fh)

unlink(tf)
invisible(uri)
Expand Down
6 changes: 5 additions & 1 deletion inst/include/tiledb.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ typedef std::unordered_map<std::string, std::shared_ptr<tiledb::ColumnBuffer>> m

// C++ compiler complains about missing delete functionality when we use tiledb_vfs_fh_t directly
struct vfs_fh {
void *fh;
#if TILEDB_VERSION_MAJOR == 2 && TILEDB_VERSION_MINOR >= 15
tiledb_vfs_fh_handle_t* fh;
#else
tiledb_vfs_fh_t* fh;
#endif
};
typedef struct vfs_fh vfs_fh_t;

Expand Down
11 changes: 11 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,16 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// libtiledb_vfs_fh_free
void libtiledb_vfs_fh_free(XPtr<vfs_fh_t> fhxp);
RcppExport SEXP _tiledb_libtiledb_vfs_fh_free(SEXP fhxpSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< XPtr<vfs_fh_t> >::type fhxp(fhxpSEXP);
libtiledb_vfs_fh_free(fhxp);
return R_NilValue;
END_RCPP
}
// libtiledb_stats_enable
void libtiledb_stats_enable();
RcppExport SEXP _tiledb_libtiledb_stats_enable() {
Expand Down Expand Up @@ -3783,6 +3793,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_tiledb_libtiledb_vfs_dir_size", (DL_FUNC) &_tiledb_libtiledb_vfs_dir_size, 2},
{"_tiledb_libtiledb_vfs_ls", (DL_FUNC) &_tiledb_libtiledb_vfs_ls, 2},
{"_tiledb_libtiledb_vfs_copy_file", (DL_FUNC) &_tiledb_libtiledb_vfs_copy_file, 3},
{"_tiledb_libtiledb_vfs_fh_free", (DL_FUNC) &_tiledb_libtiledb_vfs_fh_free, 1},
{"_tiledb_libtiledb_stats_enable", (DL_FUNC) &_tiledb_libtiledb_stats_enable, 0},
{"_tiledb_libtiledb_stats_disable", (DL_FUNC) &_tiledb_libtiledb_stats_disable, 0},
{"_tiledb_libtiledb_stats_reset", (DL_FUNC) &_tiledb_libtiledb_stats_reset, 0},
Expand Down
26 changes: 20 additions & 6 deletions src/libtiledb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4324,11 +4324,15 @@ XPtr<vfs_fh_t> libtiledb_vfs_open(XPtr<tiledb::Context> ctxxp, XPtr<tiledb::VFS>
check_xptr_tag<tiledb::VFS>(vfsxp);
std::shared_ptr<tiledb_ctx_t> ctx = ctxxp.get()->ptr();
std::shared_ptr<tiledb_vfs_t> vfs = vfsxp.get()->ptr();
#if TILEDB_VERSION >= TileDB_Version(2,15,0)
tiledb_vfs_fh_handle_t *fh = nullptr;
#else
tiledb_vfs_fh_t *fh = nullptr;
#endif
tiledb_vfs_mode_t vfsmode = _string_to_tiledb_vfs_mode_t(mode);
tiledb_vfs_open(ctx.get(), vfs.get(), uri.c_str(), vfsmode, &fh);
XPtr<vfs_fh_t> ptr = make_xptr<vfs_fh_t>(new vfs_fh_t);
ptr->fh = static_cast<void*>(fh);
ptr->fh = fh;
return ptr;
}

Expand All @@ -4337,7 +4341,7 @@ void libtiledb_vfs_close(XPtr<tiledb::Context> ctxxp, XPtr<vfs_fh_t> fh) {
check_xptr_tag<tiledb::Context>(ctxxp);
check_xptr_tag<vfs_fh_t>(fh);
std::shared_ptr<tiledb_ctx_t> ctx = ctxxp.get()->ptr();
tiledb_vfs_close(ctx.get(), static_cast<tiledb_vfs_fh_t*>(fh->fh));
tiledb_vfs_close(ctx.get(), fh->fh);
}

// [[Rcpp::export]]
Expand All @@ -4346,8 +4350,7 @@ void libtiledb_vfs_write(XPtr<tiledb::Context> ctxxp, XPtr<vfs_fh_t> fh,
check_xptr_tag<tiledb::Context>(ctxxp);
check_xptr_tag<vfs_fh_t>(fh);
std::shared_ptr<tiledb_ctx_t> ctx = ctxxp.get()->ptr();
tiledb_vfs_write(ctx.get(), static_cast<tiledb_vfs_fh_t*>(fh->fh),
&(vec[0]), vec.size()*sizeof(int));
tiledb_vfs_write(ctx.get(), fh->fh, &(vec[0]), vec.size()*sizeof(int));
}

// [[Rcpp::export]]
Expand All @@ -4359,7 +4362,7 @@ Rcpp::IntegerVector libtiledb_vfs_read(XPtr<tiledb::Context> ctxxp, XPtr<vfs_fh_
std::int64_t offs = fromInteger64(offset);
std::int64_t nb = fromInteger64(nbytes);
Rcpp::IntegerVector buf(nb/4);
tiledb_vfs_read(ctx.get(), static_cast<tiledb_vfs_fh_t*>(fh->fh), offs, &(buf[0]), nb);
tiledb_vfs_read(ctx.get(), fh->fh, offs, &(buf[0]), nb);
return buf;
}

Expand All @@ -4368,7 +4371,7 @@ void libtiledb_vfs_sync(XPtr<tiledb::Context> ctxxp, XPtr<vfs_fh_t> fh) {
check_xptr_tag<tiledb::Context>(ctxxp);
check_xptr_tag<vfs_fh_t>(fh);
std::shared_ptr<tiledb_ctx_t> ctx = ctxxp.get()->ptr();
tiledb_vfs_sync(ctx.get(), static_cast<tiledb_vfs_fh_t*>(fh->fh));
tiledb_vfs_sync(ctx.get(), fh->fh);
}

// [[Rcpp::export]]
Expand All @@ -4390,6 +4393,17 @@ std::string libtiledb_vfs_copy_file(XPtr<tiledb::VFS> vfs, std::string old_uri,
return new_uri;
}

// [[Rcpp::export]]
void libtiledb_vfs_fh_free(XPtr<vfs_fh_t> fhxp) {
check_xptr_tag<vfs_fh_t>(fhxp);
spdl::trace("[libtiledb_vfs_clear_handle] entered");
vfs_fh_t* strptr = fhxp.get();
#if TILEDB_VERSION >= TileDB_Version(2,15,0)
tiledb_vfs_fh_handle_t *fh = strptr->fh;
tiledb_vfs_fh_free(&fh);
#endif
}

/**
* Stats
*/
Expand Down

0 comments on commit 0ede752

Please sign in to comment.