[PATCH] WIP: overload 'is:' prefix to support matching all, and roots

Subject: [PATCH] WIP: overload 'is:' prefix to support matching all, and roots

Date: Wed, 9 May 2018 16:16:09 -0400

To: notmuch@notmuchmail.org

Cc:

From: David Bremner


- "*" is problematic because it is not composable. is:* works as part
  of any valid query

- is:root matches messages w/o replyto terms in the database. Except
  for the case of reference loops, these correspond to roots of
  threads.
---

this is a quick prototype. It's not clear the constructor really needs
the query parser and the database, currently they are unused.

I'm not sure how people feel about overloading is. Personally I never
use it, so the threat of collisions is small. We could also choose a
different prefix.

Also this version doesn't deal with regular expressions for is://, but
it could. I'd have to think about how to limit code duplication.

 lib/Makefile.local |  3 ++-
 lib/database.cc    |  3 +++
 lib/is-fp.cc       | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/is-fp.h        | 42 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 lib/is-fp.cc
 create mode 100644 lib/is-fp.h

diff --git a/lib/Makefile.local b/lib/Makefile.local
index 5dc057c0..e2b60ee0 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -59,7 +59,8 @@ libnotmuch_cxx_srcs =		\
 	$(dir)/config.cc	\
 	$(dir)/regexp-fields.cc	\
 	$(dir)/thread.cc \
-	$(dir)/thread-fp.cc
+	$(dir)/thread-fp.cc \
+	$(dir)/is-fp.cc
 
 libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)
 
diff --git a/lib/database.cc b/lib/database.cc
index 9cf8062c..4c3ca281 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -22,6 +22,7 @@
 #include "parse-time-vrp.h"
 #include "query-fp.h"
 #include "thread-fp.h"
+#include "is-fp.h"
 #include "regexp-fields.h"
 #include "string-util.h"
 
@@ -321,6 +322,8 @@ _setup_query_field (const prefix_t *prefix, notmuch_database_t *notmuch)
 	    fp = (new QueryFieldProcessor (*notmuch->query_parser, notmuch))->release ();
 	else if (STRNCMP_LITERAL(prefix->name, "thread") == 0)
 	    fp = (new ThreadFieldProcessor (*notmuch->query_parser, notmuch))->release ();
+	else if (STRNCMP_LITERAL(prefix->name, "is") == 0)
+	    fp = (new IsFieldProcessor (*notmuch->query_parser, notmuch))->release ();
 	else
 	    fp = (new RegexpFieldProcessor (prefix->name, prefix->flags,
 					    *notmuch->query_parser, notmuch))->release ();
diff --git a/lib/is-fp.cc b/lib/is-fp.cc
new file mode 100644
index 00000000..23c62c9b
--- /dev/null
+++ b/lib/is-fp.cc
@@ -0,0 +1,50 @@
+/* is-fp.cc - "is:" field processor glue
+ *
+ * This file is part of notmuch.
+ *
+ * Copyright © 2018 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"
+#include "is-fp.h"
+#include <iostream>
+
+#if HAVE_XAPIAN_FIELD_PROCESSOR
+
+Xapian::Query
+IsFieldProcessor::operator() (const std::string & str)
+{
+    if (str == "root") {
+	const char *reply_to_prefix = _find_prefix("replyto");
+	return Xapian::Query (Xapian::Query::OP_AND_NOT,
+			      Xapian::Query::MatchAll,
+			      Xapian::Query(Xapian::Query::OP_WILDCARD,
+					    reply_to_prefix,
+					    1,
+					    Xapian::Query::WILDCARD_LIMIT_FIRST));
+    } else if (str == "*") {
+	return Xapian::Query::MatchAll;
+    } else {
+	/* fall back on tag */
+	const char *is_prefix = _find_prefix ("is");
+	std::string term = is_prefix + str;
+	return Xapian::Query (term);
+    }
+
+}
+#endif
diff --git a/lib/is-fp.h b/lib/is-fp.h
new file mode 100644
index 00000000..635e2931
--- /dev/null
+++ b/lib/is-fp.h
@@ -0,0 +1,42 @@
+/* is-fp.h - thread field processor glue
+ *
+ * This file is part of notmuch.
+ *
+ * Copyright © 2018 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>
+ */
+
+#ifndef NOTMUCH_IS_FP_H
+#define NOTMUCH_IS_FP_H
+
+#include <xapian.h>
+#include "notmuch.h"
+
+#if HAVE_XAPIAN_FIELD_PROCESSOR
+class IsFieldProcessor : public Xapian::FieldProcessor {
+ protected:
+    Xapian::QueryParser &parser;
+    notmuch_database_t *notmuch;
+
+ public:
+    IsFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)
+	: parser(parser_), notmuch(notmuch_) { };
+
+    Xapian::Query operator()(const std::string & str);
+};
+#endif
+#endif /* NOTMUCH_THREAD_FP_H */
-- 
2.17.0

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch

Thread: