hrsh-4 / datastore

File based Key-Value datastore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

File based Key-Value datastore

Supports basic CRD (Create, Read, Delete)

Functionalities:

  • It can be initialized using an optional file path. If not provided, it will reliably create itself in a reasonable location on the laptop
  • A new key-value pair can be added to the data store using the Create operation. The key is always a string - capped at 32chars. The value is always a JSON object - capped at 16KB.
  • A Read operation on a key can be performed by providing the key, and receiving the value in response, as a JSON object.
  • A Delete operation can be performed by providing the key
  • Every key supports setting a Time To Live property when it is created. This property is optional. If provided, it will be evaluated as an integer defining the number of seconds the key must be retained in the data store. Once the Time T -Live for a key has expired, the key will no longer be available for Read or Delete operations.
  • The size of the file storing data never exceeds 1GB
  • More than one client process will not be allowed to use the same file as a data store at any given time and the datastore is thread safe.

Example

from key_value_datastore import *

object = create_object("new_file")   # new file with name "new_file" will be created

If file name is not provided in create_object() , it will generate 128-bit long random file name.

object.create_key("test_key",{"value" : 1000},300) # "test_key" will be created in the file and will be accessible till 300 seconds

object.get_key("test_key")                         # returns value of "test_key", if key found and  time to live not expired 

object.delete_key("test_key")                      # deletes key "test_key", if key found

Unit test of the datastore have been written in tests.py and can be tested by,

python tests.py

About

File based Key-Value datastore


Languages

Language:Python 100.0%