]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MiscIdx.pm
miscsearch: a new Xapian sub-DB for extindex
[public-inbox.git] / lib / PublicInbox / MiscIdx.pm
1 # Copyright (C) 2020 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
24 sub new {
25         my ($class, $eidx) = @_;
26         PublicInbox::SearchIdx::load_xapian_writable();
27         my $mi_dir = "$eidx->{xpfx}/misc";
28         File::Path::mkpath($mi_dir);
29         nodatacow_dir($mi_dir);
30         my $flags = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
31         $flags |= $PublicInbox::SearchIdx::DB_NO_SYNC if $eidx->{-no_fsync};
32         bless {
33                 mi_dir => $mi_dir,
34                 flags => $flags,
35                 indexlevel => 'full', # small DB, no point in medium?
36         }, $class;
37 }
38
39 sub begin_txn {
40         my ($self) = @_;
41         croak 'BUG: already in txn' if $self->{xdb}; # XXX make lazy?
42         my $wdb = $PublicInbox::Search::X{WritableDatabase};
43         my $xdb = eval { $wdb->new($self->{mi_dir}, $self->{flags}) };
44         croak "Failed opening $self->{mi_dir}: $@" if $@;
45         $self->{xdb} = $xdb;
46         $xdb->begin_transaction;
47 }
48
49 sub commit_txn {
50         my ($self) = @_;
51         croak 'BUG: not in txn' unless $self->{xdb}; # XXX make lazy?
52         delete($self->{xdb})->commit_transaction;
53 }
54
55 sub index_ibx {
56         my ($self, $ibx) = @_;
57         my $eidx_key = $ibx->eidx_key;
58         my $xdb = $self->{xdb};
59         # Q = uniQue in Xapian terminology
60         my $head = $xdb->postlist_begin('Q'.$eidx_key);
61         my $tail = $xdb->postlist_end('Q'.$eidx_key);
62         my ($docid, @drop);
63         for (; $head != $tail; $head++) {
64                 if (defined $docid) {
65                         my $i = $head->get_docid;
66                         push @drop, $i;
67                         warn <<EOF;
68 W: multiple inboxes keyed to `$eidx_key', deleting #$i
69 EOF
70                 } else {
71                         $docid = $head->get_docid;
72                 }
73         }
74         $xdb->delete_document($_) for @drop; # just in case
75
76         my $doc = $PublicInbox::Search::X{Document}->new;
77
78         # allow sorting by modified
79         add_val($doc, $PublicInbox::MiscSearch::MODIFIED, $ibx->modified);
80
81         $doc->add_boolean_term('Q'.$eidx_key);
82         $doc->add_boolean_term('T'.'inbox');
83         term_generator($self)->set_document($doc);
84
85         # description = S/Subject (or title)
86         # address = A/Author
87         index_text($self, $ibx->description, 1, 'S');
88         my %map = (
89                 address => 'A',
90                 listid => 'XLISTID',
91                 infourl => 'XINFOURL',
92                 url => 'XURL'
93         );
94         while (my ($f, $pfx) = each %map) {
95                 for my $v (@{$ibx->{$f} // []}) {
96                         index_text($self, $v, 1, $pfx);
97                 }
98         }
99         index_text($self, $ibx->{name}, 1, 'XNAME');
100         if (defined $docid) {
101                 $xdb->replace_document($docid, $doc);
102         } else {
103                 $xdb->add_document($doc);
104         }
105 }
106
107 1;