Re: [PATCH 1/3] Add notmuch_database_close_compact
authorJani Nikula <jani@nikula.org>
Wed, 17 Oct 2012 18:56:37 +0000 (21:56 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:49:49 +0000 (09:49 -0800)
3a/87df963a0f44c1b29aeb8eff101e56e7d0e92a [new file with mode: 0644]

diff --git a/3a/87df963a0f44c1b29aeb8eff101e56e7d0e92a b/3a/87df963a0f44c1b29aeb8eff101e56e7d0e92a
new file mode 100644 (file)
index 0000000..8dbca32
--- /dev/null
@@ -0,0 +1,272 @@
+Return-Path: <jani@nikula.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 A942F431FBD\r
+       for <notmuch@notmuchmail.org>; Wed, 17 Oct 2012 11:56:45 -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 2m7YkggBlSyj for <notmuch@notmuchmail.org>;\r
+       Wed, 17 Oct 2012 11:56:44 -0700 (PDT)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+       [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 6F3B3431FB6\r
+       for <notmuch@notmuchmail.org>; Wed, 17 Oct 2012 11:56:44 -0700 (PDT)\r
+Received: by mail-lb0-f181.google.com with SMTP id gg6so5832955lbb.26\r
+       for <notmuch@notmuchmail.org>; Wed, 17 Oct 2012 11:56:41 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type:x-gm-message-state;\r
+       bh=MutdBUVJQL7LcUYGv0M6vPG3sqjeS7+gJhRauSGOW7I=;\r
+       b=HcdpiNLfAOgVwqEN4LE9VXJAj3pEMHav/RtjWNVbnjeVkffornnu8hMkKN131ujDht\r
+       jQI5WjVqv8nmtg3xzEtEBhuHqcAh1qyQYCkpKk0CuXKTeKfic01gvQIbqJQH0XKTiYLq\r
+       1yDTafMhuC8RXHaeT8UYnVu27kXAu0v8sGlQoxpv8BOJRpuwdRVThdoAWp3cnq92TgzG\r
+       kXOn/3yXRBmf0QDCFzyPHS0ZMV4WWZkNqvFYER/ydOBUghuc4+eB38wQ5Y8apzgYd+jS\r
+       za0d9qQWjEEU6DBS5Q7CYgt5AGOmRXhcjM+2kEq2altwl/RGpOXh/VUQU7iAwlNI4t8B\r
+       BMcw==\r
+Received: by 10.152.105.103 with SMTP id gl7mr16352899lab.10.1350500201194;\r
+       Wed, 17 Oct 2012 11:56:41 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id jk8sm6827954lab.7.2012.10.17.11.56.38\r
+       (version=SSLv3 cipher=OTHER); Wed, 17 Oct 2012 11:56:39 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Ben Gamari <bgamari.foss@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/3] Add notmuch_database_close_compact\r
+In-Reply-To: <1350487737-32058-2-git-send-email-bgamari.foss@gmail.com>\r
+References: <1350487737-32058-1-git-send-email-bgamari.foss@gmail.com>\r
+       <1350487737-32058-2-git-send-email-bgamari.foss@gmail.com>\r
+User-Agent: Notmuch/0.14+46~g272a1f1 (http://notmuchmail.org) Emacs/23.3.1\r
+       (i686-pc-linux-gnu)\r
+Date: Wed, 17 Oct 2012 21:56:37 +0300\r
+Message-ID: <874nlt2ahm.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQkSMDMYS07Tm4AljSWrxj1HWExJl8BZb/mzlMChLSmQqsoToGrMNmixIbPaLiOhCGTfwVzK\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: Wed, 17 Oct 2012 18:56:45 -0000\r
+\r
+\r
+Hi Ben -\r
+\r
+I'd like some meaningful commit messages here. Please find other\r
+comments inline.\r
+\r
+BR,\r
+Jani.\r
+\r
+On Wed, 17 Oct 2012, Ben Gamari <bgamari.foss@gmail.com> wrote:\r
+> ---\r
+>  configure       |   21 ++++++++++++++++++++-\r
+>  lib/database.cc |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  lib/notmuch.h   |   14 ++++++++++++++\r
+>  3 files changed, 88 insertions(+), 1 deletion(-)\r
+>\r
+> diff --git a/configure b/configure\r
+> index acb90a8..6551b13 100755\r
+> --- a/configure\r
+> +++ b/configure\r
+> @@ -270,7 +270,8 @@ printf "Checking for Xapian development files... "\r
+>  have_xapian=0\r
+>  for xapian_config in ${XAPIAN_CONFIG}; do\r
+>      if ${xapian_config} --version > /dev/null 2>&1; then\r
+> -    printf "Yes (%s).\n" $(${xapian_config} --version | sed -e 's/.* //')\r
+> +    xapian_version=$(${xapian_config} --version | sed -e 's/.* //')\r
+> +    printf "Yes (%s).\n" ${xapian_version}\r
+>      have_xapian=1\r
+>      xapian_cxxflags=$(${xapian_config} --cxxflags)\r
+>      xapian_ldflags=$(${xapian_config} --libs)\r
+> @@ -282,6 +283,20 @@ if [ ${have_xapian} = "0" ]; then\r
+>      errors=$((errors + 1))\r
+>  fi\r
+>  \r
+> +have_xapian_compact=0\r
+> +if [ ${have_xapian} = "1" ]; then\r
+> +    printf "Checking for Xapian compact support... "\r
+> +    case "${xapian_version}" in\r
+> +        0.*|1.[01].*|1.2.[0-5])\r
+> +            printf "No.\n" ;;\r
+> +        [1-9]*.[0-9]*.[0-9]*) \r
+> +            have_xapian_compact=1\r
+> +            printf "Yes.\n" ;;\r
+> +        *)\r
+> +            printf "Unknown version.\n" ;;\r
+> +    esac\r
+> +fi\r
+> +\r
+>  printf "Checking for GMime development files... "\r
+>  have_gmime=0\r
+>  IFS=';'\r
+> @@ -679,6 +694,9 @@ LINKER_RESOLVES_LIBRARY_DEPENDENCIES = ${linker_resolves_library_dependencies}\r
+>  XAPIAN_CXXFLAGS = ${xapian_cxxflags}\r
+>  XAPIAN_LDFLAGS = ${xapian_ldflags}\r
+>  \r
+> +# Whether compact is supported by this version of Xapian\r
+> +HAVE_XAPIAN_COMPACT = ${have_xapian_compact}\r
+> +\r
+>  # Flags needed to compile and link against GMime-2.4\r
+>  GMIME_CFLAGS = ${gmime_cflags}\r
+>  GMIME_LDFLAGS = ${gmime_ldflags}\r
+> @@ -715,6 +733,7 @@ CONFIGURE_CFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS)      \\\r
+>  CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS)    \\\r
+>                   \$(TALLOC_CFLAGS) -DHAVE_VALGRIND=\$(HAVE_VALGRIND) \\\r
+>                   \$(VALGRIND_CFLAGS) \$(XAPIAN_CXXFLAGS)             \\\r
+> +                     -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT)       \\\r
+>                       -DHAVE_STRCASESTR=\$(HAVE_STRCASESTR)\r
+>  CONFIGURE_LDFLAGS =  \$(GMIME_LDFLAGS) \$(TALLOC_LDFLAGS) \$(XAPIAN_LDFLAGS)\r
+>  EOF\r
+> diff --git a/lib/database.cc b/lib/database.cc\r
+> index 761dc1a..6e83a61 100644\r
+> --- a/lib/database.cc\r
+> +++ b/lib/database.cc\r
+> @@ -781,6 +781,60 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+>  }\r
+>  \r
+>  void\r
+> +notmuch_database_close_compact (notmuch_database_t *notmuch)\r
+\r
+It is clear that there are error conditions, so this should be of type\r
+notmuch_status_t. Even if you don't know what to do with the errors now,\r
+you'll find that changing the API is a PITA later.\r
+\r
+> +{\r
+> +    void *local = talloc_new (NULL);\r
+> +    Xapian::Compactor compactor;\r
+> +    char *notmuch_path, *xapian_path, *compact_xapian_path, *old_xapian_path;\r
+> +\r
+> +#if HAVE_XAPIAN_COMPACT\r
+> +    if (! (notmuch_path = talloc_asprintf (local, "%s/%s", notmuch->path, ".notmuch"))) {\r
+> +    fprintf (stderr, "Out of memory\n");\r
+> +    goto DONE;\r
+> +    }\r
+\r
+You could include notmuch->path and .notmuch above into the asprintf\r
+below.\r
+\r
+A personal preference, I think this style would be much more readable:\r
+\r
+    xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian");\r
+    if (!xapian_path) {\r
+        ...\r
+    }\r
+\r
+> +\r
+> +    if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {\r
+> +    fprintf (stderr, "Out of memory\n");\r
+> +    goto DONE;\r
+> +    }\r
+> +\r
+> +    if (! (compact_xapian_path = talloc_asprintf (local, "%s.compact", xapian_path))) {\r
+> +    fprintf (stderr, "Out of memory\n");\r
+> +    goto DONE;\r
+> +    }\r
+> +\r
+> +    if (! (old_xapian_path = talloc_asprintf (local, "%s.old", xapian_path))) {\r
+> +    fprintf (stderr, "Out of memory\n");\r
+> +    goto DONE;\r
+> +    }\r
+> +\r
+> +    try {\r
+> +    compactor.set_renumber(false);\r
+> +    compactor.add_source(xapian_path);\r
+> +    compactor.set_destdir(compact_xapian_path);\r
+> +    compactor.compact();\r
+> +\r
+> +    if (rename(xapian_path, old_xapian_path)) {\r
+> +        fprintf (stderr, "Error moving old database out of the way\n");\r
+> +        goto DONE;\r
+> +    }\r
+> +\r
+> +    if (rename(compact_xapian_path, xapian_path)) {\r
+> +        fprintf (stderr, "Error moving compacted database\n");\r
+> +        goto DONE;\r
+> +    }\r
+\r
+Please add strerror(errno) into the two error prints above. The user\r
+would probably like to know why the errors occurred.\r
+\r
+> +    } catch (Xapian::InvalidArgumentError e) {\r
+\r
+Should this be catch (const Xapian::Error &error) ?\r
+\r
+> +    fprintf (stderr, "Error while compacting: %s", e.get_msg().c_str());\r
+> +    }\r
+> +    \r
+> +#endif\r
+> +\r
+> +    notmuch_database_close(notmuch);\r
+\r
+The database gets closed on Xapian errors, but not on talloc or rename\r
+errors, and the caller has no way of knowing. See the return status\r
+above, but also read on...\r
+\r
+> +DONE:\r
+> +    talloc_free(local);\r
+> +}\r
+> +\r
+> +void\r
+>  notmuch_database_destroy (notmuch_database_t *notmuch)\r
+>  {\r
+>      notmuch_database_close (notmuch);\r
+> diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+> index 3633bed..50babfb 100644\r
+> --- a/lib/notmuch.h\r
+> +++ b/lib/notmuch.h\r
+> @@ -215,6 +215,20 @@ notmuch_database_open (const char *path,\r
+>  void\r
+>  notmuch_database_close (notmuch_database_t *database);\r
+>  \r
+> +/* Close the given notmuch database and then compact it.\r
+\r
+It's the other way round, isn't it?\r
+\r
+But do we want the call to do two things, one of which we already have a\r
+call for (closing the database)? That's not orthogonal. Maybe closing\r
+the database after compaction is the right thing to do (is it?) but even\r
+so, could we leave that responsibility to the API user? The caller does\r
+have to open the database too, and calls to open, compact, close seem\r
+good.\r
+\r
+> + *\r
+> + * After notmuch_database_close_compact has been called, calls to\r
+> + * other functions on objects derived from this database may either\r
+> + * behave as if the database had not been closed (e.g., if the\r
+> + * required data has been cached) or may fail with a\r
+> + * NOTMUCH_STATUS_XAPIAN_EXCEPTION.\r
+> + *\r
+> + * notmuch_database_close_compact can be called multiple times.  Later\r
+> + * calls have no effect.\r
+> + */\r
+> +void\r
+> +notmuch_database_close_compact (notmuch_database_t *notmuch);\r
+> +\r
+>  /* Destroy the notmuch database, closing it if necessary and freeing\r
+>  * all associated resources. */\r
+>  void\r
+> -- \r
+> 1.7.10.4\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r