--- /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 341046DE0222\r
+ for <notmuch@notmuchmail.org>; Sun, 22 May 2016 07:29:29 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.012\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
+ tests=[AWL=-0.001, 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 3RVPcHjpcTy0 for <notmuch@notmuchmail.org>;\r
+ Sun, 22 May 2016 07:29:19 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 244996DE02C9\r
+ for <notmuch@notmuchmail.org>; Sun, 22 May 2016 07:29:12 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1b4UNL-00051b-Gz; Sun, 22 May 2016 10:29:03 -0400\r
+Received: (nullmailer pid 5523 invoked by uid 1000);\r
+ Sun, 22 May 2016 14:29:10 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC patch 2/2] RFC message-property API\r
+Date: Sun, 22 May 2016 11:28:59 -0300\r
+Message-Id: <1463927339-5441-3-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1463927339-5441-1-git-send-email-david@tethera.net>\r
+References: <1463927339-5441-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, 22 May 2016 14:29:29 -0000\r
+\r
+"properties" are (key,value) pairs that can be attached to messages with\r
+multiple instances of the same key being allowed.\r
+\r
+In this initial prototype, we ignore the existing caching mechanism for\r
+message metadata; worse, we invalidate the cache needlessly.\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/database.cc | 1 +\r
+ lib/message-private.h | 29 ++++++\r
+ lib/message-property.cc | 214 ++++++++++++++++++++++++++++++++++++++++++\r
+ lib/message.cc | 25 +----\r
+ lib/notmuch.h | 33 +++++++\r
+ test/T610-message-property.sh | 148 +++++++++++++++++++++++++++++\r
+ 7 files changed, 427 insertions(+), 24 deletions(-)\r
+ create mode 100644 lib/message-private.h\r
+ create mode 100644 lib/message-property.cc\r
+ create mode 100755 test/T610-message-property.sh\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index beb9635..1f55542 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -48,6 +48,7 @@ libnotmuch_cxx_srcs = \\r
+ $(dir)/directory.cc \\r
+ $(dir)/index.cc \\r
+ $(dir)/message.cc \\r
++ $(dir)/message-property.cc \\r
+ $(dir)/query.cc \\r
+ $(dir)/query-fp.cc \\r
+ $(dir)/config.cc \\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 9630000..3ee5a12 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -252,6 +252,7 @@ static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {\r
+ * colon, which keeps our own logic simpler.\r
+ */\r
+ { "folder", "XFOLDER:" },\r
++ { "property", "XPROPERTY:"},\r
+ };\r
+ \r
+ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+diff --git a/lib/message-private.h b/lib/message-private.h\r
+new file mode 100644\r
+index 0000000..77a0b3d\r
+--- /dev/null\r
++++ b/lib/message-private.h\r
+@@ -0,0 +1,29 @@\r
++#ifndef MESSAGE_PRIVATE_H\r
++#define MESSAGE_PRIVATE_H\r
++\r
++struct visible _notmuch_message {\r
++ notmuch_database_t *notmuch;\r
++ Xapian::docid doc_id;\r
++ int frozen;\r
++ char *message_id;\r
++ char *thread_id;\r
++ char *in_reply_to;\r
++ notmuch_string_list_t *tag_list;\r
++ notmuch_string_list_t *filename_term_list;\r
++ notmuch_string_list_t *filename_list;\r
++ char *author;\r
++ notmuch_message_file_t *message_file;\r
++ notmuch_message_list_t *replies;\r
++ unsigned long flags;\r
++ /* For flags that are initialized on-demand, lazy_flags indicates\r
++ * if each flag has been initialized. */\r
++ unsigned long lazy_flags;\r
++\r
++ /* Message document modified since last sync */\r
++ notmuch_bool_t modified;\r
++\r
++ Xapian::Document doc;\r
++ Xapian::termcount termpos;\r
++};\r
++\r
++#endif\r
+diff --git a/lib/message-property.cc b/lib/message-property.cc\r
+new file mode 100644\r
+index 0000000..3e8293a\r
+--- /dev/null\r
++++ b/lib/message-property.cc\r
+@@ -0,0 +1,214 @@\r
++/* message-property.cc - Properties are like tags, but (key,value) pairs.\r
++ * keys are allowed to repeat.\r
++ *\r
++ * This file is part of notmuch.\r
++ *\r
++ * Copyright © 2016 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-private.h"\r
++#include "database-private.h"\r
++#include "message-private.h"\r
++\r
++struct _notmuch_message_property_list {\r
++ Xapian::TermIterator *iterator;\r
++ notmuch_message_t *message;\r
++ const char *prefix;\r
++ char *current;\r
++};\r
++\r
++static int\r
++_notmuch_message_property_list_destroy (notmuch_message_property_list_t *list)\r
++{\r
++ delete list->iterator;\r
++ return 0;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value)\r
++{\r
++ notmuch_private_status_t private_status;\r
++ char *term = NULL;\r
++\r
++ if (! value)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ term = talloc_asprintf (message, "%s%s=",\r
++ _find_prefix ("property"), key);\r
++ if (term == NULL) {\r
++ private_status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
++\r
++ private_status = _notmuch_message_get_prefixed_term (message, term, value);\r
++ if (private_status == NOTMUCH_PRIVATE_STATUS_NO_TERM_FOUND) {\r
++ *value = NULL;\r
++ private_status = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++ }\r
++\r
++ DONE:\r
++ if (term)\r
++ talloc_free (term);\r
++\r
++ if (private_status)\r
++ return COERCE_STATUS (private_status,\r
++ "Unhandled error retrieving message property");\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++static notmuch_status_t\r
++_notmuch_message_modify_property (notmuch_message_t *message, const char *key, const char *value,\r
++ notmuch_bool_t delete_it)\r
++{\r
++ notmuch_private_status_t private_status;\r
++ notmuch_status_t status;\r
++ char *term = NULL;\r
++\r
++ status = _notmuch_database_ensure_writable (_notmuch_message_database (message));\r
++ if (status)\r
++ return status;\r
++\r
++ if (key == NULL || value== NULL)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ if (index (key, '=') || index (value, '='))\r
++ return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;\r
++\r
++ term = talloc_asprintf (message, "%s=%s", key, value);\r
++\r
++ /* this invalidates all of the property, which it currently shouldn't */\r
++ if (delete_it)\r
++ private_status = _notmuch_message_remove_term (message, "property", term);\r
++ else\r
++ private_status = _notmuch_message_add_term (message, "property", term);\r
++\r
++ if (private_status)\r
++ return COERCE_STATUS (private_status,\r
++ "Unhandled error modifying message property");\r
++\r
++ _notmuch_message_sync (message);\r
++\r
++ if (term)\r
++ talloc_free (term);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value)\r
++{\r
++ return _notmuch_message_modify_property (message, key, value, FALSE);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value)\r
++{\r
++ return _notmuch_message_modify_property (message, key, value, TRUE);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_message_get_property_list (notmuch_message_t *message,\r
++ const char *key,\r
++ notmuch_message_property_list_t **out)\r
++{\r
++ notmuch_message_property_list_t *list = NULL;\r
++ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
++ notmuch_database_t *notmuch = _notmuch_message_database (message);\r
++\r
++ const char *term;\r
++\r
++ list = talloc (message, notmuch_message_property_list_t);\r
++ if (! list) {\r
++ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
++\r
++ talloc_set_destructor (list, _notmuch_message_property_list_destroy);\r
++ list->current = NULL;\r
++ list->message = message;\r
++\r
++\r
++ term = talloc_asprintf (message, "%s%s=",\r
++ _find_prefix ("property"), key);\r
++\r
++ list->prefix = talloc_strdup (list, term);\r
++\r
++ if (term == NULL) {\r
++ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
++\r
++ try {\r
++\r
++ /* force copying onto the heap */\r
++ list->iterator = new Xapian::TermIterator;\r
++ *list->iterator = message->doc.termlist_begin ();\r
++\r
++ (*list->iterator).skip_to (term);\r
++\r
++ } catch (const Xapian::Error &error) {\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred getting property 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_message_property_list_valid (notmuch_message_property_list_t *list)\r
++{\r
++ if (*(list->iterator) == list->message->doc.termlist_end ())\r
++ return FALSE;\r
++\r
++ if (strncmp (list->prefix, (**list->iterator).c_str (), strlen(list->prefix)) != 0)\r
++ return FALSE;\r
++\r
++ return TRUE;\r
++}\r
++\r
++const char *\r
++notmuch_message_property_list_value (notmuch_message_property_list_t *list)\r
++{\r
++\r
++ if (list->current)\r
++ talloc_free (list->current);\r
++\r
++ list->current = talloc_strdup (list, (**list->iterator).c_str () + strlen (list->prefix));\r
++\r
++ return list->current;\r
++}\r
++\r
++void\r
++notmuch_message_property_list_move_to_next (notmuch_message_property_list_t *list)\r
++{\r
++ (*(list->iterator))++;\r
++}\r
++\r
++void\r
++notmuch_message_property_list_destroy (notmuch_message_property_list_t *list)\r
++{\r
++ talloc_free (list);\r
++}\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 90c2bb2..cc9ba93 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -25,30 +25,7 @@\r
+ \r
+ #include <gmime/gmime.h>\r
+ \r
+-struct visible _notmuch_message {\r
+- notmuch_database_t *notmuch;\r
+- Xapian::docid doc_id;\r
+- int frozen;\r
+- char *message_id;\r
+- char *thread_id;\r
+- char *in_reply_to;\r
+- notmuch_string_list_t *tag_list;\r
+- notmuch_string_list_t *filename_term_list;\r
+- notmuch_string_list_t *filename_list;\r
+- char *author;\r
+- notmuch_message_file_t *message_file;\r
+- notmuch_message_list_t *replies;\r
+- unsigned long flags;\r
+- /* For flags that are initialized on-demand, lazy_flags indicates\r
+- * if each flag has been initialized. */\r
+- unsigned long lazy_flags;\r
+-\r
+- /* Message document modified since last sync */\r
+- notmuch_bool_t modified;\r
+-\r
+- Xapian::Document doc;\r
+- Xapian::termcount termpos;\r
+-};\r
++#include <message-private.h>\r
+ \r
+ #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
+ \r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index bd977c3..4a1519b 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -180,6 +180,11 @@ typedef enum _notmuch_status {\r
+ */\r
+ NOTMUCH_STATUS_PATH_ERROR,\r
+ /**\r
++ * One of the arguments violates the preconditions for the\r
++ * function, in a way not covered by a more specific argument.\r
++ */\r
++ NOTMUCH_STATUS_ILLEGAL_ARGUMENT,\r
++ /**\r
+ * Not an actual status value. Just a way to find out how many\r
+ * valid status values there are.\r
+ */\r
+@@ -1651,6 +1656,34 @@ notmuch_message_thaw (notmuch_message_t *message);\r
+ void\r
+ notmuch_message_destroy (notmuch_message_t *message);\r
+ \r
++notmuch_status_t\r
++notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value);\r
++\r
++notmuch_status_t\r
++notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
++notmuch_status_t\r
++notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value);\r
++\r
++typedef struct _notmuch_message_property_list notmuch_message_property_list_t;\r
++\r
++notmuch_status_t\r
++notmuch_message_get_property_list (notmuch_message_t *message,\r
++ const char *key,\r
++ notmuch_message_property_list_t **out);\r
++\r
++notmuch_bool_t\r
++notmuch_message_property_list_valid (notmuch_message_property_list_t *metadata);\r
++\r
++void\r
++notmuch_message_property_list_move_to_next (notmuch_message_property_list_t *list);\r
++\r
++void\r
++notmuch_message_property_list_destroy (notmuch_message_property_list_t *list);\r
++\r
++const char *\r
++notmuch_message_property_list_value (notmuch_message_property_list_t *list);\r
++\r
+ /**\r
+ * Is the given 'tags' iterator pointing at a valid tag.\r
+ *\r
+diff --git a/test/T610-message-property.sh b/test/T610-message-property.sh\r
+new file mode 100755\r
+index 0000000..dcf9c3e\r
+--- /dev/null\r
++++ b/test/T610-message-property.sh\r
+@@ -0,0 +1,148 @@\r
++#!/usr/bin/env bash\r
++test_description="message property 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
++ notmuch_message_t *message = NULL;\r
++ const char *val;\r
++ notmuch_status_t stat;\r
++\r
++ RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
++ RUN(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));\r
++ if (message == NULL) {\r
++ fprintf (stderr, "unable to find message");\r
++ exit (1);\r
++ }\r
++EOF\r
++\r
++cat <<EOF > c_tail\r
++ RUN(notmuch_database_destroy(db));\r
++}\r
++EOF\r
++\r
++test_begin_subtest "notmuch_message_{add,get,remove}_property"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ RUN(notmuch_message_add_property (message, "testkey1", "testvalue1"));\r
++ RUN(notmuch_message_add_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey1", &val));\r
++ printf("testkey1 = %s\n", val);\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* Add second value for key */\r
++ RUN(notmuch_message_add_property (message, "testkey2", "testvalue3"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove first value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove non-existant value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue2"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++\r
++ /* remove only value for key */\r
++ RUN(notmuch_message_remove_property (message, "testkey2", "testvalue3"));\r
++ RUN(notmuch_message_get_property (message, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val == NULL ? "NULL" : val);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++testkey1 = testvalue1\r
++testkey2 = testvalue2\r
++testkey2 = testvalue2\r
++testkey2 = testvalue3\r
++testkey2 = testvalue3\r
++testkey2 = NULL\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++test_begin_subtest "notmuch_message_get_property_list: empty list"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_message_property_list_t *list;\r
++ RUN(notmuch_message_get_property_list (message, "nonexistent", &list));\r
++ printf("valid = %d\n", notmuch_message_property_list_valid (list));\r
++ notmuch_message_property_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
++test_begin_subtest "notmuch_message_property_list: one value"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_message_property_list_t *list;\r
++\r
++ RUN(notmuch_message_get_property_list (message, "testkey1", &list));\r
++ for (; notmuch_message_property_list_valid (list); notmuch_message_property_list_move_to_next (list)) {\r
++ printf("%s\n", notmuch_message_property_list_value(list));\r
++ }\r
++ notmuch_message_property_list_destroy (list);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++testvalue1\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++test_begin_subtest "notmuch_message_property_list: multiple values"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ notmuch_message_property_list_t *list;\r
++ RUN(notmuch_message_add_property (message, "testkey1", "bob"));\r
++ RUN(notmuch_message_add_property (message, "testkey1", "testvalue2"));\r
++ RUN(notmuch_message_add_property (message, "testkey1", "alice"));\r
++\r
++ RUN(notmuch_message_get_property_list (message, "testkey1", &list));\r
++ for (; notmuch_message_property_list_valid (list); notmuch_message_property_list_move_to_next (list)) {\r
++ printf("%s\n", notmuch_message_property_list_value(list));\r
++ }\r
++ notmuch_message_property_list_destroy (list);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++alice\r
++bob\r
++testvalue1\r
++testvalue2\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++\r
++test_done\r
+-- \r
+2.8.1\r
+\r