| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Telefonica S.A. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 18 | from copy import deepcopy |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame] | 19 | from http import HTTPStatus |
| 20 | import logging |
| 21 | from uuid import uuid4 |
| 22 | |
| 23 | from osm_common.dbbase import DbBase, DbException |
| 24 | from osm_common.dbmongo import deep_update |
| 25 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 26 | |
| 27 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 28 | |
| 29 | |
| 30 | class DbMemory(DbBase): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 31 | def __init__(self, logger_name="db", lock=False): |
| Gulsum Atici | 76394ef | 2023-01-09 23:19:18 +0300 | [diff] [blame] | 32 | super().__init__(logger_name=logger_name, lock=lock) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 33 | self.db = {} |
| 34 | |
| 35 | def db_connect(self, config): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 36 | """ |
| 37 | Connect to database |
| 38 | :param config: Configuration of database |
| 39 | :return: None or raises DbException on error |
| 40 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 41 | if "logger_name" in config: |
| 42 | self.logger = logging.getLogger(config["logger_name"]) |
| tierno | eef7cb7 | 2018-11-12 11:51:49 +0100 | [diff] [blame] | 43 | master_key = config.get("commonkey") or config.get("masterpassword") |
| 44 | if master_key: |
| 45 | self.set_secret_key(master_key) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 46 | |
| 47 | @staticmethod |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 48 | def _format_filter(q_filter): |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 49 | db_filter = {} |
| 50 | # split keys with ANYINDEX in this way: |
| 51 | # {"A.B.ANYINDEX.C.D.ANYINDEX.E": v } -> {"A.B.ANYINDEX": {"C.D.ANYINDEX": {"E": v}}} |
| 52 | if q_filter: |
| 53 | for k, v in q_filter.items(): |
| 54 | db_v = v |
| 55 | kleft, _, kright = k.rpartition(".ANYINDEX.") |
| 56 | while kleft: |
| 57 | k = kleft + ".ANYINDEX" |
| 58 | db_v = {kright: db_v} |
| 59 | kleft, _, kright = k.rpartition(".ANYINDEX.") |
| 60 | deep_update(db_filter, {k: db_v}) |
| 61 | |
| 62 | return db_filter |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 63 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 64 | def _find(self, table, q_filter): |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 65 | def recursive_find(key_list, key_next_index, content, oper, target): |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 66 | if key_next_index == len(key_list) or content is None: |
| 67 | try: |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 68 | if oper in ("eq", "cont"): |
| 69 | if isinstance(target, list): |
| 70 | if isinstance(content, list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 71 | return any( |
| 72 | content_item in target for content_item in content |
| 73 | ) |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 74 | return content in target |
| 75 | elif isinstance(content, list): |
| 76 | return target in content |
| 77 | else: |
| 78 | return content == target |
| 79 | elif oper in ("neq", "ne", "ncont"): |
| 80 | if isinstance(target, list): |
| 81 | if isinstance(content, list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 82 | return all( |
| 83 | content_item not in target |
| 84 | for content_item in content |
| 85 | ) |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 86 | return content not in target |
| 87 | elif isinstance(content, list): |
| 88 | return target not in content |
| 89 | else: |
| 90 | return content != target |
| 91 | if oper == "gt": |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 92 | return content > target |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 93 | elif oper == "gte": |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 94 | return content >= target |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 95 | elif oper == "lt": |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 96 | return content < target |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 97 | elif oper == "lte": |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 98 | return content <= target |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 99 | else: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 100 | raise DbException( |
| 101 | "Unknown filter operator '{}' in key '{}'".format( |
| 102 | oper, ".".join(key_list) |
| 103 | ), |
| 104 | http_code=HTTPStatus.BAD_REQUEST, |
| 105 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 106 | except TypeError: |
| 107 | return False |
| 108 | |
| 109 | elif isinstance(content, dict): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 110 | return recursive_find( |
| 111 | key_list, |
| 112 | key_next_index + 1, |
| 113 | content.get(key_list[key_next_index]), |
| 114 | oper, |
| 115 | target, |
| 116 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 117 | elif isinstance(content, list): |
| 118 | look_for_match = True # when there is a match return immediately |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 119 | if (target is None) != ( |
| 120 | oper in ("neq", "ne", "ncont") |
| 121 | ): # one True and other False (Xor) |
| 122 | look_for_match = ( |
| 123 | False # when there is not a match return immediately |
| 124 | ) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 125 | |
| 126 | for content_item in content: |
| 127 | if key_list[key_next_index] == "ANYINDEX" and isinstance(v, dict): |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 128 | matches = True |
| k4.rahul | b2d732a | 2023-04-27 16:20:47 +0530 | [diff] [blame] | 129 | if target: |
| 130 | for k2, v2 in target.items(): |
| 131 | k_new_list = k2.split(".") |
| 132 | new_operator = "eq" |
| 133 | if k_new_list[-1] in ( |
| 134 | "eq", |
| 135 | "ne", |
| 136 | "gt", |
| 137 | "gte", |
| 138 | "lt", |
| 139 | "lte", |
| 140 | "cont", |
| 141 | "ncont", |
| 142 | "neq", |
| 143 | ): |
| 144 | new_operator = k_new_list.pop() |
| 145 | if not recursive_find( |
| 146 | k_new_list, 0, content_item, new_operator, v2 |
| 147 | ): |
| 148 | matches = False |
| 149 | break |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 150 | |
| 151 | else: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 152 | matches = recursive_find( |
| 153 | key_list, key_next_index, content_item, oper, target |
| 154 | ) |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 155 | if matches == look_for_match: |
| 156 | return matches |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 157 | if key_list[key_next_index].isdecimal() and int( |
| 158 | key_list[key_next_index] |
| 159 | ) < len(content): |
| 160 | matches = recursive_find( |
| 161 | key_list, |
| 162 | key_next_index + 1, |
| 163 | content[int(key_list[key_next_index])], |
| 164 | oper, |
| 165 | target, |
| 166 | ) |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 167 | if matches == look_for_match: |
| 168 | return matches |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 169 | return not look_for_match |
| 170 | else: # content is not dict, nor list neither None, so not found |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 171 | if oper in ("neq", "ne", "ncont"): |
| 172 | return target is not None |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 173 | else: |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 174 | return target is None |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 175 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 176 | for i, row in enumerate(self.db.get(table, ())): |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 177 | q_filter = q_filter or {} |
| 178 | for k, v in q_filter.items(): |
| 179 | k_list = k.split(".") |
| 180 | operator = "eq" |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 181 | if k_list[-1] in ( |
| 182 | "eq", |
| 183 | "ne", |
| 184 | "gt", |
| 185 | "gte", |
| 186 | "lt", |
| 187 | "lte", |
| 188 | "cont", |
| 189 | "ncont", |
| 190 | "neq", |
| 191 | ): |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 192 | operator = k_list.pop() |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 193 | matches = recursive_find(k_list, 0, row, operator, v) |
| 194 | if not matches: |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 195 | break |
| 196 | else: |
| 197 | # match |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 198 | yield i, row |
| 199 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 200 | def get_list(self, table, q_filter=None): |
| 201 | """ |
| 202 | Obtain a list of entries matching q_filter |
| 203 | :param table: collection or table |
| 204 | :param q_filter: Filter |
| 205 | :return: a list (can be empty) with the found entries. Raises DbException on error |
| 206 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 207 | try: |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 208 | result = [] |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 209 | with self.lock: |
| 210 | for _, row in self._find(table, self._format_filter(q_filter)): |
| 211 | result.append(deepcopy(row)) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 212 | return result |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 213 | except DbException: |
| 214 | raise |
| 215 | except Exception as e: # TODO refine |
| 216 | raise DbException(str(e)) |
| 217 | |
| delacruzramo | ae049d8 | 2019-09-17 16:05:17 +0200 | [diff] [blame] | 218 | def count(self, table, q_filter=None): |
| 219 | """ |
| 220 | Count the number of entries matching q_filter |
| 221 | :param table: collection or table |
| 222 | :param q_filter: Filter |
| 223 | :return: number of entries found (can be zero) |
| 224 | :raise: DbException on error |
| 225 | """ |
| 226 | try: |
| 227 | with self.lock: |
| 228 | return sum(1 for x in self._find(table, self._format_filter(q_filter))) |
| 229 | except DbException: |
| 230 | raise |
| 231 | except Exception as e: # TODO refine |
| 232 | raise DbException(str(e)) |
| 233 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 234 | def get_one(self, table, q_filter=None, fail_on_empty=True, fail_on_more=True): |
| 235 | """ |
| 236 | Obtain one entry matching q_filter |
| 237 | :param table: collection or table |
| 238 | :param q_filter: Filter |
| 239 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case |
| 240 | it raises a DbException |
| 241 | :param fail_on_more: If more than one matches filter it returns one of then unless this flag is set tu True, so |
| 242 | that it raises a DbException |
| 243 | :return: The requested element, or None |
| 244 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 245 | try: |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 246 | result = None |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 247 | with self.lock: |
| 248 | for _, row in self._find(table, self._format_filter(q_filter)): |
| 249 | if not fail_on_more: |
| 250 | return deepcopy(row) |
| 251 | if result: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 252 | raise DbException( |
| 253 | "Found more than one entry with filter='{}'".format( |
| 254 | q_filter |
| 255 | ), |
| 256 | HTTPStatus.CONFLICT.value, |
| 257 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 258 | result = row |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 259 | if not result and fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 260 | raise DbException( |
| 261 | "Not found entry with filter='{}'".format(q_filter), |
| 262 | HTTPStatus.NOT_FOUND, |
| 263 | ) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 264 | return deepcopy(result) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 265 | except Exception as e: # TODO refine |
| 266 | raise DbException(str(e)) |
| 267 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 268 | def del_list(self, table, q_filter=None): |
| 269 | """ |
| 270 | Deletes all entries that match q_filter |
| 271 | :param table: collection or table |
| 272 | :param q_filter: Filter |
| 273 | :return: Dict with the number of entries deleted |
| 274 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 275 | try: |
| 276 | id_list = [] |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 277 | with self.lock: |
| 278 | for i, _ in self._find(table, self._format_filter(q_filter)): |
| 279 | id_list.append(i) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 280 | deleted = len(id_list) |
| Eduardo Sousa | 857731b | 2018-04-26 15:55:05 +0100 | [diff] [blame] | 281 | for i in reversed(id_list): |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 282 | del self.db[table][i] |
| 283 | return {"deleted": deleted} |
| 284 | except DbException: |
| 285 | raise |
| 286 | except Exception as e: # TODO refine |
| 287 | raise DbException(str(e)) |
| 288 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 289 | def del_one(self, table, q_filter=None, fail_on_empty=True): |
| 290 | """ |
| 291 | Deletes one entry that matches q_filter |
| 292 | :param table: collection or table |
| 293 | :param q_filter: Filter |
| 294 | :param fail_on_empty: If nothing matches filter it returns '0' deleted unless this flag is set tu True, in |
| 295 | which case it raises a DbException |
| 296 | :return: Dict with the number of entries deleted |
| 297 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 298 | try: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 299 | with self.lock: |
| 300 | for i, _ in self._find(table, self._format_filter(q_filter)): |
| 301 | break |
| 302 | else: |
| 303 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 304 | raise DbException( |
| 305 | "Not found entry with filter='{}'".format(q_filter), |
| 306 | HTTPStatus.NOT_FOUND, |
| 307 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 308 | return None |
| 309 | del self.db[table][i] |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 310 | return {"deleted": 1} |
| 311 | except Exception as e: # TODO refine |
| 312 | raise DbException(str(e)) |
| 313 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 314 | def _update( |
| 315 | self, |
| 316 | db_item, |
| 317 | update_dict, |
| 318 | unset=None, |
| 319 | pull=None, |
| 320 | push=None, |
| 321 | push_list=None, |
| 322 | pull_list=None, |
| 323 | ): |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 324 | """ |
| 325 | Modifies an entry at database |
| 326 | :param db_item: entry of the table to update |
| 327 | :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value |
| 328 | :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is |
| 329 | ignored. If not exist, it is ignored |
| 330 | :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value |
| 331 | if exist in the array is removed. If not exist, it is ignored |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 332 | :param pull_list: Same as pull but values are arrays where each item is removed from the array |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 333 | :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value |
| 334 | is appended to the end of the array |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 335 | :param push_list: Same as push but values are arrays where each item is and appended instead of appending the |
| 336 | whole array |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 337 | :return: True if database has been changed, False if not; Exception on error |
| 338 | """ |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 339 | |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 340 | def _iterate_keys(k, db_nested, populate=True): |
| 341 | k_list = k.split(".") |
| 342 | k_item_prev = k_list[0] |
| 343 | populated = False |
| tierno | bf6c572 | 2020-03-12 09:54:35 +0000 | [diff] [blame] | 344 | if k_item_prev not in db_nested and populate: |
| 345 | populated = True |
| 346 | db_nested[k_item_prev] = None |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 347 | for k_item in k_list[1:]: |
| 348 | if isinstance(db_nested[k_item_prev], dict): |
| 349 | if k_item not in db_nested[k_item_prev]: |
| 350 | if not populate: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 351 | raise DbException( |
| 352 | "Cannot set '{}', not existing '{}'".format(k, k_item) |
| 353 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 354 | populated = True |
| 355 | db_nested[k_item_prev][k_item] = None |
| 356 | elif isinstance(db_nested[k_item_prev], list) and k_item.isdigit(): |
| 357 | # extend list with Nones if index greater than list |
| 358 | k_item = int(k_item) |
| 359 | if k_item >= len(db_nested[k_item_prev]): |
| 360 | if not populate: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 361 | raise DbException( |
| 362 | "Cannot set '{}', index too large '{}'".format( |
| 363 | k, k_item |
| 364 | ) |
| 365 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 366 | populated = True |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 367 | db_nested[k_item_prev] += [None] * ( |
| 368 | k_item - len(db_nested[k_item_prev]) + 1 |
| 369 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 370 | elif db_nested[k_item_prev] is None: |
| 371 | if not populate: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 372 | raise DbException( |
| 373 | "Cannot set '{}', not existing '{}'".format(k, k_item) |
| 374 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 375 | populated = True |
| 376 | db_nested[k_item_prev] = {k_item: None} |
| 377 | else: # number, string, boolean, ... or list but with not integer key |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 378 | raise DbException( |
| 379 | "Cannot set '{}' on existing '{}={}'".format( |
| 380 | k, k_item_prev, db_nested[k_item_prev] |
| 381 | ) |
| 382 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 383 | db_nested = db_nested[k_item_prev] |
| 384 | k_item_prev = k_item |
| 385 | return db_nested, k_item_prev, populated |
| 386 | |
| 387 | updated = False |
| 388 | try: |
| 389 | if update_dict: |
| 390 | for dot_k, v in update_dict.items(): |
| 391 | dict_to_update, key_to_update, _ = _iterate_keys(dot_k, db_item) |
| 392 | dict_to_update[key_to_update] = v |
| 393 | updated = True |
| 394 | if unset: |
| 395 | for dot_k in unset: |
| 396 | try: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 397 | dict_to_update, key_to_update, _ = _iterate_keys( |
| 398 | dot_k, db_item, populate=False |
| 399 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 400 | del dict_to_update[key_to_update] |
| 401 | updated = True |
| aticig | d3b582a | 2022-08-24 22:41:56 +0300 | [diff] [blame] | 402 | except Exception as unset_error: |
| 403 | self.logger.error(f"{unset_error} occured while updating DB.") |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 404 | if pull: |
| 405 | for dot_k, v in pull.items(): |
| 406 | try: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 407 | dict_to_update, key_to_update, _ = _iterate_keys( |
| 408 | dot_k, db_item, populate=False |
| 409 | ) |
| aticig | d3b582a | 2022-08-24 22:41:56 +0300 | [diff] [blame] | 410 | except Exception as pull_error: |
| 411 | self.logger.error(f"{pull_error} occured while updating DB.") |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 412 | continue |
| aticig | d3b582a | 2022-08-24 22:41:56 +0300 | [diff] [blame] | 413 | |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 414 | if key_to_update not in dict_to_update: |
| 415 | continue |
| 416 | if not isinstance(dict_to_update[key_to_update], list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 417 | raise DbException( |
| 418 | "Cannot pull '{}'. Target is not a list".format(dot_k) |
| 419 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 420 | while v in dict_to_update[key_to_update]: |
| 421 | dict_to_update[key_to_update].remove(v) |
| 422 | updated = True |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 423 | if pull_list: |
| 424 | for dot_k, v in pull_list.items(): |
| 425 | if not isinstance(v, list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 426 | raise DbException( |
| 427 | "Invalid content at pull_list, '{}' must be an array".format( |
| 428 | dot_k |
| 429 | ), |
| 430 | http_code=HTTPStatus.BAD_REQUEST, |
| 431 | ) |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 432 | try: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 433 | dict_to_update, key_to_update, _ = _iterate_keys( |
| 434 | dot_k, db_item, populate=False |
| 435 | ) |
| aticig | d3b582a | 2022-08-24 22:41:56 +0300 | [diff] [blame] | 436 | except Exception as iterate_error: |
| 437 | self.logger.error( |
| 438 | f"{iterate_error} occured while iterating keys in db update." |
| 439 | ) |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 440 | continue |
| aticig | d3b582a | 2022-08-24 22:41:56 +0300 | [diff] [blame] | 441 | |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 442 | if key_to_update not in dict_to_update: |
| 443 | continue |
| 444 | if not isinstance(dict_to_update[key_to_update], list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 445 | raise DbException( |
| 446 | "Cannot pull_list '{}'. Target is not a list".format(dot_k) |
| 447 | ) |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 448 | for single_v in v: |
| 449 | while single_v in dict_to_update[key_to_update]: |
| 450 | dict_to_update[key_to_update].remove(single_v) |
| 451 | updated = True |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 452 | if push: |
| 453 | for dot_k, v in push.items(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 454 | dict_to_update, key_to_update, populated = _iterate_keys( |
| 455 | dot_k, db_item |
| 456 | ) |
| 457 | if ( |
| 458 | isinstance(dict_to_update, dict) |
| 459 | and key_to_update not in dict_to_update |
| 460 | ): |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 461 | dict_to_update[key_to_update] = [v] |
| 462 | updated = True |
| 463 | elif populated and dict_to_update[key_to_update] is None: |
| 464 | dict_to_update[key_to_update] = [v] |
| 465 | updated = True |
| 466 | elif not isinstance(dict_to_update[key_to_update], list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 467 | raise DbException( |
| 468 | "Cannot push '{}'. Target is not a list".format(dot_k) |
| 469 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 470 | else: |
| 471 | dict_to_update[key_to_update].append(v) |
| 472 | updated = True |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 473 | if push_list: |
| 474 | for dot_k, v in push_list.items(): |
| 475 | if not isinstance(v, list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 476 | raise DbException( |
| 477 | "Invalid content at push_list, '{}' must be an array".format( |
| 478 | dot_k |
| 479 | ), |
| 480 | http_code=HTTPStatus.BAD_REQUEST, |
| 481 | ) |
| 482 | dict_to_update, key_to_update, populated = _iterate_keys( |
| 483 | dot_k, db_item |
| 484 | ) |
| 485 | if ( |
| 486 | isinstance(dict_to_update, dict) |
| 487 | and key_to_update not in dict_to_update |
| 488 | ): |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 489 | dict_to_update[key_to_update] = v.copy() |
| 490 | updated = True |
| 491 | elif populated and dict_to_update[key_to_update] is None: |
| 492 | dict_to_update[key_to_update] = v.copy() |
| 493 | updated = True |
| 494 | elif not isinstance(dict_to_update[key_to_update], list): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 495 | raise DbException( |
| 496 | "Cannot push '{}'. Target is not a list".format(dot_k), |
| 497 | http_code=HTTPStatus.CONFLICT, |
| 498 | ) |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 499 | else: |
| 500 | dict_to_update[key_to_update] += v |
| 501 | updated = True |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 502 | |
| 503 | return updated |
| 504 | except DbException: |
| 505 | raise |
| 506 | except Exception as e: # TODO refine |
| 507 | raise DbException(str(e)) |
| 508 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 509 | def set_one( |
| 510 | self, |
| 511 | table, |
| 512 | q_filter, |
| 513 | update_dict, |
| 514 | fail_on_empty=True, |
| 515 | unset=None, |
| 516 | pull=None, |
| 517 | push=None, |
| 518 | push_list=None, |
| 519 | pull_list=None, |
| 520 | ): |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 521 | """ |
| 522 | Modifies an entry at database |
| 523 | :param table: collection or table |
| 524 | :param q_filter: Filter |
| 525 | :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value |
| 526 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case |
| 527 | it raises a DbException |
| 528 | :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is |
| 529 | ignored. If not exist, it is ignored |
| 530 | :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value |
| 531 | if exist in the array is removed. If not exist, it is ignored |
| tierno | 0d8e4bc | 2020-06-22 12:18:18 +0000 | [diff] [blame] | 532 | :param pull_list: Same as pull but values are arrays where each item is removed from the array |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 533 | :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value |
| 534 | is appended to the end of the array |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 535 | :param push_list: Same as push but values are arrays where each item is and appended instead of appending the |
| 536 | whole array |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 537 | :return: Dict with the number of entries modified. None if no matching is found. |
| 538 | """ |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 539 | with self.lock: |
| 540 | for i, db_item in self._find(table, self._format_filter(q_filter)): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 541 | updated = self._update( |
| 542 | db_item, |
| 543 | update_dict, |
| 544 | unset=unset, |
| 545 | pull=pull, |
| 546 | push=push, |
| 547 | push_list=push_list, |
| 548 | pull_list=pull_list, |
| 549 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 550 | return {"updated": 1 if updated else 0} |
| 551 | else: |
| 552 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 553 | raise DbException( |
| 554 | "Not found entry with _id='{}'".format(q_filter), |
| 555 | HTTPStatus.NOT_FOUND, |
| 556 | ) |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 557 | return None |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 558 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 559 | def set_list( |
| 560 | self, |
| 561 | table, |
| 562 | q_filter, |
| 563 | update_dict, |
| 564 | unset=None, |
| 565 | pull=None, |
| 566 | push=None, |
| 567 | push_list=None, |
| 568 | pull_list=None, |
| 569 | ): |
| tierno | 399f6c3 | 2020-05-12 07:36:41 +0000 | [diff] [blame] | 570 | """Modifies al matching entries at database. Same as push. Do not fail if nothing matches""" |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 571 | with self.lock: |
| 572 | updated = 0 |
| tierno | 77e2d6a | 2020-03-18 07:31:54 +0000 | [diff] [blame] | 573 | found = 0 |
| 574 | for _, db_item in self._find(table, self._format_filter(q_filter)): |
| 575 | found += 1 |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 576 | if self._update( |
| 577 | db_item, |
| 578 | update_dict, |
| 579 | unset=unset, |
| 580 | pull=pull, |
| 581 | push=push, |
| 582 | push_list=push_list, |
| 583 | pull_list=pull_list, |
| 584 | ): |
| tierno | 7fc50dd | 2020-02-17 12:01:38 +0000 | [diff] [blame] | 585 | updated += 1 |
| tierno | 70911f0 | 2020-03-30 08:56:15 +0000 | [diff] [blame] | 586 | # if not found and fail_on_empty: |
| 587 | # raise DbException("Not found entry with '{}'".format(q_filter), HTTPStatus.NOT_FOUND) |
| tierno | 77e2d6a | 2020-03-18 07:31:54 +0000 | [diff] [blame] | 588 | return {"updated": updated} if found else None |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 589 | |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 590 | def replace(self, table, _id, indata, fail_on_empty=True): |
| 591 | """ |
| 592 | Replace the content of an entry |
| 593 | :param table: collection or table |
| 594 | :param _id: internal database id |
| 595 | :param indata: content to replace |
| 596 | :param fail_on_empty: If nothing matches filter it returns None unless this flag is set tu True, in which case |
| 597 | it raises a DbException |
| 598 | :return: Dict with the number of entries replaced |
| 599 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 600 | try: |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 601 | with self.lock: |
| 602 | for i, _ in self._find(table, self._format_filter({"_id": _id})): |
| 603 | break |
| 604 | else: |
| 605 | if fail_on_empty: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 606 | raise DbException( |
| 607 | "Not found entry with _id='{}'".format(_id), |
| 608 | HTTPStatus.NOT_FOUND, |
| 609 | ) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 610 | return None |
| 611 | self.db[table][i] = deepcopy(indata) |
| Eduardo Sousa | 22f0fcd | 2018-04-26 15:43:28 +0100 | [diff] [blame] | 612 | return {"updated": 1} |
| tierno | 136f295 | 2018-10-19 13:01:03 +0200 | [diff] [blame] | 613 | except DbException: |
| 614 | raise |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 615 | except Exception as e: # TODO refine |
| 616 | raise DbException(str(e)) |
| 617 | |
| 618 | def create(self, table, indata): |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 619 | """ |
| 620 | Add a new entry at database |
| 621 | :param table: collection or table |
| 622 | :param indata: content to be added |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 623 | :return: database '_id' of the inserted element. Raises a DbException on error |
| tierno | 87858ca | 2018-10-08 16:30:15 +0200 | [diff] [blame] | 624 | """ |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 625 | try: |
| 626 | id = indata.get("_id") |
| 627 | if not id: |
| 628 | id = str(uuid4()) |
| 629 | indata["_id"] = id |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 630 | with self.lock: |
| 631 | if table not in self.db: |
| 632 | self.db[table] = [] |
| 633 | self.db[table].append(deepcopy(indata)) |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 634 | return id |
| 635 | except Exception as e: # TODO refine |
| 636 | raise DbException(str(e)) |
| 637 | |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 638 | def create_list(self, table, indata_list): |
| 639 | """ |
| 640 | Add a new entry at database |
| 641 | :param table: collection or table |
| 642 | :param indata_list: list content to be added |
| tierno | 2c9794c | 2020-04-29 10:24:28 +0000 | [diff] [blame] | 643 | :return: list of inserted 'id's. Raises a DbException on error |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 644 | """ |
| 645 | try: |
| 646 | _ids = [] |
| tierno | 40e326a | 2019-09-19 09:23:44 +0000 | [diff] [blame] | 647 | with self.lock: |
| 648 | for indata in indata_list: |
| 649 | _id = indata.get("_id") |
| 650 | if not _id: |
| 651 | _id = str(uuid4()) |
| 652 | indata["_id"] = _id |
| 653 | with self.lock: |
| 654 | if table not in self.db: |
| 655 | self.db[table] = [] |
| 656 | self.db[table].append(deepcopy(indata)) |
| 657 | _ids.append(_id) |
| tierno | 6472e2b | 2019-09-02 16:04:16 +0000 | [diff] [blame] | 658 | return _ids |
| 659 | except Exception as e: # TODO refine |
| 660 | raise DbException(str(e)) |
| 661 | |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 662 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 663 | if __name__ == "__main__": |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 664 | # some test code |
| tierno | 3054f78 | 2018-04-25 16:59:53 +0200 | [diff] [blame] | 665 | db = DbMemory() |
| tierno | 5c01261 | 2018-04-19 16:01:59 +0200 | [diff] [blame] | 666 | db.create("test", {"_id": 1, "data": 1}) |
| 667 | db.create("test", {"_id": 2, "data": 2}) |
| 668 | db.create("test", {"_id": 3, "data": 3}) |
| 669 | print("must be 3 items:", db.get_list("test")) |
| 670 | print("must return item 2:", db.get_list("test", {"_id": 2})) |
| 671 | db.del_one("test", {"_id": 2}) |
| 672 | print("must be emtpy:", db.get_list("test", {"_id": 2})) |