Re: [PATCH] lib: reword comment about XFOLDER: prefix
[notmuch-archives.git] / cc / 06bfdb4e7ee5f1f2dedf4017f7f8814fff03f6
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 42E9C431FBC\r
6         for <notmuch@notmuchmail.org>; Mon,  7 Dec 2009 19:15:04 -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 8LcHcrDNToTx for <notmuch@notmuchmail.org>;\r
11         Mon,  7 Dec 2009 19:15:03 -0800 (PST)\r
12 Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57])\r
13         by olra.theworths.org (Postfix) with ESMTP id 4BD4A431FAE\r
14         for <notmuch@notmuchmail.org>; Mon,  7 Dec 2009 19:15:03 -0800 (PST)\r
15 Received: from\r
16         fctnnbsc30w-142167182194.pppoe-dynamic.high-speed.nb.bellaliant.net\r
17         ([142.167.182.194] helo=localhost)\r
18         by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32)\r
19         (Exim 4.69) (envelope-from <bremner@pivot.cs.unb.ca>)\r
20         id 1NHqXT-0007Ji-Ja; Mon, 07 Dec 2009 23:15:02 -0400\r
21 Received: from bremner by localhost with local (Exim 4.69)\r
22         (envelope-from <bremner@pivot.cs.unb.ca>)\r
23         id 1NHqXN-00047O-I7; Mon, 07 Dec 2009 23:14:53 -0400\r
24 From: david@tethera.net\r
25 To: notmuch@notmuchmail.org\r
26 Date: Mon,  7 Dec 2009 23:14:48 -0400\r
27 Message-Id: <1260242088-15680-1-git-send-email-david@tethera.net>\r
28 X-Mailer: git-send-email 1.6.5.3\r
29 In-Reply-To: <1260124225-10830-1-git-send-email-david@tethera.net>\r
30 References: <1260124225-10830-1-git-send-email-david@tethera.net>\r
31 X-Sender-Verified: bremner@pivot.cs.unb.ca\r
32 Cc: David Bremner <bremner@unb.ca>\r
33 Subject: [notmuch] [PATCH] notmuch-restore.c: only update tags for messages\r
34         that differ from dump file.\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.12\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Tue, 08 Dec 2009 03:15:04 -0000\r
48 \r
49 From: David Bremner <bremner@unb.ca>\r
50 \r
51 The main feature of this patch is that it compares the list of current\r
52 tags on a message with those read by restore. Only if the two lists\r
53 differ is the tag list in the message replaced.  In my experiments this leads to\r
54 a large performance improvement.\r
55 \r
56 Since I had to rewrite the parsing of tags from the dump file anyway\r
57 to keep a list of tags (in case they should be written to the\r
58 database), I decided to make it a bit more robust. It sorts the\r
59 incoming tags (it is critical for the comparison of the two tag lists\r
60 that they are both sorted), and allows arbitrary whitespace (as\r
61 determined by "isspace") between tags.\r
62 \r
63 The patch allocates a temporary array to keep track of the current\r
64 list of tags using calloc and grows it as neccesary using realloc.\r
65 ---\r
66 \r
67 This is the second version of the patch, the only difference is in the test \r
68 on line 144 (used to be 142), and the comment before explaining it.\r
69 \r
70  notmuch-restore.c |   75 ++++++++++++++++++++++++++++++++++++++++++++--------\r
71  1 files changed, 63 insertions(+), 12 deletions(-)\r
72 \r
73 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
74 index 1b9598d..9d7bd17 100644\r
75 --- a/notmuch-restore.c\r
76 +++ b/notmuch-restore.c\r
77 @@ -18,8 +18,17 @@\r
78   * Author: Carl Worth <cworth@cworth.org>\r
79   */\r
80  \r
81 +#include <stdlib.h>\r
82 +#include <string.h>\r
83 +#include <ctype.h>\r
84  #include "notmuch-client.h"\r
85  \r
86 +#define DEFAULT_TAG_ARRAY_SIZE 2\r
87 +/* for qsort */\r
88 +static int scmp( const void *sp1, const void *sp2 )\r
89 +{\r
90 +    return( strcmp(*(const char **)sp1, *(const char **)sp2) );\r
91 +}\r
92  int\r
93  notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
94  {\r
95 @@ -31,6 +40,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
96      ssize_t line_len;\r
97      regex_t regex;\r
98      int rerr;\r
99 +    char **tag_array=NULL;\r
100 +    int tag_array_size=DEFAULT_TAG_ARRAY_SIZE;\r
101 +\r
102  \r
103      config = notmuch_config_open (ctx, NULL, NULL);\r
104      if (config == NULL)\r
105 @@ -61,11 +73,18 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
106               "^([^ ]+) \\(([^)]*)\\)$",\r
107               REG_EXTENDED);\r
108  \r
109 +    /* Make an array of pointers to point to individual tokens */\r
110 +    tag_array=calloc(tag_array_size,sizeof(char*));\r
111 +\r
112      while ((line_len = getline (&line, &line_size, input)) != -1) {\r
113         regmatch_t match[3];\r
114 -       char *message_id, *tags, *tag, *next;\r
115 +       char *message_id, *tags,  *next;\r
116         notmuch_message_t *message;\r
117         notmuch_status_t status;\r
118 +       int tag_count;\r
119 +\r
120 +       notmuch_tags_t *tag_list;\r
121 +       int i;\r
122  \r
123         chomp_newline (line);\r
124  \r
125 @@ -89,26 +108,55 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
126             goto NEXT_LINE;\r
127         }\r
128  \r
129 -       notmuch_message_freeze (message);\r
130 +       next=tags;\r
131 +       tag_count=0;\r
132 +       while(*next){\r
133 +         while(*next && isspace(*next))\r
134 +           next++;\r
135 +         if (*next) {\r
136 +           while (tag_count>= tag_array_size){\r
137 +             tag_array_size*=2;\r
138 +             tag_array=realloc(tag_array,tag_array_size*sizeof(char *));\r
139 +           }\r
140 +           tag_array[tag_count]=next;\r
141 +           tag_count++;\r
142 +         }\r
143 +         while (*next && !isspace(*next))\r
144 +           next++;\r
145 +         if (*next){\r
146 +           *next='\0';\r
147 +           next++;\r
148 +         }\r
149 +       }\r
150 +\r
151 +       qsort(tag_array,tag_count,sizeof(char*),scmp);\r
152 +       \r
153 +       tag_list = notmuch_message_get_tags (message);\r
154 +       i=0;\r
155 +       while (notmuch_tags_has_more (tag_list) && i<tag_count &&\r
156 +              (strcmp(notmuch_tags_get (tag_list),tag_array[i])==0)){\r
157 +         notmuch_tags_advance (tag_list);\r
158 +         i++;\r
159 +       }\r
160  \r
161 -       notmuch_message_remove_all_tags (message);\r
162 +       /* the only success condition is for the tag list comparison is to run\r
163 +          off the end of both lists at the same time */\r
164 +       if (notmuch_tags_has_more (tag_list) || i<tag_count ){\r
165 +         notmuch_message_freeze (message);\r
166 +         notmuch_message_remove_all_tags (message);\r
167  \r
168 -       next = tags;\r
169 -       while (next) {\r
170 -           tag = strsep (&next, " ");\r
171 -           if (*tag == '\0')\r
172 -               continue;\r
173 -           status = notmuch_message_add_tag (message, tag);\r
174 +         for (i=0; i<tag_count; i++) {\r
175 +           status = notmuch_message_add_tag (message, tag_array[i]);\r
176             if (status) {\r
177                 fprintf (stderr,\r
178                          "Error applying tag %s to message %s:\n",\r
179 -                        tag, message_id);\r
180 +                        tag_array[i], message_id);\r
181                 fprintf (stderr, "%s\n",\r
182                          notmuch_status_to_string (status));\r
183             }\r
184 +         }\r
185 +         notmuch_message_thaw (message);\r
186         }\r
187 -\r
188 -       notmuch_message_thaw (message);\r
189         notmuch_message_destroy (message);\r
190        NEXT_LINE:\r
191         free (message_id);\r
192 @@ -120,6 +168,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
193      if (line)\r
194         free (line);\r
195  \r
196 +    if (tag_array)\r
197 +      free (tag_array);\r
198 +\r
199      notmuch_database_close (notmuch);\r
200      if (input != stdin)\r
201         fclose (input);\r
202 -- \r
203 1.6.5.3\r
204 \r