lib/PublicInbox/LeiExtinbox.pm
lib/PublicInbox/LeiSearch.pm
lib/PublicInbox/LeiStore.pm
+lib/PublicInbox/LeiXSearch.pm
lib/PublicInbox/Linkify.pm
lib/PublicInbox/Listener.pm
lib/PublicInbox/Lock.pm
t/lei-oneshot.t
t/lei.t
t/lei_store.t
+t/lei_xsearch.t
t/linkify.t
t/main-bin/spamc
t/mda-mime.eml
use parent qw(PublicInbox::ExtSearch);
use PublicInbox::Search;
-sub combined_docid ($$) {
+# get combined docid from over.num:
+# (not generic Xapian, only works with our sharding scheme)
+sub num2docid ($$) {
my ($self, $num) = @_;
- ($num - 1) * $self->{nshard} + 1;
+ my $nshard = $self->{nshard};
+ ($num - 1) * $nshard + $num % $nshard + 1;
}
sub msg_keywords {
my ($self, $num) = @_; # num_or_mitem
my $xdb = $self->xdb; # set {nshard};
- my $docid = ref($num) ? $num->get_docid : do {
- # get combined docid from over.num:
- # (not generic Xapian, only works with our sharding scheme)
- my $nshard = $self->{nshard};
- ($num - 1) * $nshard + $num % $nshard + 1;
- };
+ my $docid = ref($num) ? $num->get_docid : num2docid($self, $num);
my %kw;
eval {
my $end = $xdb->termlist_end($docid);
--- /dev/null
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Combine any combination of PublicInbox::Search,
+# PublicInbox::ExtSearch, and PublicInbox::LeiSearch objects
+# into one Xapian DB
+package PublicInbox::LeiXSearch;
+use strict;
+use v5.10.1;
+use parent qw(PublicInbox::LeiSearch);
+
+sub new {
+ my ($class) = @_;
+ PublicInbox::Search::load_xapian();
+ bless {
+ qp_flags => $PublicInbox::Search::QP_FLAGS |
+ PublicInbox::Search::FLAG_PURE_NOT(),
+ }, $class
+}
+
+sub attach_extinbox {
+ my ($self, $ibxish) = @_; # ibxish = ExtSearch or Inbox
+ if (!$ibxish->can('over')) {
+ push @{$self->{remotes}}, $ibxish
+ }
+ if (delete $self->{xdb}) { # XXX: do we need this?
+ # clobber existing {xdb} if amending
+ my $expect = delete $self->{nshard};
+ my $shards = delete $self->{shards_flat};
+ scalar(@$shards) == $expect or die
+ "BUG: {nshard}$expect != shards=".scalar(@$shards);
+
+ my $prev = {};
+ for my $old_ibxish (@{$self->{shard2ibx}}) {
+ next if $prev == $old_ibxish;
+ $prev = $old_ibxish;
+ my @shards = $old_ibxish->search->xdb_shards_flat;
+ push @{$self->{shards_flat}}, @shards;
+ }
+ my $nr = scalar(@{$self->{shards_flat}});
+ $nr == $expect or die
+ "BUG: reloaded $nr shards, expected $expect"
+ }
+ my @shards = $ibxish->search->xdb_shards_flat;
+ push @{$self->{shards_flat}}, @shards;
+ push(@{$self->{shard2ibx}}, $ibxish) for (@shards);
+}
+
+# called by PublicInbox::Search::xdb
+sub xdb_shards_flat { @{$_[0]->{shards_flat}} }
+
+# like over->get_art
+sub smsg_for {
+ my ($self, $mitem) = @_;
+ # cf. https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
+ my $nshard = $self->{nshard};
+ my $docid = $mitem->get_docid;
+ my $shard = ($docid - 1) % $nshard;
+ my $num = int(($docid - 1) / $nshard) + 1;
+ my $smsg = $self->{shard2ibx}->[$shard]->over->get_art($num);
+ $smsg->{docid} = $docid;
+ $smsg;
+}
+
+sub recent {
+ my ($self, $qstr, $opt) = @_;
+ $opt //= {};
+ $opt->{relevance} //= -2;
+ $self->mset($qstr //= 'bytes:1..', $opt);
+}
+
+1;
my ($self) = @_;
my $xpfx = $self->{xpfx};
my (@xdb, $slow_phrase);
+ load_xapian();
if ($xpfx =~ m/xapian${\SCHEMA_VERSION}\z/) {
@xdb = ($X{Database}->new($xpfx));
$self->{qp_flags} |= FLAG_PHRASE() if !-f "$xpfx/iamchert";
@xdb;
}
-sub _xdb {
- my ($self) = @_;
- $self->{qp_flags} //= $QP_FLAGS;
- my @xdb = xdb_shards_flat($self) or return;
- $self->{nshard} = scalar(@xdb);
- my $xdb = shift @xdb;
- $xdb->add_database($_) for @xdb;
- $xdb;
-}
-
# v2 Xapian docids don't conflict, so they're identical to
# NNTP article numbers and IMAP UIDs.
# https://trac.xapian.org/wiki/FAQ/MultiDatabaseDocumentID
sub xdb ($) {
my ($self) = @_;
$self->{xdb} //= do {
- load_xapian();
- $self->_xdb;
+ $self->{qp_flags} //= $QP_FLAGS;
+ my @xdb = $self->xdb_shards_flat or return;
+ $self->{nshard} = scalar(@xdb);
+ my $xdb = shift @xdb;
+ $xdb->add_database($_) for @xdb;
+ $xdb;
};
}
--- /dev/null
+#!perl -w
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use v5.10.1;
+use Test::More;
+use List::Util qw(shuffle max);
+use PublicInbox::TestCommon;
+use PublicInbox::ExtSearchIdx;
+use PublicInbox::Eml;
+use PublicInbox::InboxWritable;
+require_mods(qw(DBD::SQLite Search::Xapian));
+require_git 2.6;
+require_ok 'PublicInbox::LeiXSearch';
+my ($home, $for_destroy) = tmpdir();
+my @ibx;
+for my $V (1..2) {
+ for my $i (3..6) {
+ my $ibx = PublicInbox::InboxWritable->new({
+ inboxdir => "$home/v$V-$i",
+ name => "test-v$V-$i",
+ version => $V,
+ indexlevel => 'medium',
+ -primary_address => "v$V-$i\@example.com",
+ }, { nproc => int(rand(8)) + 1 });
+ push @ibx, $ibx;
+ my $im = $ibx->importer(0);
+ for my $j (0..9) {
+ my $eml = PublicInbox::Eml->new(<<EOF);
+From: x\@example.com
+To: $ibx->{-primary_address}
+Date: Fri, 02 Oct 1993 0$V:0$i:0$j +0000
+Subject: v${V}i${i}j$j
+Message-ID: <v${V}i${i}j$j\@example>
+
+${V}er ${i}on j$j
+EOF
+ $im->add($eml);
+ }
+ $im->done;
+ }
+}
+my $first = shift @ibx; is($first->{name}, 'test-v1-3', 'first plucked');
+my $last = pop @ibx; is($last->{name}, 'test-v2-6', 'last plucked');
+my $eidx = PublicInbox::ExtSearchIdx->new("$home/eidx");
+$eidx->attach_inbox($first);
+$eidx->attach_inbox($last);
+$eidx->eidx_sync({fsync => 0});
+my $es = PublicInbox::ExtSearch->new("$home/eidx");
+my $lxs = PublicInbox::LeiXSearch->new;
+for my $ibxish (shuffle($es, @ibx)) {
+ $lxs->attach_extinbox($ibxish);
+}
+my $nr = $lxs->xdb->get_doccount;
+my $mset = $lxs->mset('d:19931002..19931003', { limit => $nr });
+is($mset->size, $nr, 'got all messages');
+my @msgs;
+for my $mi ($mset->items) {
+ if (my $smsg = $lxs->smsg_for($mi)) {
+ push @msgs, $smsg;
+ } else {
+ diag "E: ${\$mi->get_docid} missing";
+ }
+}
+is(scalar(@msgs), $nr, 'smsgs retrieved for all');
+
+$mset = $lxs->recent(undef, { limit => 1 });
+is($mset->size, 1, 'one result');
+my $max = max(map { $_->{docid} } @msgs);
+is($lxs->smsg_for(($mset->items)[0])->{docid}, $max,
+ 'got highest docid');
+
+done_testing;