--- /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 A88E66DE1502\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:51:59 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.312\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.312 tagged_above=-999 required=5 tests=[AWL=0.239,\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 tcLveYXDaGuQ for <notmuch@notmuchmail.org>;\r
+ Sat, 9 Jan 2016 18:51:56 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 5FBA36DE02CB\r
+ for <notmuch@notmuchmail.org>; Sat, 9 Jan 2016 18:51:54 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aI66a-0007DL-UC; Sat, 09 Jan 2016 21:51:44 -0500\r
+Received: (nullmailer pid 29638 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 1/9] lib: initial API for prefixed metadata\r
+Date: Sat, 9 Jan 2016 22:51:33 -0400\r
+Message-Id: <1452394301-29499-2-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
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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:59 -0000\r
+\r
+Start with get and set of a single key\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/metadata.cc | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch.h | 25 +++++++++\r
+ test/T590-metadata.sh | 58 +++++++++++++++++++\r
+ 4 files changed, 234 insertions(+)\r
+ create mode 100644 lib/metadata.cc\r
+ create mode 100755 test/T590-metadata.sh\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 3a07090..ccc1e49 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -48,6 +48,7 @@ libnotmuch_cxx_srcs = \\r
+ $(dir)/index.cc \\r
+ $(dir)/message.cc \\r
+ $(dir)/query.cc \\r
++ $(dir)/metadata.cc \\r
+ $(dir)/thread.cc\r
+ \r
+ libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)\r
+diff --git a/lib/metadata.cc b/lib/metadata.cc\r
+new file mode 100644\r
+index 0000000..a068ed1\r
+--- /dev/null\r
++++ b/lib/metadata.cc\r
+@@ -0,0 +1,150 @@\r
++/* metadata.cc - API for database metadata\r
++ *\r
++ * Copyright © 2015 David Bremner\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: David Bremner <david@tethera.net>\r
++ */\r
++\r
++#include "notmuch.h"\r
++#include "notmuch-private.h"\r
++#include "database-private.h"\r
++\r
++static\r
++const char *\r
++_find_metadata_prefix (notmuch_metadata_class_t mclass)\r
++{\r
++ switch (mclass) {\r
++ case NOTMUCH_METADATA_CONFIG:\r
++ return "C";\r
++ default:\r
++ return NULL;\r
++ }\r
++}\r
++\r
++notmuch_status_t _make_key(void *ctx, notmuch_metadata_class_t mclass,\r
++ const char *in, const char **out)\r
++{\r
++ const char *term;\r
++ const char *prefix = NULL;\r
++\r
++ if (!out)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ prefix = _find_metadata_prefix(mclass);\r
++ if (!prefix)\r
++ return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;\r
++\r
++ term = talloc_asprintf (ctx, "%s%s",\r
++ prefix, in);\r
++ if (!term)\r
++ return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++\r
++ *out = term;\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_database_set_metadata (notmuch_database_t *notmuch,\r
++ notmuch_metadata_class_t mclass,\r
++ const char *key,\r
++ const char *value)\r
++{\r
++ notmuch_status_t status;\r
++ Xapian::WritableDatabase *db;\r
++ const char *key_term = NULL;\r
++ void *local;\r
++\r
++ local = talloc_new (NULL);\r
++\r
++ status = _notmuch_database_ensure_writable (notmuch);\r
++ if (status)\r
++ goto DONE;\r
++\r
++ status = _make_key (local, mclass, key, &key_term);\r
++ if (status)\r
++ goto DONE;\r
++\r
++ try {\r
++ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
++ db->set_metadata (key_term, value);\r
++ } catch (const Xapian::Error &error) {\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ notmuch->exception_reported = TRUE;\r
++ if (! notmuch->exception_reported) {\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\r
++ }\r
++ DONE:\r
++ talloc_free (local);\r
++\r
++ return status;\r
++}\r
++\r
++static notmuch_status_t\r
++_metadata_value (notmuch_database_t *notmuch,\r
++ notmuch_metadata_class_t mclass,\r
++ const char *key,\r
++ std::string &value)\r
++{\r
++ notmuch_status_t status;\r
++\r
++ const char *key_term = NULL;\r
++ void *local;\r
++\r
++ local = talloc_new (NULL);\r
++\r
++ status = _make_key (local, mclass, key, &key_term);\r
++ if (status)\r
++ goto DONE;\r
++\r
++ try {\r
++\r
++ value = notmuch->xapian_db->get_metadata (key_term);\r
++\r
++ } catch (const Xapian::Error &error) {\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ notmuch->exception_reported = TRUE;\r
++ if (! notmuch->exception_reported) {\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\r
++ }\r
++\r
++ DONE:\r
++ talloc_free (local);\r
++ return status;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_database_get_metadata (notmuch_database_t *notmuch,\r
++ notmuch_metadata_class_t mclass,\r
++ const char *key,\r
++ char **value) {\r
++ std::string strval;\r
++ notmuch_status_t status;\r
++\r
++ if (!value)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ status = _metadata_value (notmuch, mclass, key, strval);\r
++ if (status)\r
++ return status;\r
++\r
++ *value = strdup (strval.c_str ());\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 310a8b8..448f405 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -1829,6 +1829,31 @@ notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);\r
+ void\r
+ notmuch_filenames_destroy (notmuch_filenames_t *filenames);\r
+ \r
++/**\r
++ * metadata class\r
++ */\r
++\r
++typedef enum _notmuch_metadata_class {\r
++ NOTMUCH_METADATA_FIRST_CLASS = 1,\r
++ NOTMUCH_METADATA_CONFIG = 1,\r
++ NOTMUCH_METADATA_LAST_CLASS\r
++} notmuch_metadata_class_t;\r
++\r
++/**\r
++ * set metadata\r
++ *\r
++ */\r
++notmuch_status_t\r
++notmuch_database_set_metadata (notmuch_database_t *db, notmuch_metadata_class_t mclass, const char *key, const char *value);\r
++\r
++/**\r
++ * retrieve one metadata value\r
++ *\r
++ * return value is allocated by malloc and should be freed by the caller.\r
++ */\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
+ \r
+ NOTMUCH_END_DECLS\r
+diff --git a/test/T590-metadata.sh b/test/T590-metadata.sh\r
+new file mode 100755\r
+index 0000000..29aeaa2\r
+--- /dev/null\r
++++ b/test/T590-metadata.sh\r
+@@ -0,0 +1,58 @@\r
++#!/usr/bin/env bash\r
++test_description="metadata API"\r
++\r
++. ./test-lib.sh || exit 1\r
++\r
++add_email_corpus\r
++\r
++cat <<EOF > c_head\r
++#include <stdio.h>\r
++#include <string.h>\r
++#include <stdlib.h>\r
++#include <notmuch.h>\r
++\r
++void run(int line, notmuch_status_t ret)\r
++{\r
++ if (ret) {\r
++ fprintf (stderr, "line %d: %s\n", line, ret);\r
++ exit (1);\r
++ }\r
++}\r
++\r
++#define RUN(v) run(__LINE__, v);\r
++\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ char *val;\r
++ notmuch_status_t stat;\r
++\r
++ RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
++\r
++EOF\r
++\r
++cat <<EOF > c_tail\r
++ RUN(notmuch_database_destroy(db));\r
++}\r
++EOF\r
++\r
++test_begin_subtest "notmuch_database_{set,get}_metadata"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ RUN(notmuch_database_set_metadata (db, NOTMUCH_METADATA_CONFIG, "testkey1", "testvalue1"));\r
++ RUN(notmuch_database_set_metadata (db, NOTMUCH_METADATA_CONFIG, "testkey2", "testvalue2"));\r
++ RUN(notmuch_database_get_metadata (db, NOTMUCH_METADATA_CONFIG, "testkey1", &val));\r
++ printf("testkey1 = %s\n", val);\r
++ RUN(notmuch_database_get_metadata (db, NOTMUCH_METADATA_CONFIG, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\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.6.4\r
+\r