+
+notmuch_status_t
+notmuch_database_remove_message (notmuch_database_t *notmuch,
+ const char *filename)
+{
+ Xapian::WritableDatabase *db;
+ void *local = talloc_new (notmuch);
+ const char *prefix = _find_prefix ("file-direntry");
+ char *direntry, *term;
+ Xapian::PostingIterator i, end;
+ Xapian::Document document;
+ notmuch_status_t status;
+
+ status = _notmuch_database_ensure_writable (notmuch);
+ if (status)
+ return status;
+
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+
+ status = _notmuch_database_filename_to_direntry (local, notmuch,
+ filename, &direntry);
+ if (status)
+ return status;
+
+ term = talloc_asprintf (notmuch, "%s%s", prefix, direntry);
+
+ find_doc_ids_for_term (notmuch, term, &i, &end);
+
+ for ( ; i != end; i++) {
+ Xapian::TermIterator j;
+
+ document = find_document_for_doc_id (notmuch, *i);
+
+ document.remove_term (term);
+
+ j = document.termlist_begin ();
+ j.skip_to (prefix);
+
+ /* Was this the last file-direntry in the message? */
+ if (j == document.termlist_end () ||
+ strncmp ((*j).c_str (), prefix, strlen (prefix)))
+ {
+ db->delete_document (document.get_docid ());
+ status = NOTMUCH_STATUS_SUCCESS;
+ } else {
+ db->replace_document (document.get_docid (), document);
+ status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
+ }
+ }
+
+ talloc_free (local);
+
+ return status;
+}
+
+notmuch_tags_t *
+_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
+ Xapian::TermIterator &end)
+{
+ const char *prefix = _find_prefix ("tag");
+ notmuch_tags_t *tags;
+ std::string tag;
+
+ /* Currently this iteration is written with the assumption that
+ * "tag" has a single-character prefix. */
+ assert (strlen (prefix) == 1);
+
+ tags = _notmuch_tags_create (ctx);
+ if (unlikely (tags == NULL))
+ return NULL;
+
+ i.skip_to (prefix);
+
+ while (i != end) {
+ tag = *i;
+
+ if (tag.empty () || tag[0] != *prefix)
+ break;
+
+ _notmuch_tags_add_tag (tags, tag.c_str () + 1);
+
+ i++;
+ }
+
+ _notmuch_tags_prepare_iterator (tags);
+
+ return tags;
+}
+
+notmuch_tags_t *
+notmuch_database_get_all_tags (notmuch_database_t *db)
+{
+ Xapian::TermIterator i, end;
+ i = db->xapian_db->allterms_begin();
+ end = db->xapian_db->allterms_end();
+ return _notmuch_convert_tags(db, i, end);
+}