|
| 1 | +# |
| 2 | +# Gramps Web API - A RESTful API for the Gramps genealogy program |
| 3 | +# |
| 4 | +# Copyright (C) 2025 David Straub |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as published by |
| 8 | +# the Free Software Foundation; either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Affero General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Affero General Public License |
| 17 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | + |
| 21 | +"""A proxy database class optionally caching people and families.""" |
| 22 | + |
| 23 | +from typing import Generator |
| 24 | + |
| 25 | +from gramps.gen.proxy.proxybase import ProxyDbBase |
| 26 | +from gramps.gen.db import DbReadBase |
| 27 | +from gramps.gen.lib import Person, Family |
| 28 | + |
| 29 | + |
| 30 | +class CachePeopleFamiliesProxy(ProxyDbBase): |
| 31 | + """Proxy database class optionally caching people and families.""" |
| 32 | + |
| 33 | + def __init__(self, db: DbReadBase) -> None: |
| 34 | + """Initialize the proxy database.""" |
| 35 | + super().__init__(db) |
| 36 | + self.db: DbReadBase # for type checker |
| 37 | + self._people_cache: dict[str, Person] = {} |
| 38 | + self._family_cache: dict[str, Family] = {} |
| 39 | + |
| 40 | + def cache_people(self) -> None: |
| 41 | + """Cache all people.""" |
| 42 | + self._people_cache = {obj.handle: obj for obj in self.db.iter_people()} |
| 43 | + |
| 44 | + def cache_families(self) -> None: |
| 45 | + """Cache all families.""" |
| 46 | + self._family_cache = {obj.handle: obj for obj in self.db.iter_families()} |
| 47 | + |
| 48 | + def get_person_from_handle(self, handle: str) -> Person: |
| 49 | + """Get a person from the cache or the database.""" |
| 50 | + if handle in self._people_cache: |
| 51 | + return self._people_cache[handle] |
| 52 | + return self.db.get_person_from_handle(handle) |
| 53 | + |
| 54 | + def get_family_from_handle(self, handle: str) -> Family: |
| 55 | + """Get a family from the cache or the database.""" |
| 56 | + if handle in self._family_cache: |
| 57 | + return self._family_cache[handle] |
| 58 | + return self.db.get_family_from_handle(handle) |
| 59 | + |
| 60 | + def find_backlink_handles( |
| 61 | + self, handle, include_classes=None |
| 62 | + ) -> Generator[tuple[str, str], None, None]: |
| 63 | + """ |
| 64 | + Find all objects that hold a reference to the object handle. |
| 65 | +
|
| 66 | + Returns an iterator over a list of (class_name, handle) tuples. |
| 67 | +
|
| 68 | + :param handle: handle of the object to search for. |
| 69 | + :type handle: str database handle |
| 70 | + :param include_classes: list of class names to include in the results. |
| 71 | + Default is None which includes all classes. |
| 72 | + :type include_classes: list of class names |
| 73 | +
|
| 74 | + This default implementation does a sequential scan through all |
| 75 | + the primary object databases and is very slow. Backends can |
| 76 | + override this method to provide much faster implementations that |
| 77 | + make use of additional capabilities of the backend. |
| 78 | +
|
| 79 | + Note that this is a generator function, it returns a iterator for |
| 80 | + use in loops. If you want a list of the results use:: |
| 81 | +
|
| 82 | + result_list = list(find_backlink_handles(handle)) |
| 83 | + """ |
| 84 | + return self.db.find_backlink_handles(handle, include_classes) |
0 commit comments