Skip to content

Commit d68ec0d

Browse files
Migrate away from deprecated TileDB C++ APIs (#1958)
* Migrate use of two deprecated APIs. * Update tests to use the new delete_fragments API. * Fix instance/static method conflict * Migrate use of tiledb_array_create_with_key deprecated API. * Migrate use of tiledb_array_consolidate_with_key deprecated API. --------- Co-authored-by: Theodore Tsirpanis <[email protected]>
1 parent f230b9c commit d68ec0d

File tree

5 files changed

+91
-60
lines changed

5 files changed

+91
-60
lines changed

tiledb/libmetadata.pyx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,12 @@ cdef class Metadata:
400400
ctx.__capsule__(), "ctx")
401401
tiledb_config_t* config_ptr = NULL
402402
tiledb_encryption_type_t key_type = TILEDB_NO_ENCRYPTION
403-
void* key_ptr = NULL
403+
const char* key_ptr = NULL
404404
uint32_t key_len = 0
405405
bytes bkey
406406
bytes buri = unicode_path(self.array.uri)
407407
str key = (<Array?>self.array).key
408+
tiledb_error_t* err_ptr = NULL
408409

409410
if config:
410411
config_ptr = <tiledb_config_t*>PyCapsule_GetPointer(
@@ -416,19 +417,28 @@ cdef class Metadata:
416417
else:
417418
bkey = bytes(self.array.key)
418419
key_type = TILEDB_AES_256_GCM
419-
key_ptr = <void *> PyBytes_AS_STRING(bkey)
420+
key_ptr = <const char *> PyBytes_AS_STRING(bkey)
420421
#TODO: unsafe cast here ssize_t -> uint64_t
421422
key_len = <uint32_t> PyBytes_GET_SIZE(bkey)
422423

424+
rc = tiledb_config_alloc(&config_ptr, &err_ptr)
425+
if rc != TILEDB_OK:
426+
_raise_ctx_err(ctx_ptr, rc)
427+
428+
rc = tiledb_config_set(config_ptr, "sm.encryption_type", "AES_256_GCM", &err_ptr)
429+
if rc != TILEDB_OK:
430+
_raise_ctx_err(ctx_ptr, rc)
431+
432+
rc = tiledb_config_set(config_ptr, "sm.encryption_key", key_ptr, &err_ptr)
433+
if rc != TILEDB_OK:
434+
_raise_ctx_err(ctx_ptr, rc)
435+
423436
cdef const char* buri_ptr = <const char*>buri
424437

425438
with nogil:
426-
rc = tiledb_array_consolidate_with_key(
439+
rc = tiledb_array_consolidate(
427440
ctx_ptr,
428441
buri_ptr,
429-
key_type,
430-
key_ptr,
431-
key_len,
432442
config_ptr)
433443
if rc != TILEDB_OK:
434444
_raise_ctx_err(ctx_ptr, rc)

tiledb/libtiledb.pxd

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,8 @@ cdef extern from "tiledb/tiledb.h":
651651
uint32_t key_length,
652652
tiledb_config_t* config) nogil
653653

654-
int tiledb_array_delete_array(
654+
int tiledb_array_delete(
655655
tiledb_ctx_t* ctx,
656-
tiledb_array_t* array,
657656
const char* uri) nogil
658657

659658
# Query
@@ -846,14 +845,6 @@ cdef extern from "tiledb/tiledb.h":
846845
const char* uri,
847846
const tiledb_array_schema_t* array_schema) nogil
848847

849-
int tiledb_array_create_with_key(
850-
tiledb_ctx_t* ctx,
851-
const char* uri,
852-
const tiledb_array_schema_t* array_schema,
853-
tiledb_encryption_type_t key_type,
854-
const void* key,
855-
unsigned int key_len) nogil
856-
857848
int tiledb_array_is_open(
858849
tiledb_ctx_t* ctx,
859850
tiledb_array_t* array,
@@ -864,17 +855,15 @@ cdef extern from "tiledb/tiledb.h":
864855
const char* array_path,
865856
tiledb_config_t* config) nogil
866857

867-
int tiledb_array_consolidate_with_key(
858+
int tiledb_array_delete_fragments(
868859
tiledb_ctx_t* ctx,
860+
tiledb_array_t* array,
869861
const char* uri,
870-
tiledb_encryption_type_t key_type,
871-
const void* key_ptr,
872-
unsigned int key_len,
873-
tiledb_config_t* config) nogil
862+
uint64_t timestamp_start,
863+
uint64_t timestamp_end)
874864

875-
int tiledb_array_delete_fragments(
865+
int tiledb_array_delete_fragments_v2(
876866
tiledb_ctx_t* ctx,
877-
tiledb_array_t* array,
878867
const char* uri,
879868
uint64_t timestamp_start,
880869
uint64_t timestamp_end)

tiledb/libtiledb.pyx

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,11 @@ cdef class Array(object):
940940

941941
cdef bytes bkey
942942
cdef tiledb_encryption_type_t key_type = TILEDB_NO_ENCRYPTION
943-
cdef void* key_ptr = NULL
943+
cdef const char* key_ptr = NULL
944944
cdef unsigned int key_len = 0
945945

946+
cdef tiledb_config_t* config_ptr = NULL
947+
cdef tiledb_error_t* err_ptr = NULL
946948
cdef int rc = TILEDB_OK
947949

948950
if key is not None:
@@ -951,10 +953,25 @@ cdef class Array(object):
951953
else:
952954
bkey = bytes(key)
953955
key_type = TILEDB_AES_256_GCM
954-
key_ptr = <void *> PyBytes_AS_STRING(bkey)
956+
key_ptr = <const char *> PyBytes_AS_STRING(bkey)
955957
#TODO: unsafe cast here ssize_t -> uint64_t
956958
key_len = <unsigned int> PyBytes_GET_SIZE(bkey)
957959

960+
rc = tiledb_config_alloc(&config_ptr, &err_ptr)
961+
if rc != TILEDB_OK:
962+
_raise_ctx_err(ctx_ptr, rc)
963+
964+
rc = tiledb_config_set(config_ptr, "sm.encryption_type", "AES_256_GCM", &err_ptr)
965+
if rc != TILEDB_OK:
966+
_raise_ctx_err(ctx_ptr, rc)
967+
968+
rc = tiledb_config_set(config_ptr, "sm.encryption_key", key_ptr, &err_ptr)
969+
if rc != TILEDB_OK:
970+
_raise_ctx_err(ctx_ptr, rc)
971+
rc = tiledb_ctx_alloc(config_ptr, &ctx_ptr)
972+
if rc != TILEDB_OK:
973+
_raise_ctx_err(ctx_ptr, rc)
974+
958975
if overwrite:
959976
if object_type(uri) == "array":
960977
if uri.startswith("file://") or "://" not in uri:
@@ -971,7 +988,7 @@ cdef class Array(object):
971988
"object to argument ctx")
972989
ctx_ptr = safe_ctx_ptr(ctx)
973990
with nogil:
974-
rc = tiledb_array_create_with_key(ctx_ptr, uri_ptr, schema_ptr, key_type, key_ptr, key_len)
991+
rc = tiledb_array_create(ctx_ptr, uri_ptr, schema_ptr)
975992
if rc != TILEDB_OK:
976993
_raise_ctx_err(ctx_ptr, rc)
977994
return
@@ -1246,7 +1263,8 @@ cdef class Array(object):
12461263
_raise_ctx_err(ctx_ptr, rc)
12471264
return Enumeration.from_capsule(self.ctx, PyCapsule_New(enum_ptr, "enum", NULL))
12481265

1249-
def delete_fragments(self, timestamp_start, timestamp_end):
1266+
@staticmethod
1267+
def delete_fragments(uri, timestamp_start, timestamp_end, ctx=None):
12501268
"""
12511269
Delete a range of fragments from timestamp_start to timestamp_end.
12521270
The array needs to be opened in 'm' mode as shown in the example below.
@@ -1270,24 +1288,31 @@ cdef class Array(object):
12701288
... A[:]
12711289
array([1., 1., 1., 1.])
12721290
1273-
>>> with tiledb.open(path, 'm') as A:
1274-
... A.delete_fragments(2, 2)
1291+
>>> tiledb.Array.delete_fragments(path, 2, 2)
12751292
12761293
>>> with tiledb.open(path, 'r') as A:
12771294
... A[:]
12781295
array([0., 0., 0., 0.])
12791296
12801297
"""
1281-
cdef tiledb_ctx_t* ctx_ptr = safe_ctx_ptr(self.ctx)
1282-
cdef tiledb_array_t* array_ptr = self.ptr
1283-
cdef tiledb_query_t* query_ptr = NULL
1284-
cdef bytes buri = self.uri.encode('UTF-8')
1298+
# If uri is an instance of Array (user calls the old instance method), issue a warning
1299+
if isinstance(uri, Array):
1300+
warnings.warn(
1301+
"The `tiledb.Array.delete_fragments` instance method is deprecated. Use the static method with the same name instead.",
1302+
DeprecationWarning,
1303+
)
1304+
uri = uri.uri
1305+
1306+
if not ctx:
1307+
ctx = default_ctx()
1308+
1309+
cdef tiledb_ctx_t* ctx_ptr = safe_ctx_ptr(ctx)
1310+
cdef bytes buri = uri.encode('UTF-8')
12851311

12861312
cdef int rc = TILEDB_OK
12871313

1288-
rc = tiledb_array_delete_fragments(
1314+
rc = tiledb_array_delete_fragments_v2(
12891315
ctx_ptr,
1290-
array_ptr,
12911316
buri,
12921317
timestamp_start,
12931318
timestamp_end
@@ -1324,12 +1349,10 @@ cdef class Array(object):
13241349

13251350
cdef tiledb_ctx_t* ctx_ptr = safe_ctx_ptr(ctx)
13261351
cdef bytes buri = uri.encode('UTF-8')
1327-
cdef ArrayPtr preload_ptr = preload_array(uri, 'm', None, None)
1328-
cdef tiledb_array_t* array_ptr = preload_ptr.ptr
13291352

13301353
cdef int rc = TILEDB_OK
13311354

1332-
rc = tiledb_array_delete_array(ctx_ptr, array_ptr, buri)
1355+
rc = tiledb_array_delete(ctx_ptr, buri)
13331356
if rc != TILEDB_OK:
13341357
_raise_ctx_err(ctx_ptr, rc)
13351358

@@ -3627,22 +3650,35 @@ def _consolidate_timestamp(uri, key=None, config=None, ctx=None, timestamp=None)
36273650
cdef:
36283651
bytes bkey
36293652
tiledb_encryption_type_t key_type = TILEDB_NO_ENCRYPTION
3630-
void* key_ptr = NULL
3653+
const char* key_ptr = NULL
36313654
unsigned int key_len = 0
3655+
tiledb_error_t* err_ptr = NULL
36323656

36333657
if key is not None:
36343658
if isinstance(key, str):
36353659
bkey = key.encode('ascii')
36363660
else:
36373661
bkey = bytes(key)
36383662
key_type = TILEDB_AES_256_GCM
3639-
key_ptr = <void *> PyBytes_AS_STRING(bkey)
3663+
key_ptr = <const char *> PyBytes_AS_STRING(bkey)
36403664
#TODO: unsafe cast here ssize_t -> uint64_t
36413665
key_len = <unsigned int> PyBytes_GET_SIZE(bkey)
3666+
3667+
rc = tiledb_config_alloc(&config_ptr, &err_ptr)
3668+
if rc != TILEDB_OK:
3669+
_raise_ctx_err(ctx_ptr, rc)
3670+
3671+
rc = tiledb_config_set(config_ptr, "sm.encryption_type", "AES_256_GCM", &err_ptr)
3672+
if rc != TILEDB_OK:
3673+
_raise_ctx_err(ctx_ptr, rc)
3674+
3675+
rc = tiledb_config_set(config_ptr, "sm.encryption_key", key_ptr, &err_ptr)
3676+
if rc != TILEDB_OK:
3677+
_raise_ctx_err(ctx_ptr, rc)
36423678

36433679
with nogil:
3644-
rc = tiledb_array_consolidate_with_key(
3645-
ctx_ptr, array_uri_ptr, key_type, key_ptr, key_len, config_ptr)
3680+
rc = tiledb_array_consolidate(
3681+
ctx_ptr, array_uri_ptr, config_ptr)
36463682
if rc != TILEDB_OK:
36473683
_raise_ctx_err(ctx_ptr, rc)
36483684
return uri

tiledb/tests/test_fragments.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,12 @@ def write_fragments(target_path, dshape, num_writes):
704704
if use_timestamps:
705705
assert frags.timestamp_range == ts
706706

707-
with tiledb.open(path, "m") as A:
708-
if use_timestamps:
709-
A.delete_fragments(3, 6)
710-
else:
711-
A.delete_fragments(
712-
frags.timestamp_range[2][0], frags.timestamp_range[5][1]
713-
)
707+
if use_timestamps:
708+
tiledb.Array.delete_fragments(path, 3, 6)
709+
else:
710+
tiledb.Array.delete_fragments(
711+
path, frags.timestamp_range[2][0], frags.timestamp_range[5][1]
712+
)
714713

715714
frags = tiledb.array_fragments(path)
716715
assert len(frags) == 6
@@ -755,13 +754,12 @@ def test_delete_fragments_with_schema_evolution(self, use_timestamps):
755754
assert_array_equal(A[:]["a1"], ts2_data)
756755
assert_array_equal(A[:]["a2"], ts2_data)
757756

758-
with tiledb.open(path, "m") as A:
759-
if use_timestamps:
760-
A.delete_fragments(2, 2)
761-
else:
762-
A.delete_fragments(
763-
frags.timestamp_range[1][0], frags.timestamp_range[1][1]
764-
)
757+
if use_timestamps:
758+
tiledb.Array.delete_fragments(path, 2, 2)
759+
else:
760+
tiledb.Array.delete_fragments(
761+
path, frags.timestamp_range[1][0], frags.timestamp_range[1][1]
762+
)
765763

766764
frags = tiledb.array_fragments(path)
767765
assert len(frags) == 1

tiledb/tests/test_libtiledb.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,10 @@ def write_fragments(target_path, dshape, num_writes):
370370
assert frags.timestamp_range == ts
371371

372372
if use_timestamps:
373-
with tiledb.open(path, "m") as arr:
374-
arr.delete_fragments(3, 6)
373+
tiledb.Array.delete_fragments(path, 3, 6)
375374
else:
376375
timestamps = [t[0] for t in tiledb.array_fragments(path).timestamp_range]
377-
with tiledb.open(path, "m") as arr:
378-
arr.delete_fragments(timestamps[2], timestamps[5])
376+
tiledb.Array.delete_fragments(path, timestamps[2], timestamps[5])
379377

380378
frags = tiledb.array_fragments(path)
381379
assert len(frags) == 6

0 commit comments

Comments
 (0)