From 0d8e4bc4bc2e7bc1e46048fe779b7e2f0012fda0 Mon Sep 17 00:00:00 2001 From: tierno Date: Mon, 22 Jun 2020 12:18:18 +0000 Subject: [PATCH] database set_one/set_list. Adding pull_list parameter Change-Id: I5cd5ba66457845d4c31235e4fc04e900a178e268 Signed-off-by: tierno --- osm_common/dbbase.py | 6 ++++-- osm_common/dbmemory.py | 31 ++++++++++++++++++++++++++----- osm_common/dbmongo.py | 18 ++++++++++++------ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/osm_common/dbbase.py b/osm_common/dbbase.py index c71c5e1..8d694a2 100644 --- a/osm_common/dbbase.py +++ b/osm_common/dbbase.py @@ -152,7 +152,7 @@ class DbBase(object): raise DbException("Method 'create_list' not implemented") def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None, - push_list=None): + push_list=None, pull_list=None): """ Modifies an entry at database :param table: collection or table @@ -166,13 +166,14 @@ class DbBase(object): if exist in the array is removed. If not exist, it is ignored :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the whole array :return: Dict with the number of entries modified. None if no matching is found. """ raise DbException("Method 'set_one' not implemented") - def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None, pull_list=None): """ Modifies al matching entries at database :param table: collection or table @@ -184,6 +185,7 @@ class DbBase(object): if exist in the array is removed. If not exist, it is ignored :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the whole array :return: Dict with the number of entries modified diff --git a/osm_common/dbmemory.py b/osm_common/dbmemory.py index 6ad93d6..51ae810 100644 --- a/osm_common/dbmemory.py +++ b/osm_common/dbmemory.py @@ -251,7 +251,7 @@ class DbMemory(DbBase): except Exception as e: # TODO refine raise DbException(str(e)) - def _update(self, db_item, update_dict, unset=None, pull=None, push=None, push_list=None): + def _update(self, db_item, update_dict, unset=None, pull=None, push=None, push_list=None, pull_list=None): """ Modifies an entry at database :param db_item: entry of the table to update @@ -260,6 +260,7 @@ class DbMemory(DbBase): ignored. If not exist, it is ignored :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value if exist in the array is removed. If not exist, it is ignored + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the @@ -328,6 +329,23 @@ class DbMemory(DbBase): while v in dict_to_update[key_to_update]: dict_to_update[key_to_update].remove(v) updated = True + if pull_list: + for dot_k, v in pull_list.items(): + if not isinstance(v, list): + raise DbException("Invalid content at pull_list, '{}' must be an array".format(dot_k), + http_code=HTTPStatus.BAD_REQUEST) + try: + dict_to_update, key_to_update, _ = _iterate_keys(dot_k, db_item, populate=False) + except Exception: + continue + if key_to_update not in dict_to_update: + continue + if not isinstance(dict_to_update[key_to_update], list): + raise DbException("Cannot pull_list '{}'. Target is not a list".format(dot_k)) + for single_v in v: + while single_v in dict_to_update[key_to_update]: + dict_to_update[key_to_update].remove(single_v) + updated = True if push: for dot_k, v in push.items(): dict_to_update, key_to_update, populated = _iterate_keys(dot_k, db_item) @@ -368,7 +386,7 @@ class DbMemory(DbBase): raise DbException(str(e)) def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None, - push_list=None): + push_list=None, pull_list=None): """ Modifies an entry at database :param table: collection or table @@ -380,6 +398,7 @@ class DbMemory(DbBase): ignored. If not exist, it is ignored :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value if exist in the array is removed. If not exist, it is ignored + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the @@ -388,21 +407,23 @@ class DbMemory(DbBase): """ with self.lock: for i, db_item in self._find(table, self._format_filter(q_filter)): - updated = self._update(db_item, update_dict, unset=unset, pull=pull, push=push, push_list=push_list) + updated = self._update(db_item, update_dict, unset=unset, pull=pull, push=push, push_list=push_list, + pull_list=pull_list) return {"updated": 1 if updated else 0} else: if fail_on_empty: raise DbException("Not found entry with _id='{}'".format(q_filter), HTTPStatus.NOT_FOUND) return None - def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None, pull_list=None): """Modifies al matching entries at database. Same as push. Do not fail if nothing matches""" with self.lock: updated = 0 found = 0 for _, db_item in self._find(table, self._format_filter(q_filter)): found += 1 - if self._update(db_item, update_dict, unset=unset, pull=pull, push=push, push_list=push_list): + if self._update(db_item, update_dict, unset=unset, pull=pull, push=push, push_list=push_list, + pull_list=pull_list): updated += 1 # if not found and fail_on_empty: # raise DbException("Not found entry with '{}'".format(q_filter), HTTPStatus.NOT_FOUND) diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py index 0784985..b0686aa 100644 --- a/osm_common/dbmongo.py +++ b/osm_common/dbmongo.py @@ -363,7 +363,7 @@ class DbMongo(DbBase): raise DbException(e) def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None, - push_list=None): + push_list=None, pull_list=None): """ Modifies an entry at database :param table: collection or table @@ -375,6 +375,7 @@ class DbMongo(DbBase): ignored. If not exist, it is ignored :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value if exist in the array is removed. If not exist, it is ignored + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the @@ -387,8 +388,10 @@ class DbMongo(DbBase): db_oper["$set"] = update_dict if unset: db_oper["$unset"] = unset - if pull: - db_oper["$pull"] = pull + if pull or pull_list: + db_oper["$pull"] = pull or {} + if pull_list: + db_oper["$pull"].update({k: {"$in": v} for k, v in pull_list.items()}) if push or push_list: db_oper["$push"] = push or {} if push_list: @@ -406,7 +409,7 @@ class DbMongo(DbBase): except Exception as e: # TODO refine raise DbException(e) - def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None, pull_list=None): """ Modifies al matching entries at database :param table: collection or table @@ -418,6 +421,7 @@ class DbMongo(DbBase): if exist in the array is removed. If not exist, it is ignored :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys, the single value is appended to the end of the array + :param pull_list: Same as pull but values are arrays where each item is removed from the array :param push_list: Same as push but values are arrays where each item is and appended instead of appending the whole array :return: Dict with the number of entries modified @@ -428,8 +432,10 @@ class DbMongo(DbBase): db_oper["$set"] = update_dict if unset: db_oper["$unset"] = unset - if pull: - db_oper["$pull"] = pull + if pull or pull_list: + db_oper["$pull"] = pull or {} + if pull_list: + db_oper["$pull"].update({k: {"$in": v} for k, v in pull_list.items()}) if push or push_list: db_oper["$push"] = push or {} if push_list: -- 2.17.1