1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # like PublicInbox::SearchIdx, but for searching for non-mail messages.
5 # Things indexed include:
8 # * (maybe) git code repository information
9 # Expect ~100K-1M documents with no parallelism opportunities,
10 # so no sharding, here.
12 # See MiscSearch for read-only counterpart
13 package PublicInbox::MiscIdx;
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);
22 use PublicInbox::MiscSearch;
23 use PublicInbox::Config;
26 my ($class, $eidx) = @_;
27 PublicInbox::SearchIdx::load_xapian_writable();
28 my $mi_dir = "$eidx->{xpfx}/misc";
29 File::Path::mkpath($mi_dir);
30 nodatacow_dir($mi_dir);
31 my $flags = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
32 $flags |= $PublicInbox::SearchIdx::DB_NO_SYNC if $eidx->{-no_fsync};
36 indexlevel => 'full', # small DB, no point in medium?
42 croak 'BUG: already in txn' if $self->{xdb}; # XXX make lazy?
43 my $wdb = $PublicInbox::Search::X{WritableDatabase};
44 my $xdb = eval { $wdb->new($self->{mi_dir}, $self->{flags}) };
45 croak "Failed opening $self->{mi_dir}: $@" if $@;
47 $xdb->begin_transaction;
52 croak 'BUG: not in txn' unless $self->{xdb}; # XXX make lazy?
53 delete($self->{xdb})->commit_transaction;
57 my ($self, $ibx) = @_;
58 my $eidx_key = $ibx->eidx_key;
59 my $xdb = $self->{xdb};
60 # Q = uniQue in Xapian terminology
61 my $head = $xdb->postlist_begin('Q'.$eidx_key);
62 my $tail = $xdb->postlist_end('Q'.$eidx_key);
64 for (; $head != $tail; $head++) {
66 my $i = $head->get_docid;
69 W: multiple inboxes keyed to `$eidx_key', deleting #$i
72 $docid = $head->get_docid;
75 $xdb->delete_document($_) for @drop; # just in case
77 my $doc = $PublicInbox::Search::X{Document}->new;
79 # allow sorting by modified
80 add_val($doc, $PublicInbox::MiscSearch::MODIFIED, $ibx->modified);
82 $doc->add_boolean_term('Q'.$eidx_key);
83 $doc->add_boolean_term('T'.'inbox');
84 term_generator($self)->set_document($doc);
86 # description = S/Subject (or title)
88 index_text($self, $ibx->description, 1, 'S');
92 infourl => 'XINFOURL',
95 while (my ($f, $pfx) = each %map) {
96 for my $v (@{$ibx->{$f} // []}) {
97 index_text($self, $v, 1, $pfx);
100 index_text($self, $ibx->{name}, 1, 'XNAME');
102 if (defined(my $max = $ibx->max_git_epoch)) { # v2
103 my $desc = $ibx->description;
104 my $pfx = "/$ibx->{name}/git/";
105 for my $epoch (0..$max) {
106 my $git = $ibx->git_epoch($epoch) or return;
107 if (my $ent = $git->manifest_entry($epoch, $desc)) {
108 $data->{"$pfx$epoch.git"} = $ent;
110 $git->cleanup; # ->modified starts cat-file --batch
112 } elsif (my $ent = $ibx->git->manifest_entry) { # v1
113 $data->{"/$ibx->{name}"} = $ent;
115 $doc->set_data(PublicInbox::Config::json()->encode($data));
116 if (defined $docid) {
117 $xdb->replace_document($docid, $doc);
119 $xdb->add_document($doc);