From a735d21e40291fda5582dea332d30f2b69d4ebdc Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Fri, 6 Jul 2012 14:58:26 +2000 Subject: [PATCH] Re: [PATCH 8/8] emacs: Switch from text to JSON format for search results --- 51/a8acf64290554fa8d441006c77f71156f0cde8 | 243 ++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 51/a8acf64290554fa8d441006c77f71156f0cde8 diff --git a/51/a8acf64290554fa8d441006c77f71156f0cde8 b/51/a8acf64290554fa8d441006c77f71156f0cde8 new file mode 100644 index 000000000..ff629692b --- /dev/null +++ b/51/a8acf64290554fa8d441006c77f71156f0cde8 @@ -0,0 +1,243 @@ +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 4BCFE431FB6 + for ; Thu, 5 Jul 2012 11:58:31 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 ys5Fpnb+gCD1 for ; + Thu, 5 Jul 2012 11:58:30 -0700 (PDT) +Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU + [18.9.25.14]) + by olra.theworths.org (Postfix) with ESMTP id 24219431FAE + for ; Thu, 5 Jul 2012 11:58:30 -0700 (PDT) +X-AuditID: 1209190e-b7fb56d0000008b2-cc-4ff5e3d48e1f +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP + id 7A.15.02226.4D3E5FF4; Thu, 5 Jul 2012 14:58:28 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q65IwRSL003765; + Thu, 5 Jul 2012 14:58:27 -0400 +Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) + (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q65IwQVx027141 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Thu, 5 Jul 2012 14:58:27 -0400 (EDT) +Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77) + (envelope-from ) + id 1SmrFy-00048a-C8; Thu, 05 Jul 2012 14:58:26 -0400 +Date: Thu, 5 Jul 2012 14:58:26 -0400 +From: Austin Clements +To: Mark Walters +Subject: Re: [PATCH 8/8] emacs: Switch from text to JSON format for search + results +Message-ID: <20120705185826.GJ21653@mit.edu> +References: <1341354059-29396-1-git-send-email-amdragon@mit.edu> + <1341354059-29396-9-git-send-email-amdragon@mit.edu> + <87y5myeh8g.fsf@qmul.ac.uk> +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +Content-Disposition: inline +In-Reply-To: <87y5myeh8g.fsf@qmul.ac.uk> +User-Agent: Mutt/1.5.21 (2010-09-15) +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFupjleLIzCtJLcpLzFFi42IRYrdT173y+Ku/wY81ahar5/JYXL85k9ni + zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgy+mZsYi04bVIxr2EKWwPj + U/UuRk4OCQETie8T7rJC2GISF+6tZ+ti5OIQEtjHKHGtfTM7hLOeUeJJ/0wo5wSTxMNZW1kh + nCWMEr+e9DGD9LMIqEhMv/QCzGYT0JDYtn85I4gtIqAjcfvQAnYQm1lAX2LlyZlgNcICIRIL + z39nArF5gWpuHVrIDDF0KqPEuSdvGCESghInZz5hgWjWkrjx7yVQAweQLS2x/B8HSJgTaNfX + 9kawclGgG6ac3MY2gVFoFpLuWUi6ZyF0L2BkXsUom5JbpZubmJlTnJqsW5ycmJeXWqRrrJeb + WaKXmlK6iREU7JySfDsYvx5UOsQowMGoxMNrnPvFX4g1say4MvcQoyQHk5Io7+urX/2F+JLy + UyozEosz4otKc1KLDzFKcDArifD2ZgDleFMSK6tSi/JhUtIcLErivFdSbvoLCaQnlqRmp6YW + pBbBZGU4OJQkeB8/AmoULEpNT61Iy8wpQUgzcXCCDOcBGv4CpIa3uCAxtzgzHSJ/ilFRCmg0 + SEIAJJFRmgfXC0tGrxjFgV4R5n0PUsUDTGRw3a+ABjMBDc5b/AlkcEkiQkqqgdH3Trh6iVcp + T2S4RchEYb19FzJs87ZktRQJbawRirF28Pm78eD9GafYUqzfPFx9uG6qwh3pH4uWOmzcmzCv + PafUtfXeFRuGG8+/ztwduDbJ13SavSfL7zd/av2Psy+q4T/6TIvDTsZd3rHlEO/M5rXuv+6k + ClhE8y3/wt7C47Rz2te3wbIW2UosxRmJhlrMRcWJAL5Ak2ohAwAA +Cc: tomi.ollila@iki.fi, notmuch@notmuchmail.org +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: Thu, 05 Jul 2012 18:58:31 -0000 + +Quoth Mark Walters on Jul 05 at 9:37 am: +> On Tue, 03 Jul 2012, Austin Clements wrote: +> > The JSON format eliminates the complex escaping issues that have +> > plagued the text search format. This uses the incremental JSON parser +> > so that, like the text parser, it can output search results +> > incrementally. +> > +> > This slows down the parser by about ~4X, but puts us in a good +> > position to optimize either by improving the JSON parser (evidence +> > suggests this can reduce the overhead to ~40% over the text format) or +> > by switching to S-expressions (evidence suggests this will more than +> > double performance over the text parser). [1] +> > +> > This also fixes the incremental search parsing test. +> > +> > [1] id:"20110720205007.GB21316@mit.edu" +> > --- +> > emacs/notmuch.el | 113 ++++++++++++++++++++++++++++++++---------------------- +> > test/emacs | 1 - +> > 2 files changed, 67 insertions(+), 47 deletions(-) +> > +> > diff --git a/emacs/notmuch.el b/emacs/notmuch.el +> > index 084cec6..2a09a98 100644 +> > --- a/emacs/notmuch.el +> > +++ b/emacs/notmuch.el +> > @@ -60,7 +60,7 @@ +> > (require 'notmuch-message) +> > +> > (defcustom notmuch-search-result-format +> > - `(("date" . "%s ") +> > + `(("date" . "%12s ") +> > ("count" . "%-7s ") +> > ("authors" . "%-20s ") +> > ("subject" . "%s ") +> > @@ -557,17 +557,14 @@ This function advances the next thread when finished." +> > (notmuch-search-tag '("-inbox")) +> > (notmuch-search-next-thread)) +> > +> > -(defvar notmuch-search-process-filter-data nil +> > - "Data that has not yet been processed.") +> > -(make-variable-buffer-local 'notmuch-search-process-filter-data) +> > - +> > (defun notmuch-search-process-sentinel (proc msg) +> > "Add a message to let user know when \"notmuch search\" exits" +> > (let ((buffer (process-buffer proc)) +> > (status (process-status proc)) +> > (exit-status (process-exit-status proc)) +> > (never-found-target-thread nil)) +> > - (if (memq status '(exit signal)) +> > + (when (memq status '(exit signal)) +> > + (kill-buffer (process-get proc 'parse-buf)) +> > (if (buffer-live-p buffer) +> > (with-current-buffer buffer +> > (save-excursion +> > @@ -577,8 +574,6 @@ This function advances the next thread when finished." +> > (if (eq status 'signal) +> > (insert "Incomplete search results (search process was killed).\n")) +> > (when (eq status 'exit) +> > - (if notmuch-search-process-filter-data +> > - (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data))) +> > (insert "End of search results.") +> > (unless (= exit-status 0) +> > (insert (format " (process returned %d)" exit-status))) +> > @@ -757,45 +752,62 @@ non-authors is found, assume that all of the authors match." +> > (insert (apply #'format string objects)) +> > (insert "\n"))) +> > +> > +(defvar notmuch-search-process-state nil +> > + "Parsing state of the search process filter.") +> > + +> > +(defvar notmuch-search-json-parser nil +> > + "Incremental JSON parser for the search process filter.") +> > + +> > (defun notmuch-search-process-filter (proc string) +> > "Process and filter the output of \"notmuch search\"" +> > - (let ((buffer (process-buffer proc))) +> > - (if (buffer-live-p buffer) +> > - (with-current-buffer buffer +> > - (let ((line 0) +> > - (more t) +> > - (inhibit-read-only t) +> > - (string (concat notmuch-search-process-filter-data string))) +> > - (setq notmuch-search-process-filter-data nil) +> > - (while more +> > - (while (and (< line (length string)) (= (elt string line) ?\n)) +> > - (setq line (1+ line))) +> > - (if (string-match "^thread:\\([0-9A-Fa-f]*\\) \\([^][]*\\) \\[\\([0-9]*\\)/\\([0-9]*\\)\\] \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line) +> > - (let* ((thread-id (match-string 1 string)) +> > - (tags-str (match-string 7 string)) +> > - (result (list :thread thread-id +> > - :date_relative (match-string 2 string) +> > - :matched (string-to-number +> > - (match-string 3 string)) +> > - :total (string-to-number +> > - (match-string 4 string)) +> > - :authors (match-string 5 string) +> > - :subject (match-string 6 string) +> > - :tags (if tags-str +> > - (save-match-data +> > - (split-string tags-str)))))) +> > - (if (/= (match-beginning 0) line) +> > - (notmuch-search-show-error +> > - (substring string line (match-beginning 0)))) +> > - (notmuch-search-show-result result) +> > - (set 'line (match-end 0))) +> > - (set 'more nil) +> > - (while (and (< line (length string)) (= (elt string line) ?\n)) +> > - (setq line (1+ line))) +> > - (if (< line (length string)) +> > - (setq notmuch-search-process-filter-data (substring string line))) +> > - )))) +> > - (delete-process proc)))) +> > + (let ((results-buf (process-buffer proc)) +> > + (parse-buf (process-get proc 'parse-buf)) +> > + (inhibit-read-only t) +> > + done) +> > + (if (not (buffer-live-p results-buf)) +> > + (delete-process proc) +> > + (with-current-buffer parse-buf +> > + ;; Insert new data +> > + (save-excursion +> > + (goto-char (point-max)) +> > + (insert string))) +> > + (with-current-buffer results-buf +> > + (while (not done) +> > + (condition-case nil +> > + (case notmuch-search-process-state +> > + ((begin) +> > + ;; Enter the results list +> > + (if (eq (notmuch-json-begin-compound +> > + notmuch-search-json-parser) 'retry) +> > + (setq done t) +> > + (setq notmuch-search-process-state 'result))) +> > + ((result) +> > + ;; Parse a result +> > + (let ((result (notmuch-json-read notmuch-search-json-parser))) +> > + (case result +> > + ((retry) (setq done t)) +> > + ((end) (setq notmuch-search-process-state 'end)) +> > + (otherwise (notmuch-search-show-result result))))) +> > + ((end) +> > + ;; Any trailing data is unexpected +> > + (with-current-buffer parse-buf +> > + (skip-chars-forward " \t\r\n") +> > + (if (eobp) +> > + (setq done t) +> > + (signal 'json-error nil))))) +> +> This looks good to me. Would it make sense to put the "Any trailing +> data" as a tiny function in notmuch-lib? something like +> +> (defun notmuch-json-assert-end-of-data () +> (skip-chars-forward " \t\r\n") +> (if (eobp) +> (setq done t) +> (signal 'json-error nil))) + +Also a good idea. + +Thanks for the review! I'll be sending v2 shortly. + +> Best wishes +> +> Mark -- 2.26.2