mirror of
1
Fork 0

[chore/performance] Further reduce nil uncached queries (#3267)

* [chore/performance] Further reduce nil uncached queries

* more checks
This commit is contained in:
tobi 2024-09-02 18:15:12 +02:00 committed by GitHub
parent 0560c5ce89
commit 7b7659f1fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 62 additions and 13 deletions

View File

@ -313,7 +313,14 @@ func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context
return gtserror.Newf("error deleting conversations for account %s: %w", accountID, err) return gtserror.Newf("error deleting conversations for account %s: %w", accountID, err)
} }
// Delete any conversation-to-status links matching the deleted conversation IDs. if len(deletedConversationIDs) == 0 {
// Nothing
// to delete.
return nil
}
// Delete any conversation-to-status links
// matching the deleted conversation IDs.
if _, err := tx.NewDelete(). if _, err := tx.NewDelete().
Model((*gtsmodel.ConversationToStatus)(nil)). Model((*gtsmodel.ConversationToStatus)(nil)).
Where("? IN (?)", bun.Ident("conversation_id"), bun.In(deletedConversationIDs)). Where("? IN (?)", bun.Ident("conversation_id"), bun.In(deletedConversationIDs)).

View File

@ -112,20 +112,25 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
// Get each filter keyword by ID from the cache or DB. // Get each filter keyword by ID from the cache or DB.
filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID", filterKeywords, err := f.state.Caches.DB.FilterKeyword.LoadIDs("ID",
filterKeywordIDs, filterKeywordIDs,
func(uncachedFilterKeywordIDs []string) ([]*gtsmodel.FilterKeyword, error) { func(uncached []string) ([]*gtsmodel.FilterKeyword, error) {
uncachedFilterKeywords := make([]*gtsmodel.FilterKeyword, 0, len(uncachedFilterKeywordIDs)) // Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
// Scan from DB. filterKeywords := make([]*gtsmodel.FilterKeyword, 0, count)
if err := f.db. if err := f.db.
NewSelect(). NewSelect().
Model(&uncachedFilterKeywords). Model(&filterKeywords).
Where("? IN (?)", bun.Ident("id"), bun.In(uncachedFilterKeywordIDs)). Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
Scan(ctx); err != nil { Scan(ctx); err != nil {
return nil, err return nil, err
} }
// Compile all the keyword regular expressions. // Compile all the keyword regular expressions.
uncachedFilterKeywords = slices.DeleteFunc(uncachedFilterKeywords, func(filterKeyword *gtsmodel.FilterKeyword) bool { filterKeywords = slices.DeleteFunc(filterKeywords, func(filterKeyword *gtsmodel.FilterKeyword) bool {
if err := filterKeyword.Compile(); err != nil { if err := filterKeyword.Compile(); err != nil {
log.Errorf(ctx, "error compiling filter keyword regex: %v", err) log.Errorf(ctx, "error compiling filter keyword regex: %v", err)
return true return true
@ -133,7 +138,7 @@ func (f *filterDB) getFilterKeywords(ctx context.Context, idColumn string, id st
return false return false
}) })
return uncachedFilterKeywords, nil return filterKeywords, nil
}, },
) )
if err != nil { if err != nil {

View File

@ -99,16 +99,23 @@ func (f *filterDB) getFilterStatuses(ctx context.Context, idColumn string, id st
// Get each filter status by ID from the cache or DB. // Get each filter status by ID from the cache or DB.
filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID", filterStatuses, err := f.state.Caches.DB.FilterStatus.LoadIDs("ID",
filterStatusIDs, filterStatusIDs,
func(uncachedFilterStatusIDs []string) ([]*gtsmodel.FilterStatus, error) { func(uncached []string) ([]*gtsmodel.FilterStatus, error) {
uncachedFilterStatuses := make([]*gtsmodel.FilterStatus, 0, len(uncachedFilterStatusIDs)) // Avoid querying
// if none uncached.
count := len(uncached)
if count == 0 {
return nil, nil
}
filterStatuses := make([]*gtsmodel.FilterStatus, 0, count)
if err := f.db. if err := f.db.
NewSelect(). NewSelect().
Model(&uncachedFilterStatuses). Model(&filterStatuses).
Where("? IN (?)", bun.Ident("id"), bun.In(uncachedFilterStatusIDs)). Where("? IN (?)", bun.Ident("id"), bun.In(uncached)).
Scan(ctx); err != nil { Scan(ctx); err != nil {
return nil, err return nil, err
} }
return uncachedFilterStatuses, nil return filterStatuses, nil
}, },
) )
if err != nil { if err != nil {

View File

@ -286,6 +286,12 @@ func (r *relationshipDB) DeleteAccountBlocks(ctx context.Context, accountID stri
return err return err
} }
if len(blockIDs) == 0 {
// Nothing
// to delete.
return nil
}
defer func() { defer func() {
// Invalidate all account's incoming / outoing blocks on return. // Invalidate all account's incoming / outoing blocks on return.
r.state.Caches.DB.Block.Invalidate("AccountID", accountID) r.state.Caches.DB.Block.Invalidate("AccountID", accountID)

View File

@ -351,6 +351,12 @@ func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID str
return err return err
} }
if len(followIDs) == 0 {
// Nothing
// to delete.
return nil
}
defer func() { defer func() {
// Invalidate all account's incoming / outoing follows on return. // Invalidate all account's incoming / outoing follows on return.
r.state.Caches.DB.Follow.Invalidate("AccountID", accountID) r.state.Caches.DB.Follow.Invalidate("AccountID", accountID)

View File

@ -387,6 +387,12 @@ func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accoun
return err return err
} }
if len(followReqIDs) == 0 {
// Nothing
// to delete.
return nil
}
defer func() { defer func() {
// Invalidate all account's incoming / outoing follow requests on return. // Invalidate all account's incoming / outoing follow requests on return.
r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID) r.state.Caches.DB.FollowRequest.Invalidate("AccountID", accountID)

View File

@ -249,6 +249,12 @@ func (r *relationshipDB) DeleteAccountMutes(ctx context.Context, accountID strin
return err return err
} }
if len(muteIDs) == 0 {
// Nothing
// to delete.
return nil
}
defer func() { defer func() {
// Invalidate all account's incoming / outoing mutes on return. // Invalidate all account's incoming / outoing mutes on return.
r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID) r.state.Caches.DB.UserMute.Invalidate("AccountID", accountID)

View File

@ -351,6 +351,12 @@ func (t *timelineDB) GetListTimeline(
return nil, fmt.Errorf("error getting entries for list %s: %w", listID, err) return nil, fmt.Errorf("error getting entries for list %s: %w", listID, err)
} }
// If there's no list entries we can't
// possibly return anything for this list.
if len(listEntries) == 0 {
return make([]*gtsmodel.Status, 0), nil
}
// Extract just the IDs of each follow. // Extract just the IDs of each follow.
followIDs := make([]string, 0, len(listEntries)) followIDs := make([]string, 0, len(listEntries))
for _, listEntry := range listEntries { for _, listEntry := range listEntries {