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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import weakref
from collections.abc import MutableMapping

from cython.operator cimport dereference as deref

from .datatypes import DataType

_NP_DATA_PREFIX = "__np_flat_"
_NP_SHAPE_PREFIX = "__np_shape_"


cdef extern from "Python.h":
    int PyBUF_READ
    object PyMemoryView_FromMemory(char*, Py_ssize_t, int)



cdef object unpack_metadata_val(
        tiledb_datatype_t value_type, uint32_t value_num, const char* value_ptr
    ):
    assert value_num != 0, "internal error: unexpected value_num==0"

    if value_type == TILEDB_STRING_UTF8:
        return value_ptr[:value_num].decode('UTF-8')  if value_ptr != NULL else ''

    if value_type in (TILEDB_BLOB, TILEDB_CHAR, TILEDB_STRING_ASCII):
        return value_ptr[:value_num] if value_ptr != NULL else b''

    if value_ptr == NULL:
        return ()

    unpacked = [None] * value_num
    cdef uint64_t itemsize = tiledb_datatype_size(value_type)
    for i in range(value_num):
        if value_type == TILEDB_INT64:
            unpacked[i] = deref(<int64_t *> value_ptr)
        elif value_type == TILEDB_FLOAT64:
            unpacked[i] = deref(<double *> value_ptr)
        elif value_type == TILEDB_FLOAT32:
            unpacked[i] = deref(<float *> value_ptr)
        elif value_type == TILEDB_INT32:
            unpacked[i] = deref(<int32_t *> value_ptr)
        elif value_type == TILEDB_UINT32:
            unpacked[i] = deref(<uint32_t *> value_ptr)
        elif value_type == TILEDB_UINT64:
            unpacked[i] = deref(<uint64_t *> value_ptr)
        elif value_type == TILEDB_INT8:
            unpacked[i] = deref(<int8_t *> value_ptr)
        elif value_type == TILEDB_UINT8:
            unpacked[i] = deref(<uint8_t *> value_ptr)
        elif value_type == TILEDB_INT16:
            unpacked[i] = deref(<int16_t *> value_ptr)
        elif value_type == TILEDB_UINT16:
            unpacked[i] = deref(<uint16_t *> value_ptr)
        else:
            raise NotImplementedError(f"TileDB datatype '{value_type}' not supported")
        value_ptr += itemsize

    # we don't differentiate between length-1 sequences and scalars
    return unpacked[0] if value_num == 1 else tuple(unpacked)


cdef np.ndarray unpack_metadata_ndarray(
        tiledb_datatype_t value_type, uint32_t value_num, const char* value_ptr
    ):
    cdef np.dtype dtype = DataType.from_tiledb(value_type).np_dtype
    if value_ptr == NULL:
        return np.array((), dtype=dtype)

    # special case for TILEDB_STRING_UTF8: TileDB assumes size=1
    if value_type != TILEDB_STRING_UTF8:
        value_num *= dtype.itemsize

    return np.frombuffer(PyMemoryView_FromMemory(<char*>value_ptr, value_num, PyBUF_READ),
                         dtype=dtype).copy()


cdef object unpack_metadata(
        bint is_ndarray,
        tiledb_datatype_t value_type,
        uint32_t value_num,
        const char * value_ptr
    ):
    if value_ptr == NULL and value_num != 1:
        raise KeyError

    if is_ndarray:
        return unpack_metadata_ndarray(value_type, value_num, value_ptr)
    else:
        return unpack_metadata_val(value_type, value_num, value_ptr)


cdef put_metadata(Array array, key, value):
    cdef:
        tiledb_datatype_t tiledb_type
        uint32_t value_num
        cdef const unsigned char[:] data_view
        cdef const void* data_ptr
        tiledb_ctx_t* ctx_ptr = NULL

    if isinstance(value, np.ndarray):
        if value.ndim != 1:
            raise TypeError(f"Only 1D Numpy arrays can be stored as metadata")

        dt = DataType.from_numpy(value.dtype)
        if dt.ncells != 1:
            raise TypeError(f"Unsupported dtype '{value.dtype}'")

        tiledb_type = dt.tiledb_type
        value_num = len(value)
        # special case for TILEDB_STRING_UTF8: TileDB assumes size=1
        if tiledb_type == TILEDB_STRING_UTF8:
            value_num *= value.itemsize
        data_ptr = np.PyArray_DATA(value)
    else:
        from .metadata import pack_metadata_val
        packed_buf = pack_metadata_val(value)
        tiledb_type = packed_buf.tdbtype
        value_num = packed_buf.value_num
        data_view = packed_buf.data
        data_ptr = &data_view[0] if value_num > 0 else NULL

    key_utf8 = key.encode('UTF-8')
    cdef const char* key_utf8_ptr = <const char*>key_utf8
    cdef int rc = TILEDB_OK
    ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer(array.ctx.__capsule__(), "ctx")
    with nogil:
        rc = tiledb_array_put_metadata(
            ctx_ptr,
            array.ptr,
            key_utf8_ptr,
            tiledb_type,
            value_num,
            data_ptr,
        )
    if rc != TILEDB_OK:
        _raise_ctx_err(ctx_ptr, rc)


cdef object get_metadata(Array array, key, is_ndarray=False):
    cdef:
        tiledb_datatype_t value_type
        uint32_t value_num = 0
        const char* value_ptr = NULL
        bytes key_utf8 = key.encode('UTF-8')
        const char* key_utf8_ptr = <const char*>key_utf8
        tiledb_ctx_t* ctx_ptr = NULL

    cdef int32_t rc = TILEDB_OK
    ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer(array.ctx.__capsule__(), "ctx")
    with nogil:
        rc = tiledb_array_get_metadata(
            ctx_ptr,
            array.ptr,
            key_utf8_ptr,
            &value_type,
            &value_num,
            <const void**>&value_ptr,
        )
    if rc != TILEDB_OK:
        _raise_ctx_err(ctx_ptr, rc)

    return unpack_metadata(is_ndarray, value_type, value_num, value_ptr)


def iter_metadata(Array array, keys_only, dump=False):
    """
    Iterate over array metadata keys or (key, value) tuples

    :param array: tiledb_array_t
    :param keys_only: whether to yield just keys or values too
    """
    cdef:
        tiledb_ctx_t* ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer(
            array.ctx.__capsule__(), "ctx")
        tiledb_array_t* array_ptr = array.ptr
        uint64_t metadata_num
        const char* key_ptr = NULL
        uint32_t key_len
        tiledb_datatype_t value_type
        uint32_t value_num
        const char* value_ptr = NULL
        const char* value_type_str = NULL
    
    if keys_only and dump:
        raise ValueError("keys_only and dump cannot both be True")

    cdef int32_t rc = TILEDB_OK
    with nogil:
        rc = tiledb_array_get_metadata_num(ctx_ptr, array_ptr, &metadata_num)
    if rc != TILEDB_OK:
        _raise_ctx_err(ctx_ptr, rc)

    for i in range(metadata_num):
        with nogil:
            rc = tiledb_array_get_metadata_from_index(
                ctx_ptr,
                array_ptr,
                i,
                &key_ptr,
                &key_len,
                &value_type,
                &value_num,
                <const void**>&value_ptr,
            )
        if rc != TILEDB_OK:
            _raise_ctx_err(ctx_ptr, rc)

        key = key_ptr[:key_len].decode('UTF-8')

        if keys_only:
            yield key
        else:
            value = unpack_metadata(key.startswith(_NP_DATA_PREFIX),
                                    value_type, value_num, value_ptr)

            if dump:
                rc = tiledb_datatype_to_str(value_type, &value_type_str)
                if rc != TILEDB_OK:
                    _raise_ctx_err(ctx_ptr, rc)

                yield (
                    "### Array Metadata ###\n"
                    f"- Key: {key}\n"
                    f"- Value: {value}\n"
                    f"- Type: {value_type_str.decode('UTF-8')}\n"
                )
            else:
                yield key, value


cdef class Metadata:
    def __init__(self, array):
        self.array_ref = weakref.ref(array)

    @property
    def array(self):
        assert self.array_ref() is not None, \
            "Internal error: invariant violation ([] from gc'd Array)"
        return self.array_ref()

    def __setitem__(self, key, value):
        if not isinstance(key, str):
            raise TypeError(f"Unexpected key type '{type(key)}': expected str")

        # ensure previous key(s) are deleted (e.g. in case of replacing a
        # non-numpy value with a numpy value or vice versa)
        del self[key]

        if isinstance(value, np.ndarray):
            flat_value = value.ravel()
            put_metadata(self.array, _NP_DATA_PREFIX + key, flat_value)
            if value.shape != flat_value.shape:
                put_metadata(self.array, _NP_SHAPE_PREFIX + key, value.shape)
        else:
            put_metadata(self.array, key, value)

    def __getitem__(self, key):
        if not isinstance(key, str):
            raise TypeError(f"Unexpected key type '{type(key)}': expected str")

        array = self.array
        try:
            return get_metadata(array, key)
        except KeyError as ex:
            try:
                np_array = get_metadata(array, _NP_DATA_PREFIX + key, is_ndarray=True)
            except KeyError:
                raise KeyError(key) from None

            try:
                shape = get_metadata(array, _NP_SHAPE_PREFIX + key)
            except KeyError:
                return np_array
            else:
                return np_array.reshape(shape)

    def __delitem__(self, key):
        if not isinstance(key, str):
            raise TypeError(f"Unexpected key type '{type(key)}': expected str")

        cdef:
            tiledb_ctx_t* ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer((
                <Array>self.array).ctx.__capsule__(), "ctx")
            tiledb_array_t* array_ptr = (<Array>self.array).ptr
            const char* key_utf8_ptr
            int32_t rc

        # key may be stored as is or it may be prefixed (for numpy values)
        # we don't know this here so delete all potential internal keys
        for k in key, _NP_DATA_PREFIX + key, _NP_SHAPE_PREFIX + key:
            key_utf8 = k.encode('UTF-8')
            key_utf8_ptr = <const char*>key_utf8
            with nogil:
                rc = tiledb_array_delete_metadata(ctx_ptr, array_ptr, key_utf8_ptr)
            if rc != TILEDB_OK:
                _raise_ctx_err(ctx_ptr, rc)

    def __contains__(self, key):
        if not isinstance(key, str):
            raise TypeError(f"Unexpected key type '{type(key)}': expected str")

        cdef:
            tiledb_ctx_t* ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer((
                <Array>self.array).ctx.__capsule__(), "ctx")
            tiledb_array_t* array_ptr = (<Array>self.array).ptr
            bytes key_utf8 = key.encode('UTF-8')
            const char* key_utf8_ptr = <const char*>key_utf8
            tiledb_datatype_t value_type
            int32_t has_key

        cdef int32_t rc = TILEDB_OK
        with nogil:
            rc = tiledb_array_has_metadata_key(
                ctx_ptr,
                array_ptr,
                key_utf8_ptr,
                &value_type,
                &has_key,
            )
        if rc != TILEDB_OK:
            _raise_ctx_err(ctx_ptr, rc)

        # if key doesn't exist, check the _NP_DATA_PREFIX prefixed key
        if not has_key and not key.startswith(_NP_DATA_PREFIX):
            has_key = self.__contains__(_NP_DATA_PREFIX + key)

        return bool(has_key)

    def consolidate(self):
        """
        Consolidate array metadata. Array must be closed.

        :return:
        """
        # TODO: ensure that the array is not x-locked?
        ctx = (<Array?> self.array).ctx
        config = ctx.config()
        cdef:
            uint32_t rc = 0
            tiledb_ctx_t* ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer(
                ctx.__capsule__(), "ctx")
            tiledb_config_t* config_ptr = NULL
            tiledb_encryption_type_t key_type = TILEDB_NO_ENCRYPTION
            const char* key_ptr = NULL
            uint32_t key_len = 0
            bytes bkey
            bytes buri = unicode_path(self.array.uri)
            str key = (<Array?>self.array).key
            tiledb_error_t* err_ptr = NULL

        if config:
            config_ptr = <tiledb_config_t*>PyCapsule_GetPointer(
                config.__capsule__(), "config")

        if key is not None:
            if isinstance(key, str):
                bkey = key.encode('ascii')
            else:
                bkey = bytes(self.array.key)
            key_type = TILEDB_AES_256_GCM
            key_ptr = <const char *> PyBytes_AS_STRING(bkey)
            #TODO: unsafe cast here ssize_t -> uint64_t
            key_len = <uint32_t> PyBytes_GET_SIZE(bkey)

            rc = tiledb_config_alloc(&config_ptr, &err_ptr)
            if rc != TILEDB_OK:
                _raise_ctx_err(ctx_ptr, rc)

            rc = tiledb_config_set(config_ptr, "sm.encryption_type", "AES_256_GCM", &err_ptr)
            if rc != TILEDB_OK:
                _raise_ctx_err(ctx_ptr, rc)

            rc = tiledb_config_set(config_ptr, "sm.encryption_key", key_ptr, &err_ptr)
            if rc != TILEDB_OK:
                _raise_ctx_err(ctx_ptr, rc)

        cdef const char* buri_ptr = <const char*>buri

        with nogil:
            rc = tiledb_array_consolidate(
                    ctx_ptr,
                    buri_ptr,
                    config_ptr)
        if rc != TILEDB_OK:
            _raise_ctx_err(ctx_ptr, rc)

    get = MutableMapping.get
    update = MutableMapping.update

    def setdefault(self, key, default=None):
        raise NotImplementedError("Metadata.setdefault requires read-write access to array")

    def pop(self, key, default=None):
        raise NotImplementedError("Metadata.pop requires read-write access to array")

    def popitem(self):
        raise NotImplementedError("Metadata.popitem requires read-write access to array")

    def clear(self):
        raise NotImplementedError("Metadata.clear requires read-write access to array")

    def __len__(self):
        cdef:
            tiledb_ctx_t* ctx_ptr = <tiledb_ctx_t*>PyCapsule_GetPointer((
                <Array>self.array).ctx.__capsule__(), "ctx")
            tiledb_array_t* array_ptr = (<Array>self.array).ptr
            uint64_t num

        cdef int32_t rc = TILEDB_OK
        with nogil:
            rc = tiledb_array_get_metadata_num(ctx_ptr, array_ptr, &num)
        if rc != TILEDB_OK:
            _raise_ctx_err(ctx_ptr, rc)

        # subtract the _NP_SHAPE_PREFIX prefixed keys
        for key in iter_metadata(self.array, keys_only=True):
            if key.startswith(_NP_SHAPE_PREFIX):
                num -= 1

        return num

    def __iter__(self):
        np_data_prefix_len = len(_NP_DATA_PREFIX)
        for key in iter_metadata(self.array, keys_only=True):
            if key.startswith(_NP_DATA_PREFIX):
                yield key[np_data_prefix_len:]
            elif not key.startswith(_NP_SHAPE_PREFIX):
                yield key
            # else: ignore the shape keys

    def keys(self):
        """
        Return metadata keys as list.

        :return: List of keys
        """
        # TODO this should be an iterator/view
        return list(self)

    def values(self):
        """
        Return metadata values as list.

        :return: List of values
        """
        # TODO this should be an iterator/view
        return [v for k, v in self._iteritems()]

    def items(self):
        # TODO this should be an iterator/view
        return tuple(self._iteritems())

    def _iteritems(self):
        np_data_prefix_len = len(_NP_DATA_PREFIX)
        np_shape_prefix_len = len(_NP_SHAPE_PREFIX)
        ndarray_items = []
        np_shape_map = {}

        # 1. yield all non-ndarray (key, value) pairs and keep track of
        # the ndarray data and shape to assemble them later
        for key, value in iter_metadata(self.array, keys_only=False):
            if key.startswith(_NP_DATA_PREFIX):
                ndarray_items.append((key[np_data_prefix_len:], value))
            elif key.startswith(_NP_SHAPE_PREFIX):
                np_shape_map[key[np_shape_prefix_len:]] = value
            else:
                yield key, value

        # 2. yield all ndarray (key, value) pairs after reshaping (if necessary)
        for key, value in ndarray_items:
            shape = np_shape_map.get(key)
            if shape is not None:
                value = value.reshape(shape)
            yield key, value

    def dump(self):
        for metadata in iter_metadata(self.array, keys_only=False, dump=True):
            print(metadata)