summaryrefslogtreecommitdiff
path: root/src/db.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.py')
-rwxr-xr-xsrc/db.py34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/db.py b/src/db.py
index 0c7e998..86c6f24 100755
--- a/src/db.py
+++ b/src/db.py
@@ -10,19 +10,31 @@
# invoking time.time() several times quickly enough.
import time
-import struct
-import couchdb
+import couch
class DictDB():
def __init__(self, database, hostname, username, password):
- self.server = couchdb.Server(
+ self.server = couch.client.Server(
f"http://{username}:{password}@{hostname}:5984/")
- if database not in self.server:
- self.server.create(database)
+ try:
+ self.couchdb = self.server.database(database)
+ except couch.exceptions.NotFound:
+ print("Creating database and indexes.")
+ index = {
+ "index": {
+ "fields": [
+ "domain"
+ ]
+ },
+ "name": "domain-json-index",
+ "type": "json"
+ }
+
+ self.couchdb = self.server.create(database)
+ print(self.couchdb.index(index))
- self.couchdb = self.server[database]
self._ts = time.time()
def unique_key(self):
@@ -33,17 +45,17 @@ class DictDB():
return self._ts
- def index_add(self, path):
- pass
-
def add(self, data, batch_write=False):
key = str(self.unique_key())
if type(data) is list:
for item in data:
- self.couchdb[key] = item
+ item['_id'] = str(self._ts)
+
+ self.couchdb.save(item)
else:
- self.couchdb[key] = data
+ data['_id'] = str(self._ts)
+ self.couchdb.save(data)
return key