annotate src/lib-storage/index/index-sort.c @ 6711:7e969a570307 HEAD

Assert-crashfix
author Timo Sirainen <tss@iki.fi>
date Tue, 06 Nov 2007 22:47:19 +0200
parents 119c5a16150c
children 414c9d631a81
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;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
252 const char *str1, *str2;
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
253 uint32_t time1, time2, size1, size2;
4935
f768dd773e6b Compiler warning fix
Timo Sirainen <tss@iki.fi>
parents: 4635
diff changeset
254 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
255
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
256 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
257 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
258 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
259 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
260 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
261 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
262 t_push();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
263 str1 = n1->seq == ctx->cache_seq &&
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
264 ctx->cache_type == sort_type ? ctx->cache_str :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
265 sort_header_get(sort_type, ctx->mail, n1->seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
266 str2 = sort_header_get(sort_type, ctx->mail, n2->seq);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
267
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
268 ret = strcmp(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
269 t_pop();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
270 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
271 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
272 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
273 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
274 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
275 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
276 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
277 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
278
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
279 mail_set_seq(ctx->mail, n2->seq);
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
280 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
281
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
282 ret = time1 < time2 ? -1 :
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
283 (time1 > time2 ? 1 : 0);
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
284 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
285 case MAIL_SORT_DATE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
286 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
287 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
288 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
289 mail_set_seq(ctx->mail, 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
290 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
291 }
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 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
294 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
295
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
296 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
297 (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
298 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
299 case MAIL_SORT_SIZE:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
300 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
301 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
302 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
303 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
304 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
305 }
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 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
308 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
309
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
310 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
311 (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
312 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
313 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
314 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
315 (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
316 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
317 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
318 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
319 }
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 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
322 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
323
6549
bf5293708132 Fixed handling reversed sorts.
Timo Sirainen <tss@iki.fi>
parents: 6547
diff changeset
324 /* 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
325 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
326 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
327 return ret;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
328 }
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 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
331 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
332 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
333 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
334
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
335 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
336 return -1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
337 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
338 return 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
339
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
340 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
341 }
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 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
344 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
345 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
346
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
347 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
348 }
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 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
351 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
352 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
353 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
354 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
355 (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
356 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
357 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
358
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
359 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
360 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
361 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
362 continue;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
363
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
364 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
365 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
366 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
367 }
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 static int
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
371 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
372 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
373 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
374 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
375 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
376 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
377 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
378 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
379 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
380 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
381 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
382 int ret = 1;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
383
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
384 t_push();
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
385 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
386 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
387 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
388 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
389
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
390 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
391 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
392 idx2--;
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
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
395 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
396 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
397 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
398
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
399 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
400 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
401 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
402 idx1++;
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
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
405 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
406 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
407 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
408
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
409 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
410 nodes[i].sort_id = last_id;
6135
bc0f8a8397a3 Use i;unicode-casemap for sorting also.
Timo Sirainen <tss@iki.fi>
parents: 5336
diff changeset
411 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
412 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
413 else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
414 /* 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
415 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
416 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
417 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
418 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
419 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
420 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
421 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
422 /* 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
423 the IDs. */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
424 ret = 0;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
425 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
426 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
427
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
428 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
429 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
430 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
431 }
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 t_pop();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
434 return ret;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
435 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
436
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
437 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
438 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
439 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
440 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
441 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
442 (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
443 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
444 unsigned int i, count;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
445 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
446
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
447 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
448 prev_sort_id = (uint32_t)-1;
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
449 for (; idx < count; idx++) {
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
450 sort_id = nodes[idx].sort_id;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
451 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
452 break;
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
453 }
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
454 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
455
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
456 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
457 /* 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
458 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
459 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
460 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
461 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
462 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
463 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
464
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
465 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
466 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
467 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
468 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
469 &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
470 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
471 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
472 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
473 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
474
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
475 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
476 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
477 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
478 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
479 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
480 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
481
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
482 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
483 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
484 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
485 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
486 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
487 &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
488 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
489 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
490 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
491
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
492 static void
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
493 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
494 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
495 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
496 unsigned int i, j, count;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
497
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
498 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
499 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
500 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
501 for (j = i + 1; j < count; j++) {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
502 if (nodes[j].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
503 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
504 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
505 if (index_sort_add_ids_range(program, mail,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
506 i == 0 ? 0 : i-1,
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
507 I_MIN(j, count-1)) == 0)
6644
119c5a16150c Fixed assert-crash for sorting by strings.
Timo Sirainen <tss@iki.fi>
parents: 6549
diff changeset
508 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
509 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
510 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
511 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
512
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
513 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
514 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
515 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
516 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
517 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
518 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
519
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
520 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
521 case MAIL_SORT_ARRIVAL:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
522 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
523 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
524 case MAIL_SORT_DATE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
525 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
526 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
527 case MAIL_SORT_SIZE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
528 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
529 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
530 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
531 i_unreached();
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
532 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
533
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
534 /* 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
535 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
536 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
537 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
538 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
539 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
540 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
541 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
542 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
543
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
544 /* @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
545 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
546 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
547 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
548
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
549 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
550 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
551 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
552
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
553 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
554 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
555 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
556 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
557 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
558
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
559 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
560 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
561 case MAIL_SORT_ARRIVAL:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
562 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
563 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
564 case MAIL_SORT_DATE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
565 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
566 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
567 case MAIL_SORT_SIZE:
6547
7a207cbc008b Don't assert-crash with node.sort_id != 0.
Timo Sirainen <tss@iki.fi>
parents: 6546
diff changeset
568 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
569 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
570 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
571 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
572 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
573 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
574 }
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 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
577 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
578 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
579 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
580 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
581 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
582
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
583 /* 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
584 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
585 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
586 binary search */
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
587 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
588 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
589 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
590
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
591 /* @UNSAFE */
4451
1a35d53c18fc Array API redesigned to work using unions. It now provides type safety
Timo Sirainen <tss@iki.fi>
parents: 4303
diff changeset
592 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
593 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
594 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
595 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
596 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
597
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
598 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
599 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
600 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
601 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
602
6711
7e969a570307 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6644
diff changeset
603 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
604 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
605 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
606 &idx);
6711
7e969a570307 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6644
diff changeset
607 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
608 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
609
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
610 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
611 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
612
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
613 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
614 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
615 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
616 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
617 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
618 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
619 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
620
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
621 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
622 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
623 /* 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
624 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
625 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
626 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
627 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
628
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
629 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
630 &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
631
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
632 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
633 *(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
634 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
635 /* 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
636 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
637 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
638 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
639 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
640 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
641 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
642
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
643 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
644 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
645 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
646 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
647 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
648 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
649 default:
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
650 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
651 break;
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
652 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
653 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
654 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
655
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
656 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
657 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
658 {
5336
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
659 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
660 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
661
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
662 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
663 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
664 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
665
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
666 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
667 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
668 sizeof(*node), sort_node_cmp,
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
669 &idx);
109ca861405f bsearch_insert_pos() API changed. Patch by Max Kellermann
Timo Sirainen <tss@iki.fi>
parents: 4935
diff changeset
670 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
671
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
672 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
673 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
674 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
675
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
676 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
677 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
678 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
679 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
680 (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
681 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
682 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
683 struct mail_sort_node node;
6532
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
684 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
685
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
686 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
687
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
688 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
689 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
690
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
691 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
692 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
693 /* 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
694 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
695 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
696 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
697 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
698 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
699 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
700 return;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
701 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
702 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
703 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
704 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
705 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
706
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
707 /* 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
708 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
709 /* 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
710 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
711 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
712 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
713 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
714 return;
4303
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
715 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
716
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
717 /* 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
718 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
719 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
720 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
721 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
722 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
723
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
724 /* 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
725 node.seq = program->last_sorted_seq + 1;
6532
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
726 last_seq = program->prev_seq;
da3093d9f572 Assert-crashfix
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
727 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
728 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
729 &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
730
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
731 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
732 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
733
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
734 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
735 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
736
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
737 /* 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
738 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
739 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
740 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
741
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
742 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
743 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
744 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
745 /* 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
746 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
747 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
748 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
749
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
750 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
751 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
752 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
753
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
754 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
755 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
756
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
757 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
758 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
759 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
760
6277
5f66277bbe40 mail_index_lookup*() can't fail anymore. Changed several APIs not to return
Timo Sirainen <tss@iki.fi>
parents: 6136
diff changeset
761 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
762 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
763 {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
764 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
765
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
766 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
767 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
768 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
769
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
770 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
771 } else {
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
772 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
773 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
774
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
775 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
776 }
5f03738219a6 Changed mail-storage API to do the mail sorting internally. Optimized it
Timo Sirainen <timo.sirainen@movial.fi>
parents:
diff changeset
777 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
778 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
779 }