]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Search.pm
nntp: fix STAT command
[public-inbox.git] / lib / PublicInbox / Search.pm
index c54cf7b9911affce9680e7160b1fdecbb3aad20f..4e08aed7acbf0697ccdd9593325080b63e8e637d 100644 (file)
@@ -164,15 +164,10 @@ chomp @HELP;
 
 sub xdir ($;$) {
        my ($self, $rdonly) = @_;
-       if ($self->{ibx_ver} == 1) {
-               "$self->{inboxdir}/public-inbox/xapian" . SCHEMA_VERSION;
-       } else {
-               my $dir = "$self->{inboxdir}/xap" . SCHEMA_VERSION;
-               return $dir if $rdonly;
-
-               my $shard = $self->{shard};
-               defined $shard or die "shard not given";
-               $dir .= "/$shard";
+       if ($rdonly || !defined($self->{shard})) {
+               $self->{xpfx};
+       } else { # v2 only:
+               "$self->{xpfx}/$self->{shard}";
        }
 }
 
@@ -182,6 +177,7 @@ sub _xdb ($) {
        my ($xdb, $slow_phrase);
        my $qpf = \($self->{qp_flags} ||= $QP_FLAGS);
        if ($self->{ibx_ver} >= 2) {
+               my $n = 0;
                foreach my $shard (<$dir/*>) {
                        -d $shard && $shard =~ m!/[0-9]+\z! or next;
                        my $sub = $X{Database}->new($shard);
@@ -191,7 +187,9 @@ sub _xdb ($) {
                                $xdb = $sub;
                        }
                        $slow_phrase ||= -f "$shard/iamchert";
+                       ++$n;
                }
+               $self->{nshard} = $n;
        } else {
                $slow_phrase = -f "$dir/iamchert";
                $xdb = $X{Database}->new($dir);
@@ -200,6 +198,15 @@ sub _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 mdocid {
+       my ($nshard, $mitem) = @_;
+       my $docid = $mitem->get_docid;
+       int(($docid - 1) / $nshard) + 1;
+}
+
 sub xdb ($) {
        my ($self) = @_;
        $self->{xdb} ||= do {
@@ -208,14 +215,24 @@ sub xdb ($) {
        };
 }
 
+sub xpfx_init ($) {
+       my ($self) = @_;
+       if ($self->{ibx_ver} == 1) {
+               $self->{xpfx} .= '/public-inbox/xapian' . SCHEMA_VERSION;
+       } else {
+               $self->{xpfx} .= '/xap'.SCHEMA_VERSION;
+       }
+}
+
 sub new {
        my ($class, $ibx) = @_;
        ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
        my $self = bless {
-               inboxdir => $ibx->{inboxdir},
+               xpfx => $ibx->{inboxdir}, # for xpfx_init
                altid => $ibx->{altid},
                ibx_ver => $ibx->version,
        }, $class;
+       xpfx_init($self);
        my $dir = xdir($self, 1);
        $self->{over_ro} = PublicInbox::Over->new("$dir/over.sqlite3");
        $self;
@@ -283,7 +300,7 @@ sub _enquire_once { # retry_reopen callback
        $enquire->set_query($query);
        $opts ||= {};
         my $desc = !$opts->{asc};
-       if (($opts->{mset} || 0) == 2) {
+       if (($opts->{mset} || 0) == 2) { # mset == 2: ORDER BY docid/UID
                $enquire->set_docid_order($ENQ_ASCENDING);
                $enquire->set_weighting_scheme($X{BoolWeight}->new);
        } elsif ($opts->{relevance}) {
@@ -322,6 +339,11 @@ sub qp {
        $qp->add_valuerangeprocessor($nvrp->new(YYYYMMDD, 'd:'));
        $qp->add_valuerangeprocessor($nvrp->new(DT, 'dt:'));
 
+       # for IMAP, undocumented for WWW and may be split off go away
+       $qp->add_valuerangeprocessor($nvrp->new(BYTES, 'bytes:'));
+       $qp->add_valuerangeprocessor($nvrp->new(TS, 'ts:'));
+       $qp->add_valuerangeprocessor($nvrp->new(UID, 'uid:'));
+
        while (my ($name, $prefix) = each %bool_pfx_external) {
                $qp->add_boolean_prefix($name, $_) foreach split(/ /, $prefix);
        }