--- /dev/null
+Return-Path: <bremner@tethera.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 arlo.cworth.org (Postfix) with ESMTP id 5A92B6DE02C9\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:51:56 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.311\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.311 tagged_above=-999 required=5 tests=[AWL=0.240,\r
+ RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id BoS80j-V-ETt for <notmuch@notmuchmail.org>;\r
+ Sat, 9 Jan 2016 18:51:53 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 79B206DE028A\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:51:53 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aI66Y-0007Cy-PY; Sat, 09 Jan 2016 21:51:42 -0500\r
+Received: (nullmailer pid 29640 invoked by uid 1000);\r
+ Sun, 10 Jan 2016 02:51:47 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP patch 2/9] lib: notmuch_metadata_t: iterators for metadata\r
+Date: Sat, 9 Jan 2016 22:51:34 -0400\r
+Message-Id: <1452394301-29499-3-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.6.4\r
+In-Reply-To: <1452394301-29499-1-git-send-email-david@tethera.net>\r
+References: <1452394301-29499-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 10 Jan 2016 02:51:56 -0000\r
+\r
+The rough idea is to give a C interface to the Xapian::TermIterator class.\r
+---\r
+ lib/metadata.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch.h | 12 ++++++++++\r
+ test/T590-metadata.sh | 15 ++++++++++++\r
+ 3 files changed, 90 insertions(+)\r
+\r
+diff --git a/lib/metadata.cc b/lib/metadata.cc\r
+index a068ed1..5d319c5 100644\r
+--- a/lib/metadata.cc\r
++++ b/lib/metadata.cc\r
+@@ -22,6 +22,17 @@\r
+ #include "notmuch-private.h"\r
+ #include "database-private.h"\r
+ \r
++struct _notmuch_metadata {\r
++ notmuch_database_t *notmuch;\r
++ Xapian::TermIterator *iterator;\r
++ notmuch_metadata_class_t mclass;\r
++};\r
++\r
++static int\r
++_notmuch_metadata_destroy (notmuch_metadata_t *list) {\r
++ delete list->iterator;\r
++ return 0;\r
++}\r
+ static\r
+ const char *\r
+ _find_metadata_prefix (notmuch_metadata_class_t mclass)\r
+@@ -148,3 +159,55 @@ notmuch_database_get_metadata (notmuch_database_t *notmuch,\r
+ \r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
++\r
++notmuch_status_t\r
++notmuch_database_get_all_metadata (notmuch_database_t *notmuch,\r
++ notmuch_metadata_class_t mclass,\r
++ notmuch_metadata_t **out)\r
++{\r
++ notmuch_metadata_t *list = NULL;\r
++ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
++\r
++ const char *prefix = _find_metadata_prefix (mclass);\r
++\r
++ list = talloc (notmuch, notmuch_metadata_t);\r
++ if (!list) {\r
++ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
++\r
++ talloc_set_destructor(list, _notmuch_metadata_destroy);\r
++ list->iterator = new Xapian::TermIterator;\r
++ list->notmuch = notmuch;\r
++ list->mclass = mclass;\r
++\r
++ try {\r
++\r
++ *list->iterator = notmuch->xapian_db->metadata_keys_begin();\r
++ list->iterator->skip_to (prefix);\r
++\r
++ } catch (const Xapian::Error &error) {\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",\r
++ error.get_msg().c_str());\r
++ notmuch->exception_reported = TRUE;\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ }\r
++\r
++ *out = list;\r
++\r
++ DONE:\r
++ if (status && list)\r
++ talloc_free (list);\r
++\r
++ return status;\r
++}\r
++\r
++notmuch_bool_t\r
++notmuch_metadata_valid (notmuch_metadata_t *metadata)\r
++{\r
++ const char *prefix = _find_metadata_prefix (metadata->mclass);\r
++ if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end())\r
++ return FALSE;\r
++\r
++ return (strncmp((**(metadata->iterator)).c_str (), prefix, strlen (prefix)) == 0);\r
++}\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 448f405..a2b7040 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -197,6 +197,7 @@ typedef struct _notmuch_message notmuch_message_t;\r
+ typedef struct _notmuch_tags notmuch_tags_t;\r
+ typedef struct _notmuch_directory notmuch_directory_t;\r
+ typedef struct _notmuch_filenames notmuch_filenames_t;\r
++typedef struct _notmuch_metadata notmuch_metadata_t;\r
+ #endif /* __DOXYGEN__ */\r
+ \r
+ /**\r
+@@ -1854,6 +1855,17 @@ notmuch_database_set_metadata (notmuch_database_t *db, notmuch_metadata_class_t\r
+ notmuch_status_t\r
+ notmuch_database_get_metadata (notmuch_database_t *db, notmuch_metadata_class_t mclass, const char *key, char **value);\r
+ \r
++/**\r
++ * get all metadata of a given class\r
++ */\r
++notmuch_status_t\r
++notmuch_database_get_all_metadata (notmuch_database_t *db, notmuch_metadata_class_t mclass, notmuch_metadata_t **out);\r
++\r
++/**\r
++ * Is 'metadata' iterator valid (i.e. _key, _value, _move_to_next can be called).\r
++ */\r
++notmuch_bool_t\r
++notmuch_metadata_valid (notmuch_metadata_t *metadata);\r
+ /* @} */\r
+ \r
+ NOTMUCH_END_DECLS\r
+diff --git a/test/T590-metadata.sh b/test/T590-metadata.sh\r
+index 29aeaa2..c36a7d7 100755\r
+--- a/test/T590-metadata.sh\r
++++ b/test/T590-metadata.sh\r
+@@ -55,4 +55,19 @@ testkey2 = testvalue2\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++test_begin_subtest "notmuch_database_get_all_metadata initially valid"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_metadata_t *meta;\r
++ RUN(notmuch_database_get_all_metadata (db, NOTMUCH_METADATA_CONFIG, &meta));\r
++ printf("valid = %d\n", notmuch_metadata_valid (meta));\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++valid = 1\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.6.4\r
+\r