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 CB25042D2BD for ; Sun, 16 Jan 2011 00:11:35 -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 9XxQpwK7JjgL for ; Sun, 16 Jan 2011 00:11:33 -0800 (PST) Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU [18.9.25.15]) by olra.theworths.org (Postfix) with ESMTP id A3B5242D297 for ; Sun, 16 Jan 2011 00:11:23 -0800 (PST) X-AuditID: 1209190f-b7c1dae000000a2b-c5-4d32a82bfdcf Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) by dmz-mailsec-scanner-4.mit.edu (Symantec Brightmail Gateway) with SMTP id 7C.E2.02603.B28A23D4; Sun, 16 Jan 2011 03:11:23 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id p0G8BNLQ024330; Sun, 16 Jan 2011 03:11:23 -0500 Received: from drake.mit.edu (a074.catapulsion.net [70.36.81.74]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id p0G8BLVw010521 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sun, 16 Jan 2011 03:11:22 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.72) (envelope-from ) id 1PeNhp-0002XX-6E; Sun, 16 Jan 2011 03:11:21 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH 4/8] Replace Xapian query parser with custom query parser. Date: Sun, 16 Jan 2011 03:10:54 -0500 Message-Id: <1295165458-9573-5-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1295165458-9573-1-git-send-email-amdragon@mit.edu> References: <1295165458-9573-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: AAAAAA== Cc: amdragon@mit.edu 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: Sun, 16 Jan 2011 08:11:36 -0000 Note that the type:mail filter is implemented as a transform pass, so it no longer has to be done everywhere queries are parsed. Furthermore, this filter now depends on the prefixing logic in the query parser instead of implementing this itself. Likewise, we don't need to special-case the queries "" and "*" in multiple places. --- lib/database-private.h | 3 +- lib/database.cc | 33 ++++++++++++++----------- lib/query.cc | 62 ++++++++++++++++++----------------------------- 3 files changed, 44 insertions(+), 54 deletions(-) diff --git a/lib/database-private.h b/lib/database-private.h index 5d2fa02..a6eea87 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -48,9 +48,8 @@ struct _notmuch_database { unsigned int last_doc_id; uint64_t last_thread_id; - Xapian::QueryParser *query_parser; Xapian::TermGenerator *term_gen; - Xapian::ValueRangeProcessor *value_range_processor; + _notmuch_qparser_t *query_parser; }; /* Return the list of terms from the given iterator matching a prefix. diff --git a/lib/database.cc b/lib/database.cc index f8245ab..a3df0ae 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -571,6 +571,17 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch) return NOTMUCH_STATUS_SUCCESS; } +static _notmuch_token_t * +transform_type_mail (_notmuch_token_t *root, unused (void *opaque)) +{ + _notmuch_token_t *mail_ast = + _notmuch_token_create_term (root, TOK_LIT, "mail"); + mail_ast->prefix = talloc_strdup (root, _find_prefix ("type")); + if (!root) + return mail_ast; + return _notmuch_token_create_op (root, TOK_AND, mail_ast, root); +} + notmuch_database_t * notmuch_database_open (const char *path, notmuch_database_mode_t mode) @@ -661,27 +672,24 @@ notmuch_database_open (const char *path, INTERNAL_ERROR ("Malformed database last_thread_id: %s", str); } - notmuch->query_parser = new Xapian::QueryParser; notmuch->term_gen = new Xapian::TermGenerator; notmuch->term_gen->set_stemmer (Xapian::Stem ("english")); - notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP); - - notmuch->query_parser->set_default_op (Xapian::Query::OP_AND); - notmuch->query_parser->set_database (*notmuch->xapian_db); - notmuch->query_parser->set_stemmer (Xapian::Stem ("english")); - notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME); - notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor); + notmuch->query_parser = _notmuch_qparser_create (notmuch, notmuch); for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) { prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i]; - notmuch->query_parser->add_boolean_prefix (prefix->name, - prefix->prefix); + _notmuch_qparser_add_db_prefix (notmuch->query_parser, prefix->name, + prefix->prefix, TRUE); } for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) { prefix_t *prefix = &PROBABILISTIC_PREFIX[i]; - notmuch->query_parser->add_prefix (prefix->name, prefix->prefix); + _notmuch_qparser_add_db_prefix (notmuch->query_parser, prefix->name, + prefix->prefix, FALSE); } + + _notmuch_qparser_add_transform (notmuch->query_parser, + transform_type_mail, NULL); } catch (const Xapian::Error &error) { fprintf (stderr, "A Xapian exception occurred opening database: %s\n", error.get_msg().c_str()); @@ -711,9 +719,6 @@ notmuch_database_close (notmuch_database_t *notmuch) } delete notmuch->term_gen; - delete notmuch->query_parser; - delete notmuch->xapian_db; - delete notmuch->value_range_processor; talloc_free (notmuch); } diff --git a/lib/query.cc b/lib/query.cc index c7ae4ee..993f498 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -131,27 +131,21 @@ notmuch_query_search_messages (notmuch_query_t *query) talloc_set_destructor (messages, _notmuch_messages_destructor); Xapian::Enquire enquire (*notmuch->xapian_db); - Xapian::Query mail_query (talloc_asprintf (query, "%s%s", - _find_prefix ("type"), - "mail")); - Xapian::Query string_query, final_query; + _notmuch_token_t *ast; + Xapian::Query final_query; Xapian::MSet mset; - unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN | - Xapian::QueryParser::FLAG_PHRASE | - Xapian::QueryParser::FLAG_LOVEHATE | - Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE | - Xapian::QueryParser::FLAG_WILDCARD | - Xapian::QueryParser::FLAG_PURE_NOT); + char *error; - if (strcmp (query_string, "") == 0 || - strcmp (query_string, "*") == 0) - { - final_query = mail_query; - } else { - string_query = notmuch->query_parser-> - parse_query (query_string, flags); - final_query = Xapian::Query (Xapian::Query::OP_AND, - mail_query, string_query); + ast = _notmuch_qparser_parse (query, notmuch->query_parser, + query_string); + ast = _notmuch_qparser_transform (notmuch->query_parser, ast); + final_query = _notmuch_qparser_generate (query, notmuch->query_parser, + ast, &error); + if (error) { + fprintf (stderr, "Query error: %s\n", error); + notmuch->exception_reported = TRUE; + talloc_free (messages); + return NULL; } enquire.set_weighting_scheme (Xapian::BoolWeight()); @@ -412,27 +406,19 @@ notmuch_query_count_messages (notmuch_query_t *query) try { Xapian::Enquire enquire (*notmuch->xapian_db); - Xapian::Query mail_query (talloc_asprintf (query, "%s%s", - _find_prefix ("type"), - "mail")); - Xapian::Query string_query, final_query; + _notmuch_token_t *ast; + Xapian::Query final_query; Xapian::MSet mset; - unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN | - Xapian::QueryParser::FLAG_PHRASE | - Xapian::QueryParser::FLAG_LOVEHATE | - Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE | - Xapian::QueryParser::FLAG_WILDCARD | - Xapian::QueryParser::FLAG_PURE_NOT); + char *error; - if (strcmp (query_string, "") == 0 || - strcmp (query_string, "*") == 0) - { - final_query = mail_query; - } else { - string_query = notmuch->query_parser-> - parse_query (query_string, flags); - final_query = Xapian::Query (Xapian::Query::OP_AND, - mail_query, string_query); + ast = _notmuch_qparser_parse (query, notmuch->query_parser, + query_string); + ast = _notmuch_qparser_transform (notmuch->query_parser, ast); + final_query = _notmuch_qparser_generate (query, notmuch->query_parser, + ast, &error); + if (error) { + fprintf (stderr, "Query error: %s\n", error); + return count; } enquire.set_weighting_scheme(Xapian::BoolWeight()); -- 1.7.2.3