[PATCH 09/12] lib: add data_map field to message structs

Subject:[PATCH 09/12] lib: add data_map field to message structs

Date:Fri, 22 Jun 2018 22:42:44 -0300

To:notmuch@notmuchmail.org

Cc:

From:David Bremner


This string-map stores the unserialized key-value map from the message
document data area. It is lazily read on the first read, and lazily
written by _notmuch_message_sync. Note that other than naming this is
independent from the other metadata, which is stored in document
terms (i.e. things you can search for).
---
 lib/message.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/lib/message.cc b/lib/message.cc
index 153e4bed..bf597bc5 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -41,6 +41,7 @@ struct _notmuch_message {
     notmuch_message_file_t *message_file;
     notmuch_string_list_t *property_term_list;
     notmuch_string_map_t *property_map;
+    notmuch_string_map_t *data_map;
     notmuch_message_list_t *replies;
     unsigned long flags;
     /* For flags that are initialized on-demand, lazy_flags indicates
@@ -129,6 +130,7 @@ _notmuch_message_create_for_document (const void *talloc_owner,
     message->author = NULL;
     message->property_term_list = NULL;
     message->property_map = NULL;
+    message->data_map = NULL;
 
     message->replies = _notmuch_message_list_create (message);
     if (unlikely (message->replies == NULL)) {
@@ -334,7 +336,6 @@ _notmuch_message_get_thread_id_only (notmuch_message_t *message)
     return message->thread_id;
 }
 
-
 static void
 _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
 {
@@ -483,6 +484,19 @@ _notmuch_message_invalidate_metadata (notmuch_message_t *message,
     }
 }
 
+static void
+_notmuch_message_ensure_data_map (notmuch_message_t *message)
+{
+    if (message->data_map)
+	return;
+
+    char *blob = talloc_strdup(message, message->doc.get_data().c_str ());
+
+    message->data_map = _notmuch_string_map_deserialize (message, blob);
+
+    talloc_free (blob);
+}
+
 unsigned int
 _notmuch_message_get_doc_id (notmuch_message_t *message)
 {
@@ -1169,6 +1183,13 @@ _notmuch_message_sync (notmuch_message_t *message)
 				    _notmuch_database_new_revision (
 					message->notmuch)));
 
+    if (message->notmuch->features & NOTMUCH_FEATURE_MESSAGE_DATA
+	&& message->data_map) {
+	const char *blob = _notmuch_string_map_serialize (message,
+							  message->data_map);
+	message->doc.set_data (blob);
+    }
+
     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
     db->replace_document (message->doc_id, message->doc);
     message->modified = false;
@@ -1930,6 +1951,36 @@ notmuch_message_get_database (const notmuch_message_t *message)
     return message->notmuch;
 }
 
+notmuch_status_t
+_notmuch_message_data_get (notmuch_message_t *message, const char *key, const char **value)
+{
+    if (! message || !key || !value)
+	return NOTMUCH_STATUS_NULL_POINTER;
+
+    _notmuch_message_ensure_data_map (message);
+
+    *value = _notmuch_string_map_get (message->data_map, key);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
+notmuch_status_t
+_notmuch_message_data_set (notmuch_message_t *message, const char *key, const char *value)
+{
+    if (! message || !key || !value)
+	return NOTMUCH_STATUS_NULL_POINTER;
+
+    notmuch_status_t status = _notmuch_database_ensure_writable (message->notmuch);
+    if (status)
+	return status;
+
+    _notmuch_message_ensure_data_map (message);
+
+    _notmuch_string_map_set (message->data_map, key, value);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 static void
 _notmuch_message_ensure_property_map (notmuch_message_t *message)
 {
-- 
2.17.1

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch

Thread: