unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: david@tethera.net
To: notmuch@notmuchmail.org
Cc: David Bremner <bremner@unb.ca>
Subject: [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file.
Date: Sun,  6 Dec 2009 14:30:25 -0400	[thread overview]
Message-ID: <1260124225-10830-1-git-send-email-david@tethera.net> (raw)

From: David Bremner <bremner@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 <cworth@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

             reply	other threads:[~2009-12-06 18:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-06 18:30 david [this message]
2009-12-08  3:14 ` [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file david
2009-12-08  6:26   ` Scott Robinson
2009-12-08  9:50   ` Carl Worth
2009-12-08 22:07     ` david
2010-03-09 18:40       ` Carl Worth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1260124225-10830-1-git-send-email-david@tethera.net \
    --to=david@tethera.net \
    --cc=bremner@unb.ca \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).