[notmuch] Quick thoughts on a notmuch daemon

2009-12-08 Thread Michiel Buddingh'
On Thu, 03 Dec 2009 14:27:05 -0800, Carl Worth  wrote:
> A simple solution would be a notmuch daemon that can accept commands on
> stdin, (in basically the exact same form as the current notmuch
> command-line interface). If the daemon does the job of periodically
> incorporating new mail, then the only command necessary to solve (1)
> above would be the tag command.

If you add a second pipe for notmuch to broadcast information about
events (such as new mail being indexed) you could farm out most of the
logic that will increasingly clutter up notmuch-new.c to an external
daemon.

Just the mailid and path would be enough for people to implement their
own tagging based on directory, Maildir flags or (for all I care)
Bayesian content filtering with relative ease.

Just a thought
-- 
Michiel Buddingh'


[notmuch] [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file.

2009-12-08 Thread da...@tethera.net
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 talloc_array and grows it as neccesary using
talloc_realloc.
---

This is posting 3 of the patch. The second version fixed a bug.  This
version replaces calloc/realloc with talloc equivalents, prettifies
the comparison function for qsort, reindents, and adds a bunch of
whitespace.

 notmuch-restore.c |   98 +++--
 1 files changed, 80 insertions(+), 18 deletions(-)

diff --git a/notmuch-restore.c b/notmuch-restore.c
index 1b9598d..818bd15 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -19,9 +19,22 @@
  */

 #include "notmuch-client.h"
+#include 
+#include 
+
+#define DEFAULT_TAG_ARRAY_SIZE 2
+
+static int 
+strcmp_for_qsort (const void *ptr1, const void *ptr2)
+{
+char * const * str1_p = ptr1;
+char * const * str2_p = ptr2;
+
+return strcmp(*str1_p, *str2_p);
+}

 int
-notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
+notmuch_restore_command (void *ctx, int argc, char *argv[])
 {
 notmuch_config_t *config;
 notmuch_database_t *notmuch;
@@ -31,6 +44,8 @@ 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 +76,21 @@ notmuch_restore_command (unused (void *ctx), int argc, char 
*argv[])
  "^([^ ]+) \\(([^)]*)\\)$",
  REG_EXTENDED);

+/* Make an array of pointers to point to individual tokens */
+tag_array = talloc_array (ctx, char *, tag_array_size);
+if  (!tag_array) {
+   fprintf (stderr, "fatal: overflow in talloc_array");
+   exit(1);
+}
+
 while ((line_len = getline (, _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 +114,61 @@ notmuch_restore_command (unused (void *ctx), int argc, 
char *argv[])
goto NEXT_LINE;
}

-   notmuch_message_freeze (message);
-
-   notmuch_message_remove_all_tags (message);
-
next = tags;
-   while (next) {
-   tag = strsep (, " ");
-   if (*tag == '\0')
-   continue;
-   status = notmuch_message_add_tag (message, tag);
-   if (status) {
-   fprintf (stderr,
-"Error applying tag %s to message %s:\n",
-tag, message_id);
-   fprintf (stderr, "%s\n",
-notmuch_status_to_string (status));
+   tag_count = 0;
+   while (*next) {
+   while (*next && isspace(*next))
+   next++;
+   if (*next) {
+   while (tag_count >= tag_array_size) {
+   tag_array_size *= 2;
+   tag_array = talloc_realloc (ctx, tag_array, char *, 
+   tag_array_size);
+   if  (!tag_array) {
+   fprintf (stderr, "fatal: overflow in talloc_realloc");
+   exit(1);
+   }
+   } 
+   tag_array[tag_count] = next;
+   tag_count++;
}
+   while (*next && !isspace(*next))
+   next++;
+   if (*next) {
+   *next = '\0';
+   next++;
+   }
+   }
+
+   qsort(tag_array,tag_count,sizeof(char*),strcmp_for_qsort);
+   
+   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_thaw (message);
+   /* the only success condition is for the tag list comparison is to run
+  off the end of both lists at the same time */
+   if (notmuch_tags_has_more (tag_list) || i < tag_count ){
+   

[notmuch] regress option to messages iterator

2009-12-08 Thread ruben pollan
Attached to this email I add a small code that use this functions, as an
example.

On 10:41, Tue 08 Dec 09, meskio at sindominio.net wrote:
> Two patches for implement the regress functions on messages. With them 
> notmuch_messages_t can be use as an iterator forwards and backwards.
> 
> Up to now not really useful, nothing use them. I send them mainly to review.
> I'll do the same to threads and tags, so the UIs can use them.
> 
> PS: It's the first time I'm using git-send-email, I hope I do everything well.
> 
> ___
> notmuch mailing list
> notmuch at notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch
> 

-- 
Rub?n Poll?n  | jabber:meskio at jabber.org
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  Si luchas puedes perder
 Si no luchas estas perdido

-- next part --
A non-text attachment was scrubbed...
Name: foo.c
Type: text/x-csrc
Size: 886 bytes
Desc: not available
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20091208/de04dc2b/attachment.c>
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20091208/de04dc2b/attachment.pgp>


[notmuch] [PATCH 2/2] Added regress option to messages iterator

2009-12-08 Thread mes...@sindominio.net
From: Ruben Pollan 

Added the functions notmuch_messages_regress and notmuch_messages_is_first to
notmuch library. With them is possible to iterate backwards on messages.

* notmuch_messages_regress do the opposite than notmuch_messages_advance,
  getting the messages iterator one position backwards.

* notmuch_messages_is_first return TRUE if the iterator is in the first
  message.
---
 lib/messages.c|   27 +++
 lib/notmuch-private.h |7 +++
 lib/notmuch.h |8 
 lib/query.cc  |   24 
 4 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/lib/messages.c b/lib/messages.c
index 5414f87..2c28738 100644
--- a/lib/messages.c
+++ b/lib/messages.c
@@ -90,6 +90,7 @@ _notmuch_messages_create (notmuch_message_list_t *list)

 messages->is_of_list_type = TRUE;
 messages->iterator = list->head;
+messages->previous_node = NULL;

 return messages;
 }
@@ -121,6 +122,18 @@ notmuch_messages_has_more (notmuch_messages_t *messages)
 return (messages->iterator != NULL);
 }

+notmuch_bool_t
+notmuch_messages_is_first (notmuch_messages_t *messages)
+{
+if (messages == NULL)
+   return TRUE;
+
+if (! messages->is_of_list_type)
+   return _notmuch_mset_messages_is_first (messages);
+
+return (messages->previous_node == NULL);
+}
+
 notmuch_message_t *
 notmuch_messages_get (notmuch_messages_t *messages)
 {
@@ -142,10 +155,24 @@ notmuch_messages_advance (notmuch_messages_t *messages)
 if (messages->iterator == NULL)
return;

+messages->previous_node = messages->iterator;
 messages->iterator = messages->iterator->next;
 }

 void
+notmuch_messages_regress (notmuch_messages_t *messages)
+{
+if (! messages->is_of_list_type)
+   return _notmuch_mset_messages_regress (messages);
+
+if (messages->previous_node == NULL)
+   return;
+
+messages->iterator = messages->previous_node;
+messages->previous_node = messages->iterator->prev;
+}
+
+void
 notmuch_messages_destroy (notmuch_messages_t *messages)
 {
 talloc_free (messages);
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 133ed0e..5852c00 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -299,6 +299,7 @@ typedef struct _notmuch_message_list {
  */
 struct _notmuch_messages {
 notmuch_bool_t is_of_list_type;
+notmuch_message_node_t *previous_node;
 notmuch_message_node_t *iterator;
 };

@@ -321,12 +322,18 @@ _notmuch_messages_create (notmuch_message_list_t *list);
 notmuch_bool_t
 _notmuch_mset_messages_has_more (notmuch_messages_t *messages);

+notmuch_bool_t
+_notmuch_mset_messages_is_first (notmuch_messages_t *messages);
+
 notmuch_message_t *
 _notmuch_mset_messages_get (notmuch_messages_t *messages);

 void
 _notmuch_mset_messages_advance (notmuch_messages_t *messages);

+void
+_notmuch_mset_messages_regress (notmuch_messages_t *messages);
+
 /* message.cc */

 void
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 60834fb..69bd98a 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -604,6 +604,10 @@ notmuch_thread_destroy (notmuch_thread_t *thread);
 notmuch_bool_t
 notmuch_messages_has_more (notmuch_messages_t *messages);

+/* Is the given notmuch_messages_t object on the first message */
+notmuch_bool_t
+notmuch_messages_is_first (notmuch_messages_t *messages);
+
 /* Get the current message from 'messages' as a notmuch_message_t.
  *
  * Note: The returned message belongs to 'messages' and has a lifetime
@@ -626,6 +630,10 @@ notmuch_messages_get (notmuch_messages_t *messages);
 void
 notmuch_messages_advance (notmuch_messages_t *messages);

+/* Regress the 'messages' iterator to the previous result. */
+void
+notmuch_messages_regress (notmuch_messages_t *messages);
+
 /* Destroy a notmuch_messages_t object.
  *
  * It's not strictly necessary to call this function. All memory from
diff --git a/lib/query.cc b/lib/query.cc
index 9106b92..94a6860 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -35,6 +35,7 @@ typedef struct _notmuch_mset_messages {
 notmuch_messages_t base;
 notmuch_database_t *notmuch;
 Xapian::MSetIterator iterator;
+Xapian::MSetIterator iterator_begin;
 Xapian::MSetIterator iterator_end;
 } notmuch_mset_messages_t;

@@ -86,6 +87,7 @@ static int
 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
 {
 messages->iterator.~MSetIterator ();
+messages->iterator_begin.~MSetIterator ();
 messages->iterator_end.~MSetIterator ();

 return 0;
@@ -108,6 +110,7 @@ notmuch_query_search_messages (notmuch_query_t *query)
messages->base.iterator = NULL;
messages->notmuch = notmuch;
new (>iterator) Xapian::MSetIterator ();
+   new (>iterator_begin) Xapian::MSetIterator ();
new (>iterator_end) Xapian::MSetIterator ();

talloc_set_destructor (messages, _notmuch_messages_destructor);
@@ -155,6 +158,7 @@ notmuch_query_search_messages 

[notmuch] [PATCH 1/2] Convert notmuch_message_list_t in a doubly linked

2009-12-08 Thread mes...@sindominio.net
From: Ruben Pollan 

The messages list now have pointers to previous nodes, so it is possible to
iterate backwards.
---
 lib/messages.c|   18 +-
 lib/notmuch-private.h |3 ++-
 lib/thread.cc |4 ++--
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/lib/messages.c b/lib/messages.c
index aa92535..5414f87 100644
--- a/lib/messages.c
+++ b/lib/messages.c
@@ -37,20 +37,28 @@ _notmuch_message_list_create (const void *ctx)
return NULL;

 list->head = NULL;
-list->tail = >head;
+list->tail = NULL;

 return list;
 }

-/* Append 'node' (which can of course point to an arbitrarily long
- * list of nodes) to the end of 'list'.
+/* Append 'node' to the end of 'list'.
  */
 void
 _notmuch_message_list_append (notmuch_message_list_t *list,
  notmuch_message_node_t *node)
 {
-*(list->tail) = node;
-list->tail = >next;
+node->prev = list->tail;
+if (list->head)
+{
+list->tail->next = node;
+}
+else
+{
+list->head = node;
+list->tail = node;
+}
+list->tail = node;
 }

 /* Allocate a new node for 'message' and append it to the end of
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 0c340a7..133ed0e 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -285,11 +285,12 @@ notmuch_message_file_get_header (notmuch_message_file_t 
*message,
 typedef struct _notmuch_message_node {
 notmuch_message_t *message;
 struct _notmuch_message_node *next;
+struct _notmuch_message_node *prev;
 } notmuch_message_node_t;

 typedef struct _notmuch_message_list {
 notmuch_message_node_t *head;
-notmuch_message_node_t **tail;
+notmuch_message_node_t *tail;
 } notmuch_message_list_t;

 /* There's a rumor that there's an alternate struct _notmuch_messages
diff --git a/lib/thread.cc b/lib/thread.cc
index 321937b..24dbe2c 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -169,8 +169,8 @@ _resolve_thread_relationships (unused (notmuch_thread_t 
*thread))
  (void **) ))
{
*prev = node->next;
-   if (thread->message_list->tail == >next)
-   thread->message_list->tail = prev;
+   if (thread->message_list->tail == node)
+   thread->message_list->tail = node->prev;
node->next = NULL;
_notmuch_message_add_reply (parent, node);
} else {
-- 
1.6.5.4



[notmuch] regress option to messages iterator

2009-12-08 Thread mes...@sindominio.net
Two patches for implement the regress functions on messages. With them 
notmuch_messages_t can be use as an iterator forwards and backwards.

Up to now not really useful, nothing use them. I send them mainly to review.
I'll do the same to threads and tags, so the UIs can use them.

PS: It's the first time I'm using git-send-email, I hope I do everything well.



[notmuch] [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file.

2009-12-08 Thread Carl Worth
On Mon,  7 Dec 2009 23:14:48 -0400, david at tethera.net wrote:
> 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.

Hi David,

This is going to be a nice performance fix. Thanks for working on it!

I noticed that the code is in a style that is inconsistent with the
prevailing style of the notmuch code. So here are a few points on some
obvious style differences (there are other similar issues that I don't
mention specifically).

And I don't mean anything personal here. We all have different styles
that we prefer and find most legible. I do think it's important that the
code be in a consistent style throughout though, (and I will make a
CODING_STYLE document for the source tree soon).

> +/* for qsort */
> +static int scmp( const void *sp1, const void *sp2 )

The function name really needs to be on its own line (flush with the
left-most column). I don't like the interior whitespace for the argument
list. I really don't like a name as dramatically abbreviated as
"scmp". I'd prefer something like strcmp_for_qsort or whatever this is
actually doing.

> +return( strcmp(*(const char **)sp1, *(const char **)sp2) );

More whitespace here. There should be a space *before* the '(' not
after, (and then not before the ')'). Finally, with void* arguments, I
like to take care of the ugly casts up front, (assigning to approriately
named local variables) rather than cluttering a function call with casts
like this.

> +char **tag_array=NULL;
> +int tag_array_size=DEFAULT_TAG_ARRAY_SIZE;

Need whitespace on either side of '=' here, (and similar throughout).

> + while(*next){

More missing space. I would prefer:

while (*next) {

> +   while(*next && isspace(*next))
> + next++;

Current indentation in notmuch is 4 columns per indent level, not 2.

> +   tag_array=realloc(tag_array,tag_array_size*sizeof(char *));

I can't read that at all with so much missing space. I'd prefer to see:

  tag_array = realloc (tag_array, tag_array_size * sizeof (char *));

> + while (notmuch_tags_has_more (tag_list) && i +(strcmp(notmuch_tags_get (tag_list),tag_array[i])==0)){
> +   notmuch_tags_advance (tag_list);
> +   i++;
> + }

While I don't mind an opening brace on the same line as an "if" or
"while" condition, I do mind it when the condition spans more than one
line. In that case, the opening brace really needs to be on a line of
its own. And again, lots of missing space in the above.

> +   for (i=0; i<tag_count; i++) {

More missing space. Should be:

  for (i = 0; i < tag_count; i++) {

-Carl

PS. Why is the commit mentioning using calloc, not talloc? Is there a
reason talloc is inappropriate here? Or were you just not familiar with
how to use it? I'd be glad to answer any questions you have about use of
talloc in notmuch.
-- next part --
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20091208/a40323c3/attachment.pgp>


Re: [notmuch] [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file.

2009-12-08 Thread Carl Worth
On Mon,  7 Dec 2009 23:14:48 -0400, da...@tethera.net wrote:
 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.

Hi David,

This is going to be a nice performance fix. Thanks for working on it!

I noticed that the code is in a style that is inconsistent with the
prevailing style of the notmuch code. So here are a few points on some
obvious style differences (there are other similar issues that I don't
mention specifically).

And I don't mean anything personal here. We all have different styles
that we prefer and find most legible. I do think it's important that the
code be in a consistent style throughout though, (and I will make a
CODING_STYLE document for the source tree soon).

 +/* for qsort */
 +static int scmp( const void *sp1, const void *sp2 )

The function name really needs to be on its own line (flush with the
left-most column). I don't like the interior whitespace for the argument
list. I really don't like a name as dramatically abbreviated as
scmp. I'd prefer something like strcmp_for_qsort or whatever this is
actually doing.

 +return( strcmp(*(const char **)sp1, *(const char **)sp2) );

More whitespace here. There should be a space *before* the '(' not
after, (and then not before the ')'). Finally, with void* arguments, I
like to take care of the ugly casts up front, (assigning to approriately
named local variables) rather than cluttering a function call with casts
like this.

 +char **tag_array=NULL;
 +int tag_array_size=DEFAULT_TAG_ARRAY_SIZE;

Need whitespace on either side of '=' here, (and similar throughout).

 + while(*next){

More missing space. I would prefer:

while (*next) {

 +   while(*next  isspace(*next))
 + next++;

Current indentation in notmuch is 4 columns per indent level, not 2.

 +   tag_array=realloc(tag_array,tag_array_size*sizeof(char *));

I can't read that at all with so much missing space. I'd prefer to see:

  tag_array = realloc (tag_array, tag_array_size * sizeof (char *));

 + while (notmuch_tags_has_more (tag_list)  itag_count 
 +(strcmp(notmuch_tags_get (tag_list),tag_array[i])==0)){
 +   notmuch_tags_advance (tag_list);
 +   i++;
 + }

While I don't mind an opening brace on the same line as an if or
while condition, I do mind it when the condition spans more than one
line. In that case, the opening brace really needs to be on a line of
its own. And again, lots of missing space in the above.

 +   for (i=0; itag_count; i++) {

More missing space. Should be:

  for (i = 0; i  tag_count; i++) {

-Carl

PS. Why is the commit mentioning using calloc, not talloc? Is there a
reason talloc is inappropriate here? Or were you just not familiar with
how to use it? I'd be glad to answer any questions you have about use of
talloc in notmuch.


pgpzD2vlnEh78.pgp
Description: PGP signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [notmuch] Quick thoughts on a notmuch daemon

2009-12-08 Thread Michiel Buddingh'
On Thu, 03 Dec 2009 14:27:05 -0800, Carl Worth cwo...@cworth.org wrote:
 A simple solution would be a notmuch daemon that can accept commands on
 stdin, (in basically the exact same form as the current notmuch
 command-line interface). If the daemon does the job of periodically
 incorporating new mail, then the only command necessary to solve (1)
 above would be the tag command.

If you add a second pipe for notmuch to broadcast information about
events (such as new mail being indexed) you could farm out most of the
logic that will increasingly clutter up notmuch-new.c to an external
daemon.

Just the mailid and path would be enough for people to implement their
own tagging based on directory, Maildir flags or (for all I care)
Bayesian content filtering with relative ease.

Just a thought
-- 
Michiel Buddingh'
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[notmuch] [PATCH] notmuch-restore.c: only update tags for messages that differ from dump file.

2009-12-08 Thread david
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 talloc_array and grows it as neccesary using
talloc_realloc.
---

This is posting 3 of the patch. The second version fixed a bug.  This
version replaces calloc/realloc with talloc equivalents, prettifies
the comparison function for qsort, reindents, and adds a bunch of
whitespace.

 notmuch-restore.c |   98 +++--
 1 files changed, 80 insertions(+), 18 deletions(-)

diff --git a/notmuch-restore.c b/notmuch-restore.c
index 1b9598d..818bd15 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -19,9 +19,22 @@
  */
 
 #include notmuch-client.h
+#include stdlib.h
+#include ctype.h
+
+#define DEFAULT_TAG_ARRAY_SIZE 2
+
+static int 
+strcmp_for_qsort (const void *ptr1, const void *ptr2)
+{
+char * const * str1_p = ptr1;
+char * const * str2_p = ptr2;
+
+return strcmp(*str1_p, *str2_p);
+}
 
 int
-notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
+notmuch_restore_command (void *ctx, int argc, char *argv[])
 {
 notmuch_config_t *config;
 notmuch_database_t *notmuch;
@@ -31,6 +44,8 @@ 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 +76,21 @@ notmuch_restore_command (unused (void *ctx), int argc, char 
*argv[])
  ^([^ ]+) \\(([^)]*)\\)$,
  REG_EXTENDED);
 
+/* Make an array of pointers to point to individual tokens */
+tag_array = talloc_array (ctx, char *, tag_array_size);
+if  (!tag_array) {
+   fprintf (stderr, fatal: overflow in talloc_array);
+   exit(1);
+}
+
 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 +114,61 @@ notmuch_restore_command (unused (void *ctx), int argc, 
char *argv[])
goto NEXT_LINE;
}
 
-   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);
-   if (status) {
-   fprintf (stderr,
-Error applying tag %s to message %s:\n,
-tag, message_id);
-   fprintf (stderr, %s\n,
-notmuch_status_to_string (status));
+   tag_count = 0;
+   while (*next) {
+   while (*next  isspace(*next))
+   next++;
+   if (*next) {
+   while (tag_count = tag_array_size) {
+   tag_array_size *= 2;
+   tag_array = talloc_realloc (ctx, tag_array, char *, 
+   tag_array_size);
+   if  (!tag_array) {
+   fprintf (stderr, fatal: overflow in talloc_realloc);
+   exit(1);
+   }
+   } 
+   tag_array[tag_count] = next;
+   tag_count++;
}
+   while (*next  !isspace(*next))
+   next++;
+   if (*next) {
+   *next = '\0';
+   next++;
+   }
+   }
+
+   qsort(tag_array,tag_count,sizeof(char*),strcmp_for_qsort);
+   
+   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_thaw (message);
+   /* the only success condition is for the tag list comparison is to run
+  off the end of both lists at the same time */
+   if (notmuch_tags_has_more (tag_list) || i  tag_count ){
+