35
35
import uuid
36
36
import time
37
37
38
- from numcodecs .compat import ensure_bytes , ensure_contiguous_ndarray
38
+ from numcodecs .compat import (
39
+ ensure_bytes ,
40
+ ensure_text ,
41
+ ensure_contiguous_ndarray
42
+ )
39
43
from numcodecs .registry import codec_registry
40
44
41
45
from zarr .errors import (
@@ -1573,18 +1577,6 @@ def migrate_1to2(store):
1573
1577
del store ['attrs' ]
1574
1578
1575
1579
1576
- def _dbm_encode_key (key ):
1577
- if hasattr (key , 'encode' ):
1578
- key = key .encode ('ascii' )
1579
- return key
1580
-
1581
-
1582
- def _dbm_decode_key (key ):
1583
- if hasattr (key , 'decode' ):
1584
- key = key .decode ('ascii' )
1585
- return key
1586
-
1587
-
1588
1580
# noinspection PyShadowingBuiltins
1589
1581
class DBMStore (MutableMapping ):
1590
1582
"""Storage class using a DBM-style database.
@@ -1730,17 +1722,20 @@ def __exit__(self, *args):
1730
1722
self .close ()
1731
1723
1732
1724
def __getitem__ (self , key ):
1733
- key = _dbm_encode_key (key )
1725
+ if isinstance (key , str ):
1726
+ key = key .encode ("ascii" )
1734
1727
return self .db [key ]
1735
1728
1736
1729
def __setitem__ (self , key , value ):
1737
- key = _dbm_encode_key (key )
1730
+ if isinstance (key , str ):
1731
+ key = key .encode ("ascii" )
1738
1732
value = ensure_bytes (value )
1739
1733
with self .write_mutex :
1740
1734
self .db [key ] = value
1741
1735
1742
1736
def __delitem__ (self , key ):
1743
- key = _dbm_encode_key (key )
1737
+ if isinstance (key , str ):
1738
+ key = key .encode ("ascii" )
1744
1739
with self .write_mutex :
1745
1740
del self .db [key ]
1746
1741
@@ -1754,7 +1749,7 @@ def __eq__(self, other):
1754
1749
)
1755
1750
1756
1751
def keys (self ):
1757
- return (_dbm_decode_key ( k ) for k in iter (self .db .keys ()))
1752
+ return (ensure_text ( k , "ascii" ) for k in iter (self .db .keys ()))
1758
1753
1759
1754
def __iter__ (self ):
1760
1755
return self .keys ()
@@ -1763,20 +1758,11 @@ def __len__(self):
1763
1758
return sum (1 for _ in self .keys ())
1764
1759
1765
1760
def __contains__ (self , key ):
1766
- key = _dbm_encode_key (key )
1761
+ if isinstance (key , str ):
1762
+ key = key .encode ("ascii" )
1767
1763
return key in self .db
1768
1764
1769
1765
1770
- def _lmdb_decode_key_buffer (key ):
1771
- # assume buffers=True
1772
- return key .tobytes ().decode ('ascii' )
1773
-
1774
-
1775
- def _lmdb_decode_key_bytes (key ):
1776
- # assume buffers=False
1777
- return key .decode ('ascii' )
1778
-
1779
-
1780
1766
class LMDBStore (MutableMapping ):
1781
1767
"""Storage class using LMDB. Requires the `lmdb <http://lmdb.readthedocs.io/>`_
1782
1768
package to be installed.
@@ -1866,10 +1852,6 @@ def __init__(self, path, buffers=True, **kwargs):
1866
1852
self .db = lmdb .open (path , ** kwargs )
1867
1853
1868
1854
# store properties
1869
- if buffers :
1870
- self .decode_key = _lmdb_decode_key_buffer
1871
- else :
1872
- self .decode_key = _lmdb_decode_key_bytes
1873
1855
self .buffers = buffers
1874
1856
self .path = path
1875
1857
self .kwargs = kwargs
@@ -1901,7 +1883,8 @@ def __exit__(self, *args):
1901
1883
self .close ()
1902
1884
1903
1885
def __getitem__ (self , key ):
1904
- key = _dbm_encode_key (key )
1886
+ if isinstance (key , str ):
1887
+ key = key .encode ("ascii" )
1905
1888
# use the buffers option, should avoid a memory copy
1906
1889
with self .db .begin (buffers = self .buffers ) as txn :
1907
1890
value = txn .get (key )
@@ -1910,18 +1893,21 @@ def __getitem__(self, key):
1910
1893
return value
1911
1894
1912
1895
def __setitem__ (self , key , value ):
1913
- key = _dbm_encode_key (key )
1896
+ if isinstance (key , str ):
1897
+ key = key .encode ("ascii" )
1914
1898
with self .db .begin (write = True , buffers = self .buffers ) as txn :
1915
1899
txn .put (key , value )
1916
1900
1917
1901
def __delitem__ (self , key ):
1918
- key = _dbm_encode_key (key )
1902
+ if isinstance (key , str ):
1903
+ key = key .encode ("ascii" )
1919
1904
with self .db .begin (write = True ) as txn :
1920
1905
if not txn .delete (key ):
1921
1906
raise KeyError (key )
1922
1907
1923
1908
def __contains__ (self , key ):
1924
- key = _dbm_encode_key (key )
1909
+ if isinstance (key , str ):
1910
+ key = key .encode ("ascii" )
1925
1911
with self .db .begin (buffers = self .buffers ) as txn :
1926
1912
with txn .cursor () as cursor :
1927
1913
return cursor .set_key (key )
@@ -1930,13 +1916,13 @@ def items(self):
1930
1916
with self .db .begin (buffers = self .buffers ) as txn :
1931
1917
with txn .cursor () as cursor :
1932
1918
for k , v in cursor .iternext (keys = True , values = True ):
1933
- yield self . decode_key ( k ), v
1919
+ yield ensure_text ( k , "ascii" ), v
1934
1920
1935
1921
def keys (self ):
1936
1922
with self .db .begin (buffers = self .buffers ) as txn :
1937
1923
with txn .cursor () as cursor :
1938
1924
for k in cursor .iternext (keys = True , values = False ):
1939
- yield self . decode_key ( k )
1925
+ yield ensure_text ( k , "ascii" )
1940
1926
1941
1927
def values (self ):
1942
1928
with self .db .begin (buffers = self .buffers ) as txn :
0 commit comments