mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-01-24 17:46:51 +01:00
[performance] replace status query relationals with separate calls in order to rely on caches more (#1073)
Signed-off-by: kim <grufwub@gmail.com> Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
parent
d98a48b446
commit
dccc2eee81
2 changed files with 50 additions and 10 deletions
|
@ -174,6 +174,8 @@ func NewBunDBService(ctx context.Context) (db.DB, error) {
|
||||||
account.status = status
|
account.status = status
|
||||||
admin.users = user
|
admin.users = user
|
||||||
status.accounts = account
|
status.accounts = account
|
||||||
|
status.emojis = emoji
|
||||||
|
status.mentions = mention
|
||||||
timeline.status = status
|
timeline.status = status
|
||||||
|
|
||||||
// Initialize db structs
|
// Initialize db structs
|
||||||
|
|
|
@ -36,6 +36,8 @@ type statusDB struct {
|
||||||
conn *DBConn
|
conn *DBConn
|
||||||
cache *result.Cache[*gtsmodel.Status]
|
cache *result.Cache[*gtsmodel.Status]
|
||||||
accounts *accountDB
|
accounts *accountDB
|
||||||
|
emojis *emojiDB
|
||||||
|
mentions *mentionDB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusDB) init() {
|
func (s *statusDB) init() {
|
||||||
|
@ -61,11 +63,6 @@ func (s *statusDB) newStatusQ(status interface{}) *bun.SelectQuery {
|
||||||
Model(status).
|
Model(status).
|
||||||
Relation("Attachments").
|
Relation("Attachments").
|
||||||
Relation("Tags").
|
Relation("Tags").
|
||||||
Relation("Mentions").
|
|
||||||
Relation("Emojis").
|
|
||||||
Relation("Account").
|
|
||||||
Relation("InReplyToAccount").
|
|
||||||
Relation("BoostOfAccount").
|
|
||||||
Relation("CreatedWithApplication")
|
Relation("CreatedWithApplication")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,10 +118,21 @@ func (s *statusDB) getStatus(ctx context.Context, lookup string, dbQuery func(*g
|
||||||
return nil, s.conn.ProcessError(err)
|
return nil, s.conn.ProcessError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is boosted, fetch from DB also
|
if status.InReplyToID != "" {
|
||||||
|
// Also load in-reply-to status
|
||||||
|
status.InReplyTo = >smodel.Status{}
|
||||||
|
err := s.conn.NewSelect().Model(status.InReplyTo).
|
||||||
|
Where("? = ?", bun.Ident("status.id"), status.InReplyToID).
|
||||||
|
Scan(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, s.conn.ProcessError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if status.BoostOfID != "" {
|
if status.BoostOfID != "" {
|
||||||
|
// Also load original boosted status
|
||||||
status.BoostOf = >smodel.Status{}
|
status.BoostOf = >smodel.Status{}
|
||||||
err := s.newStatusQ(status.BoostOf).
|
err := s.conn.NewSelect().Model(status.BoostOf).
|
||||||
Where("? = ?", bun.Ident("status.id"), status.BoostOfID).
|
Where("? = ?", bun.Ident("status.id"), status.BoostOfID).
|
||||||
Scan(ctx)
|
Scan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -140,13 +148,43 @@ func (s *statusDB) getStatus(ctx context.Context, lookup string, dbQuery func(*g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the status author account
|
// Set the status author account
|
||||||
author, err := s.accounts.GetAccountByID(ctx, status.AccountID)
|
status.Account, err = s.accounts.GetAccountByID(ctx, status.AccountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the prepared status
|
if id := status.BoostOfAccountID; id != "" {
|
||||||
status.Account = author
|
// Set boost of status' author account
|
||||||
|
status.BoostOfAccount, err = s.accounts.GetAccountByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if id := status.InReplyToAccountID; id != "" {
|
||||||
|
// Set in-reply-to status' author account
|
||||||
|
status.InReplyToAccount, err = s.accounts.GetAccountByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(status.EmojiIDs) > 0 {
|
||||||
|
// Fetch status emojis
|
||||||
|
status.Emojis, err = s.emojis.emojisFromIDs(ctx, status.EmojiIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(status.MentionIDs) > 0 {
|
||||||
|
// Fetch status mentions
|
||||||
|
status.Mentions, err = s.mentions.GetMentions(ctx, status.MentionIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue