+sub _xdb_release {
+ my ($self) = @_;
+ my $xdb = delete $self->{xdb} or croak 'not acquired';
+ $xdb->close;
+ _lock_release($self) if $self->{creat};
+ undef;
+}
+
+sub _xdb_acquire {
+ my ($self) = @_;
+ croak 'already acquired' if $self->{xdb};
+ my $dir = PublicInbox::Search->xdir($self->{git_dir});
+ my $flag = Search::Xapian::DB_OPEN;
+ if ($self->{creat}) {
+ require File::Path;
+ _lock_acquire($self);
+ File::Path::mkpath($dir);
+ $self->{batch_size} = 100;
+ $flag = Search::Xapian::DB_CREATE_OR_OPEN;
+ }
+ $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
+}
+
+# we only acquire the flock if creating or reindexing;
+# PublicInbox::Import already has the lock on its own.
+sub _lock_acquire {
+ my ($self) = @_;
+ croak 'already locked' if $self->{lockfh};
+ sysopen(my $lockfh, $self->{lock_path}, O_WRONLY|O_CREAT) or
+ die "failed to open lock $self->{lock_path}: $!\n";
+ flock($lockfh, LOCK_EX) or die "lock failed: $!\n";
+ $self->{lockfh} = $lockfh;
+}
+
+sub _lock_release {
+ my ($self) = @_;
+ my $lockfh = delete $self->{lockfh} or croak 'not locked';
+ flock($lockfh, LOCK_UN) or die "unlock failed: $!\n";
+ close $lockfh or die "close failed: $!\n";
+}
+
+sub add_val ($$$) {
+ my ($doc, $col, $num) = @_;
+ $num = Search::Xapian::sortable_serialise($num);
+ $doc->add_value($col, $num);
+}
+
+sub add_values ($$$) {
+ my ($smsg, $bytes, $num) = @_;
+
+ my $ts = $smsg->ts;
+ my $doc = $smsg->{doc};
+ add_val($doc, &PublicInbox::Search::TS, $ts);
+
+ defined($num) and add_val($doc, &PublicInbox::Search::NUM, $num);
+
+ defined($bytes) and add_val($doc, &PublicInbox::Search::BYTES, $bytes);
+
+ add_val($doc, &PublicInbox::Search::LINES,
+ $smsg->{mime}->body_raw =~ tr!\n!\n!);
+
+ my $yyyymmdd = strftime('%Y%m%d', gmtime($ts));
+ add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd);
+}
+
+sub index_users ($$) {
+ my ($tg, $smsg) = @_;
+
+ my $from = $smsg->from;
+ my $to = $smsg->to;
+ my $cc = $smsg->cc;
+
+ $tg->index_text($from, 1, 'A'); # A - author
+ $tg->increase_termpos;
+ $tg->index_text($to, 1, 'XTO') if $to ne '';
+ $tg->increase_termpos;
+ $tg->index_text($cc, 1, 'XCC') if $cc ne '';
+ $tg->increase_termpos;
+}
+
+sub index_body ($$$) {
+ my ($tg, $lines, $inc) = @_;
+ $tg->index_text(join("\n", @$lines), $inc, $inc ? 'XNQ' : 'XQUOT');
+ @$lines = ();
+ $tg->increase_termpos;
+}
+