---
 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 <da...@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

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-le...@notmuchmail.org

Reply via email to