-
Notifications
You must be signed in to change notification settings - Fork 18
/
Attribute.R
420 lines (378 loc) · 15.4 KB
/
Attribute.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# MIT License
#
# Copyright (c) 2017-2023 TileDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#' An S4 class for a TileDB attribute
#'
#' @slot ptr External pointer to the underlying implementation
#' @exportClass tiledb_attr
setClass("tiledb_attr",
slots = list(ptr = "externalptr"))
tiledb_attr.from_ptr <- function(ptr) {
stopifnot(`The 'ptr' argument must be a non-NULL external pointer to an Attribute instance` =
!missing(ptr) && is(ptr, "externalptr") && !is.null(ptr))
new("tiledb_attr", ptr = ptr)
}
#' Contructs a `tiledb_attr` object
#'
#' @param name The dimension name / label string; if missing default `""` is used.
#' @param type The tiledb_attr TileDB datatype string; if missing the user is alerted
#' that this is a _required_ parameter.
#' @param filter_list (default filter_list("NONE")) An optional tiledb_filter_list object
#' @param ncells (default 1) The number of cells, use \code{NA} to signal variable length
#' @param nullable (default FALSE) A logical switch whether the attribute can have missing
#' values
#' @param enumeration (default NULL) A character vector of dictionary values
#' @param ctx tiledb_ctx object (optional)
#' @return `tiledb_dim` object
#' @examples
#' \dontshow{ctx <- tiledb_ctx(limitTileDBCores())}
#' flt <- tiledb_filter_list(list(tiledb_filter("GZIP")))
#' attr <- tiledb_attr(name = "a1", type = "INT32",
#' filter_list = flt)
#' attr
#'
#' @importFrom methods new
#' @export
tiledb_attr <- function(name,
type,
filter_list = tiledb_filter_list(),
ncells = 1,
nullable = FALSE,
enumeration = NULL,
ctx = tiledb_get_context()
) {
if (missing(name)) name <- ""
if (is.na(ncells)) ncells <- NA_integer_ # the specific NA for ints (as basic NA is bool)
stopifnot("The 'name' argument must be a scalar string" = is.scalar(name, "character"),
"The 'type' argument is mandatory" = !missing(type),
"The 'ncells' argument must be numeric or NA" = is.integer(ncells) || is.na(ncells),
"The 'filter_list' argument must be a tiledb_filter_list instance" =
is(filter_list, "tiledb_filter_list"),
"The 'ctx' argument must be a tiledb_ctx" = is(ctx, "tiledb_ctx"))
ptr <- libtiledb_attribute(ctx@ptr, name, type, filter_list@ptr, ncells, nullable)
attr <- new("tiledb_attr", ptr = ptr)
if (!is.null(enumeration))
attr <- tiledb_attribute_set_enumeration_name(attr, name, ctx)
invisible(attr)
}
#' Raw display of an attribute object
#'
#' This method used the display method provided by the underlying
#' library.
#'
#' @param object An attribute object
#' @export
setMethod("raw_dump",
signature(object = "tiledb_attr"),
definition = function(object) libtiledb_attribute_dump(object@ptr))
# internal function returning text use here and in other higher-level show() methods
.as_text_attribute <- function(object, arrptr=NULL) {
fl <- filter_list(object)
ndct <- 0 # default
dct <- character() # default
ord <- FALSE # default
if (!is.null(arrptr)) {
if (!libtiledb_array_is_open_for_reading(arrptr)) arrptr <- libtiledb_array_open_with_ptr(arrptr, "READ")
if (tiledb_attribute_has_enumeration(object)) {
dct <- tiledb_attribute_get_enumeration_ptr(object, arrptr)
ord <- tiledb_attribute_is_ordered_enumeration_ptr(object, arrptr)
ndct <- length(dct)
}
}
dictionary_txt <- if (ord) "ordered_dictionary" else "dictionary"
txt <- paste0("tiledb_attr(name=\"", name(object), "\", ",
"type=\"", datatype(object), "\", ",
"ncells=", cell_val_num(object), ", ",
"nullable=", tiledb_attribute_get_nullable(object),
if (nfilters(fl) > 0) paste0(", filter_list=", .as_text_filter_list(fl)),
if (ndct > 0) paste0(", ", dictionary_txt, "=c(\"", paste(dct[seq(1, min(5, ndct))], collapse="\",\""), if (ndct > 5) "\",...", "\")"))
txt <- paste0(txt, ")")
txt
}
#' Prints an attribute object
#'
#' @param object An attribute object
#' @export
setMethod("show",
signature(object = "tiledb_attr"),
definition = function(object) {
cat(.as_text_attribute(object), "\n")
})
#' @rdname generics
#' @export
setGeneric("name", function(object) standardGeneric("name"))
#' Return the `tiledb_attr` name
#'
#' @param object `tiledb_attr` object
#' @return string name, empty string if the attribute is anonymous
#' @examples
#' \dontshow{ctx <- tiledb_ctx(limitTileDBCores())}
#' a1 <- tiledb_attr("a1", type = "INT32")
#' name(a1)
#'
#' a2 <- tiledb_attr(type = "INT32")
#' name(a2)
#'
#' @export
setMethod("name", signature(object = "tiledb_attr"),
function(object) {
libtiledb_attribute_get_name(object@ptr)
})
#' @rdname generics
#' @export
setGeneric("datatype", function(object) standardGeneric("datatype"))
#' Return the `tiledb_attr` datatype
#'
#' @param object `tiledb_attr` object
#' @return tiledb datatype string
#' @examples
#' a1 <- tiledb_attr("a1", type = "INT32")
#' datatype(a1)
#'
#' a2 <- tiledb_attr("a1", type = "FLOAT64")
#' datatype(a2)
#'
#' @export
setMethod("datatype", signature(object = "tiledb_attr"),
function(object) {
libtiledb_attribute_get_type(object@ptr)
})
## Generic in ArraySchema.R
#' Returns the TileDB Filter List object associated with the given TileDB Attribute
#'
#' @param object TileDB Attribute
#' @return a tiledb_filter_list object
#' @examples
#' \dontshow{ctx <- tiledb_ctx(limitTileDBCores())}
#' attr <- tiledb_attr(type = "INT32", filter_list=tiledb_filter_list(list(tiledb_filter("ZSTD"))))
#' filter_list(attr)
#'
#' @export
setMethod("filter_list", "tiledb_attr", function(object) {
ptr <- libtiledb_attribute_get_filter_list(object@ptr)
return(tiledb_filter_list.from_ptr(ptr))
})
## Generic in ArraySchema.R
#' Sets the TileDB Filter List for the TileDB Attribute object
#'
#' @param x TileDB Attribute
#' @param value TileDB Filter List
#' @return The modified TileDB Attribute object
#' @export
setReplaceMethod("filter_list", "tiledb_attr", function(x, value) {
x@ptr <- libtiledb_attribute_set_filter_list(x@ptr, value@ptr)
x
})
#' @rdname tiledb_attribute_get_cell_val_num
#' @export
setGeneric("cell_val_num", function(object) standardGeneric("cell_val_num"))
#' @rdname tiledb_attribute_get_cell_val_num
#' @export
setMethod("cell_val_num", signature(object = "tiledb_attr"), function(object) {
libtiledb_attribute_get_cell_val_num(object@ptr)
})
#' Return the number of scalar values per attribute cell
#'
#' @param object `tiledb_attr` object
#' @return integer number of cells
#' @examples
#' \dontshow{ctx <- tiledb_ctx(limitTileDBCores())}
#' a1 <- tiledb_attr("a1", type = "FLOAT64", ncells = 1)
#' cell_val_num(a1)
#' @export
tiledb_attribute_get_cell_val_num <- function(object) {
libtiledb_attribute_get_cell_val_num(object@ptr)
}
#' @rdname tiledb_attribute_set_cell_val_num
#' @export
setGeneric("cell_val_num<-", function(x, value) standardGeneric("cell_val_num<-"))
#' @rdname tiledb_attribute_set_cell_val_num
#' @export
setReplaceMethod("cell_val_num", signature("tiledb_attr"), function(x, value) {
libtiledb_attribute_set_cell_val_num(x@ptr, value)
x
})
#' Set the number of scalar values per attribute cell
#'
#' @param x A TileDB Attribute object
#' @param value An integer value of number of cells
#' @return The modified attribute is returned
#' @export
tiledb_attribute_set_cell_val_num <- function(x, value) {
libtiledb_attribute_set_cell_val_num(x@ptr, value)
}
#' Returns TRUE if the tiledb_dim is anonymous
#'
#' A TileDB attribute is anonymous if no name/label is defined
#'
#' @param object `tiledb_attr` object
#' @return TRUE or FALSE
#' @examples
#' \dontshow{ctx <- tiledb_ctx(limitTileDBCores())}
#' a1 <- tiledb_attr("a1", type = "FLOAT64")
#' is.anonymous(a1)
#'
#' a2 <- tiledb_attr("", type = "FLOAT64")
#' is.anonymous(a2)
#'
#' @export
is.anonymous <- function(object) UseMethod("is.anonymous", object)
#' @rdname is.anonymous
#' @export
is.anonymous.tiledb_attr <- function(object) {
name <- libtiledb_attribute_get_name(object@ptr)
nchar(name) == 0
}
#' Get the fill value for a TileDB Attribute
#'
#' @param attr A TileDB Attribute object
#' @return The fill value for the attribute
#' @export
tiledb_attribute_get_fill_value <- function(attr) {
stopifnot(attr_object=is(attr, "tiledb_attr"))
libtiledb_attribute_get_fill_value(attr@ptr)
}
#' Set the fill value for a TileDB Attribute
#'
#' @param attr A TileDB Attribute object
#' @param value A fill value
#' @return \code{NULL} is returned invisibly
#' @export
tiledb_attribute_set_fill_value <- function(attr, value) {
stopifnot(`The first argument must be an attribute` = is(attr, "tiledb_attr"),
`The second argument must be int, numeric or char` = is.integer(value) || is.numeric(value) || is.character(value))
libtiledb_attribute_set_fill_value(attr@ptr, value)
invisible()
}
#' Check whether TileDB Attribute is variable-sized
#'
#' @param attr A TileDB Attribute object
#' @return A boolean value indicating variable-size or not
#' @export
tiledb_attribute_is_variable_sized <- function(attr) {
stopifnot(`The argument must be an attribute` = is(attr, "tiledb_attr"))
libtiledb_attribute_is_variable_sized(attr@ptr)
}
#' Get the TileDB Attribute cell size
#'
#' @param attr A TileDB Attribute object
#' @return A numeric value with the cell size
#' @export
tiledb_attribute_get_cell_size <- function(attr) {
stopifnot(`The argument must be an attribute` = is(attr, "tiledb_attr"))
libtiledb_attribute_get_cell_size(attr@ptr)
}
#' Set the TileDB Attribute Nullable flags
#'
#' @param attr A TileDB Attribute object
#' @param flag A boolean flag to turn \sQuote{Nullable} on or off
#' @return Nothing is returned
#' @export
tiledb_attribute_set_nullable <- function(attr, flag) {
stopifnot(`The first argument must be an attribute` = is(attr, "tiledb_attr"),
`The second argument must be a logical` = is.logical(flag) && !is.na(flag))
libtiledb_attribute_set_nullable(attr@ptr, flag)
}
#' Get the TileDB Attribute Nullable flag value
#'
#' @param attr A TileDB Attribute object
#' @return A boolean value with the \sQuote{Nullable} status
#' @export
tiledb_attribute_get_nullable <- function(attr) {
stopifnot(`The argument must be an attribute` = is(attr, "tiledb_attr"))
libtiledb_attribute_get_nullable(attr@ptr)
}
#' Test if TileDB Attribute has an Enumeration
#'
#' @param attr A TileDB Attribute object
#' @param ctx A Tiledb Context object (optional)
#' @return A logical value indicating if the attribute has an enumeration
#' @export
tiledb_attribute_has_enumeration <- function(attr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"))
libtiledb_attribute_has_enumeration(ctx@ptr, attr@ptr)
}
#' Get the TileDB Attribute Enumeration
#'
#' @param attr A TileDB Attribute object
#' @param arr A Tiledb Array object
#' @param ctx A Tiledb Context object (optional)
#' @return A character vector with the enumeration (of length zero if none)
#' @export
tiledb_attribute_get_enumeration <- function(attr, arr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arr' argument must be an array" = is(arr, "tiledb_array"))
libtiledb_attribute_get_enumeration(ctx@ptr, attr@ptr, arr@ptr)
}
#' @rdname tiledb_attribute_get_enumeration
#' @param arrptr A Tiledb Array object pointer
#' @export
tiledb_attribute_get_enumeration_ptr <- function(attr, arrptr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arrptr' argument must be an external pointer" = is(arrptr, "externalptr"))
libtiledb_attribute_get_enumeration(ctx@ptr, attr@ptr, arrptr)
}
#' Set a TileDB Attribute Enumeration Name
#'
#' @param attr A TileDB Attribute object
#' @param enum_name A character value with the enumeration value
#' @param ctx A Tiledb Context object (optional)
#' @return The modified TileDB Attribute object
#' @export
tiledb_attribute_set_enumeration_name <- function(attr, enum_name, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'enum_name' argument must be character" = is.character(enum_name))
attr@ptr <- libtiledb_attribute_set_enumeration(ctx@ptr, attr@ptr, enum_name)
attr
}
#' Check if TileDB Attribute Enumeration is Ordered
#'
#' @param attr A Tiledb Array object
#' @param arrptr A Tiledb Array object pointer
#' @param ctx A Tiledb Context object (optional)
#' @return A character vector with the enumeration (of length zero if none)
#' @export
tiledb_attribute_is_ordered_enumeration_ptr <- function(attr, arrptr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arrptr' argument must be an external pointer" = is(arrptr, "externalptr"))
libtiledb_attribute_is_ordered_enumeration(ctx@ptr, attr@ptr, arrptr)
}
# internal function to access enumeration data type
#' @noRd
tiledb_attribute_get_enumeration_type <- function(attr, arr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arr' argument must be an array" = is(arr, "tiledb_array"))
libtiledb_attribute_get_enumeration_type(ctx@ptr, attr@ptr, arr@ptr)
}
# internal function to access enumeration data type
#' @noRd
tiledb_attribute_get_enumeration_type_ptr <- function(attr, arrptr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arrptr' argument must be an external pointer" = is(arrptr, "externalptr"))
libtiledb_attribute_get_enumeration_type(ctx@ptr, attr@ptr, arrptr)
}
# internal function to get (non-string) enumeration vector
#' @noRd
tiledb_attribute_get_enumeration_vector_ptr <- function(attr, arrptr, ctx = tiledb_get_context()) {
stopifnot("The 'attr' argument must be an attribute" = is(attr, "tiledb_attr"),
"The 'arrptr' argument must be an external pointer" = is(arrptr, "externalptr"))
libtiledb_attribute_get_enumeration_vector(ctx@ptr, attr@ptr, arrptr)
}