annotate src/lib-storage/index/index-sort.c @ 6940:414c9d631a81 HEAD

Replaced t_push/t_pop calls with T_FRAME*() macros.
author Timo Sirainen <tss@iki.fi>
date Wed, 05 Dec 2007 17:47:44 +0200
parents 7e969a570307
children 09e1e8d4aa53
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
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
56 ARRAY_TYPE(mail_sort_node) 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
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
60 ARRAY_TYPE(mail_sort_node) 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
61
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
62 uint32_t ext_id;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
63 uint32_t prev_seq, last_sorted_seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
64
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
65 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
66 unsigned int skipped_mails:1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
67 unsigned int sort_ids_added:1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
68 };
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
69
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
70 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
71 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
72 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
73
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
74 uint32_t cache_seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
75 enum mail_sort_type cache_type;
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
76 uint32_t cache_value;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
77 const char *cache_str;
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
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
80 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
81
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
82 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
83 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
84 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
85 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
86 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
87 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
88 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
89 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
90
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
91 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
92 return NULL;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
93
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
94 /* 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
95 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
96 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
97 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
98 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
99
6549
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
100 /* 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
101 iterating backwards at the end. */
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
102 program->reverse = (sort_program[0] & MAIL_SORT_FLAG_REVERSE) != 0;
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
103 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
104 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
105 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
106 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
107 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
108 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
109 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
110 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
111
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
112 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
113 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
114 name = "rdate";
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_CC:
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-c";
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 = "Cc";
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_DATE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
121 name = "date";
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_FROM:
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-f";
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 = "From";
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_SIZE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
128 name = "size";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
129 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
130 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
131 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
132 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
133 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
134 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
135 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
136 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
137 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
138 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
139 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
140 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
141 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
142 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
143 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
144 return program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
145 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
146
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
147 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
148 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
149 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
150
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
151 *_program = NULL;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
152 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
153 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
154 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
155 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
156
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
157 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
158 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
159 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
160 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
161
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
162 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
163 return "";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
164
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
165 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
166 (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
167 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
168 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
169 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
170
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
171 static const char *
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
172 sort_header_get(enum mail_sort_type sort_type, struct mail *mail, 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
173 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
174 const char *str;
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
175 string_t *buf;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
176
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
177 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
178 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
179 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
180 if (mail_get_first_header(mail, "Subject", &str) <= 0)
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
181 return "";
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
182 return imap_get_base_subject_cased(pool_datastack_create(),
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6277
diff changeset
183 str, NULL);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
184 case MAIL_SORT_CC:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
185 str = get_first_mailbox(mail, "Cc");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
186 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
187 case MAIL_SORT_FROM:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
188 str = get_first_mailbox(mail, "From");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
189 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
190 case MAIL_SORT_TO:
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
191 str = get_first_mailbox(mail, "To");
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
192 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
193 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
194 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
195 }
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
196
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
197 buf = t_str_new(128);
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
198 (void)uni_utf8_to_decomposed_titlecase(str, (size_t)-1, buf);
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
199 return str_c(buf);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
200 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
201
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
202 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
203 {
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
204 time_t t;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
205
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
206 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
207 t = 0;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
208
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
209 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
210 /* FIXME: truncation isn't good.. */
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
211 return t <= 0 ? 1 :
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
212 (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
213 }
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
214
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
215 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
216 {
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
217 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
218
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
219 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
220 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
221 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
222 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
223 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
224 }
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
225 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
226 /* FIXME: truncation isn't good.. */
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
227 return t <= 0 ? 1 :
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
228 (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
229 }
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 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
232 {
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
233 uoff_t size;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
234
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
235 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
236 return 1;
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
237
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
238 /* 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
239 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
240 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
241 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
242 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
243 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
244 }
a53bc41fade4 Nowadays the SORT draft specifies that if Date: header is missing or broken,
Timo Sirainen <tss@iki.fi>
parents: 6532
diff changeset
245
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
246 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
247 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
248 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
249 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
250 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
251 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
252 uint32_t time1, time2, size1, size2;
4935
f768dd773e6b Compiler warning fix
Timo Sirainen <tss@iki.fi>
parents: 4635
diff changeset
253 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
254
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
255 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
256 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
257 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
258 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
259 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
260 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
261 T_FRAME_BEGIN {
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
262 const char *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
263
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
264 str1 = n1->seq == ctx->cache_seq &&
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
265 ctx->cache_type == sort_type ? ctx->cache_str :
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
266 sort_header_get(sort_type, ctx->mail, n1->seq);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
267 str2 = sort_header_get(sort_type, ctx->mail, n2->seq);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
268
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
269 ret = strcmp(str1, str2);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
270 } 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
271 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
272 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
273 if (n1->seq == ctx->cache_seq && ctx->cache_type == sort_type)
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
274 time1 = ctx->cache_value;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
275 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
276 mail_set_seq(ctx->mail, n1->seq);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
277 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
278 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
279
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
280 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
281 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
282
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
283 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
284 (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
285 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
286 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
287 if (n1->seq == ctx->cache_seq && ctx->cache_type == sort_type)
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
288 time1 = ctx->cache_value;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
289 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
290 mail_set_seq(ctx->mail, n1->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
291 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
292 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
293
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
294 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
295 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
296
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
297 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
298 (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
299 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
300 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
301 if (n1->seq == ctx->cache_seq && ctx->cache_type == sort_type)
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
302 size1 = ctx->cache_value;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
303 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
304 mail_set_seq(ctx->mail, n1->seq);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
305 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
306 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
307
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
308 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
309 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
310
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
311 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
312 (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
313 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
314 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
315 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
316 (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
317 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
318 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
319 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
320 }
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 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
323 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
324
6549
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
325 /* 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
326 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
327 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
328 return ret;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
329 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
330
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
331 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
332 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
333 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
334 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
335
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
336 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
337 return -1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
338 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
339 return 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
340
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
341 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
342 }
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 static int sort_node_cmp_no_sort_id(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
345 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
346 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
347
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
348 return sort_node_cmp_type(ctx, ctx->program->sort_program, p1, p2);
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
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
351 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
352 index_sort_save_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
353 uint32_t first_seq)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
354 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
355 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
356 (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
357 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
358 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
359
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
360 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
361 for (i = 0; i < count; i++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
362 if (nodes[i].seq < first_seq)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
363 continue;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
364
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
365 i_assert(nodes[i].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
366 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
367 program->ext_id, &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
368 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
369 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
370
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
371 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
372 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
373 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
374 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
375 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
376 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
377 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
378 const char *last_str = "";
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
379 uint32_t prev_id = 0, last_id = (uint32_t)-1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
380 string_t *prev_str;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
381 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
382 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
383
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
384 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
385 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
386 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
387 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
388
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
389 last_str = sort_header_get(program->sort_program[0], mail,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
390 nodes[idx2].seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
391 idx2--;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
392 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
393
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
394 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
395 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
396 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
397
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
398 str_append(prev_str,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
399 sort_header_get(program->sort_program[0], mail,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
400 nodes[idx1].seq));
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
401 idx1++;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
402 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
403
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
404 for (i = idx1; i <= idx2; i++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
405 str = sort_header_get(program->sort_program[0], mail,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
406 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
407
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
408 if (i == idx2 && strcmp(str, last_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
409 nodes[i].sort_id = last_id;
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
410 else if (strcmp(str, str_c(prev_str)) == 0 && prev_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
411 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
412 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
413 /* 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
414 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
415 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
416 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
417 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
418 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
419 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
420 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
421 /* 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
422 the IDs. */
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
423 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
424 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
425
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
426 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
427 str_truncate(prev_str, 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
428 str_append(prev_str, str);
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 }
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
431 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
432 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
433
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
434 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
435 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
436 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
437 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
438 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
439 (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
440 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
441 unsigned int i, count;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
442 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
443
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
444 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
445 prev_sort_id = (uint32_t)-1;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
446 for (; idx < count; idx++) {
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
447 sort_id = nodes[idx].sort_id;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
448 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
449 break;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
450 }
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
451 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
452
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
453 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
454 /* 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
455 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
456 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
457 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
458 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
459 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
460 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
461
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
462 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
463 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
464 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
465 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
466 &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
467 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
468 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
469 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
470 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
471
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
472 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
473 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
474 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
475 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
476 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
477 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
478
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
479 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
480 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
481 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
482 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
483 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
484 &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
485 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
486 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
487 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
488
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
489 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
490 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
491 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
492 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
493 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
494 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
495
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
496 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
497 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
498 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
499 continue;
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
500
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
501 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
502 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
503 break;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
504 }
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
505 T_FRAME(
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
506 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
507 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
508 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
509 );
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
510 if (!ret)
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6711
diff changeset
511 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
512 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
513 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
514
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
515 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
516 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
517 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
518 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
519 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
520 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
521
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
522 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
523 case MAIL_SORT_ARRIVAL:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
524 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
525 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
526 case MAIL_SORT_DATE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
527 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
528 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
529 case MAIL_SORT_SIZE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
530 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
531 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
532 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
533 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
534 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
535
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
536 /* 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
537 mail = 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
538 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
539 for (; node.seq <= last_seq; node.seq++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
540 mail_set_seq(mail, node.seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
541 node.sort_id = get_sort_id(mail);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
542 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
543 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
544 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
545
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
546 /* @UNSAFE: and sort them */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
547 memset(&static_node_cmp_context, 0, sizeof(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
548 static_node_cmp_context.program = program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
549 static_node_cmp_context.mail = mail;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
550
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
551 qsort(array_idx_modifiable(&program->all_nodes, 0), 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
552 sizeof(struct mail_sort_node), sort_node_cmp);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
553 }
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 static void index_sort_cache_seq(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
556 enum mail_sort_type sort_type, 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
557 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
558 ctx->cache_seq = seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
559 ctx->cache_type = 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
560
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
561 mail_set_seq(ctx->mail, seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
562 switch (ctx->cache_type) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
563 case MAIL_SORT_ARRIVAL:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
564 ctx->cache_value = 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
565 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
566 case MAIL_SORT_DATE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
567 ctx->cache_value = 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
568 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
569 case MAIL_SORT_SIZE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
570 ctx->cache_value = 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
571 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
572 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
573 ctx->cache_str = sort_header_get(sort_type, ctx->mail, seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
574 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
575 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
576 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
577
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
578 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
579 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
580 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
581 struct mail_sort_node *nodes, node;
5336
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
582 const struct mail_sort_node *cnodes;
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
583 unsigned int count, idx;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
584
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
585 /* we wish to avoid reading the actual headers as much as possible.
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
586 first sort the nodes which already have sort_ids, then start
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
587 inserting the new nodes by finding their insertion position with
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
588 binary search */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
589 memset(&static_node_cmp_context, 0, sizeof(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
590 static_node_cmp_context.program = program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
591 static_node_cmp_context.mail = 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
592
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
593 /* @UNSAFE */
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
594 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
595 if (program->last_sorted_seq != count) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
596 qsort(nodes, count, sizeof(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
597 sort_node_cmp);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
598 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
599
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
600 node.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
601 for (node.seq = count + 1; node.seq <= last_seq; node.seq++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
602 index_sort_cache_seq(&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
603 program->sort_program[0], node.seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
604
6711
7e969a570307 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6644
diff changeset
605 cnodes = array_get_modifiable(&program->all_nodes, &count);
5336
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
606 bsearch_insert_pos(&node, cnodes, count, sizeof(*cnodes),
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
607 sort_node_cmp_no_sort_id,
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
608 &idx);
6711
7e969a570307 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6644
diff changeset
609 array_insert(&program->all_nodes, idx, &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
610 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
611
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
612 index_sort_add_ids(program, static_node_cmp_context.mail);
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 static void index_sort_build(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 uint32_t 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
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_mailbox *ibox = (struct index_mailbox *)program->t->box;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
619 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
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 unsigned int i, first_missing_sort_id_seq;
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 = array_count(&program->all_nodes);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
624 if (i == 0) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
625 /* we're building the array from scratch. add here only the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
626 messages that have sort_ids set. */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
627 program->last_sorted_seq = 0;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
628 for (; i < last_seq; i++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
629 node.seq = i+1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
630
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
631 mail_index_lookup_ext(ibox->view, i+1, program->ext_id,
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
632 &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
633
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
634 node.sort_id = data == NULL ? 0 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
635 *(const uint32_t *)data;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
636 if (node.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
637 /* the rest don't have sort_ids either */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
638 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
639 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
640 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
641 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
642 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
643 first_missing_sort_id_seq = i + 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
644
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
645 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
646 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
647 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
648 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
649 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
650 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
651 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
652 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
653 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
654 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
655 index_sort_save_ids(program, first_missing_sort_id_seq);
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
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
658 static void index_sort_add_node(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
659 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
660 {
5336
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
661 const struct mail_sort_node *nodes;
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
662 unsigned int count, idx;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
663
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
664 memset(&static_node_cmp_context, 0, sizeof(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
665 static_node_cmp_context.program = program;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
666 static_node_cmp_context.mail = 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
667
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
668 nodes = array_get(&program->nodes, &count);
5336
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
669 bsearch_insert_pos(node, nodes, count,
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
670 sizeof(*node), sort_node_cmp,
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
671 &idx);
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
672 array_insert(&program->nodes, idx, 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
673
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
674 program->last_sorted_seq = node->seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
675 program->prev_seq = node->seq;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
676 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
677
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
678 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
679 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
680 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
681 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
682 (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
683 const struct mail_index_header *hdr;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
684 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
685 struct mail_sort_node node;
6532
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
686 uint32_t 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
687
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
688 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
689
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
690 if (program->prev_seq + 1 != mail->seq)
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
691 program->skipped_mails = TRUE;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
692
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
693 node.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
694 if (program->last_sorted_seq == program->prev_seq) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
695 /* we're still on the fast path using sort_ids from the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
696 index file */
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
697 mail_index_lookup_ext(t->trans_view, mail->seq,
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
698 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
699 node.sort_id = data == NULL ? 0 : *(const uint32_t *)data;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
700 if (node.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
701 index_sort_add_node(program, &node);
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
702 return;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
703 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
704 i_assert(!program->sort_ids_added);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
705 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
706 node.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
707 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
708
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
709 /* sort_ids are missing, have to generate them */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
710 if (!program->skipped_mails) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
711 /* as long as we think we're returning all the mails sorted,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
712 which is the common case, we want to avoid duplicating the
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
713 node array. so here we just keep counting the sequences
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
714 until either we skip a sequence or we reach list_finish() */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
715 program->prev_seq = mail->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
716 return;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
717 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
718
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
719 /* we're not returning all the mails. have to create a temporary array
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
720 for all the nodes so we can set all 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
721 hdr = mail_index_get_header(t->ibox->view);
4596
bf4e98a0de3f Replaced ARRAY_CREATE() macro with [ipt]_array_init() macros. The macro
Timo Sirainen <tss@iki.fi>
parents: 4594
diff changeset
722 i_array_init(&program->all_nodes, hdr->messages_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
723 index_sort_build(program, hdr->messages_count);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
724 array_free(&program->all_nodes);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
725
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
726 /* add the nodes in the middle */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
727 node.seq = program->last_sorted_seq + 1;
6532
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
728 last_seq = program->prev_seq;
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
729 for (; node.seq <= last_seq; 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
730 mail_index_lookup_ext(t->trans_view, mail->seq, program->ext_id,
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
731 &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
732
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
733 node.sort_id = *(const uint32_t *)data;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
734 i_assert(node.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
735
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
736 index_sort_add_node(program, &node);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
737 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
738
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
739 /* and add this last node */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
740 program->sort_ids_added = TRUE;
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
741 index_sort_list_add(program, mail);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
742 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
743
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
744 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
745 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
746 if (program->last_sorted_seq != program->prev_seq) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
747 /* nodes array contains a contiguous range of sequences from
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
748 the beginning, with the last ones missing sort_id. we can
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
749 just sort the array directly without copying it. */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
750 i_assert(!program->sort_ids_added);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
751
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
752 program->all_nodes = program->nodes;
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
753 index_sort_build(program, program->prev_seq);
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
754 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
755
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
756 program->nodes_ptr =
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
757 array_get(&program->nodes, &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
758
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
759 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
760 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
761 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
762
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
763 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
764 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
765 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
766 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
767
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
768 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
769 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
770 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
771
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
772 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
773 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
774 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
775 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
776
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
777 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
778 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
779 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
780 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
781 }