[notmuch] [PATCH 4/5] Added backwards iterator to threads
authorRuben Pollan <meskio@sindominio.net>
Sat, 20 Mar 2010 10:23:24 +0000 (11:23 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:36:24 +0000 (09:36 -0800)
d8/1a88342fb96169188abb8a726d52ad28624845 [new file with mode: 0644]

diff --git a/d8/1a88342fb96169188abb8a726d52ad28624845 b/d8/1a88342fb96169188abb8a726d52ad28624845
new file mode 100644 (file)
index 0000000..cdbd087
--- /dev/null
@@ -0,0 +1,337 @@
+Return-Path: <meskio@sindominio.net>\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 F2A6E431FC1\r
+       for <notmuch@notmuchmail.org>; Sat, 20 Mar 2010 03:21:50 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.9\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5\r
+       tests=[BAYES_00=-1.9] autolearn=ham\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 bFeKf-mL5SPx for <notmuch@notmuchmail.org>;\r
+       Sat, 20 Mar 2010 03:21:49 -0700 (PDT)\r
+Received: from flatline.sindominio.net (flatline.sindominio.net [82.144.4.26])\r
+       by olra.theworths.org (Postfix) with ESMTP id 19C2D414BB9\r
+       for <notmuch@notmuchmail.org>; Sat, 20 Mar 2010 03:21:48 -0700 (PDT)\r
+Received: from localhost (localhost.localdomain [127.0.0.1])\r
+       by flatline.sindominio.net (Postfix) with ESMTP id 51FEE262E3F;\r
+       Sat, 20 Mar 2010 11:21:47 +0100 (CET)\r
+X-Virus-Scanned: Debian amavisd-new at sindominio.net\r
+Received: from flatline.sindominio.net ([127.0.0.1])\r
+       by localhost (flatline.sindominio.net [127.0.0.1]) (amavisd-new,\r
+       port 10024)\r
+       with ESMTP id dEDiqtirI3iO; Sat, 20 Mar 2010 11:21:44 +0100 (CET)\r
+Received: from blackspot (heal.cauterized.net [89.140.131.167])\r
+       by flatline.sindominio.net (Postfix) with ESMTPA id 505D9262E3D;\r
+       Sat, 20 Mar 2010 11:21:33 +0100 (CET)\r
+Received: by blackspot (Postfix, from userid 1000)\r
+       id 79F598BDFE; Sat, 20 Mar 2010 11:24:54 +0100 (CET)\r
+From: Ruben Pollan <meskio@sindominio.net>\r
+To: notmuch@notmuchmail.org\r
+Date: Sat, 20 Mar 2010 11:23:24 +0100\r
+Message-Id: <1269080605-5617-5-git-send-email-meskio@sindominio.net>\r
+X-Mailer: git-send-email 1.7.0\r
+In-Reply-To: <873a09jt2t.fsf@yoom.home.cworth.org>\r
+References: <873a09jt2t.fsf@yoom.home.cworth.org>\r
+Subject: [notmuch] [PATCH 4/5] Added backwards iterator to threads\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: Sat, 20 Mar 2010 10:21:51 -0000\r
+\r
+Added the functions notmuch_threads_move_to_prevoius,\r
+notmuch_threads_move_to_last and  notmuch_threads_move_to_first to\r
+notmuch library. With them is possible to iterate backwards on threads.\r
+\r
+* notmuch_threads_move_to_prevoius do the opposite than\r
+  notmuch_threads_move_to_next, getting the threads iterator one\r
+  position backwards.\r
+\r
+* notmuch_threads_move_to_last move the iterator to the first last thread.\r
+\r
+* notmuch_threads_move_to_first move the iterator to the first valid\r
+  thread.\r
+\r
+For it has been implemented notmuch_thread_list_t structur that stores\r
+the thread_ids so the backwards iteration gets the thread_id in the same\r
+order that was show on forward iteration.\r
+---\r
+ lib/notmuch.h |   28 +++++++++++\r
+ lib/query.cc  |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----\r
+ 2 files changed, 159 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 753f3bb..b96b624 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -466,6 +466,15 @@ notmuch_threads_valid (notmuch_threads_t *threads);\r
+ notmuch_thread_t *\r
+ notmuch_threads_get (notmuch_threads_t *threads);\r
\r
++/* Move the 'threads' iterator to the first thread.\r
++ *\r
++ * After that the 'threads' iterator will be set to the first valid \r
++ * thread, so it can be use to iterate with \r
++ * notmuch_threads_move_to_next.\r
++ */\r
++void\r
++notmuch_threads_move_to_first (notmuch_threads_t *threads);\r
++\r
+ /* Move the 'threads' iterator to the next thread.\r
+  *\r
+  * If 'threads' is already pointing at the last thread then the\r
+@@ -479,6 +488,25 @@ notmuch_threads_get (notmuch_threads_t *threads);\r
+ void\r
+ notmuch_threads_move_to_next (notmuch_threads_t *threads);\r
\r
++/* Move the 'threads' iterator to the last thread.\r
++ *\r
++ * After that the 'threads' iterator will be set to the last valid \r
++ * thread, so it can be use to iterate with \r
++ * notmuch_threads_move_to_previous.\r
++ */\r
++void\r
++notmuch_threads_move_to_last (notmuch_threads_t *threads);\r
++\r
++/* Move the 'threads' iterator to the previous thread.\r
++ *\r
++ * If 'threads' is already pointing at the first thread then the\r
++ * iterator will be moved to a point just beyond that first thread,\r
++ * (where notmuch_threads_valid will return FALSE and\r
++ * notmuch_threads_get will return NULL).\r
++ */\r
++void\r
++notmuch_threads_move_to_previous (notmuch_threads_t *threads);\r
++\r
+ /* Destroy a notmuch_threads_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 44950c1..39985e7 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -39,13 +39,25 @@ typedef struct _notmuch_mset_messages {\r
+     Xapian::MSetIterator iterator_end;\r
+ } notmuch_mset_messages_t;\r
\r
++typedef struct _notmuch_thread_node {\r
++    const char *thread_id;\r
++    struct _notmuch_thread_node *next;\r
++    struct _notmuch_thread_node *prev;\r
++} notmuch_thread_node_t;\r
++\r
++typedef struct _notmuch_thread_list {\r
++    notmuch_thread_node_t *head;\r
++    notmuch_thread_node_t *tail;\r
++    notmuch_thread_node_t *iterator;\r
++} notmuch_thread_list_t;\r
++\r
+ struct _notmuch_threads {\r
+     notmuch_query_t *query;\r
+     GHashTable *threads;\r
+     notmuch_messages_t *messages;\r
\r
+-    /* This thread ID is our iterator state. */\r
+-    const char *thread_id;\r
++    /* thread list with the thread_id of the showed messages */\r
++    notmuch_thread_list_t *list;\r
+ };\r
\r
+ notmuch_query_t *\r
+@@ -269,6 +281,64 @@ _notmuch_mset_messages_move_to_previous (notmuch_messages_t *messages)\r
+     }\r
+ }\r
\r
++static void\r
++_notmuch_thread_list_create (notmuch_thread_list_t *list, const char *thread_id)\r
++{\r
++    list->head = talloc (list, notmuch_thread_node_t);\r
++    list->tail = list->head;\r
++    list->iterator = list->head;\r
++    list->iterator->thread_id = thread_id;\r
++    list->iterator->next = NULL;\r
++    list->iterator->prev = NULL;\r
++}\r
++\r
++static void\r
++_notmuch_thread_list_append (notmuch_thread_list_t *list, const char *thread_id)\r
++{\r
++    list->tail->next = talloc (list, notmuch_thread_node_t);\r
++    list->iterator = list->tail->next;\r
++    list->iterator->thread_id = thread_id;\r
++    list->iterator->next = NULL;\r
++    list->iterator->prev = list->tail;\r
++    list->tail = list->iterator;\r
++}\r
++\r
++static const char *\r
++_notmuch_thread_list_get_id (notmuch_thread_list_t *list)\r
++{\r
++    return list->iterator->thread_id;\r
++}\r
++\r
++static notmuch_bool_t\r
++_notmuch_thread_list_valid (notmuch_thread_list_t *list)\r
++{\r
++    return (list->iterator != NULL);\r
++}\r
++\r
++static void\r
++_notmuch_thread_list_move_to_first (notmuch_thread_list_t *list)\r
++{\r
++    list->iterator = list->head;\r
++}\r
++\r
++static void\r
++_notmuch_thread_list_move_to_next (notmuch_thread_list_t *list)\r
++{\r
++    list->iterator = list->iterator->next;\r
++}\r
++\r
++static void\r
++_notmuch_thread_list_move_to_last (notmuch_thread_list_t *list)\r
++{\r
++    list->iterator = list->tail;\r
++}\r
++\r
++static void\r
++_notmuch_thread_list_move_to_previous (notmuch_thread_list_t *list)\r
++{\r
++    list->iterator = list->iterator->prev;\r
++}\r
++\r
+ /* Glib objects force use to use a talloc destructor as well, (but not\r
+  * nearly as ugly as the for messages due to C++ objects). At\r
+  * this point, I'd really like to have some talloc-friendly\r
+@@ -297,10 +367,13 @@ notmuch_query_search_threads (notmuch_query_t *query)\r
\r
+     threads->messages = notmuch_query_search_messages (query);\r
\r
++    threads->list = talloc (threads, notmuch_thread_list_t);\r
+     message = notmuch_messages_get (threads->messages);\r
+-    threads->thread_id = notmuch_message_get_thread_id (message);\r
++    _notmuch_thread_list_create (threads->list,\r
++                                  notmuch_message_get_thread_id (message));\r
+     g_hash_table_insert (threads->threads,\r
+-                         xstrdup (threads->thread_id), NULL);\r
++                         xstrdup (_notmuch_thread_list_get_id (threads->list)),\r
++                         NULL);\r
\r
+     talloc_set_destructor (threads, _notmuch_threads_destructor);\r
\r
+@@ -316,7 +389,7 @@ notmuch_query_destroy (notmuch_query_t *query)\r
+ notmuch_bool_t\r
+ notmuch_threads_valid (notmuch_threads_t *threads)\r
+ {\r
+-    return (threads->thread_id != NULL);\r
++    return _notmuch_thread_list_valid (threads->list);\r
+ }\r
\r
+ notmuch_thread_t *\r
+@@ -327,37 +400,83 @@ notmuch_threads_get (notmuch_threads_t *threads)\r
\r
+     return _notmuch_thread_create (threads->query,\r
+                                  threads->query->notmuch,\r
+-                                 threads->thread_id,\r
++                                 _notmuch_thread_list_get_id (threads->list),\r
+                                  threads->query->query_string);\r
+ }\r
\r
+ void\r
++notmuch_threads_move_to_first (notmuch_threads_t *threads)\r
++{\r
++    _notmuch_thread_list_move_to_first (threads->list);\r
++}\r
++\r
++void\r
+ notmuch_threads_move_to_next (notmuch_threads_t *threads)\r
+ {\r
+-    notmuch_message_t *message;\r
++    if (!_notmuch_thread_list_valid (threads->list))\r
++        return;\r
++\r
++    _notmuch_thread_list_move_to_next (threads->list);\r
++    if (_notmuch_thread_list_valid (threads->list))\r
++        return;\r
\r
+     while (notmuch_messages_valid (threads->messages))\r
+     {\r
+-      message = notmuch_messages_get (threads->messages);\r
++        notmuch_message_t *message;\r
++        const char *thread_id;\r
\r
+-      threads->thread_id = notmuch_message_get_thread_id (message);\r
++      message = notmuch_messages_get (threads->messages);\r
++      thread_id = notmuch_message_get_thread_id (message);\r
\r
+       if (! g_hash_table_lookup_extended (threads->threads,\r
+-                                          threads->thread_id,\r
++                                          thread_id,\r
+                                           NULL, NULL))\r
+       {\r
+           g_hash_table_insert (threads->threads,\r
+-                               xstrdup (threads->thread_id), NULL);\r
++                               xstrdup (thread_id), NULL);\r
++            _notmuch_thread_list_append (threads->list, thread_id);\r
+           notmuch_messages_move_to_next (threads->messages);\r
+           return;\r
+       }\r
\r
+       notmuch_messages_move_to_next (threads->messages);\r
+     }\r
++}\r
++\r
++void\r
++notmuch_threads_move_to_last (notmuch_threads_t *threads)\r
++{\r
++    _notmuch_thread_list_move_to_last (threads->list);\r
++\r
++    while (notmuch_messages_valid (threads->messages))\r
++    {\r
++        notmuch_message_t *message;\r
++        const char *thread_id;\r
++\r
++      message = notmuch_messages_get (threads->messages);\r
++      thread_id = notmuch_message_get_thread_id (message);\r
++\r
++      if (! g_hash_table_lookup_extended (threads->threads,\r
++                                          thread_id,\r
++                                          NULL, NULL))\r
++      {\r
++          g_hash_table_insert (threads->threads,\r
++                               xstrdup (thread_id), NULL);\r
++            _notmuch_thread_list_append (threads->list, thread_id);\r
++        }\r
\r
+-    threads->thread_id = NULL;\r
++      notmuch_messages_move_to_next (threads->messages);\r
++    }\r
+ }\r
\r
++void\r
++notmuch_threads_move_to_previous (notmuch_threads_t *threads)\r
++{\r
++    if (!_notmuch_thread_list_valid (threads->list))\r
++        return;\r
++\r
++    _notmuch_thread_list_move_to_previous (threads->list);\r
++}\r
\r
+ void\r
+ notmuch_threads_destroy (notmuch_threads_t *threads)\r
+-- \r
+1.7.0\r
+\r