Skip to content

Commit dc18335

Browse files
committed
WIP synchronized group
1 parent 39a66c9 commit dc18335

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

zarr/core.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -848,36 +848,36 @@ def __init__(self, store, synchronizer, readonly=False, path=None,
848848
super(SynchronizedArray, self).__init__(store, readonly=readonly,
849849
path=path,
850850
chunk_store=chunk_store)
851-
self.synchronizer = synchronizer
851+
self._synchronizer = synchronizer
852852
akey = self._key_prefix + attrs_key
853853
self._attrs = SynchronizedAttributes(store, synchronizer, key=akey,
854854
readonly=readonly)
855855

856856
def __repr__(self):
857857
r = super(SynchronizedArray, self).__repr__()
858-
r += ('; synchronizer: %s.%s' %
859-
(type(self.synchronizer).__module__,
860-
type(self.synchronizer).__name__))
858+
r += ('\n synchronizer: %s.%s' %
859+
(type(self._synchronizer).__module__,
860+
type(self._synchronizer).__name__))
861861
return r
862862

863863
def __getstate__(self):
864-
return self._store, self.synchronizer, self._readonly, self._path, \
864+
return self._store, self._synchronizer, self._readonly, self._path, \
865865
self._chunk_store
866866

867867
def __setstate__(self, state):
868868
self.__init__(*state)
869869

870870
def _chunk_setitem(self, cidx, key, value):
871871
ckey = self._chunk_key(cidx)
872-
with self.synchronizer[ckey]:
872+
with self._synchronizer[ckey]:
873873
super(SynchronizedArray, self)._chunk_setitem(cidx, key, value)
874874

875875
def resize(self, *args):
876876
mkey = self._key_prefix + array_meta_key
877-
with self.synchronizer[mkey]:
877+
with self._synchronizer[mkey]:
878878
super(SynchronizedArray, self).resize(*args)
879879

880880
def append(self, *args, **kwargs):
881881
mkey = self._key_prefix + array_meta_key
882-
with self.synchronizer[mkey]:
882+
with self._synchronizer[mkey]:
883883
super(SynchronizedArray, self).append(*args, **kwargs)

zarr/hierarchy.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99

10-
from zarr.attrs import Attributes
10+
from zarr.attrs import Attributes, SynchronizedAttributes
1111
from zarr.core import Array
1212
from zarr.storage import contains_array, contains_group, init_group, \
1313
DictStore, DirectoryStore, group_meta_key, attrs_key, listdir
@@ -526,14 +526,6 @@ def create_dataset(self, name, data=None, shape=None, chunks=None,
526526
# setup
527527
if self._readonly:
528528
raise ReadOnlyError('group is read-only')
529-
path = self._item_path(name)
530-
self._require_parent_group(path)
531-
532-
# guard conditions
533-
if contains_array(self._store, path):
534-
raise KeyError(name)
535-
if contains_group(self._store, path):
536-
raise KeyError(name)
537529

538530
# N.B., additional kwargs are included in method signature to
539531
# improve compatibility for users familiar with h5py and adapting
@@ -546,6 +538,26 @@ def create_dataset(self, name, data=None, shape=None, chunks=None,
546538
else:
547539
warn('ignoring keyword argument %r' % k)
548540

541+
self._create_dataset(name, data=data, shape=shape, chunks=chunks,
542+
dtype=dtype, compression=compression,
543+
compression_opts=compression_opts,
544+
fill_value=fill_value, order=order,
545+
synchronizer=synchronizer)
546+
547+
def _create_dataset(self, name, data=None, shape=None, chunks=None,
548+
dtype=None, compression='default',
549+
compression_opts=None, fill_value=None, order='C',
550+
synchronizer=None):
551+
552+
path = self._item_path(name)
553+
self._require_parent_group(path)
554+
555+
# guard conditions
556+
if contains_array(self._store, path):
557+
raise KeyError(name)
558+
if contains_group(self._store, path):
559+
raise KeyError(name)
560+
549561
if data is not None:
550562
a = array(data, chunks=chunks, dtype=dtype,
551563
compression=compression,
@@ -600,8 +612,10 @@ def require_dataset(self, name, shape, dtype=None, exact=False, **kwargs):
600612
return a
601613

602614
else:
603-
return self.create_dataset(name, shape=shape, dtype=dtype,
604-
**kwargs)
615+
if self._readonly:
616+
raise ReadOnlyError('group is read-only')
617+
return self._create_dataset(name, shape=shape, dtype=dtype,
618+
**kwargs)
605619

606620
def create(self, name, **kwargs):
607621
"""Create an array. Keyword arguments as per
@@ -830,3 +844,35 @@ def open_group(path, mode='a'):
830844
readonly = mode == 'r'
831845

832846
return Group(store, readonly=readonly)
847+
848+
849+
class SynchronizedGroup(Group):
850+
"""TODO doc me"""
851+
852+
def __init__(self, store, synchronizer, path=None, readonly=False,
853+
chunk_store=None):
854+
super(SynchronizedGroup, self).__init__(store, path=path,
855+
readonly=readonly,
856+
chunk_store=chunk_store)
857+
self._synchronizer = synchronizer
858+
akey = self._key_prefix + attrs_key
859+
self._attrs = SynchronizedAttributes(store, synchronizer, key=akey,
860+
readonly=readonly)
861+
862+
def __repr__(self):
863+
r = super(SynchronizedGroup, self).__repr__()
864+
r += ('\n synchronizer: %s.%s' %
865+
(type(self._synchronizer).__module__,
866+
type(self._synchronizer).__name__))
867+
return r
868+
869+
def __getstate__(self):
870+
return self._store, self._synchronizer, self._path, self._readonly, \
871+
self._chunk_store
872+
873+
def __setstate__(self, state):
874+
self.__init__(*state)
875+
876+
def __getitem__(self, item):
877+
# TODO
878+
pass

0 commit comments

Comments
 (0)