-
Notifications
You must be signed in to change notification settings - Fork 23
Add Profile API to TileDB-R #856
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
Changes from 17 commits
78c84cf
0314ee3
06f80e1
3e073e1
b3300d9
c6dedaf
c4a7a33
cb44dc6
8b1a960
7e3c0e2
1847134
5a8d1c0
c78b128
8cba032
e1f0142
3545278
a520af9
5103065
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # | ||
| # Copyright (c) TileDB Inc. under the MIT License | ||
| # | ||
|
|
||
| #' An S4 class for a TileDB Profile object | ||
| #' | ||
| #' @slot ptr An external pointer to the underlying implementation | ||
| #' @exportClass tiledb_profile | ||
| setClass("tiledb_profile", | ||
| slots = list(ptr = "externalptr") | ||
| ) | ||
|
|
||
| #' Raw display of a profile object | ||
| #' | ||
| #' This method uses the display method provided by the underlying library. | ||
| #' | ||
| #' @param object A profile object | ||
| #' @export | ||
| setMethod( | ||
| "raw_dump", | ||
| signature(object = "tiledb_profile"), | ||
| definition = function(object) libtiledb_profile_dump(object@ptr) | ||
| ) | ||
|
|
||
| #' Create a 'tiledb_profile' object | ||
| #' | ||
| #' @param name (optional) Name for the profile | ||
| #' @param dir (optional) Directory to create the profile in | ||
| #' @return A new 'tiledb_profile' object | ||
| #' @export | ||
| tiledb_profile <- function(name = NULL, dir = NULL) { | ||
| stopifnot(`The 'name' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| stopifnot(`The 'dir' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| ptr <- libtiledb_profile_new(name, dir) | ||
| profile <- new("tiledb_profile", ptr = ptr) | ||
| return(profile) | ||
| } | ||
|
|
||
| #' Load a saved 'tiledb_profile' object | ||
| #' | ||
| #' @param name (optional) Name of the profile to load | ||
| #' @param dir (optional) Directory where the profile to load is saved | ||
| #' @return The loaded 'tiledb_profile' object | ||
| #' @export | ||
| tiledb_profile_load <- function(name = NULL, dir = NULL) { | ||
| stopifnot(`The 'name' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| stopifnot(`The 'dir' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| ptr <- libtiledb_profile_load(name, dir) | ||
| profile <- new("tiledb_profile", ptr = ptr) | ||
| return(profile) | ||
| } | ||
|
|
||
| #' Remove a saved 'tiledb_profile' | ||
| #' | ||
| #' @param name (optional) Name of the profile to remove | ||
| #' @param dir (optional) Directory where the profile to remove is saved | ||
| #' @export | ||
| tiledb_profile_remove <- function(name = NULL, dir = NULL) { | ||
| stopifnot(`The 'name' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| stopifnot(`The 'dir' for the profile must be null or a character type` = is.null(name) || is.character(name)) | ||
| libtiledb_profile_remove(name, dir) | ||
| return(invisible(NULL)) | ||
| } | ||
|
|
||
| #' Get the name of a 'tiledb_profile' object | ||
| #' | ||
| #' @param profile A TileDB profile object | ||
| #' @return The name of the 'tiledb_profile' object | ||
| #' @export | ||
| tiledb_profile_name <- function(profile) { | ||
| stopifnot(`The 'profile' argument must be a tiledb_profile object` = is(profile, "tiledb_profile")) | ||
| name <- libtiledb_profile_name(profile@ptr) | ||
| return(name) | ||
| } | ||
|
|
||
| #' Get the directory of a 'tiledb_profile' object | ||
| #' | ||
| #' @param profile A TileDB profile object | ||
| #' @return The directory of the 'tiledb_profile' object | ||
| #' @export | ||
| tiledb_profile_dir <- function(profile) { | ||
| stopifnot(`The 'profile' argument must be a tiledb_profile object` = is(profile, "tiledb_profile")) | ||
| dir <- libtiledb_profile_dir(profile@ptr) | ||
| return(dir) | ||
| } | ||
|
|
||
| #' Set a parameter on the 'tiledb_profile' object | ||
| #' | ||
| #' @param profile A TileDB profile object | ||
| #' @param param The key for the new parameter | ||
| #' @param value The value for the new parameter | ||
| #' @export | ||
| tiledb_profile_set_param <- function(profile, param, value) { | ||
| stopifnot(`The 'profile' argument must be a tiledb_profile object` = is(profile, "tiledb_profile")) | ||
| stopifnot(`The 'param' arugment must have character type` = is.character(param)) | ||
| stopifnot(`The 'value' arugment must have character type` = is.character(value)) | ||
| libtiledb_profile_set_param(profile@ptr, param, value) | ||
| return(invisible(NULL)) | ||
| } | ||
|
|
||
| #' Get the value of a parameter set on the 'tiledb_profile' object | ||
| #' | ||
| #' @param profile A TileDB profile object | ||
| #' @param param The key for the parameter to fetch | ||
| #' @return The value of the requested parameter or NULL if no such parameter exists | ||
| #' @export | ||
| tiledb_profile_get_param <- function(profile, param) { | ||
| stopifnot(`The 'profile' argument must be a tiledb_profile object` = is(profile, "tiledb_profile")) | ||
| stopifnot(`The 'param' arugment must have character type` = is.character(param)) | ||
| value <- libtiledb_profile_get_param(profile@ptr, param) | ||
| return(value) | ||
| } | ||
|
|
||
| #' Save the 'tiledb_profile' object | ||
| #' | ||
| #' This will save the 'tiledb_profile' with the name and directory set at creation. | ||
| #' | ||
| #' @param profile The 'tiledb_profile' object to save | ||
| #' @export | ||
| tiledb_profile_save <- function(profile) { | ||
| stopifnot(`The 'profile' argument must be a tiledb_profile object` = is(profile, "tiledb_profile")) | ||
| libtiledb_profile_save(profile@ptr) | ||
| return(invisible(NULL)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| library(tinytest) | ||
| library(tiledb) | ||
|
|
||
| # Use base directory to prevent over-writing user profiles. | ||
| name <- "test_profile" | ||
| dir <- file.path(tempdir(), "tiledb_config_profile/") | ||
|
|
||
| # Create profile | ||
| profile <- tiledb_profile(name, dir) | ||
|
|
||
| # Set some parameters | ||
| token <- "12345" | ||
| server_address <- "https://profile_address.com" | ||
| tiledb_profile_set_param(profile, "rest.token", token) | ||
| tiledb_profile_set_param(profile, "rest.server_address", server_address) | ||
|
|
||
| # Save the profile | ||
| tiledb_profile_save(profile) | ||
|
|
||
|
|
||
| # Create a config and set the profile directory | ||
| config <- tiledb_config(c(profile_name = name, profile_dir = dir)) | ||
| config_profile_name <- config["profile_name"] | ||
| config_profile_dir <- config["profile_dir"] | ||
| config_rest_token <- config["rest.token"] | ||
| config_rest_server_address <- config["rest.server_address"] | ||
|
|
||
| # Test that the config parameters are set correctly | ||
| expect_equal(config["profile_name"], c(profile_name = name)) | ||
| expect_equal(config["profile_dir"], c(profile_dir = dir)) | ||
| expect_equal(config["rest.token"], c(rest.token = token)) | ||
| expect_equal(config["rest.server_address"], c(rest.server_address = server_address)) | ||
|
|
||
|
|
||
| # Remove the profile | ||
| tiledb_profile_remove(name, dir) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| library(tinytest) | ||
| library(tiledb) | ||
|
|
||
| # Use base directory to prevent over-writing user profiles. | ||
| base_dir <- tempdir() | ||
| dir1 <- file.path(base_dir, "tiledb_profile") | ||
|
|
||
|
|
||
| # 1. Test creating profiles. | ||
| profile1 <- tiledb_profile() | ||
| profile2 <- tiledb_profile("profile2") | ||
| profile3 <- tiledb_profile(dir=dir1) | ||
| profile4 <- tiledb_profile("profile4", dir1) | ||
|
|
||
| expect_equal(tiledb_profile_name(profile1), "default") | ||
| expect_equal(tiledb_profile_name(profile2), "profile2") | ||
| expect_equal(tiledb_profile_name(profile3), "default") | ||
| expect_equal(tiledb_profile_name(profile4), "profile4") | ||
|
|
||
| # Skipping checks for default directory since it is platform dependent. | ||
| # Normalize paths on Windows to UNIX-style | ||
| expected_dir <- normalizePath( | ||
| path = dir1, | ||
| winslash = "/", | ||
| mustWork = FALSE | ||
| ) | ||
| actual_dir_profile3 <- normalizePath( | ||
| path = tiledb_profile_dir(profile3), | ||
| winslash = "/", | ||
| mustWork = FALSE | ||
| ) | ||
| expect_identical( | ||
| # trim trailing slashes | ||
| current = sub(pattern = "/+$", replacement = "", x = actual_dir_profile3), | ||
| target = expected_dir | ||
| ) | ||
| actual_dir_profile4 <- normalizePath( | ||
| path = tiledb_profile_dir(profile4), | ||
| winslash = "/", | ||
| mustWork = FALSE | ||
| ) | ||
| expect_identical( | ||
| current = sub(pattern = "/+$", replacement = "", x = actual_dir_profile4), | ||
| target = expected_dir | ||
| ) | ||
|
|
||
|
|
||
| # 2. Test setting/getting profile parameters. | ||
| key1 <- "username" | ||
| key2 <- "server_address" | ||
| key3 <- "rest.token" | ||
| expected_value1 <- "my_username" | ||
| expected_value2 <- "https://my.address" | ||
| expected_value3 <- "123456" | ||
|
|
||
| tiledb_profile_set_param(profile4, key1, expected_value1) | ||
| tiledb_profile_set_param(profile4, key2, expected_value2) | ||
| tiledb_profile_set_param(profile4, key3, expected_value3) | ||
|
|
||
| actual_value1 <- tiledb_profile_get_param(profile4, key1) | ||
| actual_value2 <- tiledb_profile_get_param(profile4, key2) | ||
| actual_value3 <- tiledb_profile_get_param(profile4, key3) | ||
| actual_non_value <- tiledb_profile_get_param(profile4, "not_a_parameter") | ||
|
|
||
| expect_equal(actual_value1, expected_value1) | ||
| expect_equal(actual_value2, expected_value2) | ||
| expect_equal(actual_value3, expected_value3) | ||
| expect_true(is.null(actual_non_value)) | ||
|
|
||
| # 3. Test save, load, and remove. | ||
| # IMPORTANT: Do not save a profile to a location that might over-write an actual profile. | ||
| tiledb_profile_save(profile3) | ||
| tiledb_profile_save(profile4) | ||
|
|
||
| # -- Check can load profile3 | ||
| profile_loaded <- tiledb_profile_load(dir=dir1) | ||
|
|
||
| # -- Remove profile2. Check can no longer load. | ||
| tiledb_profile_remove(dir=dir1) | ||
| expect_error(tiledb_profile_load(dir=dir1)) | ||
|
|
||
| # -- Check loaded profile4 | ||
| profile_loaded <- tiledb_profile_load("profile4", dir1) | ||
| expect_equal(tiledb_profile_name(profile_loaded), "profile4") | ||
| expect_equal(tiledb_profile_name(profile_loaded), "profile4") | ||
| loaded_value1 <- tiledb_profile_get_param(profile_loaded, key1) | ||
| loaded_value2 <- tiledb_profile_get_param(profile_loaded, key2) | ||
| loaded_value3 <- tiledb_profile_get_param(profile_loaded, key3) | ||
| loaded_non_value <- tiledb_profile_get_param(profile_loaded, "not_a_parameter") | ||
| expect_equal(loaded_value1, expected_value1) | ||
| expect_equal(loaded_value2, expected_value2) | ||
| expect_equal(loaded_value3, expected_value3) | ||
| expect_true(is.null(loaded_non_value)) | ||
|
|
||
| # -- Remove profile4 | ||
| tiledb_profile_remove("profile4", dir1) | ||
|
|
||
| # -- Check cannot load profile2 or profile4. | ||
| expect_error(tiledb_profile_load(dir=dir1)) | ||
| expect_error(tiledb_profile_load("profile4", dir1)) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.