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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
from typing import MutableMapping, Optional, Union

import numpy as np

import tiledb.cc as lt

from .ctx import Config, Ctx, CtxMixin, default_ctx
from .datatypes import DataType
from .object import Object


class Group(CtxMixin, lt.Group):
    """
    Support for organizing multiple arrays in arbitrary directory hierarchies.

    Group members may be any number of nested groups and arrays. Members are stored as tiledb.Objects which indicate the member's URI and type.

    Groups may contain associated metadata similar to array metadata where
    keys are strings. Singleton values may be of type int, float, str, or bytes. Multiple values of the same type may be placed in containers of type list, tuple, or 1-D np.ndarray. The values within containers are limited to type int or float.

    See more at: https://docs.tiledb.com/main/background/key-concepts-and-data-format#arrays-and-groups

    :param uri: The URI to the Group
    :type uri: str
    :param mode: Read mode ('r'), write mode ('w'), or modify exclusive ('m')
    :type mode: str
    :param config: A TileDB config
    :type config: Config or dict
    :param ctx: A TileDB context
    :type ctx: tiledb.Ctx

    **Example:**

    >>> # Create a group
    >>> grp_path = "root_group"
    >>> tiledb.Group.create(grp_path)
    >>> grp = tiledb.Group(grp_path, "w")
    >>>
    >>> # Create an array and add as a member to the group
    >>> array_path = "array.tdb"
    >>> domain = tiledb.Domain(tiledb.Dim(domain=(1, 8), tile=2))
    >>> a1 = tiledb.Attr("val", dtype="f8")
    >>> schema = tiledb.ArraySchema(domain=domain, attrs=(a1,))
    >>> tiledb.Array.create(array_path, schema)
    >>> grp.add(array_path)
    >>>
    >>> # Create a group and add as a subgroup
    >>> subgrp_path = "sub_group"
    >>> tiledb.Group.create(subgrp_path)
    >>> grp.add(subgrp_path)
    >>>
    >>> # Add metadata to the subgroup
    >>> grp.meta["ints"] = [1, 2, 3]
    >>> grp.meta["str"] = "string_metadata"
    >>> grp.close()
    >>>
    >>> grp.open("r")
    >>> # Dump all the members in string format
    >>> mbrs_repr = grp
    >>> # Or create a list of Objects in the Group
    >>> mbrs_iter = list(grp)
    >>> # Get the first member's uri and type
    >>> member_uri, member_type = grp[0].uri, grp[0].type
    >>> grp.close()
    >>>
    >>> # Remove the subgroup
    >>> grp.open("w")
    >>> grp.remove(subgrp_path)
    >>> grp.close()
    >>>
    >>> # Delete the subgroup
    >>> grp.open("m")
    >>> grp.delete(subgrp_path)
    >>> grp.close()
    """

    _NP_DATA_PREFIX = "__np_flat_"
    _NP_SHAPE_PREFIX = "__np_shape_"

    _mode_to_query_type = {
        "r": lt.QueryType.READ,
        "w": lt.QueryType.WRITE,
        "m": lt.QueryType.MODIFY_EXCLUSIVE,
    }

    _query_type_to_mode = {t: m for m, t in _mode_to_query_type.items()}

    __was_deleted__ = False

    class GroupMetadata(MutableMapping):
        """
        Holds metadata for the associated Group in a dictionary-like structure.
        """

        GroupMetadataValueType = Union[int, float, str, bytes, np.ndarray]

        def __init__(self, group: "Group"):
            self._group = group

        def __setitem__(self, key: str, value: GroupMetadataValueType):
            """
            :param str key: Key for the Group metadata entry
            :param value: Value for the Group metadata entry
            :type value: Union[int, float, str, bytes, np.ndarray]

            """
            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]

            put_metadata = self._group._put_metadata
            if isinstance(value, np.ndarray):
                flat_value = value.ravel()
                put_metadata(f"{Group._NP_DATA_PREFIX}{key}", flat_value)
                if value.shape != flat_value.shape:
                    # If the value is not a 1D ndarray, store its associated shape.
                    # The value's shape will be stored as separate metadata with the correct prefix.
                    self.__setitem__(f"{Group._NP_SHAPE_PREFIX}{key}", value.shape)
            elif isinstance(value, np.generic):
                tiledb_type = DataType.from_numpy(value.dtype).tiledb_type
                if tiledb_type in (lt.DataType.BLOB, lt.DataType.CHAR):
                    put_metadata(key, tiledb_type, len(value), value)
                elif tiledb_type == lt.DataType.STRING_UTF8:
                    put_metadata(
                        key, lt.DataType.STRING_UTF8, len(value), value.encode("UTF-8")
                    )
                else:
                    put_metadata(key, tiledb_type, 1, 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

                put_metadata(key, tiledb_type, value_num, data_view)

        def __getitem__(self, key: str, include_type=False) -> GroupMetadataValueType:
            """
            :param str key: Key of the Group metadata entry
            :rtype: Union[int, float, str, bytes, np.ndarray]
            :return: The value associated with the key

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

            if self._group._has_metadata(key):
                data, tdb_type = self._group._get_metadata(key, False)
                dtype = DataType.from_tiledb(tdb_type).np_dtype
                # we return all int and float values as numpy scalars
                if dtype.kind in ("i", "f") and not isinstance(data, tuple):
                    data = np.dtype(dtype).type(data)
            elif self._group._has_metadata(f"{Group._NP_DATA_PREFIX}{key}"):
                data, tdb_type = self._group._get_metadata(
                    f"{Group._NP_DATA_PREFIX}{key}", True
                )
                # reshape numpy array back to original shape, if needed
                # this will not be found in any case for TileDB-Py <= 0.32.3.
                shape_key = f"{Group._NP_SHAPE_PREFIX}{key}"
                if self._group._has_metadata(shape_key):
                    shape, tdb_type = self._group._get_metadata(shape_key, False)
                    data = data.reshape(shape)
            else:
                raise KeyError(f"KeyError: {key}")

            return (data, tdb_type) if include_type else data

        def __delitem__(self, key: str):
            """Removes the entry from the Group metadata.

            :param str key: Key of the Group metadata entry

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

            # 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, Group._NP_DATA_PREFIX + key, Group._NP_SHAPE_PREFIX + key:
                self._group._delete_metadata(k)

        def __contains__(self, key: str) -> bool:
            """
            :param str key: Key of the Group metadata entry
            :rtype: bool
            :return: True if the key is in the Group metadata, otherwise False

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

            # key may be stored as is or it may be prefixed (for numpy values)
            # we don't know this here so check all potential internal keys
            return self._group._has_metadata(key) or self._group._has_metadata(
                f"{Group._NP_DATA_PREFIX}{key}"
            )

        def __len__(self) -> int:
            """
            :rtype: int
            :return: Number of entries in the Group metadata

            """
            num = self._group._metadata_num()
            # subtract the _NP_SHAPE_PREFIX prefixed keys
            for key in self._iter(keys_only=True):
                if key.startswith(Group._NP_SHAPE_PREFIX):
                    num -= 1

            return num

        def _iter(self, keys_only: bool = True, dump: bool = False):
            """
            Iterate over Group metadata keys or (key, value) tuples
            :param keys_only: whether to yield just keys or values too
            :param dump: whether to yield a formatted string for each metadata entry
            """
            if keys_only and dump:
                raise ValueError("keys_only and dump cannot both be True")

            metadata_num = self._group._metadata_num()
            for i in range(metadata_num):
                key = self._group._get_key_from_index(i)

                if keys_only:
                    yield key
                else:
                    val, val_dtype = self.__getitem__(key, include_type=True)

                    if dump:
                        yield (
                            "### Array Metadata ###\n"
                            f"- Key: {key}\n"
                            f"- Value: {val}\n"
                            f"- Type: {val_dtype}\n"
                        )
                    else:
                        yield key, val

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

        def __repr__(self):
            return str(dict(self))

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

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

        def popitem(self):
            raise NotImplementedError(
                "Group.GroupMetadata.popitem requires read-write access"
            )

        def clear(self):
            raise NotImplementedError(
                "Group.GroupMetadata.clear requires read-write access"
            )

        def dump(self):
            """Output information about all group metadata to stdout."""
            for metadata in self._iter(keys_only=False, dump=True):
                print(metadata)

    def __init__(
        self,
        uri: str,
        mode: str = "r",
        config: Config = None,
        ctx: Optional[Ctx] = None,
    ):
        if mode not in Group._mode_to_query_type:
            raise ValueError(f"invalid mode {mode}")
        query_type = Group._mode_to_query_type[mode]

        if config is None:
            super().__init__(ctx, uri, query_type)
        else:
            super().__init__(ctx, uri, query_type, config)

        self._meta = self.GroupMetadata(self)

    @staticmethod
    def create(uri: str, ctx: Optional[Ctx] = None):
        """
        Create a new Group.

        :param uri: The URI to the to-be created Group
        :type uri: str
        :param ctx: A TileDB context
        :type ctx: tiledb.Ctx
        """
        ctx = ctx or default_ctx()
        lt.Group._create(ctx, uri)

    def open(self, mode: str = "r"):
        """
        Open a Group in read mode ("r") or write mode ("w").

        :param mode: Read mode ('r') or write mode ('w')
        :type mode: str
        """
        if mode not in Group._mode_to_query_type:
            raise ValueError(f"invalid mode {mode}")
        query_type = Group._mode_to_query_type[mode]

        self._open(query_type)

    def close(self):
        """
        Close a Group.
        """
        self._close()

    def add(self, uri: str, name: str = None, relative: bool = False):
        """
        Adds a member to the Group.

        :param uri: The URI of the member to add
        :type uri: str
        :param relative: Whether the path of the URI is a relative path (default=relative: False)
        :type relative: bool
        :param name: An optional name for the Group (default=None)
        :type name: str
        """
        if name:
            self._add(uri, relative, name)
        else:
            self._add(uri, relative)

    def delete(self, recursive: bool = False):
        """
        Delete a Group. The group needs to be opened in 'm' mode.

        :param uri: The URI of the group to delete
        """
        self._delete_group(self.uri, recursive)
        self.__was_deleted__ = True

    def __getitem__(self, member: Union[int, str]) -> Object:
        """
        Retrieve a member from the Group as an Object.

        :param member: The index or name of the member
        :type member: Union[int, str]
        :return: The member as an Object
        :rtype: Object
        """
        if not isinstance(member, (int, str)):
            raise TypeError(
                f"Unexpected member type '{type(member)}': expected int or str"
            )

        obj = self._member(member)
        return Object(obj._type, obj._uri, obj._name)

    def __len__(self) -> int:
        """
        :rtype: int
        :return: Number of members in the Group

        """
        return self._member_count()

    def remove(self, member: str):
        """
        Remove a member from the Group.

        :param member: The URI or name of the member
        :type member: str
        """
        if not isinstance(member, str):
            raise TypeError(f"Unexpected member type '{type(member)}': expected str")

        self._remove(member)

    def __delitem__(self, uri: str):
        """
        Remove a member from the group.

        :param uri: The URI to the member
        :type uri: str
        """
        self._remove(uri)

    def __iter__(self):
        return iter(self[i] for i in range(len(self)))

    def __contains__(self, member: str) -> bool:
        """
        :return: Whether the Group contains a member with the given name
        :rtype: bool
        """
        return self._has_member(member)

    def __repr__(self):
        # use safe repr if pybind11 constructor failed
        if self._ctx is None:
            return object.__repr__(self)

        return self._dump(True)

    def __enter__(self):
        """
        The `__enter__` and `__exit__` methods allow TileDB groups to be opened (and auto-closed)
        using with-as syntax.
        """
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        The `__enter__` and `__exit__` methods allow TileDB groups to be opened (and auto-closed)
        using with-as syntax.
        """
        # Don't close if this was a delete operation: the group will be closed
        # automatically.
        if not (hasattr(self, "__deleted") or self.__was_deleted__):
            self.__was_deleted__ = False
            self.close()

    @property
    def meta(self) -> GroupMetadata:
        """
        :return: The Group's metadata as a key-value structure
        :rtype: GroupMetadata
        """
        return self._meta

    @property
    def isopen(self) -> bool:
        """
        :return: Whether or not the Group is open
        :rtype: bool
        """
        return self._isopen

    @property
    def uri(self) -> str:
        """
        :return: URI of the Group
        :rtype: str
        """
        return self._uri

    @property
    def mode(self) -> str:
        """
        :return: Read mode ('r'), write mode ('w'), or modify exclusive ('m')
        :rtype: str
        """
        return self._query_type_to_mode[self._query_type]

    def is_relative(self, name: str) -> bool:
        """
        :param name: Name of member to retrieve associated relative indicator
        :type name: str
        :return: Whether the attribute is relative
        :rtype: bool
        """
        return self._is_relative(name)

    def set_config(self, cfg: Config):
        """
        :param cfg: Config to set on the Group
        :type cfg: Config
        """
        if self.isopen:
            raise ValueError(
                "`set_config` can only be used on closed groups. "
                "Use `group.cl0se()` or Group(.., closed=True)"
            )
        self._set_config(cfg)

    @staticmethod
    def consolidate_metadata(
        uri: str, config: Config = None, ctx: Optional[Ctx] = None
    ):
        """
        Consolidate the group metadata.

        :param uri: The URI of the TileDB group to be consolidated
        :type uri: str
        :param config: Optional configuration parameters for the consolidation
        :type config: Config
        :param ctx: Optional TileDB context
        :type ctx: Ctx
        """
        if ctx is None:
            ctx = default_ctx()

        lt.Group._consolidate_metadata(ctx, uri, config)

    @staticmethod
    def vacuum_metadata(uri: str, config: Config = None, ctx: Optional[Ctx] = None):
        """
        Vacuum the group metadata.

        :param uri: The URI of the TileDB group to be vacuum
        :type uri: str
        :param config: Optional configuration parameters for the vacuuming
        :type config: Config
        :param ctx: Optional TileDB context
        :type ctx: Ctx
        """
        if ctx is None:
            ctx = default_ctx()

        lt.Group._vacuum_metadata(ctx, uri, config)