X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=message.cc;h=6e15b51118be82b960fb13aae667241fdbf36048;hp=5922cfccff1ae7464df8f78bf0d4da248c19b89f;hb=f2bcc256fb24a77d57e1ac7050593d7b854a7eb6;hpb=c7482b4dce114b1c09cbac2f4ef6d0defdb23258 diff --git a/message.cc b/message.cc index 5922cfcc..6e15b511 100644 --- a/message.cc +++ b/message.cc @@ -26,22 +26,14 @@ struct _notmuch_message { notmuch_database_t *notmuch; Xapian::docid doc_id; + int frozen; char *message_id; char *thread_id; char *filename; + notmuch_message_file_t *message_file; Xapian::Document doc; }; -typedef struct _notmuch_terms { - char prefix_char; - Xapian::TermIterator iterator; - Xapian::TermIterator iterator_end; -} notmuch_terms_t; - -struct _notmuch_tags { - notmuch_terms_t terms; -}; - /* "128 bits of thread-id ought to be enough for anybody" */ #define NOTMUCH_THREAD_ID_BITS 128 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4) @@ -107,9 +99,14 @@ _notmuch_message_create (const void *talloc_owner, message->notmuch = notmuch; message->doc_id = doc_id; - message->message_id = NULL; /* lazily created */ - message->thread_id = NULL; /* lazily created */ - message->filename = NULL; /* lazily created */ + + message->frozen = 0; + + /* Each of these will be lazily created as needed. */ + message->message_id = NULL; + message->thread_id = NULL; + message->filename = NULL; + message->message_file = NULL; /* This is C++'s creepy "placement new", which is really just an * ugly way to call a constructor for a pre-allocated object. So @@ -233,6 +230,28 @@ notmuch_message_get_message_id (notmuch_message_t *message) return message->message_id; } +const char * +_notmuch_message_get_subject (notmuch_message_t *message) +{ + if (! message->message_file) { + notmuch_message_file_t *message_file; + const char *filename; + + filename = notmuch_message_get_filename (message); + if (unlikely (filename == NULL)) + return NULL; + + message_file = _notmuch_message_file_open_ctx (message, filename); + if (unlikely (message_file == NULL)) + return NULL; + + message->message_file = message_file; + } + + return notmuch_message_file_get_header (message->message_file, + "subject"); +} + const char * notmuch_message_get_thread_id (notmuch_message_t *message) { @@ -295,64 +314,41 @@ notmuch_message_get_filename (notmuch_message_t *message) return message->filename; } -/* We end up having to call the destructors explicitly because we had - * to use "placement new" in order to initialize C++ objects within a - * block that we allocated with talloc. So C++ is making talloc - * slightly less simple to use, (we wouldn't need - * talloc_set_destructor at all otherwise). - */ -static int -_notmuch_terms_destructor (notmuch_terms_t *terms) -{ - terms->iterator.~TermIterator (); - terms->iterator_end.~TermIterator (); - - return 0; -} - -static notmuch_terms_t * -_notmuch_terms_create (void *ctx, - Xapian::Document doc, - const char *prefix_name) +notmuch_tags_t * +notmuch_message_get_tags (notmuch_message_t *message) { - notmuch_terms_t *terms; - const char *prefix = _find_prefix (prefix_name); + const char *prefix = _find_prefix ("tag"); + Xapian::TermIterator i, end; + notmuch_tags_t *tags; + std::string tag; - /* Currently, notmuch_terms_t is written with the assumption that - * any prefix its derivatives use will be only a single - * character. */ + /* Currently this iteration is written with the assumption that + * "tag" has a single-character prefix. */ assert (strlen (prefix) == 1); - terms = talloc (ctx, notmuch_terms_t); - if (unlikely (terms == NULL)) + tags = _notmuch_tags_create (message); + if (unlikely (tags == NULL)) return NULL; - terms->prefix_char = *prefix; - new (&terms->iterator) Xapian::TermIterator; - new (&terms->iterator_end) Xapian::TermIterator; + i = message->doc.termlist_begin (); + end = message->doc.termlist_end (); - talloc_set_destructor (terms, _notmuch_terms_destructor); + i.skip_to (prefix); - terms->iterator = doc.termlist_begin (); - terms->iterator.skip_to (prefix); - terms->iterator_end = doc.termlist_end (); + while (1) { + tag = *i; - return terms; -} + if (tag.empty () || tag[0] != *prefix) + break; -/* The assertion is to ensure that 'type' is a derivative of - * notmuch_terms_t in that it contains a notmuch_terms_t as its first - * member. We do this by name of 'terms' as opposed to type, because - * that's as clever as I've been so far. */ -#define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \ - (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0), \ - (type *) _notmuch_terms_create (ctx, doc, prefix_name)) + _notmuch_tags_add_tag (tags, tag.c_str () + 1); -notmuch_tags_t * -notmuch_message_get_tags (notmuch_message_t *message) -{ - return _notmuch_terms_create_type (message, message->doc, "tag", - notmuch_tags_t); + i++; + } + + _notmuch_tags_prepare_iterator (tags); + + return tags; } void @@ -493,7 +489,8 @@ notmuch_message_add_tag (notmuch_message_t *message, const char *tag) status); } - _notmuch_message_sync (message); + if (! message->frozen) + _notmuch_message_sync (message); return NOTMUCH_STATUS_SUCCESS; } @@ -515,70 +512,57 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag) status); } - _notmuch_message_sync (message); + if (! message->frozen) + _notmuch_message_sync (message); return NOTMUCH_STATUS_SUCCESS; } void -notmuch_message_destroy (notmuch_message_t *message) -{ - talloc_free (message); -} - -static notmuch_bool_t -_notmuch_terms_has_more (notmuch_terms_t *terms) +notmuch_message_remove_all_tags (notmuch_message_t *message) { - std::string s; - - if (terms->iterator == terms->iterator_end) - return FALSE; - - s = *terms->iterator; - if (! s.empty () && s[0] == terms->prefix_char) - return TRUE; - else - return FALSE; -} - -static const char * -_notmuch_terms_get (notmuch_terms_t *terms) -{ - return talloc_strdup (terms, (*terms->iterator).c_str () + 1); -} + notmuch_private_status_t status; + notmuch_tags_t *tags; + const char *tag; -static void -_notmuch_terms_advance (notmuch_terms_t *terms) -{ - terms->iterator++; -} + for (tags = notmuch_message_get_tags (message); + notmuch_tags_has_more (tags); + notmuch_tags_advance (tags)) + { + tag = notmuch_tags_get (tags); -static void -_notmuch_terms_destroy (notmuch_terms_t *terms) -{ - talloc_free (terms); -} + status = _notmuch_message_remove_term (message, "tag", tag); + if (status) { + INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n", + status); + } + } -notmuch_bool_t -notmuch_tags_has_more (notmuch_tags_t *tags) -{ - return _notmuch_terms_has_more (&tags->terms); + if (! message->frozen) + _notmuch_message_sync (message); } -const char * -notmuch_tags_get (notmuch_tags_t *tags) +void +notmuch_message_freeze (notmuch_message_t *message) { - return _notmuch_terms_get (&tags->terms); + message->frozen++; } -void -notmuch_tags_advance (notmuch_tags_t *tags) +notmuch_status_t +notmuch_message_thaw (notmuch_message_t *message) { - return _notmuch_terms_advance (&tags->terms); + if (message->frozen > 0) { + message->frozen--; + if (message->frozen == 0) + _notmuch_message_sync (message); + return NOTMUCH_STATUS_SUCCESS; + } else { + return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW; + } } void -notmuch_tags_destroy (notmuch_tags_t *tags) +notmuch_message_destroy (notmuch_message_t *message) { - return _notmuch_terms_destroy (&tags->terms); + talloc_free (message); }