@@ -845,11 +845,20 @@ def _transform(items: Sequence[Rom]) -> list[SimpleRomSchema]:
845845 for item in items
846846 ]
847847
848+ def resolve_total () -> int | None :
849+ if with_rom_id_index :
850+ # The index already spans the result set, so the count is free.
851+ return len (rom_id_index )
852+ # Without the index the count is its own scan of the filtered set,
853+ # so a caller scrolling a gallery it already sized opts out.
854+ return (
855+ db_rom_handler .get_rom_count (query = query , session = session )
856+ if with_total
857+ else None
858+ )
859+
848860 params = resolve_params ()
849- total : int | None
850861 if with_rom_id_index :
851- # The index already spans the result set, so the count is free.
852- total = len (rom_id_index )
853862 page_ids = list (rom_id_index [params .offset : params .offset + params .limit ])
854863 if page_ids :
855864 page_rows = session .scalars (query .where (Rom .id .in_ (page_ids ))).all ()
@@ -863,18 +872,11 @@ def _transform(items: Sequence[Rom]) -> list[SimpleRomSchema]:
863872 page_items = list (
864873 session .scalars (query .offset (params .offset ).limit (params .limit )).all ()
865874 )
866- # Without the index the count is its own scan of the filtered set,
867- # so a caller scrolling a gallery it already sized opts out.
868- total = (
869- db_rom_handler .get_rom_count (query = query , session = session )
870- if with_total
871- else None
872- )
873875
874876 return CustomLimitOffsetPage .create (
875877 _transform (page_items ),
876878 params ,
877- total = total ,
879+ total = resolve_total () ,
878880 char_index = char_index_dict ,
879881 rom_id_index = list (rom_id_index ),
880882 filter_values = filter_values ,
0 commit comments