From 2be3fc7478bbfce47ae466482d2e7873f1a54a6b Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Thu, 7 Oct 2021 10:59:56 +0200 Subject: Fixed db.get() and commented things somewhat. --- src/db.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/db.py b/src/db.py index 4e7bcc5..c4244e1 100755 --- a/src/db.py +++ b/src/db.py @@ -14,6 +14,11 @@ import index class DictDB(): def __init__(self, database, hostname, username, password): + """ + Check if the database exists, otherwise we will create it together + with the indexes specified in index.py. + """ + self.server = couch.client.Server( f"http://{username}:{password}@{hostname}:5984/") @@ -30,6 +35,11 @@ class DictDB(): self._ts = time.time() def unique_key(self): + """ + Create a unique key based on the current time. We will use this as + the ID for any new documents we store in CouchDB. + """ + ts = time.time() while ts == self._ts: ts = time.time() @@ -38,6 +48,10 @@ class DictDB(): return self._ts def add(self, data, batch_write=False): + """ + Store a document in CouchDB. + """ + key = str(self.unique_key()) if type(data) is list: for item in data: @@ -51,12 +65,26 @@ class DictDB(): return key def get(self, key): - return self.couchdb[key] + """ + Get a document based on its ID, return an empty dict if not found. + """ + + try: + doc = self.couchdb.get(key) + except couch.exceptions.NotFound: + doc = {} + + return doc def slice(self, key_from=None, key_to=None): pass def search(self, **kwargs): + """ + Execute a Mango query, ideally we should have an index matching the, + the query otherwise things will be slow. + """ + data = list() selector = dict() -- cgit v1.1