diff options
Diffstat (limited to 'internal/db/bundb/util.go')
-rw-r--r-- | internal/db/bundb/util.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index 3c3249daf..1d820d081 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -20,7 +20,9 @@ package bundb import ( "strings" + "github.com/superseriousbusiness/gotosocial/internal/cache" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/uptrace/bun" ) @@ -83,6 +85,29 @@ func whereStartsLike( ) } +// loadPagedIDs loads a page of IDs from given SliceCache by `key`, resorting to `loadDESC` if required. Uses `page` to sort + page resulting IDs. +// NOTE: IDs returned from `cache` / `loadDESC` MUST be in descending order, otherwise paging will not work correctly / return things out of order. +func loadPagedIDs(cache *cache.SliceCache[string], key string, page *paging.Page, loadDESC func() ([]string, error)) ([]string, error) { + // Check cache for IDs, else load. + ids, err := cache.Load(key, loadDESC) + if err != nil { + return nil, err + } + + // Our cached / selected IDs are ALWAYS + // fetched from `loadDESC` in descending + // order. Depending on the paging requested + // this may be an unexpected order. + if page.GetOrder().Ascending() { + ids = paging.Reverse(ids) + } + + // Page the resulting IDs. + ids = page.Page(ids) + + return ids, nil +} + // updateWhere parses []db.Where and adds it to the given update query. func updateWhere(q *bun.UpdateQuery, where []db.Where) { for _, w := range where { |