From: David Bremner <brem...@unb.ca> The main feature of this patch is that it compares the list of current tags on a message with those read by restore. Only if the two lists differ is the tag list in the message replaced. In my experiments this leads to a large performance improvement.
Since I had to rewrite the parsing of tags from the dump file anyway to keep a list of tags (in case they should be written to the database), I decided to make it a bit more robust. It sorts the incoming tags (it is critical for the comparison of the two tag lists that they are both sorted), and allows arbitrary whitespace (as determined by "isspace") between tags. The patch allocates a temporary array to keep track of the current list of tags using calloc and grows it as neccesary using realloc. --- notmuch-restore.c | 73 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 61 insertions(+), 12 deletions(-) diff --git a/notmuch-restore.c b/notmuch-restore.c index 1b9598d..31e29f6 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -18,8 +18,17 @@ * Author: Carl Worth <cwo...@cworth.org> */ +#include <stdlib.h> +#include <string.h> +#include <ctype.h> #include "notmuch-client.h" +#define DEFAULT_TAG_ARRAY_SIZE 2 +/* for qsort */ +static int scmp( const void *sp1, const void *sp2 ) +{ + return( strcmp(*(const char **)sp1, *(const char **)sp2) ); +} int notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) { @@ -31,6 +40,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) ssize_t line_len; regex_t regex; int rerr; + char **tag_array=NULL; + int tag_array_size=DEFAULT_TAG_ARRAY_SIZE; + config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) @@ -61,11 +73,18 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) "^([^ ]+) \\(([^)]*)\\)$", REG_EXTENDED); + /* Make an array of pointers to point to individual tokens */ + tag_array=calloc(tag_array_size,sizeof(char*)); + while ((line_len = getline (&line, &line_size, input)) != -1) { regmatch_t match[3]; - char *message_id, *tags, *tag, *next; + char *message_id, *tags, *next; notmuch_message_t *message; notmuch_status_t status; + int tag_count; + + notmuch_tags_t *tag_list; + int i; chomp_newline (line); @@ -89,26 +108,53 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) goto NEXT_LINE; } - notmuch_message_freeze (message); + next=tags; + tag_count=0; + while(*next){ + while(*next && isspace(*next)) + next++; + if (*next) { + while (tag_count>= tag_array_size){ + tag_array_size*=2; + tag_array=realloc(tag_array,tag_array_size*sizeof(char *)); + } + tag_array[tag_count]=next; + tag_count++; + } + while (*next && !isspace(*next)) + next++; + if (*next){ + *next='\0'; + next++; + } + } + + qsort(tag_array,tag_count,sizeof(char*),scmp); + + tag_list = notmuch_message_get_tags (message); + i=0; + while (notmuch_tags_has_more (tag_list) && i<tag_count && + (strcmp(notmuch_tags_get (tag_list),tag_array[i])==0)){ + notmuch_tags_advance (tag_list); + i++; + } - notmuch_message_remove_all_tags (message); + if (notmuch_tags_has_more (tag_list) && i<tag_count ){ + notmuch_message_freeze (message); + notmuch_message_remove_all_tags (message); - next = tags; - while (next) { - tag = strsep (&next, " "); - if (*tag == '\0') - continue; - status = notmuch_message_add_tag (message, tag); + for (i=0; i<tag_count; i++) { + status = notmuch_message_add_tag (message, tag_array[i]); if (status) { fprintf (stderr, "Error applying tag %s to message %s:\n", - tag, message_id); + tag_array[i], message_id); fprintf (stderr, "%s\n", notmuch_status_to_string (status)); } + } + notmuch_message_thaw (message); } - - notmuch_message_thaw (message); notmuch_message_destroy (message); NEXT_LINE: free (message_id); @@ -120,6 +166,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) if (line) free (line); + if (tag_array) + free (tag_array); + notmuch_database_close (notmuch); if (input != stdin) fclose (input); -- 1.6.5.3 _______________________________________________ notmuch mailing list notmuch@notmuchmail.org http://notmuchmail.org/mailman/listinfo/notmuch