From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id +DWaKLR3v2GUbgAAgWs5BA (envelope-from ) for ; Sun, 19 Dec 2021 19:19:32 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id 2BBDJLR3v2HfFAAAB5/wlQ (envelope-from ) for ; Sun, 19 Dec 2021 18:19:32 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 544A545A7 for ; Sun, 19 Dec 2021 19:19:32 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id B068628E33; Sun, 19 Dec 2021 13:19:14 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id A8B8427E1E for ; Sun, 19 Dec 2021 13:19:05 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 8252F5FC42; Sun, 19 Dec 2021 13:19:05 -0500 (EST) Received: (nullmailer pid 115984 invoked by uid 1000); Sun, 19 Dec 2021 18:19:03 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [RFC Patch 2/3] CLI/address: sort output by frequency with deduplicate=address Date: Sun, 19 Dec 2021 14:18:53 -0400 Message-Id: <20211219181854.115918-3-david@tethera.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20211219181854.115918-1-david@tethera.net> References: <20211219181854.115918-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: VBPVD42QLN3QXDQ6PJPBA74KBMAMGTRN X-Message-ID-Hash: VBPVD42QLN3QXDQ6PJPBA74KBMAMGTRN X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Country: FR ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1639937972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-owner:list-unsubscribe:list-subscribe:list-post; bh=hxq1hYY/Fg5B4/a7+Aunm2xy1GKpwm/H1w4XMTn5WOI=; b=aNVozPAlmYUFh6ifY9gvuVVJr/I/g9JsEywRiD5FSN4LnBVtfMSPwbl6UfwEl/Qv43O4m5 B+LmknHy6phcBiHgGaZJRTbY/6S0zVkmWD9SUaWipGcDLoerXl9Ff1zFxbM1b78BJm9HqO eaumVhrcL/yVgW3T+EuIJj2M6P4B9LjVJdW6muc0QhIoxkwF8N7VaknCzmj0vKmKA2OXPk W/FNe6GNziLWE2OZhuG/l8sBxseh3ti3GtSh8MEQksQqHp4EdvfD0fFhxMOODQjADy73QU ZztJESTHTkIFpKujFUi/hAV23WGdDRvFhi+2pZe7bYkesBC5izf65dAuP5G0tA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1639937972; a=rsa-sha256; cv=none; b=GybrVr4+YcbLq5t19VBPdnusnTM/ElL4gkHW2JXOPWVW0NFcJMunZ0QoltIQ5D94WWFkjw ZRpqCqDqNd0QXByBJwzT489ttzGVIAYcBrhp2Xh86GB864OlZBn1v9JWNVuM6kuBd/cJHf Pz6/sd3h6S6PaCTQnjpkk7LYHB7VTi56EyloX9mndHYUQzAaQt57pa1y20WcgZ1b1hZiCZ HMGoFM62u8x4g0oUkuNfEiDwoJFl++/NkXhXxCXowJMcWayc2t1IaQBgD60Tf7JOElzDok zKeFCH4C0AaNfaq1yQDLIgxv/pyvMkcg8jWB3mcZ0awBuc+ZoLvYFAnhaILgJQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -0.47 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 544A545A7 X-Spam-Score: -0.47 X-Migadu-Scanner: scn0.migadu.com X-TUID: vMcS57yuYqwy Since the current output is not in any guaranteed order, changing the output ordering should not break anything. This is one of the easy cases, since we are already making a complete pass of the matches and storing them in a hash table. --- notmuch-search.c | 19 +++++++++++++++---- test/T095-address.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index 47eec601..ff3967fd 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -64,6 +64,7 @@ typedef struct { int limit; int dupe; GHashTable *addresses; + GList *output_mailboxes; int dedup; } search_context_t; @@ -507,14 +508,20 @@ print_list_value (void *mailbox, void *context) print_mailbox (context, mailbox); } +static gint +compare_count (gconstpointer a, gconstpointer b) +{ + return ((mailbox_t *) b)->count - ((mailbox_t *) a)->count; +} + static void -print_hash_value (unused (void *key), void *list, void *context) +process_hash_value (unused (void *key), void *list, void *context) { - const search_context_t *ctx = context; + search_context_t *ctx = context; if (ctx->dedup == DEDUP_ADDRESS) { mailbox_t *mailbox = summarize_mailboxes (list); - print_mailbox (ctx, mailbox); + ctx->output_mailboxes = g_list_prepend (ctx->output_mailboxes, mailbox); } else g_list_foreach (list, print_list_value, context); } @@ -621,7 +628,10 @@ do_search_messages (search_context_t *ctx) if (ctx->addresses && (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)) { - g_hash_table_foreach (ctx->addresses, print_hash_value, ctx); + g_hash_table_foreach (ctx->addresses, process_hash_value, ctx); + ctx->output_mailboxes = g_list_sort (ctx->output_mailboxes, compare_count); + if (ctx->dedup == DEDUP_ADDRESS) + g_list_foreach (ctx->output_mailboxes, print_list_value, ctx); } notmuch_messages_destroy (messages); @@ -910,6 +920,7 @@ notmuch_address_command (notmuch_database_t *notmuch, int argc, char *argv[]) ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal, _talloc_free_for_g_hash, _list_free_for_g_hash); + ctx->output_mailboxes = NULL; /* empty list, according to glib */ /* The order is not guaranteed if a full pass is required, so go * for fastest. */ diff --git a/test/T095-address.sh b/test/T095-address.sh index 8bb3627a..53886591 100755 --- a/test/T095-address.sh +++ b/test/T095-address.sh @@ -276,6 +276,26 @@ notmuch@notmuchmail.org EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "--deduplicate=address --output=sender --output=recipients --output=count (sort by frequency)" +notmuch address --deduplicate=address --output=sender --output=recipients --output=count '*' | head -n 4 >OUTPUT +cat <EXPECTED +50 notmuch@notmuchmail.org +12 Carl Worth +8 Keith Packard +6 Alexander Botero-Lowry +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "--deduplicate=address --output=sender --output=recipients --output=count (sort by frequency)" +notmuch address --deduplicate=address --output=sender --output=recipients '*' | head -n 4 >OUTPUT +cat <EXPECTED +notmuch@notmuchmail.org +Carl Worth +Keith Packard +Alexander Botero-Lowry +EOF +test_expect_equal_file EXPECTED OUTPUT + generate_message '[from]="Foo Bar "' generate_message '[from]="Foo Bar "' generate_message '[from]="Foo Bar "' @@ -325,6 +345,14 @@ cat <EXPECTED EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "--deduplicate=address --output=sender --output=count (sort by frequency)" +notmuch address --deduplicate=address --output=sender --output=count from:example.com >OUTPUT +cat <EXPECTED +7 Foo Bar +3 Baz +EOF +test_expect_equal_file EXPECTED OUTPUT + if [[ NOTMUCH_HAVE_SFSEXP = 1 ]]; then test_begin_subtest "sexpr query: all messages" notmuch address '*' > EXPECTED -- 2.34.1