unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [WIP patch 2/9] lib: notmuch_metadata_t: iterators for metadata
Date: Sat,  9 Jan 2016 22:51:34 -0400	[thread overview]
Message-ID: <1452394301-29499-3-git-send-email-david@tethera.net> (raw)
In-Reply-To: <1452394301-29499-1-git-send-email-david@tethera.net>

The rough idea is to give a C interface to the Xapian::TermIterator class.
---
 lib/metadata.cc       | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/notmuch.h         | 12 ++++++++++
 test/T590-metadata.sh | 15 ++++++++++++
 3 files changed, 90 insertions(+)

diff --git a/lib/metadata.cc b/lib/metadata.cc
index a068ed1..5d319c5 100644
--- a/lib/metadata.cc
+++ b/lib/metadata.cc
@@ -22,6 +22,17 @@
 #include "notmuch-private.h"
 #include "database-private.h"
 
+struct _notmuch_metadata {
+    notmuch_database_t *notmuch;
+    Xapian::TermIterator *iterator;
+    notmuch_metadata_class_t mclass;
+};
+
+static int
+_notmuch_metadata_destroy (notmuch_metadata_t *list) {
+    delete list->iterator;
+    return 0;
+}
 static
 const char *
 _find_metadata_prefix (notmuch_metadata_class_t mclass)
@@ -148,3 +159,55 @@ notmuch_database_get_metadata (notmuch_database_t *notmuch,
 
     return NOTMUCH_STATUS_SUCCESS;
 }
+
+notmuch_status_t
+notmuch_database_get_all_metadata (notmuch_database_t *notmuch,
+				   notmuch_metadata_class_t mclass,
+				   notmuch_metadata_t **out)
+{
+    notmuch_metadata_t *list = NULL;
+    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+
+    const char *prefix = _find_metadata_prefix (mclass);
+
+    list = talloc (notmuch, notmuch_metadata_t);
+    if (!list) {
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
+
+    talloc_set_destructor(list, _notmuch_metadata_destroy);
+    list->iterator = new Xapian::TermIterator;
+    list->notmuch = notmuch;
+    list->mclass = mclass;
+
+    try {
+
+	*list->iterator = notmuch->xapian_db->metadata_keys_begin();
+	list->iterator->skip_to (prefix);
+
+    } catch (const Xapian::Error &error) {
+	_notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
+			       error.get_msg().c_str());
+	notmuch->exception_reported = TRUE;
+	status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+    }
+
+    *out = list;
+
+ DONE:
+    if (status && list)
+	talloc_free (list);
+
+    return status;
+}
+
+notmuch_bool_t
+notmuch_metadata_valid (notmuch_metadata_t *metadata)
+{
+    const char *prefix = _find_metadata_prefix (metadata->mclass);
+    if (*(metadata->iterator) == metadata->notmuch->xapian_db->metadata_keys_end())
+	return FALSE;
+
+    return (strncmp((**(metadata->iterator)).c_str (), prefix, strlen (prefix)) == 0);
+}
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 448f405..a2b7040 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -197,6 +197,7 @@ typedef struct _notmuch_message notmuch_message_t;
 typedef struct _notmuch_tags notmuch_tags_t;
 typedef struct _notmuch_directory notmuch_directory_t;
 typedef struct _notmuch_filenames notmuch_filenames_t;
+typedef struct _notmuch_metadata notmuch_metadata_t;
 #endif /* __DOXYGEN__ */
 
 /**
@@ -1854,6 +1855,17 @@ notmuch_database_set_metadata (notmuch_database_t *db, notmuch_metadata_class_t
 notmuch_status_t
 notmuch_database_get_metadata (notmuch_database_t *db, notmuch_metadata_class_t mclass, const char *key, char **value);
 
+/**
+ * get all metadata of a given class
+ */
+notmuch_status_t
+notmuch_database_get_all_metadata (notmuch_database_t *db, notmuch_metadata_class_t mclass, notmuch_metadata_t **out);
+
+/**
+ * Is 'metadata' iterator valid (i.e. _key, _value, _move_to_next can be called).
+ */
+notmuch_bool_t
+notmuch_metadata_valid (notmuch_metadata_t *metadata);
 /* @} */
 
 NOTMUCH_END_DECLS
diff --git a/test/T590-metadata.sh b/test/T590-metadata.sh
index 29aeaa2..c36a7d7 100755
--- a/test/T590-metadata.sh
+++ b/test/T590-metadata.sh
@@ -55,4 +55,19 @@ testkey2 = testvalue2
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
+test_begin_subtest "notmuch_database_get_all_metadata initially valid"
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
+{
+   notmuch_metadata_t *meta;
+   RUN(notmuch_database_get_all_metadata (db, NOTMUCH_METADATA_CONFIG, &meta));
+   printf("valid = %d\n", notmuch_metadata_valid (meta));
+}
+EOF
+cat <<'EOF' >EXPECTED
+== stdout ==
+valid = 1
+== stderr ==
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
 test_done
-- 
2.6.4

  parent reply	other threads:[~2016-01-10  2:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-10  2:51 WIP: add metadata to dump output David Bremner
2016-01-10  2:51 ` [WIP patch 1/9] lib: initial API for prefixed metadata David Bremner
2016-01-10  2:51 ` David Bremner [this message]
2016-01-10  2:51 ` [WIP patch 3/9] lib: add and test function to retrive current key David Bremner
2016-01-10  2:51 ` [WIP patch 4/9] lib: add and test function to retrieve current metadata value David Bremner
2016-01-10  2:51 ` [WIP patch 5/9] lib: add notmuch_metadata_move_to_next David Bremner
2016-01-10  2:51 ` [WIP patch 6/9] lib: add notmuch_metadata_destroy David Bremner
2016-01-10  2:51 ` [WIP patch 7/9] CLI: add print_status_database David Bremner
2016-01-10  2:51 ` [WIP patch 8/9] lib: make string representation of metadata class public David Bremner
2016-01-10  2:51 ` [WIP patch 9/9] CLI: add optional metadata to dump output David Bremner
2016-01-10 14:36 ` WIP: add " Tomi Ollila

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1452394301-29499-3-git-send-email-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).