Re: Hi all
[notmuch-archives.git] / c2 / 7776819568f492ce9c692e90f98a4d55776a53
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
30 Precedence: list\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
41 \r
42 From: David Bremner <bremner@unb.ca>\r
43 \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
48 \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
55 \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
58 talloc_realloc.\r
59 ---\r
60 \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
64 whitespace.\r
65 \r
66  notmuch-restore.c |   98 +++++++++++++++++++++++++++++++++++++++++++----------\r
67  1 files changed, 80 insertions(+), 18 deletions(-)\r
68 \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
73 @@ -19,9 +19,22 @@\r
74   */\r
75  \r
76  #include "notmuch-client.h"\r
77 +#include <stdlib.h>\r
78 +#include <ctype.h>\r
79 +\r
80 +#define DEFAULT_TAG_ARRAY_SIZE 2\r
81 +\r
82 +static int \r
83 +strcmp_for_qsort (const void *ptr1, const void *ptr2)\r
84 +{\r
85 +    char * const * str1_p = ptr1;\r
86 +    char * const * str2_p = ptr2;\r
87 +\r
88 +    return strcmp(*str1_p, *str2_p);\r
89 +}\r
90  \r
91  int\r
92 -notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
93 +notmuch_restore_command (void *ctx, int argc, char *argv[])\r
94  {\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
98      ssize_t line_len;\r
99      regex_t regex;\r
100      int rerr;\r
101 +    char **tag_array = NULL;\r
102 +    int tag_array_size = DEFAULT_TAG_ARRAY_SIZE;\r
103  \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
108               REG_EXTENDED);\r
109  \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
114 +       exit(1);\r
115 +    }\r
116 +    \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
123 +       int tag_count;\r
124 +       notmuch_tags_t *tag_list;\r
125 +       int i;\r
126  \r
127         chomp_newline (line);\r
128  \r
129 @@ -89,26 +114,61 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
130             goto NEXT_LINE;\r
131         }\r
132  \r
133 -       notmuch_message_freeze (message);\r
134 -\r
135 -       notmuch_message_remove_all_tags (message);\r
136 -\r
137         next = tags;\r
138 -       while (next) {\r
139 -           tag = strsep (&next, " ");\r
140 -           if (*tag == '\0')\r
141 -               continue;\r
142 -           status = notmuch_message_add_tag (message, tag);\r
143 -           if (status) {\r
144 -               fprintf (stderr,\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
149 +       tag_count = 0;\r
150 +       while (*next) {\r
151 +           while (*next && isspace(*next))\r
152 +               next++;\r
153 +           if (*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
157 +                                               tag_array_size);\r
158 +                   if  (!tag_array) {\r
159 +                       fprintf (stderr, "fatal: overflow in talloc_realloc");\r
160 +                       exit(1);\r
161 +                   }\r
162 +               } \r
163 +               tag_array[tag_count] = next;\r
164 +               tag_count++;\r
165             }\r
166 +           while (*next && !isspace(*next))\r
167 +               next++;\r
168 +           if (*next) {\r
169 +               *next = '\0';\r
170 +               next++;\r
171 +           }\r
172 +       }\r
173 +\r
174 +       qsort(tag_array,tag_count,sizeof(char*),strcmp_for_qsort);\r
175 +       \r
176 +       tag_list = notmuch_message_get_tags (message);\r
177 +       i = 0;\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
180 +       {\r
181 +           notmuch_tags_advance (tag_list);\r
182 +           i++;\r
183         }\r
184  \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
191 +\r
192 +           for (i=0; i < tag_count; i++) {\r
193 +               status = notmuch_message_add_tag (message, tag_array[i]);\r
194 +               if (status) {\r
195 +                   fprintf (stderr,\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
200 +               }\r
201 +           }\r
202 +           notmuch_message_thaw (message);\r
203 +       }\r
204         notmuch_message_destroy (message);\r
205        NEXT_LINE:\r
206         free (message_id);\r
207 @@ -120,6 +180,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
208      if (line)\r
209         free (line);\r
210  \r
211 +    talloc_free (tag_array);\r
212 +\r
213      notmuch_database_close (notmuch);\r
214      if (input != stdin)\r
215         fclose (input);\r
216 -- \r
217 1.6.5.3\r
218 \r