Re: [python] get all messages of a thread
authorAustin Clements <amdragon@mit.edu>
Thu, 2 Jun 2011 14:38:46 +0000 (10:38 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:38:30 +0000 (09:38 -0800)
cd/ba152eaa139ba6a606f7ba4390f55ffdc44ded [new file with mode: 0644]

diff --git a/cd/ba152eaa139ba6a606f7ba4390f55ffdc44ded b/cd/ba152eaa139ba6a606f7ba4390f55ffdc44ded
new file mode 100644 (file)
index 0000000..102637d
--- /dev/null
@@ -0,0 +1,218 @@
+Return-Path: <amdragon@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 38457431FD0\r
+       for <notmuch@notmuchmail.org>; Thu,  2 Jun 2011 07:38:49 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.699\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001,\r
+       RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id S+TvpbWxgSr3 for <notmuch@notmuchmail.org>;\r
+       Thu,  2 Jun 2011 07:38:48 -0700 (PDT)\r
+Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com\r
+       [209.85.216.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 24CD7431FB6\r
+       for <notmuch@notmuchmail.org>; Thu,  2 Jun 2011 07:38:48 -0700 (PDT)\r
+Received: by qyg14 with SMTP id 14so525363qyg.5\r
+       for <notmuch@notmuchmail.org>; Thu, 02 Jun 2011 07:38:47 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+       h=domainkey-signature:mime-version:sender:in-reply-to:references:date\r
+       :x-google-sender-auth:message-id:subject:from:to:cc:content-type;\r
+       bh=WGnx4TC0uJY+Q7IhUFou5EPL0Y4Nqgr7SRfsC14aEoc=;\r
+       b=Gc67lnVLks9xqaI+0qz8/sGuGVg5ESXLUVPY3ES35SAE9bta9ZTX2CYVpXm04OFK5H\r
+       HRXfjZBIoOdRdzdPp+rGo6W2RKjI8qMf8tuMp4mlX0ORKYHrZaYJuzRhdGm7vmedLe4U\r
+       S6tqk5JuqKUmt9dVGu7pYYtjFqL/JJ+QK0Pp8=\r
+DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;\r
+       h=mime-version:sender:in-reply-to:references:date\r
+       :x-google-sender-auth:message-id:subject:from:to:cc:content-type;\r
+       b=iEanlP7mLxM9AOJjlHr1ibgHbUxyFYBcoCT7eumgIScwXgZlaZVO4t5P+U4ETpFucL\r
+       G5ts+54Itkpvy6qbll2tO+8ZPWtofwE12mMF+AMyRz3v1qBWD68XuDcx7wiiqpJBUml1\r
+       iRD/h8tiHMh4L3nJRTxHJFolv3w8ACMahFIEY=\r
+MIME-Version: 1.0\r
+Received: by 10.224.187.9 with SMTP id cu9mr532984qab.381.1307025527091; Thu,\r
+       02 Jun 2011 07:38:47 -0700 (PDT)\r
+Sender: amdragon@gmail.com\r
+Received: by 10.229.188.68 with HTTP; Thu, 2 Jun 2011 07:38:46 -0700 (PDT)\r
+In-Reply-To: <87r57cuxgh.fsf@SSpaeth.de>\r
+References: <1306588052-sup-9838@brick>\r
+       <BANLkTikEJ9dJ5kLg2QJQ+egHg2sh7S1wiA@mail.gmail.com>\r
+       <87aae07lxi.fsf@SSpaeth.de>\r
+       <BANLkTi=df3+LBWasFic9ZNrKaLGA7Bmdew@mail.gmail.com>\r
+       <87r57cuxgh.fsf@SSpaeth.de>\r
+Date: Thu, 2 Jun 2011 10:38:46 -0400\r
+X-Google-Sender-Auth: 0ESy2bkvXgnvGVXhVHQ2pab55KI\r
+Message-ID: <BANLkTimMqJJLWx8AfLz8CKuOWjky_HR0Ag@mail.gmail.com>\r
+Subject: Re: [python] get all messages of a thread\r
+From: Austin Clements <amdragon@mit.edu>\r
+To: Sebastian Spaeth <Sebastian@sspaeth.de>\r
+Content-Type: text/plain; charset=ISO-8859-1\r
+Cc: notmuch <notmuch@notmuchmail.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 02 Jun 2011 14:38:49 -0000\r
+\r
+On Thu, Jun 2, 2011 at 10:20 AM, Sebastian Spaeth <Sebastian@sspaeth.de> wrote:\r
+> On Thu, 2 Jun 2011 19:43:29 +1000, Brian May wrote:\r
+>> On 2 June 2011 17:05, Sebastian Spaeth <Sebastian@sspaeth.de> wrote:\r
+>>\r
+>> > What would be the best way to solve this (besides fixing the C api to\r
+>> > allow to reset the iterator ;-) ?)\r
+>\r
+>> * It is not easy to fix the C api to reset the iterator (what about\r
+>> repeating the search?)\r
+>\r
+> I am not sure about the difficulty of that, I am not a C-kind of\r
+> guy. Repeating the search would be easy but potentially gives you\r
+> different results since the db could have changed since then.\r
+\r
+Not too hard.  Here's an utterly untested patch that implements\r
+iterator resetting for notmuch_messages_t iterators.  It *should* be\r
+much more efficient than performing the query again, but if you use\r
+it, I'd love to know if that's actually true.\r
+\r
+This may not be useful if __len__ is gone, unless you really want to\r
+turn Messages/Threads into iterators rather than generators (as I've\r
+pointed out before, there is absolutely nothing unusual or un-Pythonic\r
+about how Messages/Threads works right now [well, except for the\r
+presence of __len__ in a generator, I suppose]).\r
+\r
+diff --git a/lib/messages.c b/lib/messages.c\r
+index 7bcd1ab..085691c 100644\r
+--- a/lib/messages.c\r
++++ b/lib/messages.c\r
+@@ -80,7 +80,8 @@ _notmuch_messages_create (notmuch_message_list_t *list)\r
+       return NULL;\r
+\r
+     messages->is_of_list_type = TRUE;\r
+-    messages->iterator = list->head;\r
++    messages->head = list->head;\r
++    notmuch_messages_reset (messages);\r
+\r
+     return messages;\r
+ }\r
+@@ -137,6 +138,15 @@ notmuch_messages_move_to_next (notmuch_messages_t\r
+*messages)\r
+ }\r
+\r
+ void\r
++notmuch_messages_reset (notmuch_messages_t *messages)\r
++{\r
++    if (! messages->is_of_list_type)\r
++      return _notmuch_mset_messages_reset (messages);\r
++\r
++    messages->iterator = messages->head;\r
++}\r
++\r
++void\r
+ notmuch_messages_destroy (notmuch_messages_t *messages)\r
+ {\r
+     talloc_free (messages);\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 02e24ee..805d60c 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -413,6 +413,7 @@ typedef struct _notmuch_message_list {\r
+  */\r
+ struct visible _notmuch_messages {\r
+     notmuch_bool_t is_of_list_type;\r
++    notmuch_message_node_t *head;\r
+     notmuch_message_node_t *iterator;\r
+ };\r
+\r
+@@ -441,6 +442,9 @@ _notmuch_mset_messages_get (notmuch_messages_t *messages);\r
+ void\r
+ _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);\r
+\r
++void\r
++_notmuch_mset_messages_reset (notmuch_messages_t *messages);\r
++\r
+ notmuch_bool_t\r
+ _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,\r
+                               unsigned int doc_id);\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 9cdcec0..044cfaa 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -734,6 +734,15 @@ notmuch_messages_get (notmuch_messages_t *messages);\r
+ void\r
+ notmuch_messages_move_to_next (notmuch_messages_t *messages);\r
+\r
++/* Reset the 'messages' iterator back to the first message.\r
++ *\r
++ * For iterators returned from notmuch_query_search_messages, this is\r
++ * both more efficient than performing the query a second time and\r
++ * guaranteed to result in the same messages as the first iteration.\r
++ */\r
++void\r
++notmuch_messages_reset (notmuch_messages_t *messages);\r
++\r
+ /* Destroy a notmuch_messages_t object.\r
+  *\r
+  * It's not strictly necessary to call this function. All memory from\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index 6f02b04..1e75be0 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -32,6 +32,7 @@ struct _notmuch_query {\r
+ typedef struct _notmuch_mset_messages {\r
+     notmuch_messages_t base;\r
+     notmuch_database_t *notmuch;\r
++    Xapian::MSet mset;\r
+     Xapian::MSetIterator iterator;\r
+     Xapian::MSetIterator iterator_end;\r
+ } notmuch_mset_messages_t;\r
+@@ -128,6 +129,7 @@ notmuch_query_search_messages (notmuch_query_t *query)\r
+       messages->base.is_of_list_type = FALSE;\r
+       messages->base.iterator = NULL;\r
+       messages->notmuch = notmuch;\r
++      new (&messages->mset) Xapian::MSet ();\r
+       new (&messages->iterator) Xapian::MSetIterator ();\r
+       new (&messages->iterator_end) Xapian::MSetIterator ();\r
+\r
+@@ -181,8 +183,8 @@ notmuch_query_search_messages (notmuch_query_t *query)\r
+\r
+       mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());\r
+\r
+-      messages->iterator = mset.begin ();\r
+-      messages->iterator_end = mset.end ();\r
++      messages->mset = mset;\r
++      _notmuch_mset_messages_reset (&messages->base);\r
+\r
+       return &messages->base;\r
+\r
+@@ -257,6 +259,17 @@ _notmuch_mset_messages_move_to_next\r
+(notmuch_messages_t *messages)\r
+     mset_messages->iterator++;\r
+ }\r
+\r
++void\r
++_notmuch_mset_messages_reset (notmuch_messages_t *messages)\r
++{\r
++    notmuch_mset_messages_t *mset_messages;\r
++\r
++    mset_messages = (notmuch_mset_messages_t *) messages;\r
++\r
++    mset_messages->iterator = mset_messages->mset.begin ();\r
++    mset_messages->iterator_end = mset_messages->mset.end ();\r
++}\r
++\r
+ static notmuch_bool_t\r
+ _notmuch_doc_id_set_init (void *ctx,\r
+                         notmuch_doc_id_set_t *doc_ids,\r