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