From 583542443adb2d66208dacb71581bc2936c59201 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Thu, 14 Oct 2021 09:48:17 +0200 Subject: Find, now with pagination! Use limit and skip to get the results you want. --- src/db.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/db.py') diff --git a/src/db.py b/src/db.py index f9d0da1..8ab98e7 100755 --- a/src/db.py +++ b/src/db.py @@ -76,24 +76,35 @@ class DictDB(): def slice(self, key_from=None, key_to=None): pass - def search(self, **kwargs): + def search(self, limit=25, skip=0, **kwargs): """ - Execute a Mango query, ideally we should have an index matching the, + Execute a Mango query, ideally we should have an index matching the query otherwise things will be slow. """ data = list() selector = dict() + try: + limit = int(limit) + skip = int(skip) + except ValueError: + limit = 25 + skip = 0 + if kwargs: - selector = {"selector": {}} + selector = { + "limit": limit, + "skip": skip, + "selector": {} + } for key in kwargs: if kwargs[key].isnumeric(): kwargs[key] = int(kwargs[key]) selector['selector'][key] = {'$eq': kwargs[key]} - for doc in self.couchdb.find(selector): + for doc in self.couchdb.find(selector, wrapper=None, limit=5): data.append(doc) return data -- cgit v1.1 From 08b98fd23a9cc493828fdcf006f609efcf82f136 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Mon, 18 Oct 2021 13:08:35 +0200 Subject: Checkk if we have a document before trying to use its key. --- src/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/db.py') diff --git a/src/db.py b/src/db.py index 8ab98e7..3dd005a 100755 --- a/src/db.py +++ b/src/db.py @@ -100,7 +100,7 @@ class DictDB(): } for key in kwargs: - if kwargs[key].isnumeric(): + if kwargs[key] and kwargs[key].isnumeric(): kwargs[key] = int(kwargs[key]) selector['selector'][key] = {'$eq': kwargs[key]} -- cgit v1.1 From 34a353a539f71b6a87413b58ea483b36f94e3516 Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Mon, 18 Oct 2021 15:15:09 +0200 Subject: Remove unused variables. --- src/db.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/db.py') diff --git a/src/db.py b/src/db.py index 3dd005a..2308e8c 100755 --- a/src/db.py +++ b/src/db.py @@ -1,7 +1,7 @@ -# A database storing dictionaries, keyed on a timestamp. -# value = A dict which will be stored as a JSON object encoded in -# UTF-8. Note that dict keys of type integer or float will become -# strings while values will keep their type. +# A database storing dictionaries, keyed on a timestamp. value = A +# dict which will be stored as a JSON object encoded in UTF-8. Note +# that dict keys of type integer or float will become strings while +# values will keep their type. # Note that there's a (slim) chance that you'd stomp on the previous # value if you're too quick with generating the timestamps, ie -- cgit v1.1