]> git.notmuchmail.org Git - notmuch/commitdiff
Add a generic function to get a list of terms with some prefix.
authorAustin Clements <amdragon@mit.edu>
Thu, 9 Dec 2010 05:32:35 +0000 (00:32 -0500)
committerAustin Clements <amdragon@mit.edu>
Mon, 21 Mar 2011 06:45:18 +0000 (02:45 -0400)
Replace _notmuch_convert_tags with this and simplify
_create_filenames_for_terms_with_prefix.  This will also come in handy
shortly to get the message file name list.

lib/database-private.h
lib/database.cc
lib/directory.cc
lib/message.cc

index 140b54edf499dea41f00c38a8bb2f00440b16648..f7050097bbccc3054b8a42a536eff6756eb0072e 100644 (file)
@@ -53,18 +53,16 @@ struct _notmuch_database {
     Xapian::ValueRangeProcessor *value_range_processor;
 };
 
-/* Convert tags from Xapian internal format to notmuch format.
- *
- * The function gets a TermIterator as argument and uses that iterator to find
- * all tag terms in the object. The tags are then converted to a
- * notmuch_tags_t list and returned. The function needs to allocate memory for
- * the resulting list and it uses the argument ctx as talloc context.
+/* Return the list of terms from the given iterator matching a prefix.
+ * The prefix will be stripped from the strings in the returned list.
+ * The list will be allocated using ctx as the talloc context.
  *
  * The function returns NULL on failure.
  */
-notmuch_tags_t *
-_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
-                      Xapian::TermIterator &end);
+notmuch_string_list_t *
+_notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
+                                        Xapian::TermIterator &end,
+                                        const char *prefix);
 
 #pragma GCC visibility pop
 
index 9c777156ab6292beb9066a1e097637021d1e9007..7f79cf47bda767a1463ae418ea5d713f950ac3c4 100644 (file)
@@ -1757,49 +1757,42 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
     return status;
 }
 
-notmuch_tags_t *
-_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
-                      Xapian::TermIterator &end)
+notmuch_string_list_t *
+_notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
+                                        Xapian::TermIterator &end,
+                                        const char *prefix)
 {
-    const char *prefix = _find_prefix ("tag");
+    int prefix_len = strlen (prefix);
     notmuch_string_list_t *list;
-    std::string tag;
-
-    /* Currently this iteration is written with the assumption that
-     * "tag" has a single-character prefix. */
-    assert (strlen (prefix) == 1);
 
     list = _notmuch_string_list_create (ctx);
     if (unlikely (list == NULL))
        return NULL;
 
-    i.skip_to (prefix);
-
-    while (i != end) {
-       tag = *i;
-
-       if (tag.empty () || tag[0] != *prefix)
+    for (i.skip_to (prefix); i != end; i++) {
+       /* Terminate loop at first term without desired prefix. */
+       if (strncmp ((*i).c_str (), prefix, prefix_len))
            break;
 
-       _notmuch_string_list_append (list, tag.c_str () + 1);
-
-       i++;
+       _notmuch_string_list_append (list, (*i).c_str () + prefix_len);
     }
 
-    _notmuch_string_list_sort (list);
-
-    return _notmuch_tags_create (ctx, list);
+    return list;
 }
 
 notmuch_tags_t *
 notmuch_database_get_all_tags (notmuch_database_t *db)
 {
     Xapian::TermIterator i, end;
+    notmuch_string_list_t *tags;
 
     try {
        i = db->xapian_db->allterms_begin();
        end = db->xapian_db->allterms_end();
-       return _notmuch_convert_tags(db, i, end);
+       tags = _notmuch_database_get_terms_with_prefix (db, i, end,
+                                                       _find_prefix ("tag"));
+       _notmuch_string_list_sort (tags);
+       return _notmuch_tags_create (db, tags);
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n",
                 error.get_msg().c_str());
index aeee9caf067580aaef1c5d309eb18b185e3078a3..70e1693ea54eaa4f3bfe5e5312610318d803621f 100644 (file)
 
 /* Create an iterator to iterate over the basenames of files (or
  * directories) that all share a common parent directory.
- *
- * The code here is general enough to be reused for any case of
- * iterating over the non-prefixed portion of terms sharing a common
- * prefix.
  */
 static notmuch_filenames_t *
 _create_filenames_for_terms_with_prefix (void *ctx,
@@ -35,21 +31,14 @@ _create_filenames_for_terms_with_prefix (void *ctx,
 {
     notmuch_string_list_t *filename_list;
     Xapian::TermIterator i, end;
-    int prefix_len = strlen (prefix);
 
-    filename_list = _notmuch_string_list_create (ctx);
+    i = notmuch->xapian_db->allterms_begin();
+    end = notmuch->xapian_db->allterms_end();
+    filename_list = _notmuch_database_get_terms_with_prefix (ctx, i, end,
+                                                            prefix);
     if (unlikely (filename_list == NULL))
        return NULL;
 
-    end = notmuch->xapian_db->allterms_end (prefix);
-
-    for (i = notmuch->xapian_db->allterms_begin (prefix); i != end; i++)
-    {
-       std::string term = *i;
-
-       _notmuch_string_list_append (filename_list, term.c_str () + prefix_len);
-    }
-
     return _notmuch_filenames_create (ctx, filename_list);
 }
 
index b87506ac82b79128145d0f4fe59ef958f67d93ca..da4a102c3fc1c10f171d837bffa7707b416f81b4 100644 (file)
@@ -711,9 +711,13 @@ notmuch_tags_t *
 notmuch_message_get_tags (notmuch_message_t *message)
 {
     Xapian::TermIterator i, end;
+    notmuch_string_list_t *tags;
     i = message->doc.termlist_begin();
     end = message->doc.termlist_end();
-    return _notmuch_convert_tags(message, i, end);
+    tags = _notmuch_database_get_terms_with_prefix (message, i, end,
+                                                   _find_prefix ("tag"));
+    _notmuch_string_list_sort (tags);
+    return _notmuch_tags_create (message, tags);
 }
 
 const char *