Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GODT-2522 #369

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions benchmarks/gluon_bench/imap_benchmarks/select_fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package imap_benchmarks

import (
"context"
"flag"
"github.com/emersion/go-imap"
"net"

"github.com/ProtonMail/gluon/benchmarks/gluon_bench/benchmark"
"github.com/bradenaw/juniper/xslices"
"github.com/emersion/go-imap/client"
)

var (
selectFetchRepetitionsFlag = flag.Uint("imap-select-fetch-repeat", 50, "Number of times to repeat the request.")
)

type SelectFetch struct {
*stateTracker
mboxInfo []MailboxInfo
}

func NewSelectFetch() benchmark.Benchmark {
return NewIMAPBenchmarkRunner(&SelectFetch{stateTracker: newStateTracker()})
}

func (*SelectFetch) Name() string {
return "imap-select-fetch"
}

func (e *SelectFetch) Setup(ctx context.Context, addr net.Addr) error {
return WithClient(addr, func(cl *client.Client) error {
for i := uint(0); i < *selectFetchRepetitionsFlag; i++ {
if _, err := e.createAndFillRandomMBox(cl); err != nil {
return err
}
}

e.mboxInfo = xslices.Map(e.MBoxes, func(m string) MailboxInfo {
return MailboxInfo{Name: m, ReadOnly: false}
})
return nil
})
}

func (e *SelectFetch) TearDown(ctx context.Context, addr net.Addr) error {
return e.cleanupWithAddr(addr)
}

func (e *SelectFetch) Run(ctx context.Context, addr net.Addr) error {
RunParallelClients(addr, func(cl *client.Client, u uint) {
for i := uint(0); i < *selectFetchRepetitionsFlag; i++ {
if _, err := cl.Select(e.mboxInfo[i].Name, e.mboxInfo[i].ReadOnly); err != nil {
panic(err)
}

if err := FetchMessage(cl, NewSequenceSetAll(), imap.FetchUid, imap.FetchFlags); err != nil {
panic(err)
}

if err := cl.Unselect(); err != nil {
panic(err)
}
}
})

return nil
}

func init() {
benchmark.RegisterBenchmark(NewSelectFetch())
}
2 changes: 2 additions & 0 deletions benchmarks/gluon_bench/utils/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dGhpcyBpcyBteSBhdHRhY2htZW50Cg==
const MessageAfterNoonMeeting = `Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
From: Fred Foobar <foobar@Blurdybloop.COM>
Subject: afternoon meeting
Date: Fri, 26 Mar 2021 20:01:23 +0100
To: mooch@owatagu.siam.edu
Message-Id: <B27397-0100000@Blurdybloop.COM>
MIME-Version: 1.0
Expand All @@ -77,6 +78,7 @@ const MessageEmbedded = `From: Nathaniel Borenstein <nsb@bellcore.com>
To: Ned Freed <ned@innosoft.com>
Subject: Sample message
MIME-Version: 1.0
Date: Fri, 26 Mar 2021 20:01:23 +0100
Content-type: multipart/mixed; boundary="simple boundary"

This is the preamble. It is to be ignored, though it
Expand Down
2 changes: 1 addition & 1 deletion db/ops_mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MailboxReadOps interface {

GetMailboxMessageIDPairs(ctx context.Context, mboxID imap.InternalMailboxID) ([]MessageIDPair, error)

GetAllMailboxesWithAttr(ctx context.Context) ([]*Mailbox, error)
GetAllMailboxesWithAttr(ctx context.Context) ([]*MailboxWithAttr, error)

GetAllMailboxesAsRemoteIDs(ctx context.Context) ([]imap.MailboxID, error)

Expand Down
21 changes: 2 additions & 19 deletions db/ops_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/ProtonMail/gluon/imap"
"github.com/bradenaw/juniper/xslices"
)

type MessageReadOps interface {
Expand All @@ -19,7 +18,7 @@ type MessageReadOps interface {

GetMessageRemoteID(ctx context.Context, id imap.InternalMessageID) (imap.MessageID, error)

GetImportedMessageData(ctx context.Context, id imap.InternalMessageID) (*Message, error)
GetImportedMessageData(ctx context.Context, id imap.InternalMessageID) (*MessageWithFlags, error)

GetMessageDateAndSize(ctx context.Context, id imap.InternalMessageID) (time.Time, int, error)

Expand All @@ -39,7 +38,7 @@ type MessageReadOps interface {
type MessageWriteOps interface {
MessageReadOps

CreateMessages(ctx context.Context, reqs ...*CreateMessageReq) ([]*Message, error)
CreateMessages(ctx context.Context, reqs ...*CreateMessageReq) error

CreateMessageAndAddToMailbox(ctx context.Context, mbox imap.InternalMailboxID, req *CreateMessageReq) (imap.UID, imap.FlagSet, error)

Expand Down Expand Up @@ -74,19 +73,3 @@ type MessageFlagSet struct {
RemoteID imap.MessageID
FlagSet imap.FlagSet
}

func NewFlagSet(msgUID *UID, flags []*MessageFlag) imap.FlagSet {
flagSet := imap.NewFlagSetFromSlice(xslices.Map(flags, func(flag *MessageFlag) string {
return flag.Value
}))

if msgUID.Deleted {
flagSet.AddToSelf(imap.FlagDeleted)
}

if msgUID.Recent {
flagSet.AddToSelf(imap.FlagRecent)
}

return flagSet
}
35 changes: 13 additions & 22 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/ProtonMail/gluon/imap"
"github.com/bradenaw/juniper/xslices"
)

type MailboxIDPair struct {
Expand Down Expand Up @@ -86,27 +85,16 @@ type MailboxAttr struct {
}

type Mailbox struct {
ID imap.InternalMailboxID
RemoteID imap.MailboxID
Name string
UIDValidity imap.UID
Subscribed bool
Flags []*MailboxFlag
PermanentFlags []*MailboxFlag
Attributes []*MailboxAttr
ID imap.InternalMailboxID
RemoteID imap.MailboxID
Name string
UIDValidity imap.UID
Subscribed bool
}

type MessageFlag struct {
ID int
Value string
}

func MessageFlagsFromFlagSet(set imap.FlagSet) []*MessageFlag {
return xslices.Map(set.ToSlice(), func(t string) *MessageFlag {
return &MessageFlag{
Value: t,
}
})
type MailboxWithAttr struct {
Mailbox
Attributes imap.FlagSet
}

type Message struct {
Expand All @@ -118,8 +106,11 @@ type Message struct {
BodyStructure string
Envelope string
Deleted bool
Flags []*MessageFlag
UIDs []*UID
}

type MessageWithFlags struct {
Message
Flags imap.FlagSet
}

type UID struct {
Expand Down
6 changes: 2 additions & 4 deletions internal/backend/connector_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (user *user) applyMessagesCreated(ctx context.Context, update *imap.Message
}

// Create message in the database
if _, err := tx.CreateMessages(ctx, xslices.Map(chunk, func(req *DBRequestWithLiteral) *db.CreateMessageReq {
if err := tx.CreateMessages(ctx, xslices.Map(chunk, func(req *DBRequestWithLiteral) *db.CreateMessageReq {
return &req.CreateMessageReq
})...); err != nil {
return err
Expand Down Expand Up @@ -686,10 +686,8 @@ func (user *user) applyMessageUpdated(ctx context.Context, update *imap.MessageU
InternalID: newInternalID,
}

if m, err := tx.CreateMessages(ctx, request); err != nil {
if err := tx.CreateMessages(ctx, request); err != nil {
return err
} else if len(m) == 0 {
return fmt.Errorf("no messages were inserted")
}

if err := user.store.Set(newInternalID, literalReader); err != nil {
Expand Down
Loading