[performance] update GetAccountsByIDs() to use the new multi cache loader endpoint (#2828)
* update GetAccountsByIDs() to use the new multi cache loader endpoint * fix broken import
This commit is contained in:
parent
a5c6dc9716
commit
1439042104
|
@ -20,6 +20,7 @@ package bundb
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -56,23 +57,49 @@ func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Ac
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.Account, error) {
|
func (a *accountDB) GetAccountsByIDs(ctx context.Context, ids []string) ([]*gtsmodel.Account, error) {
|
||||||
accounts := make([]*gtsmodel.Account, 0, len(ids))
|
// Load all input account IDs via cache loader callback.
|
||||||
|
accounts, err := a.state.Caches.GTS.Account.LoadIDs("ID",
|
||||||
|
ids,
|
||||||
|
func(uncached []string) ([]*gtsmodel.Account, error) {
|
||||||
|
// Preallocate expected length of uncached accounts.
|
||||||
|
accounts := make([]*gtsmodel.Account, 0, len(uncached))
|
||||||
|
|
||||||
for _, id := range ids {
|
// Perform database query scanning
|
||||||
// Attempt to fetch account from DB.
|
// the remaining (uncached) account IDs.
|
||||||
account, err := a.GetAccountByID(
|
if err := a.db.NewSelect().
|
||||||
gtscontext.SetBarebones(ctx),
|
Model(&accounts).
|
||||||
id,
|
Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
|
||||||
)
|
Scan(ctx); err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
log.Errorf(ctx, "error getting account %q: %v", id, err)
|
}
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append account to return slice.
|
return accounts, nil
|
||||||
accounts = append(accounts, account)
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reorder the statuses by their
|
||||||
|
// IDs to ensure in correct order.
|
||||||
|
getID := func(a *gtsmodel.Account) string { return a.ID }
|
||||||
|
util.OrderBy(accounts, ids, getID)
|
||||||
|
|
||||||
|
if gtscontext.Barebones(ctx) {
|
||||||
|
// no need to fully populate.
|
||||||
|
return accounts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate all loaded accounts, removing those we fail to
|
||||||
|
// populate (removes needing so many nil checks everywhere).
|
||||||
|
accounts = slices.DeleteFunc(accounts, func(account *gtsmodel.Account) bool {
|
||||||
|
if err := a.PopulateAccount(ctx, account); err != nil {
|
||||||
|
log.Errorf(ctx, "error populating account %s: %v", account.ID, err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
return accounts, nil
|
return accounts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue