1 Return-Path: <bremner@pivot.cs.unb.ca>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 33967431FBD
\r
6 for <notmuch@notmuchmail.org>; Tue, 8 Dec 2009 14:07:48 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 Received: from olra.theworths.org ([127.0.0.1])
\r
9 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
10 with ESMTP id 71OSzzg-A2xC for <notmuch@notmuchmail.org>;
\r
11 Tue, 8 Dec 2009 14:07:47 -0800 (PST)
\r
12 Received: from convex-new.cs.unb.ca (convex-new.cs.unb.ca [131.202.245.35])
\r
13 by olra.theworths.org (Postfix) with ESMTP id 27187431FAE
\r
14 for <notmuch@notmuchmail.org>; Tue, 8 Dec 2009 14:07:47 -0800 (PST)
\r
15 Received: from bremner by convex-new.cs.unb.ca with local (Exim 4.69)
\r
16 (envelope-from <bremner@pivot.cs.unb.ca>)
\r
17 id 1NI8Di-00033l-FN; Tue, 08 Dec 2009 18:07:46 -0400
\r
18 From: david@tethera.net
\r
19 To: notmuch@notmuchmail.org
\r
20 Date: Tue, 8 Dec 2009 18:07:43 -0400
\r
21 Message-Id: <1260310063-11731-1-git-send-email-david@tethera.net>
\r
22 X-Mailer: git-send-email 1.6.5.3
\r
23 In-Reply-To: <87ws0x23r8.fsf@yoom.home.cworth.org>
\r
24 References: <87ws0x23r8.fsf@yoom.home.cworth.org>
\r
25 Cc: David Bremner <bremner@unb.ca>
\r
26 Subject: [notmuch] [PATCH] notmuch-restore.c: only update tags for messages
\r
27 that differ from dump file.
\r
28 X-BeenThere: notmuch@notmuchmail.org
\r
29 X-Mailman-Version: 2.1.12
\r
31 List-Id: "Use and development of the notmuch mail system."
\r
32 <notmuch.notmuchmail.org>
\r
33 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
34 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
35 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
36 List-Post: <mailto:notmuch@notmuchmail.org>
\r
37 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
38 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
39 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
40 X-List-Received-Date: Tue, 08 Dec 2009 22:07:48 -0000
\r
42 From: David Bremner <bremner@unb.ca>
\r
44 The main feature of this patch is that it compares the list of current
\r
45 tags on a message with those read by restore. Only if the two lists
\r
46 differ is the tag list in the message replaced. In my experiments this leads to
\r
47 a large performance improvement.
\r
49 Since I had to rewrite the parsing of tags from the dump file anyway
\r
50 to keep a list of tags (in case they should be written to the
\r
51 database), I decided to make it a bit more robust. It sorts the
\r
52 incoming tags (it is critical for the comparison of the two tag lists
\r
53 that they are both sorted), and allows arbitrary whitespace (as
\r
54 determined by "isspace") between tags.
\r
56 The patch allocates a temporary array to keep track of the current
\r
57 list of tags using talloc_array and grows it as neccesary using
\r
61 This is posting 3 of the patch. The second version fixed a bug. This
\r
62 version replaces calloc/realloc with talloc equivalents, prettifies
\r
63 the comparison function for qsort, reindents, and adds a bunch of
\r
66 notmuch-restore.c | 98 +++++++++++++++++++++++++++++++++++++++++++----------
\r
67 1 files changed, 80 insertions(+), 18 deletions(-)
\r
69 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
70 index 1b9598d..818bd15 100644
\r
71 --- a/notmuch-restore.c
\r
72 +++ b/notmuch-restore.c
\r
76 #include "notmuch-client.h"
\r
77 +#include <stdlib.h>
\r
80 +#define DEFAULT_TAG_ARRAY_SIZE 2
\r
83 +strcmp_for_qsort (const void *ptr1, const void *ptr2)
\r
85 + char * const * str1_p = ptr1;
\r
86 + char * const * str2_p = ptr2;
\r
88 + return strcmp(*str1_p, *str2_p);
\r
92 -notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
93 +notmuch_restore_command (void *ctx, int argc, char *argv[])
\r
95 notmuch_config_t *config;
\r
96 notmuch_database_t *notmuch;
\r
97 @@ -31,6 +44,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
101 + char **tag_array = NULL;
\r
102 + int tag_array_size = DEFAULT_TAG_ARRAY_SIZE;
\r
104 config = notmuch_config_open (ctx, NULL, NULL);
\r
105 if (config == NULL)
\r
106 @@ -61,11 +76,21 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
107 "^([^ ]+) \\(([^)]*)\\)$",
\r
110 + /* Make an array of pointers to point to individual tokens */
\r
111 + tag_array = talloc_array (ctx, char *, tag_array_size);
\r
112 + if (!tag_array) {
\r
113 + fprintf (stderr, "fatal: overflow in talloc_array");
\r
117 while ((line_len = getline (&line, &line_size, input)) != -1) {
\r
118 regmatch_t match[3];
\r
119 - char *message_id, *tags, *tag, *next;
\r
120 + char *message_id, *tags, *next;
\r
121 notmuch_message_t *message;
\r
122 notmuch_status_t status;
\r
124 + notmuch_tags_t *tag_list;
\r
127 chomp_newline (line);
\r
129 @@ -89,26 +114,61 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
133 - notmuch_message_freeze (message);
\r
135 - notmuch_message_remove_all_tags (message);
\r
139 - tag = strsep (&next, " ");
\r
140 - if (*tag == '\0')
\r
142 - status = notmuch_message_add_tag (message, tag);
\r
145 - "Error applying tag %s to message %s:\n",
\r
146 - tag, message_id);
\r
147 - fprintf (stderr, "%s\n",
\r
148 - notmuch_status_to_string (status));
\r
151 + while (*next && isspace(*next))
\r
154 + while (tag_count >= tag_array_size) {
\r
155 + tag_array_size *= 2;
\r
156 + tag_array = talloc_realloc (ctx, tag_array, char *,
\r
158 + if (!tag_array) {
\r
159 + fprintf (stderr, "fatal: overflow in talloc_realloc");
\r
163 + tag_array[tag_count] = next;
\r
166 + while (*next && !isspace(*next))
\r
174 + qsort(tag_array,tag_count,sizeof(char*),strcmp_for_qsort);
\r
176 + tag_list = notmuch_message_get_tags (message);
\r
178 + while (notmuch_tags_has_more (tag_list) && i < tag_count &&
\r
179 + (strcmp(notmuch_tags_get (tag_list),tag_array[i]) == 0))
\r
181 + notmuch_tags_advance (tag_list);
\r
185 - notmuch_message_thaw (message);
\r
186 + /* the only success condition is for the tag list comparison is to run
\r
187 + off the end of both lists at the same time */
\r
188 + if (notmuch_tags_has_more (tag_list) || i < tag_count ){
\r
189 + notmuch_message_freeze (message);
\r
190 + notmuch_message_remove_all_tags (message);
\r
192 + for (i=0; i < tag_count; i++) {
\r
193 + status = notmuch_message_add_tag (message, tag_array[i]);
\r
196 + "Error applying tag %s to message %s:\n",
\r
197 + tag_array[i], message_id);
\r
198 + fprintf (stderr, "%s\n",
\r
199 + notmuch_status_to_string (status));
\r
202 + notmuch_message_thaw (message);
\r
204 notmuch_message_destroy (message);
\r
207 @@ -120,6 +180,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
211 + talloc_free (tag_array);
\r
213 notmuch_database_close (notmuch);
\r
214 if (input != stdin)
\r