Re: RFC: notmuch-cache.el: simple caching of MIME parts
authorDavid Edmondson <dme@dme.org>
Tue, 7 Oct 2014 07:57:09 +0000 (08:57 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:05:08 +0000 (10:05 -0800)
4f/ae6d407a55c04ab643d0d8b85d48bfc483b3a9 [new file with mode: 0644]

diff --git a/4f/ae6d407a55c04ab643d0d8b85d48bfc483b3a9 b/4f/ae6d407a55c04ab643d0d8b85d48bfc483b3a9
new file mode 100644 (file)
index 0000000..ec96c39
--- /dev/null
@@ -0,0 +1,191 @@
+Return-Path: <dme@dme.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 190B8431FB6\r
+       for <notmuch@notmuchmail.org>; Tue,  7 Oct 2014 00:57:19 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id nhCCKu3eRI+9 for <notmuch@notmuchmail.org>;\r
+       Tue,  7 Oct 2014 00:57:14 -0700 (PDT)\r
+Received: from mail-wg0-f49.google.com (mail-wg0-f49.google.com\r
+ [74.125.82.49])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 2B21E431FAE   for <notmuch@notmuchmail.org>; Tue,  7 Oct 2014 00:57:14 -0700\r
+ (PDT)\r
+Received: by mail-wg0-f49.google.com with SMTP id x12so8538054wgg.20\r
+       for <notmuch@notmuchmail.org>; Tue, 07 Oct 2014 00:57:13 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=1e100.net; s=20130820;\r
+       h=x-gm-message-state:to:subject:in-reply-to:references:user-agent\r
+       :from:date:message-id:mime-version:content-type;\r
+       bh=F8Q2p2uCHjkXkNFu+8eMwVRPI9n0G/ButLRhBMNo6/w=;\r
+       b=Kwyd0QoSPFiOM4r7raPBMimZl0dKeVnYayvGtTKhc7LYGH0eNp1VRThsGz5GOPdJEI\r
+       TzkNCge6SqAXV44x9b45hhjWR7/VoSPDLr6StiG4ZBi4A0HQJucS0ECry0PAh7UOAXsj\r
+       GG3ng19M4GPwec9rgQvSXTyutF0Biff7dBGk6qnfkD7XqGF7g5mUJxpoq/+F16v9h/c1\r
+       POdF3o96SwKVQwgx2KAZUq1ccEAxFoS6OsqqIfZ/XWj9fOgDGA6ULB15UahjPWWrhf9J\r
+       lDizaDidVNcBZBDRiduj3JGplWh0KaRT71UXOawaO6ENE5SeswsfnxjVMpdX9wSJZczv\r
+       wOTg==\r
+X-Gm-Message-State:\r
+ ALoCoQnAWptzOvjC3ngQ9uwy2UFb1mVFqxL/A3eGRPh7Zlr2//Ws90WQikRSNPJ+QAOcJnCvcov6\r
+X-Received: by 10.194.119.9 with SMTP id kq9mr2617393wjb.25.1412668632947;\r
+       Tue, 07 Oct 2014 00:57:12 -0700 (PDT)\r
+Received: from localhost ([2a01:348:1a2:1:bc6b:bac4:2954:d414])\r
+       by mx.google.com with ESMTPSA id\r
+       fs13sm13703833wic.19.2014.10.07.00.57.11 for <multiple recipients>\r
+       (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+       Tue, 07 Oct 2014 00:57:12 -0700 (PDT)\r
+To: notmuch@notmuchmail.org\r
+Subject: Re: RFC: notmuch-cache.el: simple caching of MIME parts\r
+In-Reply-To: <m2wq8cmi2c.fsf@fenchurch.hh.sledj.net>\r
+References: <m2wq8cmi2c.fsf@fenchurch.hh.sledj.net>\r
+User-Agent: Notmuch/0.18.1 (http://notmuchmail.org) Emacs/24.3.1\r
+       (x86_64-apple-darwin13.3.0)\r
+From: David Edmondson <dme@dme.org>\r
+Date: Tue, 07 Oct 2014 08:57:09 +0100\r
+Message-ID: <m2tx3gmhwq.fsf@fenchurch.hh.sledj.net>\r
+MIME-Version: 1.0\r
+Content-Type: multipart/mixed; boundary="=-=-="\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 07 Oct 2014 07:57:19 -0000\r
+\r
+--=-=-=\r
+Content-Type: text/plain\r
+\r
+On Tue, Oct 07 2014, David Edmondson wrote:\r
+> I've been using remote-notmuch over a 3G connection quite a lot\r
+> recently. Dragging down the same MIME part more than once is annoying,\r
+> so here is a very simple filesystem based cache of MIME parts for\r
+> notmuch. It's integrated using defadvice for now, but a cleaner approach\r
+> is obviously possible.\r
+>\r
+> No suggestion that this should be integrated - it's just a toy to prompt\r
+> discussion.\r
+\r
+Bah. Attached the auto-save file :-p\r
+\r
+\r
+--=-=-=\r
+Content-Type: application/emacs-lisp\r
+Content-Disposition: attachment; filename=notmuch-cache.el\r
+Content-Transfer-Encoding: quoted-printable\r
+\r
+;; notmuch-cache.el --- a simple local filesystem cache of MIME parts\r
+;;\r
+;; Copyright =C2=A9 David Edmondson\r
+;;\r
+;; This file is not part of Notmuch.\r
+;;\r
+;; Notmuch is free software: you can redistribute it and/or modify it\r
+;; under the terms of the GNU General Public License as published by\r
+;; the Free Software Foundation, either version 3 of the License, or\r
+;; (at your option) any later version.\r
+;;\r
+;; Notmuch is distributed in the hope that it will be useful, but\r
+;; WITHOUT ANY WARRANTY; without even the implied warranty of\r
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+;; General Public License for more details.\r
+;;\r
+;; You should have received a copy of the GNU General Public License\r
+;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
+;;\r
+;; Authors: David Edmondson <dme@dme.org>\r
+\r
+;; Things to consider:\r
+;;\r
+;; - Keep an in-memory cache of parts? What would be an appropriate\r
+;;   way to limit the size of that cache?\r
+;;\r
+;; - In a long thread with mostly non-matching messages (i.e. new\r
+;;   message in old thread), the default show output includes the text\r
+;;   of all of the non-matching parts. Given that we can now insert\r
+;;   previously unshown parts on the fly, maybe non-matching body\r
+;;   parts should not be included in the default output, and code\r
+;;   added to glue them in when a non-matching message is\r
+;;   opened. (Would this be simpler if we convert simple text/plain\r
+;;   messages to look like MIME messages with a single text/plain\r
+;;   part?)\r
+;;\r
+;; - There is no cache management functionality. Just delete the\r
+;;   directory and its' contents for now.\r
+\r
+;; Stats:\r
+\r
+(defvar notmuch-cache-stats [0 0 0]\r
+  "Statistics about the notmuch part cache.\r
+\r
+This is an array with three elements:\r
+ - the number of requests to the cache,\r
+ - the number of reads satisified from the cache,\r
+ - the number of writes to the cache.")\r
+\r
+(defun notmuch-cache-stat-incr (nth)\r
+  (aset notmuch-cache-stats nth (+ 1 (aref notmuch-cache-stats nth))))\r
+\r
+;; The cache:\r
+\r
+(defcustom notmuch-cache-directory "~/.notmuch-cache/parts"\r
+  "A directory used to store cached message parts."\r
+  :type 'directory)\r
+\r
+(defun notmuch-cache-filename (id part)\r
+  (expand-file-name (format "%s/%s/%d" notmuch-cache-directory id part)))\r
+\r
+(defun notmuch-cache-get (id part)\r
+  "Return a cached copy of ID's MIME PART, or nil."\r
+\r
+  (notmuch-cache-stat-incr 0) ;; Request.\r
+=20=20\r
+  (let ((filename (notmuch-cache-filename id part)))\r
+    (if (not (file-exists-p filename))\r
+       nil\r
+      (notmuch-cache-stat-incr 1) ;; Read.\r
+      (let ((coding-system-for-read 'no-conversion))\r
+       (with-temp-buffer\r
+         (insert-file-contents (notmuch-cache-filename id part))\r
+         (buffer-substring (point-min) (point-max)))))))\r
+\r
+(defun notmuch-cache-put (id part content)\r
+  "Cache a copy of ID's MIME PART using CONTENT."\r
+\r
+  (let ((filename (notmuch-cache-filename id part)))\r
+    (unless (file-exists-p filename)\r
+      (notmuch-cache-stat-incr 2) ;; Write.\r
+      (make-directory (file-name-directory filename) t)\r
+      (let ((coding-system-for-write 'no-conversion))\r
+       (with-temp-buffer\r
+         (insert content)\r
+         (write-region (point-min) (point-max) (notmuch-cache-filename id part)))=\r
+))))\r
+\r
+;; Integration:\r
+\r
+(defadvice notmuch-get-bodypart-internal (around notmuch-bodypart-cache act=\r
+ivate)\r
+  "Use a local cache of notmuch body parts."\r
+\r
+  (let ((content (notmuch-cache-get query part-number)))\r
+    (if content\r
+       (setq ad-return-value content)\r
+      ad-do-it\r
+      (when ad-return-value\r
+       (notmuch-cache-put query part-number ad-return-value)))))\r
+\r
+--=-=-=--\r