Optimize stoken-using functions to only account for current revisions.

No need to account for revisions that are not current when calculating stokens
because those, by definition, are not the latest ones, and therefore won't have
the most recent stokens.

This becomes a problem when collections have many associated revisions.
tom-mods
Tom Hacohen 9 months ago
parent 4293acb3a3
commit a54afd5210

@ -66,7 +66,7 @@ class Collection(models.Model):
@cached_property
def stoken(self) -> str:
stoken_id = (
self.__class__.objects.filter(main_item=self.main_item)
self.__class__.objects.filter(main_item=self.main_item, items__revisions__current=True)
.annotate(max_stoken=self.stoken_annotation)
.values("max_stoken")
.first()["max_stoken"]

@ -208,7 +208,7 @@ def collection_list_common(
prefetch: Prefetch,
) -> CollectionListResponse:
result, new_stoken_obj, done = filter_by_stoken_and_limit(
stoken, limit, queryset, models.Collection.stoken_annotation
stoken, limit, queryset.filter(items__revisions__current=True), models.Collection.stoken_annotation
)
new_stoken = new_stoken_obj and new_stoken_obj.uid
context = Context(user, prefetch)
@ -428,7 +428,7 @@ def item_list_common(
prefetch: Prefetch,
) -> CollectionItemListResponse:
result, new_stoken_obj, done = filter_by_stoken_and_limit(
stoken, limit, queryset, models.CollectionItem.stoken_annotation
stoken, limit, queryset.filter(revisions__current=True), models.CollectionItem.stoken_annotation
)
new_stoken = new_stoken_obj and new_stoken_obj.uid
context = Context(user, prefetch)

Loading…
Cancel
Save