[performance] replace account emojis relational query with separate calls to emojiDB to rely on cache (#1074)

Signed-off-by: kim <grufwub@gmail.com>

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2022-11-18 17:29:25 +00:00 committed by GitHub
parent dccc2eee81
commit 45ae719bd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -36,6 +36,7 @@ import (
type accountDB struct { type accountDB struct {
conn *DBConn conn *DBConn
cache *result.Cache[*gtsmodel.Account] cache *result.Cache[*gtsmodel.Account]
emojis *emojiDB
status *statusDB status *statusDB
} }
@ -63,8 +64,7 @@ func (a *accountDB) newAccountQ(account *gtsmodel.Account) *bun.SelectQuery {
NewSelect(). NewSelect().
Model(account). Model(account).
Relation("AvatarMediaAttachment"). Relation("AvatarMediaAttachment").
Relation("HeaderMediaAttachment"). Relation("HeaderMediaAttachment")
Relation("Emojis")
} }
func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Account, db.Error) { func (a *accountDB) GetAccountByID(ctx context.Context, id string) (*gtsmodel.Account, db.Error) {
@ -149,7 +149,8 @@ func (a *accountDB) GetInstanceAccount(ctx context.Context, domain string) (*gts
} }
func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Account) error, keyParts ...any) (*gtsmodel.Account, db.Error) { func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func(*gtsmodel.Account) error, keyParts ...any) (*gtsmodel.Account, db.Error) {
return a.cache.Load(lookup, func() (*gtsmodel.Account, error) { // Fetch account from database cache with loader callback
account, err := a.cache.Load(lookup, func() (*gtsmodel.Account, error) {
var account gtsmodel.Account var account gtsmodel.Account
// Not cached! Perform database query // Not cached! Perform database query
@ -159,6 +160,19 @@ func (a *accountDB) getAccount(ctx context.Context, lookup string, dbQuery func(
return &account, nil return &account, nil
}, keyParts...) }, keyParts...)
if err != nil {
return nil, err
}
if len(account.EmojiIDs) > 0 {
// Set the account's related emojis
account.Emojis, err = a.emojis.emojisFromIDs(ctx, account.EmojiIDs)
if err != nil {
return nil, err
}
}
return account, nil
} }
func (a *accountDB) PutAccount(ctx context.Context, account *gtsmodel.Account) db.Error { func (a *accountDB) PutAccount(ctx context.Context, account *gtsmodel.Account) db.Error {

View file

@ -171,6 +171,7 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
user := &userDB{conn: conn} user := &userDB{conn: conn}
// Setup DB cross-referencing // Setup DB cross-referencing
account.emojis = emoji
account.status = status account.status = status
admin.users = user admin.users = user
status.accounts = account status.accounts = account