Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 8D3C6431FB6 for ; Thu, 9 Dec 2010 13:00:42 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LvZ6sl0KU3kC for ; Thu, 9 Dec 2010 13:00:39 -0800 (PST) Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU [18.7.68.34]) by olra.theworths.org (Postfix) with ESMTP id D2C6F41A54C for ; Thu, 9 Dec 2010 13:00:34 -0800 (PST) X-AuditID: 12074422-b7c3eae000000a70-03-4d01437258e2 Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-5.mit.edu (Symantec Brightmail Gateway) with SMTP id DE.D7.02672.273410D4; Thu, 9 Dec 2010 16:00:34 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id oB9L0YZR026448; Thu, 9 Dec 2010 16:00:34 -0500 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id oB9L0XPF010109 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Thu, 9 Dec 2010 16:00:34 -0500 (EST) Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.72) (envelope-from ) id 1PQnbN-0007Hs-6j; Thu, 09 Dec 2010 16:00:33 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH 3/5] Add a generic function to get a list of terms with some prefix. Date: Thu, 9 Dec 2010 15:59:54 -0500 Message-Id: <1291928396-27937-4-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1291928396-27937-1-git-send-email-amdragon@mit.edu> References: <1291928396-27937-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: AAAAAA== Cc: Austin Clements X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Dec 2010 21:00:43 -0000 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 | 16 +++++++--------- lib/database.cc | 36 ++++++++++++++---------------------- lib/directory.cc | 18 +++--------------- lib/message.cc | 6 +++++- 4 files changed, 29 insertions(+), 47 deletions(-) diff --git a/lib/database-private.h b/lib/database-private.h index 140b54e..9f83407 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -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_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i, + Xapian::TermIterator &end, + const char *prefix); #pragma GCC visibility pop diff --git a/lib/database.cc b/lib/database.cc index 45613bd..f8245ab 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1747,49 +1747,41 @@ 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_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, TRUE); + 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_get_terms_with_prefix (db, i, end, _find_prefix ("tag")); + _notmuch_string_list_sort (tags); + return _notmuch_tags_create (db, tags, TRUE); } catch (const Xapian::Error &error) { fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n", error.get_msg().c_str()); diff --git a/lib/directory.cc b/lib/directory.cc index aeee9ca..fb6ef03 100644 --- a/lib/directory.cc +++ b/lib/directory.cc @@ -23,10 +23,6 @@ /* 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,13 @@ _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_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); } diff --git a/lib/message.cc b/lib/message.cc index 031eda5..dbf683c 100644 --- a/lib/message.cc +++ b/lib/message.cc @@ -640,9 +640,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_get_terms_with_prefix (message, i, end, + _find_prefix ("tag")); + _notmuch_string_list_sort (tags); + return _notmuch_tags_create (message, tags, TRUE); } const char * -- 1.7.2.3