From ae049d8467e5ce1b1be8487ed93031b594dc0230 Mon Sep 17 00:00:00 2001 From: delacruzramo Date: Tue, 17 Sep 2019 16:05:17 +0200 Subject: [PATCH] Add 'count' to DB utilities Change-Id: I1a137ceeea21c70cf9358a7bc812ca4e2fe5a240 Signed-off-by: delacruzramo --- osm_common/dbbase.py | 10 ++++++++++ osm_common/dbmemory.py | 16 ++++++++++++++++ osm_common/dbmongo.py | 19 +++++++++++++++++++ osm_common/tests/test_dbmemory.py | 2 ++ 4 files changed, 47 insertions(+) diff --git a/osm_common/dbbase.py b/osm_common/dbbase.py index e9152e5..1319fd8 100644 --- a/osm_common/dbbase.py +++ b/osm_common/dbbase.py @@ -88,6 +88,16 @@ class DbBase(object): """ raise DbException("Method 'get_list' not implemented") + def count(self, table, q_filter=None): + """ + Count the number of entries matching q_filter + :param table: collection or table + :param q_filter: Filter + :return: number of entries found (can be zero) + :raise: DbException on error + """ + raise DbException("Method 'count' not implemented") + def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): """ Obtain one entry matching q_filter diff --git a/osm_common/dbmemory.py b/osm_common/dbmemory.py index c994640..d12d03d 100644 --- a/osm_common/dbmemory.py +++ b/osm_common/dbmemory.py @@ -161,6 +161,22 @@ class DbMemory(DbBase): except Exception as e: # TODO refine raise DbException(str(e)) + def count(self, table, q_filter=None): + """ + Count the number of entries matching q_filter + :param table: collection or table + :param q_filter: Filter + :return: number of entries found (can be zero) + :raise: DbException on error + """ + try: + with self.lock: + return sum(1 for x in self._find(table, self._format_filter(q_filter))) + except DbException: + raise + except Exception as e: # TODO refine + raise DbException(str(e)) + def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): """ Obtain one entry matching q_filter diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py index 5334ef9..86ec7b7 100644 --- a/osm_common/dbmongo.py +++ b/osm_common/dbmongo.py @@ -221,6 +221,25 @@ class DbMongo(DbBase): except Exception as e: # TODO refine raise DbException(e) + def count(self, table, q_filter=None): + """ + Count the number of entries matching q_filter + :param table: collection or table + :param q_filter: Filter + :return: number of entries found (can be zero) + :raise: DbException on error + """ + try: + with self.lock: + collection = self.db[table] + db_filter = self._format_filter(q_filter) + count = collection.count(db_filter) + return count + except DbException: + raise + except Exception as e: # TODO refine + raise DbException(e) + def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): """ Obtain one entry matching q_filter diff --git a/osm_common/tests/test_dbmemory.py b/osm_common/tests/test_dbmemory.py index e89560b..552e8f1 100644 --- a/osm_common/tests/test_dbmemory.py +++ b/osm_common/tests/test_dbmemory.py @@ -223,6 +223,8 @@ def test_get_list(db_memory_with_many_data, db_filter, expected_ids): assert len(db_memory_with_many_data.db) == 1 assert "test" in db_memory_with_many_data.db assert len(db_memory_with_many_data.db["test"]) == 8 + result = db_memory_with_many_data.count("test", db_filter) + assert result == len(expected_ids) @pytest.mark.parametrize("table, db_filter, expected_data", [ -- 2.17.1