unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: pabs@debian.org
Subject: [PATCH 1/2] WIP/lib: add count query backend
Date: Mon, 13 Feb 2023 08:26:30 -0400	[thread overview]
Message-ID: <20230213122631.2088558-2-david@tethera.net> (raw)
In-Reply-To: <20230213122631.2088558-1-david@tethera.net>

---
 lib/Makefile.local     |  3 +-
 lib/count-query.cc     | 62 ++++++++++++++++++++++++++++++++++++++++++
 lib/database-private.h |  6 ++++
 3 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 lib/count-query.cc

diff --git a/lib/Makefile.local b/lib/Makefile.local
index 4e766305..cc646946 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -66,7 +66,8 @@ libnotmuch_cxx_srcs =		\
 	$(dir)/init.cc		\
 	$(dir)/parse-sexp.cc	\
 	$(dir)/sexp-fp.cc	\
-	$(dir)/lastmod-fp.cc
+	$(dir)/lastmod-fp.cc    \
+	$(dir)/count-query.cc
 
 libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)
 
diff --git a/lib/count-query.cc b/lib/count-query.cc
new file mode 100644
index 00000000..5d258880
--- /dev/null
+++ b/lib/count-query.cc
@@ -0,0 +1,62 @@
+/* count-query.cc - generate queries for terms on few / many messages.
+ *
+ * This file is part of notmuch.
+ *
+ * Copyright © 2023 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_count_strings_to_query (notmuch_database_t *notmuch, std::string field,
+				 const std::string &from, const std::string &to,
+				 Xapian::Query &output, std::string &msg)
+{
+
+    long from_idx = 0, to_idx = LONG_MAX;
+    std::string term_prefix = _find_prefix (field.c_str ());
+    std::vector<std::string> terms;
+
+    if (! from.empty ()) {
+	try {
+	    from_idx = std::stol(from);
+	} catch (std::logic_error &e) {
+	    msg = "bad 'from' count: '" + from + "'";
+	    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+	}
+    }
+
+    if (! to.empty ()) {
+	try {
+	    to_idx = std::stod(to);
+	} catch (std::logic_error &e) {
+	    msg = "bad 'to' count: '" + to + "'";
+	    return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
+	}
+    }
+
+    for (Xapian::TermIterator it = notmuch->xapian_db->allterms_begin (term_prefix);
+	 it != notmuch->xapian_db->allterms_end (); ++it) {
+	Xapian::doccount freq = it.get_termfreq();
+	if (from_idx <= freq && freq <= to_idx)
+	    terms.push_back (*it);
+    }
+
+    output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
+    return NOTMUCH_STATUS_SUCCESS;
+}
diff --git a/lib/database-private.h b/lib/database-private.h
index b9be4e22..ba96a93c 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -387,5 +387,11 @@ 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);
+
+/* count-query.cc */
+notmuch_status_t
+_notmuch_count_strings_to_query (notmuch_database_t *notmuch, std::string field,
+				 const std::string &from, const std::string &to,
+				 Xapian::Query &output, std::string &msg);
 #endif
 #endif
-- 
2.39.1
\r

  reply	other threads:[~2023-02-13 12:27 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-13 12:26 Proof of concept for counting messages in thread David Bremner
2023-02-13 12:26 ` David Bremner [this message]
2023-02-13 12:26 ` [PATCH 2/2] WIP: support thread count queries David Bremner
2023-02-13 15:39 ` Proof of concept for counting messages in thread Michael J Gruber
2023-02-13 16:32   ` David Bremner
2023-02-13 17:03     ` Michael J Gruber
2023-02-13 20:23       ` David Bremner
2023-02-13 22:36         ` Michael J Gruber
2023-02-14  1:47           ` David Bremner
2023-02-18 17:47             ` Michael J Gruber
2023-02-19 13:04               ` David Bremner
2023-02-19 13:56                 ` 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=20230213122631.2088558-2-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    --cc=pabs@debian.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).