Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 211106DE01BA for ; Thu, 30 Jun 2016 01:40:47 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.005 X-Spam-Level: X-Spam-Status: No, score=-0.005 tagged_above=-999 required=5 tests=[AWL=-0.006, HEADER_FROM_DIFFERENT_DOMAINS=0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tkfA5gYg-LGq for ; Thu, 30 Jun 2016 01:40:39 -0700 (PDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by arlo.cworth.org (Postfix) with ESMTPS id 156186DE00C9 for ; Thu, 30 Jun 2016 01:40:39 -0700 (PDT) Received: from remotemail by fethera.tethera.net with local (Exim 4.84) (envelope-from ) id 1bIXWF-0007e8-Sm; Thu, 30 Jun 2016 04:40:19 -0400 Received: (nullmailer pid 30882 invoked by uid 1000); Thu, 30 Jun 2016 08:40:30 -0000 From: David Bremner To: David Bremner , notmuch@notmuchmail.org Subject: [PATCH 1/2] WIP: refactor tag search into library routine Date: Thu, 30 Jun 2016 10:40:26 +0200 Message-Id: <1467276027-30633-2-git-send-email-david@tethera.net> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1467276027-30633-1-git-send-email-david@tethera.net> References: <87fustdsx9.fsf@zancas.localnet> <1467276027-30633-1-git-send-email-david@tethera.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 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, 30 Jun 2016 08:40:47 -0000 --- lib/notmuch.h | 13 +++++++++++++ lib/query.cc | 35 +++++++++++++++++++++++++++++++++++ notmuch-search.c | 24 ++++-------------------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/lib/notmuch.h b/lib/notmuch.h index 2faa146..927ea3c 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -927,6 +927,19 @@ notmuch_messages_t * notmuch_query_search_messages (notmuch_query_t *query); /** + * Execute a query for tags, outputing a notmuch_tags_t object + * which can be used to iterate over the results. The output + * tags object is owned by the query and as such, will only be + * valid until notmuch_query_destroy. + * + * @param[in] query the query to collect tags for + * @param[out] out the return tags list + * + */ +notmuch_status_t +notmuch_query_search_tags (notmuch_query_t *query, notmuch_tags_t **out); + +/** * Destroy a notmuch_query_t along with any associated resources. * * This will in turn destroy any notmuch_threads_t and diff --git a/lib/query.cc b/lib/query.cc index 7eb73a1..7245b12 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -310,6 +310,41 @@ _notmuch_query_search_documents (notmuch_query_t *query, } } +notmuch_status_t +notmuch_query_search_tags (notmuch_query_t *query, notmuch_tags_t **tags) +{ + notmuch_messages_t *messages = NULL; + notmuch_database_t *notmuch = notmuch_query_get_database (query); + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + const char *query_string = notmuch_query_get_query_string (query); + + if (tags == NULL) + return NOTMUCH_STATUS_NULL_POINTER; + + /* Special-case query of "*" or '' for better performance. */ + if (strcmp (query_string, "*") == 0 || *query_string == '\0') { + *tags = notmuch_database_get_all_tags (notmuch); + if (*tags == NULL) + status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + } else { + notmuch_status_t status; + status = notmuch_query_search_messages_st (query, &messages); + if (status) + goto DONE; + + *tags = notmuch_messages_collect_tags (messages); + talloc_steal (query, *tags); + if (*tags == NULL) + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + } + + DONE: + if (messages) + notmuch_messages_destroy (messages); + + return status; +} + notmuch_bool_t _notmuch_mset_messages_valid (notmuch_messages_t *messages) { diff --git a/notmuch-search.c b/notmuch-search.c index 8c65d5a..8722735 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -611,31 +611,18 @@ do_search_messages (search_context_t *ctx) return 0; } + static int do_search_tags (const search_context_t *ctx) { - notmuch_messages_t *messages = NULL; notmuch_tags_t *tags; const char *tag; sprinter_t *format = ctx->format; notmuch_query_t *query = ctx->query; - notmuch_database_t *notmuch = ctx->notmuch; - - /* should the following only special case if no excluded terms - * specified? */ - - /* Special-case query of "*" for better performance. */ - if (strcmp (notmuch_query_get_query_string (query), "*") == 0) { - tags = notmuch_database_get_all_tags (notmuch); - } else { - notmuch_status_t status; - status = notmuch_query_search_messages_st (query, &messages); - if (print_status_query ("notmuch search", query, status)) - return 1; - tags = notmuch_messages_collect_tags (messages); - } - if (tags == NULL) + if (print_status_query ("notmuch search", + query, + notmuch_query_search_tags (query, &tags))) return 1; format->begin_list (format); @@ -653,9 +640,6 @@ do_search_tags (const search_context_t *ctx) notmuch_tags_destroy (tags); - if (messages) - notmuch_messages_destroy (messages); - format->end (format); return 0; -- 2.8.1