Skip to content

Commit

Permalink
fix(GODT-1570): Reduce amount of data loaded from SQL
Browse files Browse the repository at this point in the history
Update SQL queries to only load the data the require to complete the
query rather than the full object.
  • Loading branch information
LBeernaertProton committed Jul 5, 2022
1 parent bc89d58 commit 2c4b5b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion internal/backend/mailbox_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func txBumpMailboxUIDNext(ctx context.Context, tx *ent.Tx, mbox *ent.Mailbox, wi
}

func txGetMailboxName(ctx context.Context, tx *ent.Tx, mboxID string) (string, error) {
mailbox, err := tx.Mailbox.Query().Where(mailbox.MailboxID(mboxID)).Only(ctx)
mailbox, err := tx.Mailbox.Query().Where(mailbox.MailboxID(mboxID)).Select(mailbox.FieldName).Only(ctx)
if err != nil {
return "", err
}
Expand All @@ -102,6 +102,7 @@ func txGetMailboxName(ctx context.Context, tx *ent.Tx, mboxID string) (string, e
func txGetMailboxMessageIDs(ctx context.Context, tx *ent.Tx, mailboxID string) ([]string, error) {
messages, err := tx.Message.Query().
Where(message.HasUIDsWith(uid.HasMailboxWith(mailbox.MailboxID(mailboxID)))).
Select(message.FieldMessageID).
All(ctx)
if err != nil {
return nil, err
Expand Down
22 changes: 16 additions & 6 deletions internal/backend/message_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package backend
import (
"context"

"github.com/ProtonMail/gluon/internal/backend/ent/messageflag"

"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/gluon/internal/backend/ent"
"github.com/ProtonMail/gluon/internal/backend/ent/mailbox"
Expand Down Expand Up @@ -128,7 +130,7 @@ func txGetMessages(ctx context.Context, tx *ent.Tx, messageIDs ...string) (map[s
}

func txGetMessageID(ctx context.Context, tx *ent.Tx, internalID string) (string, error) {
message, err := tx.Message.Query().Where(message.InternalID(internalID)).Only(ctx)
message, err := tx.Message.Query().Where(message.InternalID(internalID)).Select(message.FieldID).Only(ctx)
if err != nil {
return "", err
}
Expand All @@ -139,7 +141,9 @@ func txGetMessageID(ctx context.Context, tx *ent.Tx, internalID string) (string,
func txGetMessageMailboxIDs(ctx context.Context, tx *ent.Tx, messageID string) ([]string, error) {
messageUIDs, err := tx.UID.Query().
Where(uid.HasMessageWith(message.MessageID(messageID))).
WithMailbox().
WithMailbox(func(query *ent.MailboxQuery) {
query.Select(mailbox.FieldMailboxID)
}).
All(ctx)
if err != nil {
return nil, err
Expand All @@ -156,7 +160,9 @@ func txGetMessageUIDs(ctx context.Context, tx *ent.Tx, mboxID string, messageIDs
uid.HasMailboxWith(mailbox.MailboxID(mboxID)),
uid.HasMessageWith(message.MessageIDIn(messageIDs...)),
).
WithMessage().
WithMessage(func(query *ent.MessageQuery) {
query.Select(message.FieldMessageID)
}).
All(ctx)
if err != nil {
return nil, err
Expand All @@ -176,7 +182,9 @@ func txGetMessageUIDs(ctx context.Context, tx *ent.Tx, mboxID string, messageIDs
func txGetMessageFlags(ctx context.Context, tx *ent.Tx, messageIDs []string) (map[string]imap.FlagSet, error) {
messages, err := tx.Message.Query().
Where(message.MessageIDIn(messageIDs...)).
WithFlags().
WithFlags(func(query *ent.MessageFlagQuery) {
query.Select(messageflag.FieldValue)
}).
All(ctx)
if err != nil {
return nil, err
Expand All @@ -199,7 +207,9 @@ func txGetMessageDeleted(ctx context.Context, tx *ent.Tx, mboxID string, message
uid.HasMailboxWith(mailbox.MailboxID(mboxID)),
uid.HasMessageWith(message.MessageIDIn(messageIDs...)),
).
WithMessage().
WithMessage(func(query *ent.MessageQuery) {
query.Select(message.FieldMessageID)
}).
All(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -377,7 +387,7 @@ func txDeleteMessages(ctx context.Context, tx *ent.Tx, messageIDs ...string) err
}

func txGetMessageIDsMarkedDeleted(ctx context.Context, tx *ent.Tx) ([]string, error) {
messages, err := tx.Message.Query().Where(message.Deleted(true)).All(ctx)
messages, err := tx.Message.Query().Where(message.Deleted(true)).Select(message.FieldMessageID).All(ctx)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/backend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func newSnapshot(ctx context.Context, state *State, mbox *ent.Mailbox) (*snapsho

for offset := 0; ; offset += limit {
list, err := mbox.QueryUIDs().
WithMessage(func(query *ent.MessageQuery) { query.WithFlags() }).Offset(offset).Limit(limit).
WithMessage(func(query *ent.MessageQuery) { query.WithFlags().Select(message.FieldMessageID) }).Offset(offset).Limit(limit).
Select(uid.FieldUID, uid.FieldRecent, uid.FieldDeleted).
All(ctx)

if err != nil {
Expand Down

0 comments on commit 2c4b5b4

Please sign in to comment.