]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/dupe-finder
pop3: reduce memory use while generating the mailbox cache
[public-inbox.git] / scripts / dupe-finder
1 #!/usr/bin/perl -w
2 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # ad-hoc tool for finding duplicates, unstable!
6 use strict;
7 use warnings;
8 use PublicInbox::Inbox;
9 use PublicInbox::Over;
10 use PublicInbox::Search;
11 use PublicInbox::Config;
12 my $repo = shift;
13 my $ibx;
14 if (index($repo, '@') > 0) {
15         $ibx = PublicInbox::Config->new->lookup($repo);
16 } elsif (-d $repo) {
17         $ibx = { inboxdir => $repo, address => 'unnamed@example.com' };
18         $ibx = PublicInbox::Inbox->new($ibx);
19 } else {
20         $ibx = PublicInbox::Config->new->lookup_name($repo);
21 }
22 $ibx or die "No inbox";
23 $ibx->search or die "search not available for inbox";
24 my $over = $ibx->over;
25 my $dbh = $over->dbh;
26
27 sub emit ($) {
28         my ($nums) = @_;
29         foreach my $n (@$nums) {
30                 my $smsg = $over->get_art($n) or next;
31                 print STDERR "$n $smsg->{blob} $smsg->{mid}\n";
32                 my $msg = $ibx->msg_by_smsg($smsg) or next;
33                 print "From $smsg->{blob}\@$n Thu Jan  1 00:00:00 1970\n";
34                 $$msg =~ s/^(>*From )/>$1/gm;
35                 print $$msg, "\n";
36         }
37 }
38
39 my $sth = $dbh->prepare(<<'');
40 SELECT id,num FROM id2num WHERE num > 0 ORDER BY id
41
42 $sth->execute;
43 my $prev_id = -1;
44 my ($id, $num, @nums);
45 while (1) {
46         ($id, $num) = $sth->fetchrow_array;
47         defined $id or last;
48         if ($prev_id != $id) {
49                 emit(\@nums) if scalar(@nums) > 1;
50                 @nums = ();
51         }
52         $prev_id = $id;
53         push @nums, $num;
54 }