summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/db.py30
1 files changed, 29 insertions, 1 deletions
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()