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 42E9C431FBC for ; Mon, 7 Dec 2009 19:15:04 -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 8LcHcrDNToTx for ; Mon, 7 Dec 2009 19:15:03 -0800 (PST) Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57]) by olra.theworths.org (Postfix) with ESMTP id 4BD4A431FAE for ; Mon, 7 Dec 2009 19:15:03 -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 1NHqXT-0007Ji-Ja; Mon, 07 Dec 2009 23:15:02 -0400 Received: from bremner by localhost with local (Exim 4.69) (envelope-from ) id 1NHqXN-00047O-I7; Mon, 07 Dec 2009 23:14:53 -0400 From: david@tethera.net To: notmuch@notmuchmail.org Date: Mon, 7 Dec 2009 23:14:48 -0400 Message-Id: <1260242088-15680-1-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.6.5.3 In-Reply-To: <1260124225-10830-1-git-send-email-david@tethera.net> References: <1260124225-10830-1-git-send-email-david@tethera.net> 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: Tue, 08 Dec 2009 03:15:04 -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. --- This is the second version of the patch, the only difference is in the test on line 144 (used to be 142), and the comment before explaining it. notmuch-restore.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 63 insertions(+), 12 deletions(-) diff --git a/notmuch-restore.c b/notmuch-restore.c index 1b9598d..9d7bd17 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,55 @@ 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