annotate src/lib-storage/index/index-sort.c @ 6968:09e1e8d4aa53 HEAD

Simplified and optimized the sorting code.
author Timo Sirainen <tss@iki.fi>
date Sat, 08 Dec 2007 21:36:20 +0200
parents 414c9d631a81
children c13473a5c0e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6429
65c69a53a7be Replaced my Copyright notices. The year range always ends with 2007 now.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
1 /* Copyright (c) 2006-2007 Dovecot authors, see the included COPYING file */
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
2
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
3 /* The idea in here is that for each used primary sort condition there's
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
4 a 32bit integer in the index file which specifies the sort order. So when
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
5 sorting we simply look up the sort IDs and sort the messages by them.
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
6
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
7 Sort IDs are allocated in two ways:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
8
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
9 1) Time and size fields can be directly used as sort IDs, so we simply
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
10 use them directly as the missing sort IDs.
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
11
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
12 2) Strings can't be used as sort IDs directly. The way they're currently
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
13 handled is that the whole 32bit integer space is used for them and whenever
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
14 adding a string, the available space is halved and the new ID is added in
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
15 the middle. For example if we add one mail the first time, it gets ID
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
16 2^31. If we then add two mails which are sorted before the first one, they
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
17 get IDs 2^31/3 and 2^31/3*2. Once we run out of the available space between
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
18 IDs, a large amount of the IDs are renumbered.
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
19
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
20 We try to avoid looking at mails' contents as much as possible. For case 1)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
21 IDs it's simple because we need to get only the new mails' sort fields once
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
22 and use them as sort IDs. For case 2) we'll have to start looking at the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
23 strings from older mails as well. To minimize this, we first sort the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
24 existing sort IDs. After that we start inserting the new mails into the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
25 sorted array by looking the position using binary search. This minimizes
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
26 the number of lookups we have to do for the old mails. Usually only a few
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
27 mails have been added, so this should be faster than other sort methods.
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
28 */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
29
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
30 #include "lib.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
31 #include "array.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
32 #include "bsearch-insert-pos.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
33 #include "str.h"
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
34 #include "unichar.h"
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
35 #include "message-address.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
36 #include "imap-base-subject.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
37 #include "index-storage.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
38 #include "index-sort.h"
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
39
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
40 #include <stdlib.h>
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
41
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
42 #define RENUMBER_SPACE 100
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
43
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
44 struct mail_sort_node {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
45 uint32_t seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
46 uint32_t sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
47 };
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
48 ARRAY_DEFINE_TYPE(mail_sort_node, struct mail_sort_node);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
49
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
50 struct mail_search_sort_program {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
51 struct mailbox_transaction_context *t;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
52 enum mail_sort_type sort_program[MAX_SORT_PROGRAM_SIZE];
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
53 const char *primary_sort_header;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
54 struct mail *temp_mail;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
55
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
56 ARRAY_TYPE(mail_sort_node) nodes, all_nodes;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
57 const struct mail_sort_node *nodes_ptr;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
58 unsigned int nodes_count, iter_idx;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
59
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
60 uint32_t ext_id;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
61 unsigned int first_missing_sort_id_idx;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
62
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
63 unsigned int reverse:1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
64 unsigned int sort_ids_added:1;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
65 unsigned int missing_sort_ids:1;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
66 };
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
67
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
68 struct sort_cmp_context {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
69 struct mail_search_sort_program *program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
70 struct mail *mail;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
71 };
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
72
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
73 static struct sort_cmp_context static_node_cmp_context;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
74
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
75 struct mail_search_sort_program *
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
76 index_sort_program_init(struct mailbox_transaction_context *t,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
77 const enum mail_sort_type *sort_program)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
78 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
79 struct index_mailbox *ibox = (struct index_mailbox *)t->box;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
80 struct mail_search_sort_program *program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
81 const char *name;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
82 unsigned int i;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
83
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
84 if (sort_program == NULL || sort_program[0] == MAIL_SORT_END)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
85 return NULL;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
86
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
87 /* we support internal sorting by the primary condition */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
88 program = i_new(struct mail_search_sort_program, 1);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
89 program->t = t;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
90 program->temp_mail = mail_alloc(t, 0, NULL);
4596
bf4e98a0de3f Replaced ARRAY_CREATE() macro with [ipt]_array_init() macros. The macro
Timo Sirainen <tss@iki.fi>
parents: 4594
diff changeset
91 i_array_init(&program->nodes, 64);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
92
6549
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
93 /* primary reversion isn't stored to sort_program. we handle it by
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
94 iterating backwards at the end. */
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
95 program->reverse = (sort_program[0] & MAIL_SORT_FLAG_REVERSE) != 0;
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
96 program->sort_program[0] = sort_program[0] & ~MAIL_SORT_FLAG_REVERSE;
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
97 for (i = 1; i < MAX_SORT_PROGRAM_SIZE; i++) {
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
98 program->sort_program[i] = sort_program[i];
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
99 if (sort_program[i] == MAIL_SORT_END)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
100 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
101 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
102 if (i == MAX_SORT_PROGRAM_SIZE)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
103 i_panic("index_sort_program_init(): Invalid sort program");
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
104
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
105 switch (program->sort_program[0] & MAIL_SORT_MASK) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
106 case MAIL_SORT_ARRIVAL:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
107 name = "rdate";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
108 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
109 case MAIL_SORT_CC:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
110 name = "sort-c";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
111 program->primary_sort_header = "Cc";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
112 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
113 case MAIL_SORT_DATE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
114 name = "date";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
115 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
116 case MAIL_SORT_FROM:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
117 name = "sort-f";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
118 program->primary_sort_header = "From";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
119 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
120 case MAIL_SORT_SIZE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
121 name = "size";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
122 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
123 case MAIL_SORT_SUBJECT:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
124 name = "sort-s";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
125 program->primary_sort_header = "Subject";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
126 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
127 case MAIL_SORT_TO:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
128 name = "sort-t";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
129 program->primary_sort_header = "To";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
130 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
131 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
132 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
133 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
134 program->ext_id =
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
135 mail_index_ext_register(ibox->index, name, 0,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
136 sizeof(uint32_t), sizeof(uint32_t));
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
137 return program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
138 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
139
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
140 void index_sort_program_deinit(struct mail_search_sort_program **_program)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
141 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
142 struct mail_search_sort_program *program = *_program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
143
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
144 *_program = NULL;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
145 mail_free(&program->temp_mail);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
146 array_free(&program->nodes);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
147 i_free(program);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
148 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
149
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
150 static const char *get_first_mailbox(struct mail *mail, const char *header)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
151 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
152 struct message_address *addr;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
153 const char *str;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
154
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
155 if (mail_get_first_header_utf8(mail, header, &str) <= 0)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
156 return "";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
157
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
158 addr = message_address_parse(pool_datastack_create(),
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
159 (const unsigned char *)str,
4635
210bb1ff0e6e Added fill_missing parameter to message_address_parse() which specifies if
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
160 strlen(str), 1, TRUE);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
161 return addr != NULL ? addr->mailbox : "";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
162 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
163
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
164 static void
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
165 sort_header_get(string_t *dest, enum mail_sort_type sort_type,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
166 struct mail *mail, uint32_t seq)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
167 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
168 const char *str;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
169
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
170 mail_set_seq(mail, seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
171 switch (sort_type & MAIL_SORT_MASK) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
172 case MAIL_SORT_SUBJECT:
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
173 if (mail_get_first_header(mail, "Subject", &str) <= 0)
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
174 return;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
175 str = imap_get_base_subject_cased(pool_datastack_create(),
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
176 str, NULL);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
177 str_append(dest, str);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
178 return;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
179 case MAIL_SORT_CC:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
180 str = get_first_mailbox(mail, "Cc");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
181 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
182 case MAIL_SORT_FROM:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
183 str = get_first_mailbox(mail, "From");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
184 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
185 case MAIL_SORT_TO:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
186 str = get_first_mailbox(mail, "To");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
187 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
188 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
189 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
190 }
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
191
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
192 (void)uni_utf8_to_decomposed_titlecase(str, (size_t)-1, dest);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
193 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
194
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
195 static uint32_t sort_get_arrival(struct mail *mail)
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
196 {
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
197 time_t t;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
198
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
199 if (mail_get_received_date(mail, &t) < 0)
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
200 t = 0;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
201
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
202 i_assert(t != (time_t)-1);
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
203 /* FIXME: truncation isn't good.. */
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
204 return t <= 0 ? 1 :
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
205 (t >= (uint32_t)-1 ? (uint32_t)-1 : t + 1);
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
206 }
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
207
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
208 static uint32_t sort_get_date(struct mail *mail)
6546
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
209 {
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
210 time_t t;
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
211
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
212 if (mail_get_date(mail, &t, NULL) < 0)
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
213 t = 0;
6546
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
214 if (t == 0) {
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
215 if (mail_get_received_date(mail, &t) < 0)
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
216 return 1;
6546
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
217 }
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
218 i_assert(t != (time_t)-1);
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
219 /* FIXME: truncation isn't good.. */
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
220 return t <= 0 ? 1 :
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
221 (t >= (uint32_t)-1 ? (uint32_t)-1 : t + 1);
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
222 }
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
223
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
224 static uint32_t sort_get_size(struct mail *mail)
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
225 {
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
226 uoff_t size;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
227
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
228 if (mail_get_virtual_size(mail, &size) < 0)
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
229 return 1;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
230
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
231 /* FIXME: elsewhere we support 64bit message sizes, but here
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
232 we support only 32bit sizes.. It's a bit too much trouble
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
233 to support 64bit here currently, so until such messages
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
234 actually start showing up somewhere, 32bit is enough */
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
235 i_assert(size < (uint32_t)-1);
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
236 return size + 1;
6546
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
237 }
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
238
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
239 static int sort_node_cmp_type(struct sort_cmp_context *ctx,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
240 const enum mail_sort_type *sort_program,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
241 const struct mail_sort_node *n1,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
242 const struct mail_sort_node *n2)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
243 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
244 enum mail_sort_type sort_type;
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
245 uint32_t time1, time2, size1, size2;
4935
f768dd773e6b Compiler warning fix
Timo Sirainen <tss@iki.fi>
parents: 4635
diff changeset
246 int ret = 0;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
247
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
248 sort_type = *sort_program & MAIL_SORT_MASK;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
249 switch (sort_type) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
250 case MAIL_SORT_CC:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
251 case MAIL_SORT_FROM:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
252 case MAIL_SORT_TO:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
253 case MAIL_SORT_SUBJECT:
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
254 T_FRAME_BEGIN {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
255 string_t *str1, *str2;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
256
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
257 str1 = t_str_new(256);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
258 str2 = t_str_new(256);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
259 sort_header_get(str1, sort_type, ctx->mail, n1->seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
260 sort_header_get(str2, sort_type, ctx->mail, n2->seq);
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
261
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
262 ret = strcmp(str_c(str1), str_c(str2));
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
263 } T_FRAME_END;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
264 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
265 case MAIL_SORT_ARRIVAL:
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
266 mail_set_seq(ctx->mail, n1->seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
267 time1 = sort_get_arrival(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
268
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
269 mail_set_seq(ctx->mail, n2->seq);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
270 time2 = sort_get_arrival(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
271
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
272 ret = time1 < time2 ? -1 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
273 (time1 > time2 ? 1 : 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
274 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
275 case MAIL_SORT_DATE:
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
276 mail_set_seq(ctx->mail, n1->seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
277 time1 = sort_get_date(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
278
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
279 mail_set_seq(ctx->mail, n2->seq);
6546
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
280 time2 = sort_get_date(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
281
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
282 ret = time1 < time2 ? -1 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
283 (time1 > time2 ? 1 : 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
284 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
285 case MAIL_SORT_SIZE:
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
286 mail_set_seq(ctx->mail, n1->seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
287 size1 = sort_get_size(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
288
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
289 mail_set_seq(ctx->mail, n2->seq);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
290 size2 = sort_get_size(ctx->mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
291
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
292 ret = size1 < size2 ? -1 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
293 (size1 > size2 ? 1 : 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
294 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
295 case MAIL_SORT_END:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
296 return n1->seq < n2->seq ? -1 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
297 (n1->seq > n2->seq ? 1 : 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
298 case MAIL_SORT_MASK:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
299 case MAIL_SORT_FLAG_REVERSE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
300 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
301 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
302
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
303 if (ret == 0)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
304 return sort_node_cmp_type(ctx, sort_program+1, n1, n2);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
305
6549
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
306 /* primary reversion isn't in sort_program */
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
307 if ((*sort_program & MAIL_SORT_FLAG_REVERSE) != 0)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
308 ret = ret < 0 ? 1 : -1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
309 return ret;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
310 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
311
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
312 static int sort_node_cmp(const void *p1, const void *p2)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
313 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
314 struct sort_cmp_context *ctx = &static_node_cmp_context;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
315 const struct mail_sort_node *n1 = p1, *n2 = p2;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
316
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
317 if (n1->sort_id < n2->sort_id)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
318 return -1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
319 if (n1->sort_id > n2->sort_id)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
320 return 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
321
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
322 return sort_node_cmp_type(ctx, ctx->program->sort_program + 1, n1, n2);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
323 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
324
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
325 static int sort_node_cmp_nozero_sort_id(const void *p1, const void *p2)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
326 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
327 struct sort_cmp_context *ctx = &static_node_cmp_context;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
328 const struct mail_sort_node *n1 = p1, *n2 = p2;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
329 const enum mail_sort_type *sort_program;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
330
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
331 /* Use sort IDs only if both have them */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
332 if (n1->sort_id != 0 && n2->sort_id != 0) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
333 if (n1->sort_id < n2->sort_id)
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
334 return -1;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
335 if (n1->sort_id > n2->sort_id)
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
336 return 1;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
337 sort_program = ctx->program->sort_program + 1;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
338 } else {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
339 sort_program = ctx->program->sort_program;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
340 }
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
341
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
342 return sort_node_cmp_type(ctx, ctx->program->sort_program, n1, n2);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
343 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
344
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
345 static bool
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
346 index_sort_add_ids_range(struct mail_search_sort_program *program,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
347 struct mail *mail, unsigned int idx1,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
348 unsigned int idx2)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
349 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
350 struct mail_sort_node *nodes;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
351 unsigned int i, count;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
352 uint32_t prev_id = 0, last_id = (uint32_t)-1;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
353 string_t *last_str, *prev_str, *str;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
354 unsigned int skip;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
355
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
356 last_str = t_str_new(256);
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
357 nodes = array_get_modifiable(&program->all_nodes, &count);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
358 if (nodes[idx2].sort_id != 0) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
359 i_assert(idx1 != idx2);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
360 last_id = nodes[idx2].sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
361
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
362 sort_header_get(last_str, program->sort_program[0], mail,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
363 nodes[idx2].seq);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
364 idx2--;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
365 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
366
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
367 prev_str = t_str_new(256);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
368 if (nodes[idx1].sort_id != 0) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
369 prev_id = nodes[idx1].sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
370
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
371 sort_header_get(prev_str, program->sort_program[0], mail,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
372 nodes[idx1].seq);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
373 idx1++;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
374 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
375
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
376 str = str_new(default_pool, 256);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
377 for (i = idx1; i <= idx2; i++) {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
378 T_FRAME(
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
379 sort_header_get(str, program->sort_program[0], mail,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
380 nodes[i].seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
381 );
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
382
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
383 if (i == idx2 && str_equals(str, last_str))
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
384 nodes[i].sort_id = last_id;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
385 else if (prev_id != 0 && str_equals(str, prev_str) == 0)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
386 nodes[i].sort_id = prev_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
387 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
388 /* divide the available space so that each message gets
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
389 an equal sized share. leave the same sized space
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
390 also between the first and the last messages */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
391 skip = (last_id - prev_id) / (idx2 - i + 2);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
392 nodes[i].sort_id = prev_id + skip;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
393 if (nodes[i].sort_id == prev_id && prev_id != last_id)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
394 nodes[i].sort_id++;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
395 if (nodes[i].sort_id == last_id) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
396 /* we ran out of ID space. have to renumber
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
397 the IDs. */
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
398 str_free(&str);
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
399 return FALSE;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
400 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
401
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
402 prev_id = nodes[i].sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
403 str_truncate(prev_str, 0);
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
404 str_append_str(prev_str, str);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
405 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
406 }
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
407 str_free(&str);
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
408 return TRUE;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
409 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
410
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
411 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
412 index_sort_renumber_ids(struct mail_search_sort_program *program,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
413 unsigned int idx)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
414 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
415 struct index_transaction_context *t =
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
416 (struct index_transaction_context *)program->t;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
417 struct mail_sort_node *nodes;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
418 unsigned int i, count;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
419 uint32_t sort_id = 0, prev_sort_id, skip;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
420
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
421 nodes = array_get_modifiable(&program->all_nodes, &count);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
422 prev_sort_id = (uint32_t)-1;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
423 for (; idx < count; idx++) {
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
424 sort_id = nodes[idx].sort_id;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
425 if (sort_id == nodes[idx+1].sort_id)
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
426 break;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
427 }
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
428 i_assert(idx != count);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
429
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
430 if (((uint32_t)-1 - sort_id) / (count - idx + 1) < RENUMBER_SPACE) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
431 /* space is running out, lets just renumber everything */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
432 sort_id = 0;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
433 skip = (uint32_t)-1 / (count + 1);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
434 for (i = 0; i < idx; i++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
435 if (sort_id != prev_sort_id)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
436 sort_id += skip;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
437 prev_sort_id = nodes[i].sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
438
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
439 i_assert(sort_id != 0);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
440 nodes[i].sort_id = sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
441 mail_index_update_ext(t->trans, nodes[i].seq,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
442 program->ext_id,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
443 &nodes[i].sort_id, NULL);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
444 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
445 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
446 skip = RENUMBER_SPACE;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
447 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
448
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
449 for (i = idx; i < count && sort_id >= nodes[i].sort_id; i++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
450 if (sort_id != prev_sort_id) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
451 i_assert(sort_id <= (uint32_t)-1 - skip);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
452 sort_id += skip;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
453 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
454 prev_sort_id = nodes[i].sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
455
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
456 i_assert(sort_id != 0);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
457 if (nodes[i].sort_id != 0) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
458 nodes[i].sort_id = sort_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
459 mail_index_update_ext(t->trans, nodes[i].seq,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
460 program->ext_id,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
461 &nodes[i].sort_id, NULL);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
462 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
463 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
464 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
465
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
466 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
467 index_sort_add_ids(struct mail_search_sort_program *program, struct mail *mail)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
468 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
469 const struct mail_sort_node *nodes;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
470 unsigned int i, j, count;
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
471 bool ret;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
472
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
473 nodes = array_get(&program->all_nodes, &count);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
474 for (i = 0; i < count; i++) {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
475 if (nodes[i].sort_id != 0)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
476 continue;
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
477
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
478 for (j = i + 1; j < count; j++) {
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
479 if (nodes[j].sort_id != 0)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
480 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
481 }
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
482 T_FRAME(
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
483 ret = index_sort_add_ids_range(program, mail,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
484 I_MAX(i, 1)-1,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
485 I_MIN(j, count-1));
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
486 );
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
487 if (!ret)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
488 index_sort_renumber_ids(program, i-1);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
489 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
490 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
491
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
492 static void index_sort_preset_sort_ids(struct mail_search_sort_program *program,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
493 uint32_t last_seq)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
494 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
495 struct mail_sort_node node;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
496 uint32_t (*get_sort_id)(struct mail *);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
497
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
498 switch (program->sort_program[0] & MAIL_SORT_MASK) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
499 case MAIL_SORT_ARRIVAL:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
500 get_sort_id = sort_get_arrival;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
501 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
502 case MAIL_SORT_DATE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
503 get_sort_id = sort_get_date;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
504 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
505 case MAIL_SORT_SIZE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
506 get_sort_id = sort_get_size;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
507 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
508 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
509 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
510 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
511
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
512 /* add the missing nodes with their sort_ids */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
513 node.seq = array_count(&program->all_nodes) + 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
514 for (; node.seq <= last_seq; node.seq++) {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
515 mail_set_seq(program->temp_mail, node.seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
516 node.sort_id = get_sort_id(program->temp_mail);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
517
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
518 i_assert(node.sort_id != 0);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
519 array_append(&program->all_nodes, &node, 1);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
520 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
521 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
522
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
523 static void index_sort_headers(struct mail_search_sort_program *program,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
524 uint32_t last_seq)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
525 {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
526 ARRAY_TYPE(mail_sort_node) seq_nodes_arr;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
527 struct mail_sort_node *nodes, node, *seq_nodes;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
528 unsigned int i, count, count2;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
529
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
530 /* insert missing nodes */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
531 node.seq = array_count(&program->all_nodes) + 1;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
532 for (; node.seq <= last_seq; node.seq++)
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
533 array_append(&program->all_nodes, &node, 1);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
534
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
535 /* sort everything. use sort_ids whenever possible */
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
536 nodes = array_get_modifiable(&program->all_nodes, &count);
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
537 i_assert(count == last_seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
538 qsort(nodes, count, sizeof(struct mail_sort_node),
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
539 sort_node_cmp_nozero_sort_id);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
540
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
541 /* we can now build the sort_ids */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
542 index_sort_add_ids(program, static_node_cmp_context.mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
543
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
544 /* @UNSAFE: and finally get the range sorted back by sequence */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
545 i_array_init(&seq_nodes_arr, count);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
546 (void)array_idx_modifiable(&seq_nodes_arr, count-1);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
547 seq_nodes = array_get_modifiable(&seq_nodes_arr, &count2);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
548 i_assert(count2 == count);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
549 for (i = 0; i < count; i++)
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
550 seq_nodes[nodes[i].seq-1] = nodes[i];
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
551
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
552 array_free(&program->all_nodes);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
553 program->all_nodes = seq_nodes_arr;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
554 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
555
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
556 static void index_sort_build(struct mail_search_sort_program *program)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
557 {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
558 struct index_transaction_context *t =
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
559 (struct index_transaction_context *)program->t;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
560 struct mail_sort_node node, *all_nodes, *nodes;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
561 const void *data;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
562 uint32_t last_seq;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
563 unsigned int seq, i, count, count2;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
564
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
565 /* add messages that have sort_ids. they're always at the beginning
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
566 of the mailbox. */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
567 last_seq = mail_index_view_get_messages_count(t->ibox->view);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
568 i_array_init(&program->all_nodes, last_seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
569 for (seq = 1; seq <= last_seq; seq++) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
570 node.seq = seq;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
571
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
572 mail_index_lookup_ext(t->ibox->view, seq, program->ext_id,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
573 &data, NULL);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
574 node.sort_id = data == NULL ? 0 : *(const uint32_t *)data;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
575 if (node.sort_id == 0) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
576 /* the rest don't have sort_ids either */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
577 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
578 }
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
579 array_append(&program->all_nodes, &node, 1);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
580 }
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
581 i_assert(seq <= last_seq);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
582
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
583 /* add the new sort_ids and sort them all */
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
584 switch (program->sort_program[0] & MAIL_SORT_MASK) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
585 case MAIL_SORT_ARRIVAL:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
586 case MAIL_SORT_DATE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
587 case MAIL_SORT_SIZE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
588 index_sort_preset_sort_ids(program, last_seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
589 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
590 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
591 index_sort_headers(program, last_seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
592 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
593 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
594
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
595 /* add the missing sort IDs to index. also update sort_id in
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
596 wanted nodes. */
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
597 all_nodes = array_get_modifiable(&program->all_nodes, &count);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
598 nodes = array_get_modifiable(&program->nodes, &count2);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
599 i = program->first_missing_sort_id_idx;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
600 i_assert(nodes[i].seq <= seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
601 for (; seq <= count; seq++) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
602 i_assert(all_nodes[seq-1].seq == seq);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
603 i_assert(all_nodes[seq-1].sort_id != 0);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
604 if (nodes[i].seq == seq) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
605 nodes[i].sort_id = all_nodes[seq-1].sort_id;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
606 i++;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
607 }
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
608
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
609 mail_index_update_ext(t->trans, seq, program->ext_id,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
610 &all_nodes[seq-1].sort_id, NULL);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
611 }
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
612 array_free(&program->all_nodes);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
613 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
614
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
615 void index_sort_list_add(struct mail_search_sort_program *program,
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
616 struct mail *mail)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
617 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
618 struct index_transaction_context *t =
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
619 (struct index_transaction_context *)program->t;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
620 const void *data;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
621 struct mail_sort_node node;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
622
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
623 i_assert(mail->transaction == program->t);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
624
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
625 mail_index_lookup_ext(t->trans_view, mail->seq,
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
626 program->ext_id, &data, NULL);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
627 node.seq = mail->seq;
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
628 node.sort_id = data == NULL ? 0 : *(const uint32_t *)data;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
629
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
630 if (node.sort_id == 0 && !program->missing_sort_ids) {
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
631 program->missing_sort_ids = TRUE;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
632 program->first_missing_sort_id_idx =
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
633 array_count(&program->nodes);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
634 }
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
635 array_append(&program->nodes, &node, 1);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
636 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
637
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
638 void index_sort_list_finish(struct mail_search_sort_program *program)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
639 {
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
640 struct mail_sort_node *nodes;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
641
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
642 memset(&static_node_cmp_context, 0, sizeof(static_node_cmp_context));
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
643 static_node_cmp_context.program = program;
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
644 static_node_cmp_context.mail = program->temp_mail;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
645
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
646 if (program->missing_sort_ids)
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
647 index_sort_build(program);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
648
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
649 nodes = array_get_modifiable(&program->nodes, &program->nodes_count);
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
650 qsort(nodes, program->nodes_count, sizeof(struct mail_sort_node),
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
651 sort_node_cmp);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
652
6968
09e1e8d4aa53 Simplified and optimized the sorting code.
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
653 program->nodes_ptr = nodes;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
654 if (program->reverse)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
655 program->iter_idx = program->nodes_count;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
656 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
657
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
658 bool index_sort_list_next(struct mail_search_sort_program *program,
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
659 struct mail *mail)
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
660 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
661 const struct mail_sort_node *node;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
662
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
663 if (!program->reverse) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
664 if (program->iter_idx == program->nodes_count)
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
665 return FALSE;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
666
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
667 node = &program->nodes_ptr[program->iter_idx++];
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
668 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
669 if (program->iter_idx == 0)
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
670 return FALSE;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
671
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
672 node = &program->nodes_ptr[--program->iter_idx];
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
673 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
674 mail_set_seq(mail, node->seq);
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
675 return TRUE;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
676 }