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