Skip to content

Commit ec595fb

Browse files
authored
Wrap array_exists with tiledb scope (#135)
1 parent 6ed5014 commit ec595fb

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

tiledb/bioimg/helpers.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,28 @@ def get_or_create(self, name: str, schema: tiledb.ArraySchema) -> Tuple[str, boo
6161
else:
6262
uri = os.path.join(self._uri, name).replace("\\", "/")
6363

64-
if not tiledb.array_exists(uri, ctx=self._ctx):
65-
tiledb.Array.create(uri, schema, ctx=self._ctx)
66-
create = True
67-
else:
68-
# The array exists, but it's not added as group member with the given name.
69-
# It is possible though that it was added as an anonymous member.
70-
# In this case we should remove the member, using as key either the uri
71-
# (if added with relative=False) or the name (if added with relative=True).
72-
for ref in uri, name:
73-
try:
74-
self.w_group.remove(ref)
75-
except tiledb.TileDBError:
76-
pass
77-
else:
78-
# Attempting to remove and then re-add a member with the same name
79-
# fails with "[TileDB::Group] Error: Cannot add group member,
80-
# member already set for removal.". To work around this we need to
81-
# close the write group (to flush the removal) and and reopen it
82-
# (to allow the add operation)
83-
self.w_group.close()
84-
self.w_group.open("w")
64+
with tiledb.scope_ctx(self._ctx):
65+
if not tiledb.array_exists(uri):
66+
tiledb.Array.create(uri, schema, ctx=self._ctx)
67+
create = True
68+
else:
69+
# The array exists, but it's not added as group member with the given name.
70+
# It is possible though that it was added as an anonymous member.
71+
# In this case we should remove the member, using as key either the uri
72+
# (if added with relative=False) or the name (if added with relative=True).
73+
for ref in uri, name:
74+
try:
75+
self.w_group.remove(ref)
76+
except tiledb.TileDBError:
77+
pass
78+
else:
79+
# Attempting to remove and then re-add a member with the same name
80+
# fails with "[TileDB::Group] Error: Cannot add group member,
81+
# member already set for removal.". To work around this we need to
82+
# close the write group (to flush the removal) and and reopen it
83+
# (to allow the add operation)
84+
self.w_group.close()
85+
self.w_group.open("w")
8586
# register the uri with the given name
8687
if self._is_cloud:
8788
self.w_group.add(uri, name, relative=False)

0 commit comments

Comments
 (0)