]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MiscIdx.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / MiscIdx.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # like PublicInbox::SearchIdx, but for searching for non-mail messages.
5 # Things indexed include:
6 # * inboxes themselves
7 # * epoch information
8 # * (maybe) git code repository information
9 # Expect ~100K-1M documents with no parallelism opportunities,
10 # so no sharding, here.
11 #
12 # See MiscSearch for read-only counterpart
13 package PublicInbox::MiscIdx;
14 use strict;
15 use v5.10.1;
16 use PublicInbox::InboxWritable;
17 use PublicInbox::Search; # for SWIG Xapian and Search::Xapian compat
18 use PublicInbox::SearchIdx qw(index_text term_generator add_val);
19 use PublicInbox::Spawn qw(nodatacow_dir);
20 use Carp qw(croak);
21 use File::Path ();
22 use PublicInbox::MiscSearch;
23 use PublicInbox::Config;
24 my $json;
25
26 sub new {
27         my ($class, $eidx) = @_;
28         PublicInbox::SearchIdx::load_xapian_writable();
29         my $mi_dir = "$eidx->{xpfx}/misc";
30         File::Path::mkpath($mi_dir);
31         nodatacow_dir($mi_dir);
32         my $flags = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
33         $flags |= $PublicInbox::SearchIdx::DB_NO_SYNC if $eidx->{-no_fsync};
34         $json //= PublicInbox::Config::json();
35         bless {
36                 mi_dir => $mi_dir,
37                 flags => $flags,
38                 indexlevel => 'full', # small DB, no point in medium?
39         }, $class;
40 }
41
42 sub begin_txn {
43         my ($self) = @_;
44         croak 'BUG: already in txn' if $self->{xdb}; # XXX make lazy?
45         my $wdb = $PublicInbox::Search::X{WritableDatabase};
46         my $xdb = eval { $wdb->new($self->{mi_dir}, $self->{flags}) };
47         croak "Failed opening $self->{mi_dir}: $@" if $@;
48         $self->{xdb} = $xdb;
49         $xdb->begin_transaction;
50 }
51
52 sub commit_txn {
53         my ($self) = @_;
54         croak 'BUG: not in txn' unless $self->{xdb}; # XXX make lazy?
55         delete($self->{xdb})->commit_transaction;
56 }
57
58 sub remove_eidx_key {
59         my ($self, $eidx_key) = @_;
60         my $xdb = $self->{xdb};
61         my $head = $xdb->postlist_begin('Q'.$eidx_key);
62         my $tail = $xdb->postlist_end('Q'.$eidx_key);
63         my @docids; # only one, unless we had bugs
64         for (; $head != $tail; $head++) {
65                 push @docids, $head->get_docid;
66         }
67         for my $docid (@docids) {
68                 $xdb->delete_document($docid);
69                 warn "I: remove inbox docid #$docid ($eidx_key)\n";
70         }
71 }
72
73 # adds or updates according to $eidx_key
74 sub index_ibx {
75         my ($self, $ibx) = @_;
76         my $eidx_key = $ibx->eidx_key;
77         my $xdb = $self->{xdb};
78         # Q = uniQue in Xapian terminology
79         my $head = $xdb->postlist_begin('Q'.$eidx_key);
80         my $tail = $xdb->postlist_end('Q'.$eidx_key);
81         my ($docid, @drop);
82         for (; $head != $tail; $head++) {
83                 if (defined $docid) {
84                         my $i = $head->get_docid;
85                         push @drop, $i;
86                         warn <<EOF;
87 W: multiple inboxes keyed to `$eidx_key', deleting #$i
88 EOF
89                 } else {
90                         $docid = $head->get_docid;
91                 }
92         }
93         $xdb->delete_document($_) for @drop; # just in case
94
95         my $doc = $PublicInbox::Search::X{Document}->new;
96         term_generator($self)->set_document($doc);
97
98         # allow sorting by modified and uidvalidity (created at)
99         add_val($doc, $PublicInbox::MiscSearch::MODIFIED, $ibx->modified);
100         add_val($doc, $PublicInbox::MiscSearch::UIDVALIDITY, $ibx->uidvalidity);
101
102         $doc->add_boolean_term('Q'.$eidx_key); # uniQue id
103         $doc->add_boolean_term('T'.'inbox'); # Type
104
105         if (defined($ibx->{newsgroup}) && $ibx->nntp_usable) {
106                 $doc->add_boolean_term('T'.'newsgroup'); # additional Type
107         }
108
109         # force reread from disk, {description} could be loaded from {misc}
110         delete $ibx->{description};
111         my $desc = $ibx->description;
112
113         # description = S/Subject (or title)
114         # address = A/Author
115         index_text($self, $desc, 1, 'S');
116         index_text($self, $ibx->{name}, 1, 'XNAME');
117         my %map = (
118                 address => 'A',
119                 listid => 'XLISTID',
120                 infourl => 'XINFOURL',
121                 url => 'XURL'
122         );
123         while (my ($f, $pfx) = each %map) {
124                 for my $v (@{$ibx->{$f} // []}) {
125                         index_text($self, $v, 1, $pfx);
126                 }
127         }
128         my $data = {};
129         if (defined(my $max = $ibx->max_git_epoch)) { # v2
130                 my $pfx = "/$ibx->{name}/git/";
131                 for my $epoch (0..$max) {
132                         my $git = $ibx->git_epoch($epoch) or return;
133                         if (my $ent = $git->manifest_entry($epoch, $desc)) {
134                                 $data->{"$pfx$epoch.git"} = $ent;
135                                 $ent->{git_dir} = $git->{git_dir};
136                         }
137                         $git->cleanup; # ->modified starts cat-file --batch
138                 }
139         } elsif (my $ent = $ibx->git->manifest_entry) { # v1
140                 $ent->{git_dir} = $ibx->{inboxdir};
141                 $data->{"/$ibx->{name}"} = $ent;
142         }
143         $doc->set_data($json->encode($data));
144         if (defined $docid) {
145                 $xdb->replace_document($docid, $doc);
146         } else {
147                 $xdb->add_document($doc);
148         }
149 }
150
151 1;