X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_common%2Fdbmemory.py;h=c2d9a113273a03566c2c8f5b476aba2eb68909ef;hb=eef7cb7a4f704bf0d29e6d8ece5195dc3faabb69;hp=bae68e2b14a15ef42e287b0b12b3d7cb6693d09b;hpb=cfc5272864156b706d7147fc4e7c0fe46dc386c8;p=osm%2Fcommon.git diff --git a/osm_common/dbmemory.py b/osm_common/dbmemory.py index bae68e2..c2d9a11 100644 --- a/osm_common/dbmemory.py +++ b/osm_common/dbmemory.py @@ -26,8 +26,8 @@ __author__ = "Alfonso Tierno " class DbMemory(DbBase): - def __init__(self, logger_name='db'): - super().__init__(logger_name) + def __init__(self, logger_name='db', lock=False): + super().__init__(logger_name, lock) self.db = {} def db_connect(self, config): @@ -38,7 +38,9 @@ class DbMemory(DbBase): """ if "logger_name" in config: self.logger = logging.getLogger(config["logger_name"]) - self.master_password = config.get("masterpassword") + master_key = config.get("commonkey") or config.get("masterpassword") + if master_key: + self.set_secret_key(master_key) @staticmethod def _format_filter(q_filter): @@ -63,8 +65,9 @@ class DbMemory(DbBase): """ try: result = [] - for _, row in self._find(table, self._format_filter(q_filter)): - result.append(deepcopy(row)) + with self.lock: + for _, row in self._find(table, self._format_filter(q_filter)): + result.append(deepcopy(row)) return result except DbException: raise @@ -84,13 +87,14 @@ class DbMemory(DbBase): """ try: result = None - for _, row in self._find(table, self._format_filter(q_filter)): - if not fail_on_more: - return deepcopy(row) - if result: - raise DbException("Found more than one entry with filter='{}'".format(q_filter), - HTTPStatus.CONFLICT.value) - result = row + with self.lock: + for _, row in self._find(table, self._format_filter(q_filter)): + if not fail_on_more: + return deepcopy(row) + if result: + raise DbException("Found more than one entry with filter='{}'".format(q_filter), + HTTPStatus.CONFLICT.value) + result = row if not result and fail_on_empty: raise DbException("Not found entry with filter='{}'".format(q_filter), HTTPStatus.NOT_FOUND) return deepcopy(result) @@ -106,8 +110,9 @@ class DbMemory(DbBase): """ try: id_list = [] - for i, _ in self._find(table, self._format_filter(q_filter)): - id_list.append(i) + with self.lock: + for i, _ in self._find(table, self._format_filter(q_filter)): + id_list.append(i) deleted = len(id_list) for i in reversed(id_list): del self.db[table][i] @@ -127,13 +132,14 @@ class DbMemory(DbBase): :return: Dict with the number of entries deleted """ try: - for i, _ in self._find(table, self._format_filter(q_filter)): - break - else: - if fail_on_empty: - raise DbException("Not found entry with filter='{}'".format(q_filter), HTTPStatus.NOT_FOUND) - return None - del self.db[table][i] + with self.lock: + for i, _ in self._find(table, self._format_filter(q_filter)): + break + else: + if fail_on_empty: + raise DbException("Not found entry with filter='{}'".format(q_filter), HTTPStatus.NOT_FOUND) + return None + del self.db[table][i] return {"deleted": 1} except Exception as e: # TODO refine raise DbException(str(e)) @@ -149,13 +155,14 @@ class DbMemory(DbBase): :return: Dict with the number of entries replaced """ try: - for i, _ in self._find(table, self._format_filter({"_id": _id})): - break - else: - if fail_on_empty: - raise DbException("Not found entry with _id='{}'".format(_id), HTTPStatus.NOT_FOUND) - return None - self.db[table][i] = deepcopy(indata) + with self.lock: + for i, _ in self._find(table, self._format_filter({"_id": _id})): + break + else: + if fail_on_empty: + raise DbException("Not found entry with _id='{}'".format(_id), HTTPStatus.NOT_FOUND) + return None + self.db[table][i] = deepcopy(indata) return {"updated": 1} except DbException: raise @@ -174,9 +181,10 @@ class DbMemory(DbBase): if not id: id = str(uuid4()) indata["_id"] = id - if table not in self.db: - self.db[table] = [] - self.db[table].append(deepcopy(indata)) + with self.lock: + if table not in self.db: + self.db[table] = [] + self.db[table].append(deepcopy(indata)) return id except Exception as e: # TODO refine raise DbException(str(e))