mbr / simplekv

A simple key-value store for binary data.
http://simplekv.readthedocs.io
MIT License
152 stars 50 forks source link

Add move support to the CopyMixin #108

Closed zeroSteiner closed 3 years ago

zeroSteiner commented 4 years ago

This adds the ability to move / rename a key to the CopyMixin. Under the hood, the default implementation performs a copy operation followed by a delete operation to remove the original key. If the backend directly support a move operation, then it can be implemented in the standard way by overriding the _move method to use what is presumably the more efficient method.

Example Usage

Shows key1 being copied to key2 and then that the data is accessible as key2 while key1 is then invalid.

In [4]: from simplekv.fs import FilesystemStore 
   ...:  
   ...: store = FilesystemStore('./data') 
   ...:  
   ...: store.put(u'key1', b'hello')                                                                                                                                                                               
Out[4]: 'key1'

In [5]: store.move(u'key1', u'key2')                                                                                                                                                                               
Out[5]: 'key2'

In [6]: store.get(u'key2')                                                                                                                                                                                         
Out[6]: b'hello'

In [7]: store.get(u'key1')                                                                                                                                                                                         
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~/Repositories/simplekv/simplekv/fs.py in _open(self, key)
     77         try:
---> 78             f = open(self._build_filename(key), 'rb')
     79             return f

FileNotFoundError: [Errno 2] No such file or directory: '/home/steiner/Repositories/simplekv/data/key1'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-7-5fee0dbea39d> in <module>
----> 1 store.get(u'key1')

~/Repositories/simplekv/simplekv/__init__.py in get(self, key)
     73         """
     74         self._check_valid_key(key)
---> 75         return self._get(key)
     76 
     77     def get_file(self, key, file):

~/Repositories/simplekv/simplekv/__init__.py in _get(self, key)
    235         buf = BytesIO()
    236 
--> 237         self._get_file(key, buf)
    238 
    239         return buf.getvalue()

~/Repositories/simplekv/simplekv/__init__.py in _get_file(self, key, file)
    254         # this allows us to support file-like objects without close as well,
    255         # such as BytesIO.
--> 256         source = self.open(key)
    257         try:
    258             while True:

~/Repositories/simplekv/simplekv/__init__.py in open(self, key)
    156         """
    157         self._check_valid_key(key)
--> 158         return self._open(key)
    159 
    160     def put(self, key, data):

~/Repositories/simplekv/simplekv/fs.py in _open(self, key)
     80         except IOError as e:
     81             if 2 == e.errno:
---> 82                 raise KeyError(key)
     83             else:
     84                 raise

KeyError: 'key1'

In [8]:                                                    
coveralls commented 4 years ago

Coverage Status

Coverage decreased (-65.8%) to 26.32% when pulling f18d7d59b3d6e7daf4afde95b7459956f8bbd03a on zeroSteiner:feat/move into f87e2da2758e22589adf482bfe10128868dc100f on mbr:master.