diff options
| author | 2025-10-17 19:41:06 +0200 | |
|---|---|---|
| committer | 2025-11-17 14:11:23 +0100 | |
| commit | 14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5 (patch) | |
| tree | 56b675dadea6b525336ec9c109d932c224df9df5 /internal/db/bundb/util.go | |
| parent | [chore] update dependencies (#4507) (diff) | |
| download | gotosocial-14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5.tar.xz | |
[performance] reduce account stats database calls (#4496)
Reduces both code complexity and the number of separate database transactions we need to make by moving account statistics operations into the database as side-effects of the operations that effect them. In contrast to currently, where we manually update account statistics at the application layer.
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4496
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/db/bundb/util.go')
| -rw-r--r-- | internal/db/bundb/util.go | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index 39849ba73..ac58dd6f4 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -25,6 +25,8 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/cache" "code.superseriousbusiness.org/gotosocial/internal/db" + "code.superseriousbusiness.org/gotosocial/internal/gtserror" + "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/log" "code.superseriousbusiness.org/gotosocial/internal/paging" "github.com/uptrace/bun" @@ -151,6 +153,7 @@ func notExists(ctx context.Context, query *bun.SelectQuery) (bool, error) { // 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 { @@ -171,6 +174,30 @@ func loadPagedIDs(cache *cache.SliceCache[string], key string, page *paging.Page return ids, nil } +// incrementAccountStats will increment the given column in the `account_stats` table matching `account_id`. +func incrementAccountStats(ctx context.Context, tx bun.Tx, col bun.Ident, accountID string) error { + if _, err := tx.NewUpdate(). + Model((*gtsmodel.AccountStats)(nil)). + Where("? = ?", bun.Ident("account_id"), accountID). + Set("? = (? + 1)", col, col). + Exec(ctx); err != nil { + return gtserror.Newf("error updating %s: %w", col, err) + } + return nil +} + +// decrementAccountStats will decrement the given column in the `account_stats` table matching `account_id`. +func decrementAccountStats(ctx context.Context, tx bun.Tx, col bun.Ident, accountID string) error { + if _, err := tx.NewUpdate(). + Model((*gtsmodel.AccountStats)(nil)). + Where("? = ?", bun.Ident("account_id"), accountID). + Set("? = (? - 1)", col, col). + Exec(ctx); err != nil { + return gtserror.Newf("error updating %s: %w", col, err) + } + return 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 { |
