Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 3EABD42D28F for ; Sun, 16 Jan 2011 00:11:39 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Tea0jO-JVXzp for ; Sun, 16 Jan 2011 00:11:37 -0800 (PST) Received: from dmz-mailsec-scanner-5.mit.edu (DMZ-MAILSEC-SCANNER-5.MIT.EDU [18.7.68.34]) by olra.theworths.org (Postfix) with ESMTP id B847942D29A for ; Sun, 16 Jan 2011 00:11:24 -0800 (PST) X-AuditID: 12074422-b7c3eae000000a70-4f-4d32a82c456f Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) by dmz-mailsec-scanner-5.mit.edu (Symantec Brightmail Gateway) with SMTP id 48.9A.02672.C28A23D4; Sun, 16 Jan 2011 03:11:24 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id p0G8BOcR004723; Sun, 16 Jan 2011 03:11:24 -0500 Received: from drake.mit.edu (a074.catapulsion.net [70.36.81.74]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id p0G8BMwV010525 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Sun, 16 Jan 2011 03:11:23 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.72) (envelope-from ) id 1PeNhq-0002Xd-FG; Sun, 16 Jan 2011 03:11:22 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH 6/8] Support maildir folder search. Date: Sun, 16 Jan 2011 03:10:56 -0500 Message-Id: <1295165458-9573-7-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1295165458-9573-1-git-send-email-amdragon@mit.edu> References: <1295165458-9573-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: AAAAAA== Cc: amdragon@mit.edu X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 16 Jan 2011 08:11:39 -0000 This implements a folder: query prefix by constructing a wildcard query that matches all files within the specified folder, folder/new, or folder/cur. This works with hierarchical folder names, and accepts both absolute and relative paths. --- lib/database.cc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-) diff --git a/lib/database.cc b/lib/database.cc index 3af82b0..20fd412 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -582,6 +582,57 @@ transform_type_mail (_notmuch_token_t *root, unused (void *opaque)) return _notmuch_token_create_op (root, TOK_AND, mail_ast, root); } +static _notmuch_token_t * +transform_folder_one (const void *ctx, notmuch_database_t *notmuch, + const char *relpath, const char *rest) +{ + const char *db_path; + notmuch_private_status_t status; + unsigned int doc_id; + _notmuch_token_t *tok; + + /* Get the docid for (relpath + rest). */ + if (*rest) + relpath = talloc_asprintf (ctx, "%s%s", relpath, rest); + db_path = _notmuch_database_get_directory_db_path (relpath); + status = _notmuch_database_find_unique_doc_id (notmuch, "directory", + db_path, &doc_id); + if (db_path != relpath) + free ((char*) db_path); + + /* Construct a wildcard query that matches files in this directory. */ + if (status) + /* Directory doesn't exist. Perhaps this should be an error? */ + doc_id = 0; + tok = _notmuch_token_create_term (ctx, TOK_LIT, + talloc_asprintf (ctx, "%u:", doc_id)); + tok->prefix = _find_prefix ("file-direntry"); + return tok; +} + +static _notmuch_token_t * +transform_folder (_notmuch_token_t *root, void *opaque) +{ + if (!root) + return NULL; + if (root->type == TOK_PREFIX && strcmp (root->text, "folder") == 0) { + notmuch_database_t *notmuch = (notmuch_database_t *)opaque; + _notmuch_token_t *lit = root->left, *subs[3], *tok; + const char *relpath; + assert (lit && lit->type == TOK_LIT); + relpath = _notmuch_database_relative_path (notmuch, lit->text); + subs[0] = transform_folder_one (root, notmuch, relpath, ""); + subs[1] = transform_folder_one (root, notmuch, relpath, "/new"); + subs[2] = transform_folder_one (root, notmuch, relpath, "/cur"); + tok = _notmuch_token_create_op (root, TOK_OR, subs[1], subs[2]); + return _notmuch_token_create_op (root, TOK_OR, subs[0], tok); + } + + root->left = transform_folder (root->left, opaque); + root->right = transform_folder (root->right, opaque); + return root; +} + notmuch_database_t * notmuch_database_open (const char *path, notmuch_database_mode_t mode) @@ -690,6 +741,11 @@ notmuch_database_open (const char *path, _notmuch_qparser_add_transform (notmuch->query_parser, transform_type_mail, NULL); + + _notmuch_qparser_add_prefix (notmuch->query_parser, "folder", + TRUE, TRUE); + _notmuch_qparser_add_transform (notmuch->query_parser, + transform_folder, notmuch); } catch (const Xapian::Error &error) { fprintf (stderr, "A Xapian exception occurred opening database: %s\n", error.get_msg().c_str()); -- 1.7.2.3