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 8FE20431FD2 for ; Sat, 9 Mar 2013 07:12:48 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" 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 FkPO22usFL2s for ; Sat, 9 Mar 2013 07:12:47 -0800 (PST) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com [209.85.215.48]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id D4298431FC9 for ; Sat, 9 Mar 2013 07:12:44 -0800 (PST) Received: by mail-la0-f48.google.com with SMTP id fq13so2568115lab.21 for ; Sat, 09 Mar 2013 07:12:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:in-reply-to:references:x-gm-message-state; bh=9A+71CoOyZaSFjWmtaMl0g0rmc+JXkDZca9xWkSmEFQ=; b=MT90g4qR2EZfACQvddxh8mJNCnCgsSKqBLAfinbfUCm6NGRfq4CPYnvFBG2rtuihDf IIpGdgLpgsZfnIdnTmPmLCZcSffJiURMOvGCuMbIZhvYNnxQR53LO+fOqnftSGf1TIEC YhVLcRwlI6YNR7w2Xu22adStlm4kgchKB+O+KsAh/xif1tkIeTdl9IMana5hN4YrG4W5 A8LhszDNoLdeRGzfUBonwjVREKE7343bgf+IfxKpMGGl4thkiJEYBZyr7dXzzNI1VSrf 1MH+BU08hx+0NJGvTAbHSAoW44FZERAEjhEAp6l5CeikuWWS27j54XiMBwDl51NCyBrB 7Mng== X-Received: by 10.112.16.199 with SMTP id i7mr2418294lbd.65.1362841963195; Sat, 09 Mar 2013 07:12:43 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. [80.223.81.27]) by mx.google.com with ESMTPS id iq6sm4274815lab.10.2013.03.09.07.12.40 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 09 Mar 2013 07:12:42 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH v2 3/6] cli: add --batch option to notmuch count Date: Sat, 9 Mar 2013 17:12:23 +0200 Message-Id: X-Mailer: git-send-email 1.7.10.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQmi/y3DgnLZQ3esgkN+9Tq0Whs6kISCi1KhtNcg56ci5w25ko5sIDnHfMC1YEODjP0etJ7O 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, 09 Mar 2013 15:12:49 -0000 Add support for reading queries from stdin, one per line, and writing results to stdin, one per line. This will bring considerable performance improvements when utilized in Emacs notmuch-hello, especially so when running remote notmuch. --- notmuch-count.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/notmuch-count.c b/notmuch-count.c index 630f036..8772cff 100644 --- a/notmuch-count.c +++ b/notmuch-count.c @@ -62,6 +62,27 @@ print_count (notmuch_database_t *notmuch, const char *query_str, return 0; } +static int +count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags, + size_t exclude_tags_length, int output) +{ + char *line = NULL; + ssize_t line_len; + size_t line_size; + int ret = 0; + + while (!ret && (line_len = getline (&line, &line_size, input)) != -1) { + chomp_newline (line); + ret = print_count (notmuch, line, exclude_tags, exclude_tags_length, + output); + } + + if (line) + free (line); + + return ret; +} + int notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) { @@ -72,6 +93,9 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) int exclude = EXCLUDE_TRUE; const char **search_exclude_tags = NULL; size_t search_exclude_tags_length = 0; + notmuch_bool_t batch = FALSE; + FILE *input = stdin; + char *input_file_name = NULL; int ret; notmuch_opt_desc_t options[] = { @@ -83,6 +107,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE }, { "false", EXCLUDE_FALSE }, { 0, 0 } } }, + { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 }, + { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 }, { 0, 0, 0, 0, 0 } }; @@ -92,6 +118,21 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) return 1; } + if (input_file_name) { + batch = TRUE; + input = fopen (input_file_name, "r"); + if (input == NULL) { + fprintf (stderr, "Error opening %s for reading: %s\n", + input_file_name, strerror (errno)); + return 1; + } + } + + if (batch && opt_index != argc) { + fprintf (stderr, "--batch and query string are not compatible\n"); + return 1; + } + if (notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) return 1; @@ -107,10 +148,17 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) (config, &search_exclude_tags_length); } - ret = print_count (notmuch, query_str, search_exclude_tags, - search_exclude_tags_length, output); + if (batch) + ret = count_file (notmuch, input, search_exclude_tags, + search_exclude_tags_length, output); + else + ret = print_count (notmuch, query_str, search_exclude_tags, + search_exclude_tags_length, output); notmuch_database_destroy (notmuch); + if (input != stdin) + fclose (input); + return ret; } -- 1.7.10.4