filesystem datastore implementation.
Bases: datastore.core.basic.Datastore
Simple flat-file datastore.
FileSystemDatastore will store objects in independent files in the host’s filesystem. The FileSystemDatastore is initialized with a root path, under which to store all objects. Each object will be stored under its own file: root/key.obj
The key portion also replaces namespace parameter delimiters (:) with slashes, creating several nested directories. For example, storing objects under root path ‘/data’ with the following keys:
Key('/Comedy:MontyPython/Actor:JohnCleese')
Key('/Comedy:MontyPython/Sketch:ArgumentClinic')
Key('/Comedy:MontyPython/Sketch:CheeseShop')
Key('/Comedy:MontyPython/Sketch:CheeseShop/Character:Mousebender')
will yield the file structure:
/data/Comedy/MontyPython/Actor/JohnCleese.obj
/data/Comedy/MontyPython/Sketch/ArgumentClinic.obj
/data/Comedy/MontyPython/Sketch/CheeseShop.obj
/data/Comedy/MontyPython/Sketch/CheeseShop/Character/Mousebender.obj
Implementation Notes:
Separating key namespaces (and their parameters) within directories allows granular querying for under a specific key. For example, a query with key:
Key('/data/Comedy:MontyPython/Sketch:CheeseShop')will query for all objects under Sketch:CheeseShop independently of queries for:
Key('/data/Comedy:MontyPython/Sketch')Also, using the .obj extension gets around the ambiguity of having both a CheeseShop object and directory:
/data/Comedy/MontyPython/Sketch/CheeseShop.obj /data/Comedy/MontyPython/Sketch/CheeseShop/
Hello World:
>>> import datastore.filesystem
>>>
>>> ds = datastore.filesystem.FileSystemDatastore('/tmp/.test_datastore')
>>>
>>> hello = datastore.Key('hello')
>>> ds.put(hello, 'world')
>>> ds.contains(hello)
True
>>> ds.get(hello)
'world'
>>> ds.delete(hello)
>>> ds.get(hello)
None
Returns whether the object named by key exists. Optimized to only check whether the file object exists.
Removes the object named by key.
Return the object named by key or None if it does not exist.
return the object path for key.
Returns the path for given key
Stores the object value named by key.
Returns an iterable of objects matching criteria expressed in query FSDatastore.query queries all the .obj files within the directory specified by the query.key.
Returns the relative path for object pointed by key.
Returns the relative path for given key
Ensures directory exists. May make directory and intermediate dirs. Raises RuntimeError if directory is a file.