X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=lcm%2Fosm_common%2Fdbmongo.py;h=46e4dc84040c4bf74f63cb7758ceabf4cd21bc44;hb=refs%2Fchanges%2F37%2F6137%2F3;hp=34e6fafb64c21bd1b3b422c70a33ca1216c444ac;hpb=ae501920e1c0e03c8571bece610dd5518e6e86b9;p=osm%2FRO.git diff --git a/lcm/osm_common/dbmongo.py b/lcm/osm_common/dbmongo.py index 34e6fafb..46e4dc84 100644 --- a/lcm/osm_common/dbmongo.py +++ b/lcm/osm_common/dbmongo.py @@ -7,6 +7,20 @@ from time import time, sleep __author__ = "Alfonso Tierno " +# TODO consider use this decorator for database access retries +# @retry_mongocall +# def retry_mongocall(call): +# def _retry_mongocall(*args, **kwargs): +# retry = 1 +# while True: +# try: +# return call(*args, **kwargs) +# except pymongo.AutoReconnect as e: +# if retry == 4: +# raise DbException(str(e)) +# sleep(retry) +# return _retry_mongocall + class DbMongo(DbBase): conn_initial_timout = 120 @@ -50,7 +64,7 @@ class DbMongo(DbBase): "ncont", "neq"): operator = "$" + query_k[dot_index+1:] if operator == "$neq": - operator = "$nq" + operator = "$ne" k = query_k[:dot_index] else: operator = "$eq" @@ -84,7 +98,6 @@ class DbMongo(DbBase): raise DbException("Invalid query string filter at {}:{}. Error: {}".format(query_k, v, e), http_code=HTTPStatus.BAD_REQUEST) - def get_list(self, table, filter={}): try: l = [] @@ -108,11 +121,12 @@ class DbMongo(DbBase): rows = collection.find(filter) if rows.count() == 0: if fail_on_empty: - raise DbException("Not found entry with filter='{}'".format(filter), HTTPStatus.NOT_FOUND) + raise DbException("Not found any {} with filter='{}'".format(table[:-1], filter), + HTTPStatus.NOT_FOUND) return None elif rows.count() > 1: if fail_on_more: - raise DbException("Found more than one entry with filter='{}'".format(filter), + raise DbException("Found more than one {} with filter='{}'".format(table[:-1], filter), HTTPStatus.CONFLICT) return rows[0] except Exception as e: # TODO refine @@ -134,7 +148,8 @@ class DbMongo(DbBase): rows = collection.delete_one(self._format_filter(filter)) if rows.deleted_count == 0: if fail_on_empty: - raise DbException("Not found entry with filter='{}'".format(filter), HTTPStatus.NOT_FOUND) + raise DbException("Not found any {} with filter='{}'".format(table[:-1], filter), + HTTPStatus.NOT_FOUND) return None return {"deleted": rows.deleted_count} except Exception as e: # TODO refine @@ -152,22 +167,25 @@ class DbMongo(DbBase): try: collection = self.db[table] rows = collection.update_one(self._format_filter(filter), {"$set": update_dict}) - if rows.updated_count == 0: + if rows.matched_count == 0: if fail_on_empty: - raise DbException("Not found entry with filter='{}'".format(filter), HTTPStatus.NOT_FOUND) + raise DbException("Not found any {} with filter='{}'".format(table[:-1], filter), + HTTPStatus.NOT_FOUND) return None - return {"deleted": rows.deleted_count} + return {"modified": rows.modified_count} except Exception as e: # TODO refine raise DbException(str(e)) def replace(self, table, id, indata, fail_on_empty=True): try: + _filter = {"_id": id} collection = self.db[table] - rows = collection.replace_one({"_id": id}, indata) - if rows.modified_count == 0: + rows = collection.replace_one(_filter, indata) + if rows.matched_count == 0: if fail_on_empty: - raise DbException("Not found entry with filter='{}'".format(filter), HTTPStatus.NOT_FOUND) + raise DbException("Not found any {} with filter='{}'".format(table[:-1], _filter), + HTTPStatus.NOT_FOUND) return None - return {"replace": rows.modified_count} + return {"replaced": rows.modified_count} except Exception as e: # TODO refine raise DbException(str(e))