unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/3] lib: factor out lastmod range handling from sexp parser.
Date: Sun, 14 Aug 2022 12:02:58 -0300	[thread overview]
Message-ID: <20220814150259.517361-2-david@tethera.net> (raw)
In-Reply-To: <20220814150259.517361-1-david@tethera.net>

This will permit the re-use of the same logic in the infix query
parser. The location of the shared code in the infix side is for
consistency with the other shared parsing logic. It will make more
sense when a Xapian field processor is added for the lastmod prefix.
---
 lib/Makefile.local     |  3 +-
 lib/database-private.h |  6 ++++
 lib/lastmod-fp.cc      | 68 ++++++++++++++++++++++++++++++++++++++++++
 lib/parse-sexp.cc      | 37 ++++-------------------
 4 files changed, 82 insertions(+), 32 deletions(-)
 create mode 100644 lib/lastmod-fp.cc

diff --git a/lib/Makefile.local b/lib/Makefile.local
index 6d67a2a4..4e766305 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -65,7 +65,8 @@ libnotmuch_cxx_srcs =		\
 	$(dir)/open.cc		\
 	$(dir)/init.cc		\
 	$(dir)/parse-sexp.cc	\
-	$(dir)/sexp-fp.cc
+	$(dir)/sexp-fp.cc	\
+	$(dir)/lastmod-fp.cc
 
 libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)
 
diff --git a/lib/database-private.h b/lib/database-private.h
index 419b9fe6..b9be4e22 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -381,5 +381,11 @@ _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *q
 notmuch_status_t
 _notmuch_date_strings_to_query (Xapian::valueno slot, const std::string &from, const std::string &to,
 				Xapian::Query &output, std::string &msg);
+
+/* lastmod-fp.h */
+notmuch_status_t
+_notmuch_lastmod_strings_to_query (notmuch_database_t *notmuch,
+				   const std::string &from, const std::string &to,
+				   Xapian::Query &output, std::string &msg);
 #endif
 #endif
diff --git a/lib/lastmod-fp.cc b/lib/lastmod-fp.cc
new file mode 100644
index 00000000..5fdaf281
--- /dev/null
+++ b/lib/lastmod-fp.cc
@@ -0,0 +1,68 @@
+/* lastmod-fp.cc - lastmod range query glue
+ *
+ * This file is part of notmuch.
+ *
+ * Copyright © 2022 David Bremner
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see https://www.gnu.org/licenses/ .
+ *
+ * Author: David Bremner <david@tethera.net>
+ */
+
+#include "database-private.h"
+
+notmuch_status_t
+_notmuch_lastmod_strings_to_query (notmuch_database_t *notmuch,
+				   const std::string &from, const std::string &to,
+				   Xapian::Query &output, std::string &msg)
+{
+    long from_idx = 0L, to_idx = LONG_MAX;
+    long current;
+    std::string str;
+
+    /* revision should not change, but for the avoidance of doubt,
+     * grab for both ends of range, if needed*/
+    current = notmuch_database_get_revision (notmuch, NULL);
+
+    try {
+	if (from.empty ())
+	    from_idx = 0L;
+	else
+	    from_idx = std::stol (from);
+    } catch (std::logic_error &e) {
+	msg = "bad 'from' revision: '" + from + "'";
+	return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+    }
+
+    if (from_idx < 0)
+	from_idx += current;
+
+    try {
+	if (EMPTY_STRING (to))
+	    to_idx = LONG_MAX;
+	else
+	    to_idx = std::stol (to);
+    } catch (std::logic_error &e) {
+	msg = "bad 'to' revision: '" + to + "'";
+	return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+    }
+
+    if (to_idx < 0)
+	to_idx += current;
+
+    output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD,
+			    Xapian::sortable_serialise (from_idx),
+			    Xapian::sortable_serialise (to_idx));
+    return NOTMUCH_STATUS_SUCCESS;
+}
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index e9ef4268..9cadbc13 100644
--- a/lib/parse-sexp.cc
+++ b/lib/parse-sexp.cc
@@ -563,38 +563,13 @@ _sexp_parse_range (notmuch_database_t *notmuch,  const _sexp_prefix_t *prefix,
     }
 
     if (strcmp (prefix->name, "lastmod") == 0) {
-	long from_idx, to_idx;
-
-	try {
-	    if (EMPTY_STRING (from))
-		from_idx = 0L;
-	    else
-		from_idx = std::stol (from);
-	} catch (std::logic_error &e) {
-	    _notmuch_database_log (notmuch, "bad 'from' revision: '%s'\n", from);
-	    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
-	}
-
-	if (from_idx < 0)
-	    from_idx += notmuch_database_get_revision (notmuch, NULL);
-
-	try {
-	    if (EMPTY_STRING (to))
-		to_idx = LONG_MAX;
-	    else
-		to_idx = std::stol (to);
-	} catch (std::logic_error &e) {
-	    _notmuch_database_log (notmuch, "bad 'to' revision: '%s'\n", to);
-	    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+	notmuch_status_t status;
+	status = _notmuch_lastmod_strings_to_query (notmuch, from, to, output, msg);
+	if (status) {
+	    if (! msg.empty ())
+		_notmuch_database_log (notmuch, "%s\n", msg.c_str ());
 	}
-
-	if (to_idx < 0)
-	    to_idx += notmuch_database_get_revision (notmuch, NULL);
-
-	output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD,
-				Xapian::sortable_serialise (from_idx),
-				Xapian::sortable_serialise (to_idx));
-	return NOTMUCH_STATUS_SUCCESS;
+	return status;
     }
 
     _notmuch_database_log (notmuch, "unimplimented range prefix: '%s'\n", prefix->name);
-- 
2.35.1
\r

  reply	other threads:[~2022-08-14 15:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-14 15:02 [PATCH 1/3] lib/sexp: provide relative lastmod queries David Bremner
2022-08-14 15:02 ` David Bremner [this message]
2022-08-14 15:02 ` [PATCH 3/3] lib: add field processor for lastmod: prefix David Bremner
2022-09-03 11:49 ` [PATCH 1/3] lib/sexp: provide relative lastmod queries David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220814150259.517361-2-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).