X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=osm_common%2Fdbmemory.py;h=c2d9a113273a03566c2c8f5b476aba2eb68909ef;hb=eef7cb7a4f704bf0d29e6d8ece5195dc3faabb69;hp=0e0c42c2fdaf9abd2ed83e6fbfe2218704d7730c;hpb=136f29577fd83028369c2c4fc4c60f738e0d26d3;p=osm%2Fcommon.git diff --git a/osm_common/dbmemory.py b/osm_common/dbmemory.py index 0e0c42c..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', master_password=None): - super().__init__(logger_name, master_password) + def __init__(self, logger_name='db', lock=False): + super().__init__(logger_name, lock) self.db = {} def db_connect(self, config): @@ -38,6 +38,9 @@ class DbMemory(DbBase): """ if "logger_name" in config: self.logger = logging.getLogger(config["logger_name"]) + master_key = config.get("commonkey") or config.get("masterpassword") + if master_key: + self.set_secret_key(master_key) @staticmethod def _format_filter(q_filter): @@ -62,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 @@ -83,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) @@ -105,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] @@ -126,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)) @@ -148,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 @@ -173,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))