diff options
author | Robin Jarry <robin@jarry.cc> | 2022-12-21 11:31:04 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-01-04 22:57:31 +0100 |
commit | 5677f93ff8e0f212be112a110fcfe09663c84f98 (patch) | |
tree | 9f47f430b312d1f0dc1acdd3735cfd76ad21e0f0 /worker | |
parent | 36fded03e762da97edde61559c8bf60d5749d6a2 (diff) | |
download | aerc-5677f93ff8e0f212be112a110fcfe09663c84f98.tar.gz |
model: change flags array to bitmask
Using a list of integers is not optimal. Use a bit mask instead.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'worker')
-rw-r--r-- | worker/imap/cache.go | 2 | ||||
-rw-r--r-- | worker/imap/flags.go | 3 | ||||
-rw-r--r-- | worker/imap/imap.go | 16 | ||||
-rw-r--r-- | worker/lib/maildir.go | 16 | ||||
-rw-r--r-- | worker/lib/parse.go | 2 | ||||
-rw-r--r-- | worker/lib/parse_test.go | 6 | ||||
-rw-r--r-- | worker/lib/search.go | 49 | ||||
-rw-r--r-- | worker/lib/sort.go | 10 | ||||
-rw-r--r-- | worker/maildir/message.go | 4 | ||||
-rw-r--r-- | worker/maildir/worker.go | 4 | ||||
-rw-r--r-- | worker/mbox/io.go | 2 | ||||
-rw-r--r-- | worker/mbox/models.go | 27 | ||||
-rw-r--r-- | worker/mbox/worker.go | 5 | ||||
-rw-r--r-- | worker/notmuch/message.go | 20 | ||||
-rw-r--r-- | worker/notmuch/worker.go | 5 | ||||
-rw-r--r-- | worker/types/messages.go | 4 | ||||
-rw-r--r-- | worker/types/worker.go | 2 |
17 files changed, 70 insertions, 107 deletions
diff --git a/worker/imap/cache.go b/worker/imap/cache.go index a889f725..b8673ae7 100644 --- a/worker/imap/cache.go +++ b/worker/imap/cache.go @@ -114,7 +114,7 @@ func (w *IMAPWorker) getCachedHeaders(msg *types.FetchMessageHeaders) []uint32 { mi := &models.MessageInfo{ BodyStructure: &ch.BodyStructure, Envelope: &ch.Envelope, - Flags: []models.Flag{models.SeenFlag}, // Always return a SEEN flag + Flags: models.SeenFlag, // Always return a SEEN flag Uid: ch.Uid, RFC822Headers: hdr, } diff --git a/worker/imap/flags.go b/worker/imap/flags.go index 9d9ecc86..180bc261 100644 --- a/worker/imap/flags.go +++ b/worker/imap/flags.go @@ -34,7 +34,6 @@ func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) { flags := []interface{}{imap.AnsweredFlag} if !msg.Answered { item = imap.FormatFlagsOp(imap.RemoveFlags, false) - flags = []interface{}{imap.AnsweredFlag} } imapw.handleStoreOps(msg, msg.Uids, item, flags, func(_msg *imap.Message) error { @@ -50,7 +49,7 @@ func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) { } func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) { - flags := []interface{}{flagToImap[msg.Flag]} + flags := []interface{}{flagToImap[msg.Flags]} item := imap.FormatFlagsOp(imap.AddFlags, false) if !msg.Enable { item = imap.FormatFlagsOp(imap.RemoveFlags, false) diff --git a/worker/imap/imap.go b/worker/imap/imap.go index 6e1341b2..82ee2e2e 100644 --- a/worker/imap/imap.go +++ b/worker/imap/imap.go @@ -79,7 +79,7 @@ func translateAddresses(addrs []*imap.Address) []*mail.Address { return converted } -var imapToFlag = map[string]models.Flag{ +var imapToFlag = map[string]models.Flags{ imap.SeenFlag: models.SeenFlag, imap.RecentFlag: models.RecentFlag, imap.AnsweredFlag: models.AnsweredFlag, @@ -87,7 +87,7 @@ var imapToFlag = map[string]models.Flag{ imap.FlaggedFlag: models.FlaggedFlag, } -var flagToImap = map[models.Flag]string{ +var flagToImap = map[models.Flags]string{ models.SeenFlag: imap.SeenFlag, models.RecentFlag: imap.RecentFlag, models.AnsweredFlag: imap.AnsweredFlag, @@ -95,20 +95,20 @@ var flagToImap = map[models.Flag]string{ models.FlaggedFlag: imap.FlaggedFlag, } -func translateImapFlags(imapFlags []string) []models.Flag { - var flags []models.Flag +func translateImapFlags(imapFlags []string) models.Flags { + var flags models.Flags for _, imapFlag := range imapFlags { if flag, ok := imapToFlag[imapFlag]; ok { - flags = append(flags, flag) + flags |= flag } } return flags } -func translateFlags(flags []models.Flag) []string { +func translateFlags(flags models.Flags) []string { var imapFlags []string - for _, flag := range flags { - if imapFlag, ok := flagToImap[flag]; ok { + for flag, imapFlag := range flagToImap { + if flags.Has(flag) { imapFlags = append(imapFlags, imapFlag) } } diff --git a/worker/lib/maildir.go b/worker/lib/maildir.go index f3fe9415..43d3b820 100644 --- a/worker/lib/maildir.go +++ b/worker/lib/maildir.go @@ -113,7 +113,7 @@ func StripUIDFromMessageFilename(basename string) string { return uidReg.ReplaceAllString(basename, "") } -var MaildirToFlag = map[maildir.Flag]models.Flag{ +var MaildirToFlag = map[maildir.Flag]models.Flags{ maildir.FlagReplied: models.AnsweredFlag, maildir.FlagSeen: models.SeenFlag, maildir.FlagTrashed: models.DeletedFlag, @@ -122,7 +122,7 @@ var MaildirToFlag = map[maildir.Flag]models.Flag{ // maildir.FlagPassed Flag = 'P' } -var FlagToMaildir = map[models.Flag]maildir.Flag{ +var FlagToMaildir = map[models.Flags]maildir.Flag{ models.AnsweredFlag: maildir.FlagReplied, models.SeenFlag: maildir.FlagSeen, models.DeletedFlag: maildir.FlagTrashed, @@ -131,20 +131,20 @@ var FlagToMaildir = map[models.Flag]maildir.Flag{ // maildir.FlagPassed Flag = 'P' } -func FromMaildirFlags(maildirFlags []maildir.Flag) []models.Flag { - var flags []models.Flag +func FromMaildirFlags(maildirFlags []maildir.Flag) models.Flags { + var flags models.Flags for _, maildirFlag := range maildirFlags { if flag, ok := MaildirToFlag[maildirFlag]; ok { - flags = append(flags, flag) + flags |= flag } } return flags } -func ToMaildirFlags(flags []models.Flag) []maildir.Flag { +func ToMaildirFlags(flags models.Flags) []maildir.Flag { var maildirFlags []maildir.Flag - for _, flag := range flags { - if maildirFlag, ok := FlagToMaildir[flag]; ok { + for flag, maildirFlag := range FlagToMaildir { + if flags.Has(flag) { maildirFlags = append(maildirFlags, maildirFlag) } } diff --git a/worker/lib/parse.go b/worker/lib/parse.go index 67955c8f..89e79684 100644 --- a/worker/lib/parse.go +++ b/worker/lib/parse.go @@ -254,7 +254,7 @@ func parseAddressList(h *mail.Header, key string) ([]*mail.Address, error) { // RawMessage is an interface that describes a raw message type RawMessage interface { NewReader() (io.ReadCloser, error) - ModelFlags() ([]models.Flag, error) + ModelFlags() (models.Flags, error) Labels() ([]string, error) UID() uint32 } diff --git a/worker/lib/parse_test.go b/worker/lib/parse_test.go index 227b121e..4bf95431 100644 --- a/worker/lib/parse_test.go +++ b/worker/lib/parse_test.go @@ -72,9 +72,9 @@ func newMockRawMessageFromPath(p string) *mockRawMessage { func (m *mockRawMessage) NewReader() (io.ReadCloser, error) { return os.Open(m.path) } -func (m *mockRawMessage) ModelFlags() ([]models.Flag, error) { return nil, nil } -func (m *mockRawMessage) Labels() ([]string, error) { return nil, nil } -func (m *mockRawMessage) UID() uint32 { return 0 } +func (m *mockRawMessage) ModelFlags() (models.Flags, error) { return 0, nil } +func (m *mockRawMessage) Labels() ([]string, error) { return nil, nil } +func (m *mockRawMessage) UID() uint32 { return 0 } func die(err error) { if err != nil { diff --git a/worker/lib/search.go b/worker/lib/search.go index c09feff6..0a124e3c 100644 --- a/worker/lib/search.go +++ b/worker/lib/search.go @@ -18,8 +18,8 @@ type searchCriteria struct { Body []string Text []string - WithFlags []models.Flag - WithoutFlags []models.Flag + WithFlags models.Flags + WithoutFlags models.Flags startDate, endDate time.Time } @@ -36,13 +36,13 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { for _, opt := range opts { switch opt.Option { case 'r': - criteria.WithFlags = append(criteria.WithFlags, models.SeenFlag) + criteria.WithFlags |= models.SeenFlag case 'u': - criteria.WithoutFlags = append(criteria.WithoutFlags, models.SeenFlag) + criteria.WithoutFlags |= models.SeenFlag case 'x': - criteria.WithFlags = append(criteria.WithFlags, getParsedFlag(opt.Value)) + criteria.WithFlags |= getParsedFlag(opt.Value) case 'X': - criteria.WithoutFlags = append(criteria.WithoutFlags, getParsedFlag(opt.Value)) + criteria.WithoutFlags |= getParsedFlag(opt.Value) case 'H': // TODO case 'f': @@ -80,8 +80,8 @@ func GetSearchCriteria(args []string) (*searchCriteria, error) { return criteria, nil } -func getParsedFlag(name string) models.Flag { - var f models.Flag +func getParsedFlag(name string) models.Flags { + var f models.Flags switch strings.ToLower(name) { case "seen": f = models.SeenFlag @@ -117,7 +117,7 @@ func searchMessage(message RawMessage, criteria *searchCriteria, // setup parts of the message to use in the search // this is so that we try to minimise reading unnecessary parts var ( - flags []models.Flag + flags models.Flags header *models.MessageInfo body string all string @@ -188,18 +188,14 @@ func searchMessage(message RawMessage, criteria *searchCriteria, } } } - if criteria.WithFlags != nil { - for _, searchFlag := range criteria.WithFlags { - if !containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithFlags != 0 { + if !flags.Has(criteria.WithFlags) { + return false, nil } } - if criteria.WithoutFlags != nil { - for _, searchFlag := range criteria.WithoutFlags { - if containsFlag(flags, searchFlag) { - return false, nil - } + if criteria.WithoutFlags != 0 { + if flags.Has(criteria.WithoutFlags) { + return false, nil } } if parts&DATE > 0 { @@ -221,17 +217,6 @@ func searchMessage(message RawMessage, criteria *searchCriteria, return true, nil } -// containsFlag returns true if searchFlag appears in flags -func containsFlag(flags []models.Flag, searchFlag models.Flag) bool { - match := false - for _, flag := range flags { - if searchFlag == flag { - match = true - } - } - return match -} - // containsSmartCase is a smarter version of strings.Contains for searching. // Is case-insensitive unless substr contains an upper case character func containsSmartCase(s string, substr string) bool { @@ -278,10 +263,10 @@ func getRequiredParts(criteria *searchCriteria) MsgParts { if criteria.Text != nil && len(criteria.Text) > 0 { required |= ALL } - if criteria.WithFlags != nil && len(criteria.WithFlags) > 0 { + if criteria.WithFlags != 0 { required |= FLAGS } - if criteria.WithoutFlags != nil && len(criteria.WithoutFlags) > 0 { + if criteria.WithoutFlags != 0 { required |= FLAGS } diff --git a/worker/lib/sort.go b/worker/lib/sort.go index 1a1bb47f..2828999c 100644 --- a/worker/lib/sort.go +++ b/worker/lib/sort.go @@ -90,18 +90,12 @@ func sortAddresses(messageInfos []*models.MessageInfo, criterion *types.SortCrit } func sortFlags(messageInfos []*models.MessageInfo, criterion *types.SortCriterion, - testFlag models.Flag, + testFlag models.Flags, ) { var slice []*boolStore for _, msgInfo := range messageInfos { - flagPresent := false - for _, flag := range msgInfo.Flags { - if flag == testFlag { - flagPresent = true - } - } slice = append(slice, &boolStore{ - Value: flagPresent, + Value: msgInfo.Flags.Has(testFlag), MsgInfo: msgInfo, }) } diff --git a/worker/maildir/message.go b/worker/maildir/message.go index d9433823..a941624a 100644 --- a/worker/maildir/message.go +++ b/worker/maildir/message.go @@ -28,10 +28,10 @@ func (m Message) Flags() ([]maildir.Flag, error) { } // ModelFlags fetches the set of models.flags currently applied to the message. -func (m Message) ModelFlags() ([]models.Flag, error) { +func (m Message) ModelFlags() (models.Flags, error) { flags, err := m.dir.Flags(m.key) if err != nil { - return nil, err + return 0, err } return lib.FromMaildirFlags(flags), nil } diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 8c4d78ab..54069538 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -712,7 +712,7 @@ func (w *Worker) handleFlagMessages(msg *types.FlagMessages) error { w.err(msg, err) continue } - flag := lib.FlagToMaildir[msg.Flag] + flag := lib.FlagToMaildir[msg.Flags] if err := m.SetOneFlag(flag, msg.Enable); err != nil { log.Errorf("could change flag %v to %v on message: %v", flag, msg.Enable, err) w.err(msg, err) @@ -817,7 +817,7 @@ func (w *Worker) msgInfoFromUid(uid uint32) (*models.MessageInfo, error) { return nil, err } if w.c.IsRecent(uid) { - info.Flags = append(info.Flags, models.RecentFlag) + info.Flags |= models.RecentFlag } return info, nil } diff --git a/worker/mbox/io.go b/worker/mbox/io.go index 31a906cc..2fe39813 100644 --- a/worker/mbox/io.go +++ b/worker/mbox/io.go @@ -28,7 +28,7 @@ func Read(r io.Reader) ([]lib.RawMessage, error) { } messages = append(messages, &message{ - uid: uid, flags: []models.Flag{models.SeenFlag}, content: content, + uid: uid, flags: models.SeenFlag, content: content, }) uid++ diff --git a/worker/mbox/models.go b/worker/mbox/models.go index eec73379..3af93a84 100644 --- a/worker/mbox/models.go +++ b/worker/mbox/models.go @@ -148,7 +148,7 @@ func (f *container) newUid() (next uint32) { return } -func (f *container) Append(r io.Reader, flags []models.Flag) error { +func (f *container) Append(r io.Reader, flags models.Flags) error { data, err := io.ReadAll(r) if err != nil { return err @@ -164,7 +164,7 @@ func (f *container) Append(r io.Reader, flags []models.Flag) error { // message implements the lib.RawMessage interface type message struct { uid uint32 - flags []models.Flag + flags models.Flags content []byte } @@ -172,7 +172,7 @@ func (m *message) NewReader() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(m.content)), nil } -func (m *message) ModelFlags() ([]models.Flag, error) { +func (m *message) ModelFlags() (models.Flags, error) { return m.flags, nil } @@ -184,22 +184,11 @@ func (m *message) UID() uint32 { return m.uid } -func (m *message) SetFlag(flag models.Flag, state bool) error { - flagSet := make(map[models.Flag]bool) - flags, err := m.ModelFlags() - if err != nil { - return err - } - for _, f := range flags { - flagSet[f] = true - } - flagSet[flag] = state - newFlags := make([]models.Flag, 0) - for flag, isSet := range flagSet { - if isSet { - newFlags = append(newFlags, flag) - } +func (m *message) SetFlag(flag models.Flags, state bool) error { + if state { + m.flags |= flag + } else { + m.flags &^= flag } - m.flags = newFlags return nil } diff --git a/worker/mbox/worker.go b/worker/mbox/worker.go index fcf1917d..c2cf9ecb 100644 --- a/worker/mbox/worker.go +++ b/worker/mbox/worker.go @@ -254,8 +254,9 @@ func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error { log.Errorf("could not get message: %v", err) continue } - if err := m.(*message).SetFlag(msg.Flag, msg.Enable); err != nil { - log.Errorf("could change flag %v to %t on message: %v", msg.Flag, msg.Enable, err) + if err := m.(*message).SetFlag(msg.Flags, msg.Enable); err != nil { + log.Errorf("could not change flag %v to %t on message: %v", + msg.Flags, msg.Enable, err) continue } info, err := lib.MessageInfo(m) diff --git a/worker/notmuch/message.go b/worker/notmuch/message.go index 0fc85b00..28731d3d 100644 --- a/worker/notmuch/message.go +++ b/worker/notmuch/message.go @@ -58,7 +58,7 @@ func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) { // SetFlag adds or removes a flag from the message. // Notmuch doesn't support all the flags, and for those this errors. -func (m *Message) SetFlag(flag models.Flag, enable bool) error { +func (m *Message) SetFlag(flag models.Flags, enable bool) error { // Translate the flag into a notmuch tag, ignoring no-op flags. var tag string switch flag { @@ -129,28 +129,22 @@ func (m *Message) Labels() ([]string, error) { return m.Tags() } -func (m *Message) ModelFlags() ([]models.Flag, error) { - var flags []models.Flag - seen := true +func (m *Message) ModelFlags() (models.Flags, error) { + var flags models.Flags = models.SeenFlag tags, err := m.Tags() if err != nil { - return nil, err + return 0, err } for _, tag := range tags { switch tag { case "replied": - flags = append(flags, models.AnsweredFlag) + flags |= models.AnsweredFlag case "flagged": - flags = append(flags, models.FlaggedFlag) + flags |= models.FlaggedFlag case "unread": - seen = false - default: - continue + flags &^= models.SeenFlag } } - if seen { - flags = append(flags, models.SeenFlag) - } return flags, nil } diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index a4f5b5c8..6d8e46d2 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -477,8 +477,9 @@ func (w *worker) handleFlagMessages(msg *types.FlagMessages) error { w.err(msg, err) continue } - if err := m.SetFlag(msg.Flag, msg.Enable); err != nil { - log.Errorf("could not set flag %v as %t for message: %v", msg.Flag, msg.Enable, err) + if err := m.SetFlag(msg.Flags, msg.Enable); err != nil { + log.Errorf("could not set flag %v as %t for message: %v", + msg.Flags, msg.Enable, err) w.err(msg, err) continue } diff --git a/worker/types/messages.go b/worker/types/messages.go index 26f29a24..7e3f8202 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -160,7 +160,7 @@ type DeleteMessages struct { type FlagMessages struct { Message Enable bool - Flag models.Flag + Flags models.Flags Uids []uint32 } @@ -185,7 +185,7 @@ type MoveMessages struct { type AppendMessage struct { Message Destination string - Flags []models.Flag + Flags models.Flags Date time.Time Reader io.Reader Length int diff --git a/worker/types/worker.go b/worker/types/worker.go index 97712afd..bd3553bc 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -170,7 +170,7 @@ func (worker *Worker) PostMessageInfoError(msg WorkerMessage, uid uint32, err er worker.PostMessage(&MessageInfo{ Info: &models.MessageInfo{ Envelope: &models.Envelope{}, - Flags: []models.Flag{models.SeenFlag}, + Flags: models.SeenFlag, Uid: uid, Error: err, }, |