Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 28822431FBC for ; Sun, 6 Dec 2009 10:30:57 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ml5u7FT1xzzu for ; Sun, 6 Dec 2009 10:30:56 -0800 (PST) Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57]) by olra.theworths.org (Postfix) with ESMTP id 5A73C431FAE for ; Sun, 6 Dec 2009 10:30:56 -0800 (PST) Received: from fctnnbsc30w-142167182194.pppoe-dynamic.high-speed.nb.bellaliant.net ([142.167.182.194] helo=localhost) by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1NHLsk-0000tQ-RB; Sun, 06 Dec 2009 14:30:55 -0400 Received: from bremner by localhost with local (Exim 4.69) (envelope-from ) id 1NHLse-0002pB-HU; Sun, 06 Dec 2009 14:30:48 -0400 From: david@tethera.net To: notmuch@notmuchmail.org Date: Sun, 6 Dec 2009 14:30:25 -0400 Message-Id: <1260124225-10830-1-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.6.5.3 X-Sender-Verified: bremner@pivot.cs.unb.ca Cc: David Bremner Subject: [notmuch] [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file. X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 06 Dec 2009 18:30:57 -0000 From: David Bremner 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 */ +#include +#include +#include #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