From 9ba1c3408c34d1b9da343b00ea963d23138d5037 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 30 Aug 2023 16:17:59 -0500 Subject: notmuch: internally sort threads newest first For proper thread ordering, notmuch needs to use a SORT_NEWEST_FIRST sort order in it's query, and then we reverse the thread order. Also give a maximum guess for size of slice we'll be returning. Signed-off-by: Tim Culverhouse Acked-by: Robin Jarry --- worker/notmuch/lib/database.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'worker/notmuch') diff --git a/worker/notmuch/lib/database.go b/worker/notmuch/lib/database.go index 3b9de011..9a6689c4 100644 --- a/worker/notmuch/lib/database.go +++ b/worker/notmuch/lib/database.go @@ -117,12 +117,18 @@ func (db *DB) ThreadsFromQuery(ctx context.Context, q string) ([]*types.Thread, return nil, err } defer query.Close() + // To get proper ordering of threads, we always sort newest first + query.Sort(notmuch.SORT_NEWEST_FIRST) threads, err := query.Threads() if err != nil { return nil, err } + n, err := query.CountMessages() + if err != nil { + return nil, err + } defer threads.Close() - var res []*types.Thread + res := make([]*types.Thread, 0, n) for threads.Next() { select { case <-ctx.Done(): @@ -136,6 +142,10 @@ func (db *DB) ThreadsFromQuery(ctx context.Context, q string) ([]*types.Thread, thread.Close() } } + // Reverse the slice + for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 { + res[i], res[j] = res[j], res[i] + } return res, err } -- cgit