Skip to content

Commit 7e2e58d

Browse files
authored
Speed up relationship lookup (#316)
* Speed up relationship lookup * Only iterate once
1 parent 174306f commit 7e2e58d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

gramps_webapi/api/resources/relations.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..util import get_db_handle, get_locale_for_language
3131
from . import ProtectedResource
3232
from .emit import GrampsJSONEncoder
33-
from .util import get_person_by_handle
33+
from .util import get_one_relationship, get_person_by_handle
3434

3535

3636
class RelationResource(ProtectedResource, GrampsJSONEncoder):
@@ -57,11 +57,12 @@ def get(self, args: Dict, handle1: Handle, handle2: Handle) -> Response:
5757
abort(404)
5858

5959
locale = get_locale_for_language(args["locale"], default=True)
60-
calc = get_relationship_calculator(reinit=True, clocale=locale)
61-
calc.set_depth(args["depth"])
62-
63-
data = calc.get_one_relationship(
64-
db_handle, person1, person2, extra_info=True, olocale=locale
60+
data = get_one_relationship(
61+
db_handle=db_handle,
62+
person1=person1,
63+
person2=person2,
64+
depth=args["depth"],
65+
locale=locale,
6566
)
6667
return self.response(
6768
200,

gramps_webapi/api/resources/util.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
)
5454
from gramps.gen.lib.primaryobj import BasicPrimaryObject as GrampsObject
5555
from gramps.gen.lib.serialize import from_json, to_json
56+
from gramps.gen.relationship import get_relationship_calculator
5657
from gramps.gen.soundex import soundex
5758
from gramps.gen.utils.db import (
5859
get_birth_or_fallback,
@@ -1102,3 +1103,28 @@ def get_missing_media_file_handles(
11021103
objects = [db_handle.get_media_from_handle(handle) for handle in handles]
11031104
objects_missing = filter_missing_files(objects)
11041105
return [obj.handle for obj in objects_missing]
1106+
1107+
1108+
def get_one_relationship(
1109+
db_handle: DbReadBase,
1110+
person1: Person,
1111+
person2: Person,
1112+
depth: int,
1113+
locale: GrampsLocale = glocale,
1114+
) -> Tuple[str, int, int]:
1115+
"""Get a relationship string and the number of generations between the people."""
1116+
calc = get_relationship_calculator(reinit=True, clocale=locale)
1117+
# the relationship calculation can be slow when depth is set to a large value
1118+
# even when the relationship path is short. To avoid this, we are iterating
1119+
# trying once with depth = 5
1120+
if depth > 5:
1121+
calc.set_depth(5)
1122+
rel_string, dist_orig, dist_other = calc.get_one_relationship(
1123+
db_handle, person1, person2, extra_info=True, olocale=locale
1124+
)
1125+
if dist_orig > -1:
1126+
return rel_string, dist_orig, dist_other
1127+
calc.set_depth(depth)
1128+
return calc.get_one_relationship(
1129+
db_handle, person1, person2, extra_info=True, olocale=locale
1130+
)

0 commit comments

Comments
 (0)