From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id iH9qFZlVFWFRMQEAgWs5BA (envelope-from ) for ; Thu, 12 Aug 2021 19:08:41 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id +K0WEZlVFWFCTAAAB5/wlQ (envelope-from ) for ; Thu, 12 Aug 2021 17:08:41 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id DD3F56C26 for ; Thu, 12 Aug 2021 19:08:40 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 8F98B29233; Thu, 12 Aug 2021 13:08:14 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 5EC7E29213 for ; Thu, 12 Aug 2021 13:08:03 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id EDF585FD5C; Thu, 12 Aug 2021 13:08:02 -0400 (EDT) Received: (nullmailer pid 1348726 invoked by uid 1000); Thu, 12 Aug 2021 17:07:43 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 03/31] lib: define notmuch_query_create_with_syntax Date: Thu, 12 Aug 2021 10:07:00 -0700 Message-Id: <20210812170728.1348333-4-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210812170728.1348333-1-david@tethera.net> References: <20210812170728.1348333-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: NWPLA5UIDU5CSRRT3VE5Y22XO62G6UHV X-Message-ID-Hash: NWPLA5UIDU5CSRRT3VE5Y22XO62G6UHV X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1628788121; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Yg/ijaht3ePH6T4ikOFx3Jh2JJs1OIYRjhjOWUX48cU=; b=BaALOOcaGFA1xAnDXbMJDaDOVjESpC0qJ8bIC3rryXz2nk1Sr0CW5x7ji5aj1UVwRcojR6 4ksxQvTpPHIlxhz8O8UGc4E52nph6jzeB0MsF3f7Qqrqn2k4b8fTHMul8VOXMn+fMOCzfn +F4vr3S8o1Lqz4Uu6qhcZ+0ukc+4RD36SDivJYhI4rV76T/ronnqlNOmeNwXo/PYjwReEJ bUKfvuJf2e8Zq8JFZKSiNjtzYKWj+CVG+6ju5Zj1sG266WTH8sKueH9h0g9HXVi5ytqrOe HlLqYfJGvRmEb9SCYtABzwz34QsmpAw0aGNwfU7PYi3+gDWIOvXClOlXNYWcNw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1628788121; a=rsa-sha256; cv=none; b=FLxRqhXoghXuAjyJWbNY3LAxL+O2DhOthT5qxgd/I7BhqGLYVbKiuJtJQ9djBU9SyJoRM6 KpMoSHGo6jM21fj1TN4vZE0GXQZUUJAujllmupXaH4X0ZVXKS/asQroaH23iNMNoOnFTlu nWSg8HE8mcLf5kerzQiYUCKwhtAkF2q/B84WMBcXF8jzgN76HSZq/z/85iPvxHmduTlT1Q Frv0oB8m7wuGCgsbaH7v+c3qzG+T0avUGsboC2qAWzv9C4m9Wt5bq2ZJp71WUTckP9fuug 31IxI0lOsmMfzUqq0sSpvN/rjE2Jfv30XyNOdSh1Qv2T2YoMm4coGjJPnjFXxA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: 0.48 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: DD3F56C26 X-Spam-Score: 0.48 X-Migadu-Scanner: scn0.migadu.com X-TUID: C84Zbp1+DM+m Set the parsing syntax when the (notmuch) query object is created. Initially the library always returns a trivial query that matches all messages when using s-expression syntax. It seems better to select the syntax at query creation time because the lazy parsing is an implementation detail. --- lib/notmuch.h | 10 +++++++ lib/query.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/lib/notmuch.h b/lib/notmuch.h index 3b28bea3..6f33c045 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -961,6 +961,16 @@ notmuch_query_t * notmuch_query_create (notmuch_database_t *database, const char *query_string); +typedef enum { + NOTMUCH_QUERY_SYNTAX_XAPIAN, + NOTMUCH_QUERY_SYNTAX_SEXP +} notmuch_query_syntax_t; + +notmuch_status_t +notmuch_query_create_with_syntax (notmuch_database_t *database, + const char *query_string, + notmuch_query_syntax_t syntax, + notmuch_query_t **output); /** * Sort values for notmuch_query_set_sort. */ diff --git a/lib/query.cc b/lib/query.cc index 39b85e91..12fd9482 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -23,6 +23,8 @@ #include /* GHashTable, GPtrArray */ +#include "sexp.h" + struct _notmuch_query { notmuch_database_t *notmuch; const char *query_string; @@ -30,6 +32,7 @@ struct _notmuch_query { notmuch_string_list_t *exclude_terms; notmuch_exclude_t omit_excluded; bool parsed; + notmuch_query_syntax_t syntax; Xapian::Query xapian_query; std::set terms; }; @@ -105,7 +108,10 @@ _notmuch_query_constructor (notmuch_database_t *notmuch, query->notmuch = notmuch; - query->query_string = talloc_strdup (query, query_string); + if (query_string) + query->query_string = talloc_strdup (query, query_string); + else + query->query_string = NULL; query->sort = NOTMUCH_SORT_NEWEST_FIRST; @@ -121,20 +127,49 @@ notmuch_query_create (notmuch_database_t *notmuch, const char *query_string) { - notmuch_query_t *query = _notmuch_query_constructor (notmuch, query_string); + notmuch_query_t *query; + notmuch_status_t status; - if (! query) + status = notmuch_query_create_with_syntax (notmuch, query_string, + NOTMUCH_QUERY_SYNTAX_XAPIAN, + &query); + if (status) return NULL; return query; } -static notmuch_status_t -_notmuch_query_ensure_parsed (notmuch_query_t *query) +notmuch_status_t +notmuch_query_create_with_syntax (notmuch_database_t *notmuch, + const char *query_string, + notmuch_query_syntax_t syntax, + notmuch_query_t **output) { - if (query->parsed) - return NOTMUCH_STATUS_SUCCESS; + notmuch_query_t *query; + + if (! output) + return NOTMUCH_STATUS_NULL_POINTER; + + query = _notmuch_query_constructor (notmuch, query_string); + if (! query) + return NOTMUCH_STATUS_OUT_OF_MEMORY; + + if (syntax == NOTMUCH_QUERY_SYNTAX_SEXP && ! HAVE_SFSEXP) { + _notmuch_database_log (notmuch, "sexp query parser not available"); + return NOTMUCH_STATUS_ILLEGAL_ARGUMENT; + } + + query->syntax = syntax; + + *output = query; + + return NOTMUCH_STATUS_SUCCESS; +} + +static notmuch_status_t +_notmuch_query_ensure_parsed_xapian (notmuch_query_t *query) +{ try { query->xapian_query = query->notmuch->query_parser-> @@ -167,6 +202,30 @@ _notmuch_query_ensure_parsed (notmuch_query_t *query) return NOTMUCH_STATUS_SUCCESS; } +static notmuch_status_t +_notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query) +{ + if (query->parsed) + return NOTMUCH_STATUS_SUCCESS; + + query->xapian_query = Xapian::Query::MatchAll; + return NOTMUCH_STATUS_SUCCESS; +} + +static notmuch_status_t +_notmuch_query_ensure_parsed (notmuch_query_t *query) +{ + if (query->parsed) + return NOTMUCH_STATUS_SUCCESS; + +#if HAVE_SFSEXP + if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP) + return _notmuch_query_ensure_parsed_sexpr (query); +#endif + + return _notmuch_query_ensure_parsed_xapian (query); +} + const char * notmuch_query_get_query_string (const notmuch_query_t *query) { -- 2.30.2