--- /dev/null
+Return-Path: <felipe.contreras@gmail.com>\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 F03FE431FDB\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Nov 2013 07:03:02 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, 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 vOf4xT0Ptb8S for <notmuch@notmuchmail.org>;\r
+ Sat, 2 Nov 2013 07:02:58 -0700 (PDT)\r
+Received: from mail-ob0-f178.google.com (mail-ob0-f178.google.com\r
+ [209.85.214.178]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id DB5DB431E84\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Nov 2013 07:02:47 -0700 (PDT)\r
+Received: by mail-ob0-f178.google.com with SMTP id wm4so5670601obc.9\r
+ for <notmuch@notmuchmail.org>; Sat, 02 Nov 2013 07:02:47 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=+ZfgrmQzPE38o4wlBz4uVAMsLZ/eboJjZBSg2WqKLx8=;\r
+ b=NCScndsoDjjS78SUmCW/3QG8iC6a1W1gwPTHJd1/rcIVHanTIcUl5gZeG5raYMTq+2\r
+ KXJqv5xE7+bAYGKAG1+emEV7PxJ+rwjBCgtJJV2S03ezp2wIFDg/AIUiVMhpRBMvKUTU\r
+ d+5q/xOgJtoYtkUXLcWsaLvmKCEUHHibXXB7Qa4R6KPlXiTjwGVMV3HXFKv6hWFTIW6o\r
+ SZCidYuxgJwbe5hOiV9k28ABFK6VNN0UUDAE+cvMYsSyPtlWyOjZNlQ6Jgw6UWn7TMDq\r
+ o+WMWzzKoWqehP+njnqwlohDSbvze2s8f1a2xzfUGfjjtRKSjwW5iYEI41q/J2Ilw2IH\r
+ VwBw==\r
+X-Received: by 10.60.146.229 with SMTP id tf5mr6429027oeb.27.1383400967397;\r
+ Sat, 02 Nov 2013 07:02:47 -0700 (PDT)\r
+Received: from localhost (187-162-140-241.static.axtel.net. [187.162.140.241])\r
+ by mx.google.com with ESMTPSA id\r
+ d8sm24737810oeu.6.2013.11.02.07.02.45 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
+ Sat, 02 Nov 2013 07:02:46 -0700 (PDT)\r
+From: Felipe Contreras <felipe.contreras@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 04/21] vim: refactor database handling\r
+Date: Sat, 2 Nov 2013 07:55:37 -0600\r
+Message-Id: <1383400554-1832-5-git-send-email-felipe.contreras@gmail.com>\r
+X-Mailer: git-send-email 1.8.4.2+fc1\r
+In-Reply-To: <1383400554-1832-1-git-send-email-felipe.contreras@gmail.com>\r
+References: <1383400554-1832-1-git-send-email-felipe.contreras@gmail.com>\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: Sat, 02 Nov 2013 14:03:03 -0000\r
+\r
+To minimize memory usage we need to destroy the queries and the\r
+databases, so we should keep track of them.\r
+\r
+Each buffer gets a database connection that is destroyed when the buffer\r
+is destroyed, and all the queries along with it.\r
+\r
+Ideally notmuch should destroy the queries when the database is\r
+destroyed, but it's not doing that at the moment.\r
+\r
+Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>\r
+---\r
+ vim/notmuch.vim | 125 ++++++++++++++++++++++++++++++++------------------------\r
+ 1 file changed, 72 insertions(+), 53 deletions(-)\r
+\r
+diff --git a/vim/notmuch.vim b/vim/notmuch.vim\r
+index 11a7c26..72bf73b 100644\r
+--- a/vim/notmuch.vim\r
++++ b/vim/notmuch.vim\r
+@@ -80,6 +80,7 @@ function! s:new_file_buffer(type, fname)\r
+ exec printf('edit %s', a:fname)\r
+ execute printf('set filetype=notmuch-%s', a:type)\r
+ execute printf('set syntax=notmuch-%s', a:type)\r
++ ruby $curbuf.init(VIM::evaluate('a:type'))\r
+ ruby $buf_queue.push($curbuf.number)\r
+ endfunction\r
+ \r
+@@ -195,6 +196,7 @@ function! s:search_search_prompt()\r
+ setlocal modifiable\r
+ ruby << EOF\r
+ $cur_search = VIM::evaluate('text')\r
++ $curbuf.reopen\r
+ search_render($cur_search)\r
+ EOF\r
+ setlocal nomodifiable\r
+@@ -206,6 +208,7 @@ endfunction\r
+ \r
+ function! s:search_refresh()\r
+ setlocal modifiable\r
++ ruby $curbuf.reopen\r
+ ruby search_render($cur_search)\r
+ setlocal nomodifiable\r
+ endfunction\r
+@@ -227,6 +230,7 @@ endfunction\r
+ \r
+ function! s:folders_refresh()\r
+ setlocal modifiable\r
++ ruby $curbuf.reopen\r
+ ruby folders_render()\r
+ setlocal nomodifiable\r
+ endfunction\r
+@@ -254,6 +258,7 @@ function! s:show_next_thread()\r
+ endfunction\r
+ \r
+ function! s:kill_this_buffer()\r
++ ruby $curbuf.close\r
+ bdelete!\r
+ ruby << EOF\r
+ $buf_queue.pop\r
+@@ -276,6 +281,7 @@ function! s:new_buffer(type)\r
+ keepjumps 0d\r
+ execute printf('set filetype=notmuch-%s', a:type)\r
+ execute printf('set syntax=notmuch-%s', a:type)\r
++ ruby $curbuf.init(VIM::evaluate('a:type'))\r
+ ruby $buf_queue.push($curbuf.number)\r
+ endfunction\r
+ \r
+@@ -295,33 +301,31 @@ ruby << EOF\r
+ $cur_thread = thread_id\r
+ $messages.clear\r
+ $curbuf.render do |b|\r
+- do_read do |db|\r
+- q = db.query(get_cur_view)\r
+- q.sort = 0\r
+- msgs = q.search_messages\r
+- msgs.each do |msg|\r
+- m = Mail.read(msg.filename)\r
+- part = m.find_first_text\r
+- nm_m = Message.new(msg, m)\r
+- $messages << nm_m\r
+- date_fmt = VIM::evaluate('g:notmuch_rb_datetime_format')\r
+- date = Time.at(msg.date).strftime(date_fmt)\r
+- nm_m.start = b.count\r
+- b << "%s %s (%s)" % [msg['from'], date, msg.tags]\r
+- b << "Subject: %s" % [msg['subject']]\r
+- b << "To: %s" % msg['to']\r
+- b << "Cc: %s" % msg['cc']\r
+- b << "Date: %s" % msg['date']\r
+- nm_m.body_start = b.count\r
+- b << "--- %s ---" % part.mime_type\r
+- part.convert.each_line do |l|\r
+- b << l.chomp\r
+- end\r
+- b << ""\r
+- nm_m.end = b.count\r
++ q = $curbuf.query(get_cur_view)\r
++ q.sort = 0\r
++ msgs = q.search_messages\r
++ msgs.each do |msg|\r
++ m = Mail.read(msg.filename)\r
++ part = m.find_first_text\r
++ nm_m = Message.new(msg, m)\r
++ $messages << nm_m\r
++ date_fmt = VIM::evaluate('g:notmuch_rb_datetime_format')\r
++ date = Time.at(msg.date).strftime(date_fmt)\r
++ nm_m.start = b.count\r
++ b << "%s %s (%s)" % [msg['from'], date, msg.tags]\r
++ b << "Subject: %s" % [msg['subject']]\r
++ b << "To: %s" % msg['to']\r
++ b << "Cc: %s" % msg['cc']\r
++ b << "Date: %s" % msg['date']\r
++ nm_m.body_start = b.count\r
++ b << "--- %s ---" % part.mime_type\r
++ part.convert.each_line do |l|\r
++ b << l.chomp\r
+ end\r
+- b.delete(b.count)\r
++ b << ""\r
++ nm_m.end = b.count\r
+ end\r
++ b.delete(b.count)\r
+ end\r
+ $messages.each_with_index do |msg, i|\r
+ VIM::command("syntax region nmShowMsg#{i}Desc start='\\%%%il' end='\\%%%il' contains=@nmShowMsgDesc" % [msg.start, msg.start + 1])\r
+@@ -469,24 +473,6 @@ ruby << EOF\r
+ end\r
+ end\r
+ \r
+- def do_write\r
+- db = Notmuch::Database.new($db_name, :mode => Notmuch::MODE_READ_WRITE)\r
+- begin\r
+- yield db\r
+- ensure\r
+- db.close\r
+- end\r
+- end\r
+-\r
+- def do_read\r
+- db = Notmuch::Database.new($db_name)\r
+- begin\r
+- yield db\r
+- ensure\r
+- db.close\r
+- end\r
+- end\r
+-\r
+ def open_reply(orig)\r
+ help_lines = [\r
+ 'Notmuch-Help: Type in your message here; to help you use these bindings:',\r
+@@ -559,21 +545,18 @@ ruby << EOF\r
+ folders = VIM::evaluate('g:notmuch_rb_folders')\r
+ count_threads = VIM::evaluate('g:notmuch_rb_folders_count_threads')\r
+ $searches.clear\r
+- do_read do |db|\r
+- folders.each do |name, search|\r
+- q = db.query(search)\r
+- $searches << search\r
+- count = count_threads ? q.search_threads.count : q.search_messages.count\r
+- b << "%9d %-20s (%s)" % [count, name, search]\r
+- end\r
++ folders.each do |name, search|\r
++ q = $curbuf.query(search)\r
++ $searches << search\r
++ count = count_threads ? q.search_threads.count : q.search_messages.count\r
++ b << "%9d %-20s (%s)" % [count, name, search]\r
+ end\r
+ end\r
+ end\r
+ \r
+ def search_render(search)\r
+ date_fmt = VIM::evaluate('g:notmuch_rb_date_format')\r
+- db = Notmuch::Database.new($db_name)\r
+- q = db.query(search)\r
++ q = $curbuf.query(search)\r
+ $threads.clear\r
+ t = q.search_threads\r
+ \r
+@@ -593,7 +576,7 @@ ruby << EOF\r
+ end\r
+ \r
+ def do_tag(filter, tags)\r
+- do_write do |db|\r
++ $curbuf.do_write do |db|\r
+ q = db.query(filter)\r
+ q.search_messages.each do |e|\r
+ e.freeze\r
+@@ -610,6 +593,40 @@ ruby << EOF\r
+ e.thaw\r
+ e.tags_to_maildir_flags\r
+ end\r
++ q.destroy!\r
++ end\r
++ end\r
++\r
++ module DbHelper\r
++ def init(name)\r
++ @name = name\r
++ @db = Notmuch::Database.new($db_name)\r
++ @queries = []\r
++ end\r
++\r
++ def query(*args)\r
++ q = @db.query(*args)\r
++ @queries << q\r
++ q\r
++ end\r
++\r
++ def close\r
++ @queries.delete_if { |q| ! q.destroy! }\r
++ @db.close\r
++ end\r
++\r
++ def reopen\r
++ close if @db\r
++ @db = Notmuch::Database.new($db_name)\r
++ end\r
++\r
++ def do_write\r
++ db = Notmuch::Database.new($db_name, :mode => Notmuch::MODE_READ_WRITE)\r
++ begin\r
++ yield db\r
++ ensure\r
++ db.close\r
++ end\r
+ end\r
+ end\r
+ \r
+@@ -658,6 +675,8 @@ ruby << EOF\r
+ end\r
+ \r
+ class VIM::Buffer\r
++ include DbHelper\r
++\r
+ def <<(a)\r
+ append(count(), a)\r
+ end\r
+-- \r
+1.8.4.2+fc1\r
+\r