--- /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 5FC666DE18A8\r
+ for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:43 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.033\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.033 tagged_above=-999 required=5\r
+ tests=[AWL=-0.022, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ 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 sesmktKokXHd for <notmuch@notmuchmail.org>;\r
+ Sat, 12 Mar 2016 04:31:40 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 3E95B6DE13EF\r
+ for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:39 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aeiiN-0004zC-U6; Sat, 12 Mar 2016 07:32:15 -0500\r
+Received: (nullmailer pid 17175 invoked by uid 1000);\r
+ Sat, 12 Mar 2016 12:31:33 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: [PATCH 2/6] lib: config list iterators\r
+Date: Sat, 12 Mar 2016 08:31:26 -0400\r
+Message-Id: <1457785890-17058-3-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.7.0\r
+In-Reply-To: <1457785890-17058-1-git-send-email-david@tethera.net>\r
+References: <1453561198-2893-1-git-send-email-david@tethera.net>\r
+ <1457785890-17058-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: Sat, 12 Mar 2016 12:31:43 -0000\r
+\r
+Since xapian provides the ability to restrict the iterator to a given\r
+prefix, we expose this ability to the user. Otherwise we mimic the other\r
+iterator interfances in notmuch (e.g. tags.c).\r
+---\r
+ lib/config.cc | 104 +++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch.h | 44 +++++++++++++++++++++\r
+ test/T590-libconfig.sh | 60 ++++++++++++++++++++++++++++\r
+ 3 files changed, 208 insertions(+)\r
+\r
+diff --git a/lib/config.cc b/lib/config.cc\r
+index af00d6f..e581f32 100644\r
+--- a/lib/config.cc\r
++++ b/lib/config.cc\r
+@@ -24,6 +24,19 @@\r
+ \r
+ static const std::string CONFIG_PREFIX="C";\r
+ \r
++struct _notmuch_config_list {\r
++ notmuch_database_t *notmuch;\r
++ Xapian::TermIterator *iterator;\r
++ char *current_key;\r
++ char *current_val;\r
++};\r
++\r
++static int\r
++_notmuch_config_list_destroy (notmuch_config_list_t *list) {\r
++ delete list->iterator;\r
++ return 0;\r
++}\r
++\r
+ notmuch_status_t\r
+ notmuch_database_set_config (notmuch_database_t *notmuch,\r
+ const char *key,\r
+@@ -88,3 +101,94 @@ notmuch_database_get_config (notmuch_database_t *notmuch,\r
+ \r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
++\r
++notmuch_status_t\r
++notmuch_database_get_config_list (notmuch_database_t *notmuch,\r
++ const char *prefix,\r
++ notmuch_config_list_t **out)\r
++{\r
++ notmuch_config_list_t *list = NULL;\r
++ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
++\r
++ list = talloc (notmuch, notmuch_config_list_t);\r
++ if (!list) {\r
++ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
++\r
++ talloc_set_destructor (list, _notmuch_config_list_destroy);\r
++ list->iterator = new Xapian::TermIterator;\r
++ list->notmuch = notmuch;\r
++ list->current_key = NULL;\r
++ list->current_val = NULL;\r
++\r
++ try {\r
++\r
++ *list->iterator = notmuch->xapian_db->metadata_keys_begin (CONFIG_PREFIX + (prefix ? 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_config_list_valid (notmuch_config_list_t *metadata)\r
++{\r
++ if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end())\r
++ return FALSE;\r
++\r
++ return TRUE;\r
++}\r
++\r
++const char *\r
++notmuch_config_list_key (notmuch_config_list_t *list)\r
++{\r
++ if (list->current_key)\r
++ talloc_free (list->current_key);\r
++\r
++ list->current_key = talloc_strdup (list, (**(list->iterator)).c_str () + CONFIG_PREFIX.length ());\r
++\r
++ return list->current_key;\r
++}\r
++\r
++const char *\r
++notmuch_config_list_value (notmuch_config_list_t *list)\r
++{\r
++ std::string strval;\r
++ notmuch_status_t status;\r
++ const char *key = notmuch_config_list_key (list);\r
++\r
++ /* TODO: better error reporting?? */\r
++ status = _metadata_value (list->notmuch, key, strval);\r
++ if (status)\r
++ return NULL;\r
++\r
++ if (list->current_val)\r
++ talloc_free(list->current_val);\r
++\r
++ list->current_val = talloc_strdup(list, strval.c_str ());\r
++ return list->current_val;\r
++}\r
++\r
++void\r
++notmuch_config_list_move_to_next (notmuch_config_list_t *list)\r
++{\r
++ (*(list->iterator))++;\r
++}\r
++\r
++void\r
++notmuch_config_list_destroy (notmuch_config_list_t *list)\r
++{\r
++ talloc_free (list);\r
++}\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index c62223b..b439c88 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_config_list notmuch_config_list_t;\r
+ #endif /* __DOXYGEN__ */\r
+ \r
+ /**\r
+@@ -1849,6 +1850,49 @@ notmuch_database_set_config (notmuch_database_t *db, const char *key, const char\r
+ notmuch_status_t\r
+ notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);\r
+ \r
++/**\r
++ * Create an iterator for all config items with keys matching a given prefix\r
++ */\r
++notmuch_status_t\r
++notmuch_database_get_config_list (notmuch_database_t *db, const char *prefix, notmuch_config_list_t **out);\r
++\r
++/**\r
++ * Is 'config_list' iterator valid (i.e. _key, _value, _move_to_next can be called).\r
++ */\r
++notmuch_bool_t\r
++notmuch_config_list_valid (notmuch_config_list_t *config_list);\r
++\r
++/**\r
++ * return key for current config pair\r
++ *\r
++ * return value is owned by the iterator, and will be destroyed by the\r
++ * next call to notmuch_config_list_key or notmuch_config_list_destroy.\r
++ */\r
++const char *\r
++notmuch_config_list_key (notmuch_config_list_t *config_list);\r
++\r
++/**\r
++ * return 'value' for current config pair\r
++ *\r
++ * return value is owned by the iterator, and will be destroyed by the\r
++ * next call to notmuch_config_list_value or notmuch config_list_destroy\r
++ */\r
++const char *\r
++notmuch_config_list_value (notmuch_config_list_t *config_list);\r
++\r
++\r
++/**\r
++ * move 'config_list' iterator to the next pair\r
++ */\r
++void\r
++notmuch_config_list_move_to_next (notmuch_config_list_t *config_list);\r
++\r
++/**\r
++ * free any resources held by 'config_list'\r
++ */\r
++void\r
++notmuch_config_list_destroy (notmuch_config_list_t *config_list);\r
++\r
+ /* @} */\r
+ \r
+ NOTMUCH_END_DECLS\r
+diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
+index 85e4497..8ca6883 100755\r
+--- a/test/T590-libconfig.sh\r
++++ b/test/T590-libconfig.sh\r
+@@ -55,4 +55,64 @@ testkey2 = testvalue2\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++\r
++test_begin_subtest "notmuch_database_get_config_list: empty list"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_config_list_t *list;\r
++ RUN(notmuch_database_get_config_list (db, "nonexistent", &list));\r
++ printf("valid = %d\n", notmuch_config_list_valid (list));\r
++ notmuch_config_list_destroy (list);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++valid = 0\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++\r
++test_begin_subtest "notmuch_database_get_config_list: all pairs"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_config_list_t *list;\r
++ RUN(notmuch_database_set_config (db, "zzzafter", "afterval"));\r
++ RUN(notmuch_database_set_config (db, "aaabefore", "beforeval"));\r
++ RUN(notmuch_database_get_config_list (db, "", &list));\r
++ for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {\r
++ printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));\r
++ }\r
++ notmuch_config_list_destroy (list);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++aaabefore beforeval\r
++testkey1 testvalue1\r
++testkey2 testvalue2\r
++zzzafter afterval\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++test_begin_subtest "notmuch_database_get_config_list: one prefix"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_config_list_t *list;\r
++ RUN(notmuch_database_get_config_list (db, "testkey", &list));\r
++ for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {\r
++ printf("%s %s\n", notmuch_config_list_key (list), notmuch_config_list_value(list));\r
++ }\r
++ notmuch_config_list_destroy (list);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++testkey1 testvalue1\r
++testkey2 testvalue2\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.7.0\r
+\r