@@ -1535,25 +1535,48 @@ def _set_selection(self, indexer, value, fields=None):
1535
1535
check_array_shape ('value' , value , sel_shape )
1536
1536
1537
1537
# iterate over chunks in range
1538
- for chunk_coords , chunk_selection , out_selection in indexer :
1538
+ if not hasattr (self .store , "setitems" ) or self ._synchronizer is not None :
1539
+ # iterative approach
1540
+ for chunk_coords , chunk_selection , out_selection in indexer :
1539
1541
1540
- # extract data to store
1541
- if sel_shape == ():
1542
- chunk_value = value
1543
- elif is_scalar (value , self ._dtype ):
1544
- chunk_value = value
1545
- else :
1546
- chunk_value = value [out_selection ]
1547
- # handle missing singleton dimensions
1548
- if indexer .drop_axes :
1549
- item = [slice (None )] * self .ndim
1550
- for a in indexer .drop_axes :
1551
- item [a ] = np .newaxis
1552
- item = tuple (item )
1553
- chunk_value = chunk_value [item ]
1554
-
1555
- # put data
1556
- self ._chunk_setitem (chunk_coords , chunk_selection , chunk_value , fields = fields )
1542
+ # extract data to store
1543
+ if sel_shape == ():
1544
+ chunk_value = value
1545
+ elif is_scalar (value , self ._dtype ):
1546
+ chunk_value = value
1547
+ else :
1548
+ chunk_value = value [out_selection ]
1549
+ # handle missing singleton dimensions
1550
+ if indexer .drop_axes :
1551
+ item = [slice (None )] * self .ndim
1552
+ for a in indexer .drop_axes :
1553
+ item [a ] = np .newaxis
1554
+ item = tuple (item )
1555
+ chunk_value = chunk_value [item ]
1556
+
1557
+ # put data
1558
+ self ._chunk_setitem (chunk_coords , chunk_selection , chunk_value , fields = fields )
1559
+ else :
1560
+ lchunk_coords , lchunk_selection , lout_selection = zip (* indexer )
1561
+ chunk_values = []
1562
+ for out_selection in lout_selection :
1563
+ if sel_shape == ():
1564
+ chunk_values .append (value )
1565
+ elif is_scalar (value , self ._dtype ):
1566
+ chunk_values .append (value )
1567
+ else :
1568
+ cv = value [out_selection ]
1569
+ # handle missing singleton dimensions
1570
+ if indexer .drop_axes : # pragma: no cover
1571
+ item = [slice (None )] * self .ndim
1572
+ for a in indexer .drop_axes :
1573
+ item [a ] = np .newaxis
1574
+ item = tuple (item )
1575
+ cv = chunk_value [item ]
1576
+ chunk_values .append (cv )
1577
+
1578
+ self ._chunk_setitems (lchunk_coords , lchunk_selection , chunk_values ,
1579
+ fields = fields )
1557
1580
1558
1581
def _process_chunk (self , out , cdata , chunk_selection , drop_axes ,
1559
1582
out_is_ndarray , fields , out_selection ):
@@ -1677,6 +1700,12 @@ def _chunk_getitems(self, lchunk_coords, lchunk_selection, out, lout_selection,
1677
1700
fill_value = self ._fill_value
1678
1701
out [out_select ] = fill_value
1679
1702
1703
+ def _chunk_setitems (self , lchunk_coords , lchunk_selection , values , fields = None ):
1704
+ ckeys = [self ._chunk_key (co ) for co in lchunk_coords ]
1705
+ cdatas = [self ._process_for_setitem (key , sel , val , fields = fields )
1706
+ for key , sel , val in zip (ckeys , lchunk_selection , values )]
1707
+ self .chunk_store .setitems ({k : v for k , v in zip (ckeys , cdatas )})
1708
+
1680
1709
def _chunk_setitem (self , chunk_coords , chunk_selection , value , fields = None ):
1681
1710
"""Replace part or whole of a chunk.
1682
1711
@@ -1704,10 +1733,12 @@ def _chunk_setitem(self, chunk_coords, chunk_selection, value, fields=None):
1704
1733
fields = fields )
1705
1734
1706
1735
def _chunk_setitem_nosync (self , chunk_coords , chunk_selection , value , fields = None ):
1707
-
1708
- # obtain key for chunk storage
1709
1736
ckey = self ._chunk_key (chunk_coords )
1737
+ cdata = self ._process_for_setitem (ckey , chunk_selection , value , fields = fields )
1738
+ # store
1739
+ self .chunk_store [ckey ] = cdata
1710
1740
1741
+ def _process_for_setitem (self , ckey , chunk_selection , value , fields = None ):
1711
1742
if is_total_slice (chunk_selection , self ._chunks ) and not fields :
1712
1743
# totally replace chunk
1713
1744
@@ -1762,10 +1793,7 @@ def _chunk_setitem_nosync(self, chunk_coords, chunk_selection, value, fields=Non
1762
1793
chunk [chunk_selection ] = value
1763
1794
1764
1795
# encode chunk
1765
- cdata = self ._encode_chunk (chunk )
1766
-
1767
- # store
1768
- self .chunk_store [ckey ] = cdata
1796
+ return self ._encode_chunk (chunk )
1769
1797
1770
1798
def _chunk_key (self , chunk_coords ):
1771
1799
return self ._key_prefix + '.' .join (map (str , chunk_coords ))
0 commit comments