Add 'count' to DB utilities

Change-Id: I1a137ceeea21c70cf9358a7bc812ca4e2fe5a240
Signed-off-by: delacruzramo <pedro.delacruzramos@altran.com>
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 @@
         """
         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 @@
         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 @@
         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 @@
     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", [