if ($writable) { # not used by the WWW interface
require Search::Xapian::WritableDatabase;
- require File::Path;
- File::Path::mkpath($dir);
- $db = Search::Xapian::WritableDatabase->new($dir,
- Search::Xapian::DB_CREATE_OR_OPEN);
+ my $flag = Search::Xapian::DB_OPEN;
+ if ($writable == 1) {
+ require File::Path;
+ File::Path::mkpath($dir);
+ $flag = Search::Xapian::DB_CREATE_OR_OPEN;
+ }
+ $db = Search::Xapian::WritableDatabase->new($dir, $flag);
} else {
$db = Search::Xapian::Database->new($dir);
}
my $ct_msg = $mime->header('Content-Type') || 'text/plain';
my $enc_msg = PublicInbox::View::enc_for($ct_msg);
- $db->begin_transaction;
eval {
my $smsg = $self->lookup_message($mid);
my $doc;
if ($@) {
warn "failed to index message <$mid>: $@\n";
- $db->cancel_transaction;
- } else {
- $db->commit_transaction;
+ return undef;
}
$doc_id;
}
$mid = mid_clean($mid);
$mid = mid_compressed($mid);
- $db->begin_transaction;
eval {
$doc_id = $self->find_unique_doc_id('mid', $mid);
$db->delete_document($doc_id) if defined $doc_id;
if ($@) {
warn "failed to remove message <$mid>: $@\n";
- $db->cancel_transaction;
- } else {
- $db->commit_transaction;
+ return undef;
}
$doc_id;
}
# indexes all unindexed messages
sub index_sync {
- my ($self, $git) = @_;
+ my ($self) = @_;
+ require PublicInbox::GitCatFile;
my $db = $self->{xdb};
- my $latest = $db->get_metadata('last_commit');
- my $range = length $latest ? "$latest..HEAD" : 'HEAD';
- $latest = undef;
-
my $hex = '[a-f0-9]';
my $h40 = $hex .'{40}';
my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
- # get indexed messages
- my @cmd = ('git', "--git-dir=$git->{git_dir}", "log",
- qw/--reverse --no-notes --no-color --raw -r --no-abbrev/,
- $range);
-
- my $pid = open(my $log, '-|', @cmd) or
- die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
- my $last;
- while (my $line = <$log>) {
- if ($line =~ /$addmsg/o) {
- $self->index_blob($git, $1);
- } elsif ($line =~ /$delmsg/o) {
- $self->unindex_blob($git, $1);
- } elsif ($line =~ /^commit ($h40)/o) {
- my $commit = $1;
- if (defined $latest) {
- $db->set_metadata('last_commit', $latest)
+ $db->begin_transaction;
+ eval {
+ my $git = PublicInbox::GitCatFile->new($self->{git_dir});
+
+ my $latest = $db->get_metadata('last_commit');
+ my $range = length $latest ? "$latest..HEAD" : 'HEAD';
+ $latest = undef;
+
+ # get indexed messages
+ my @cmd = ('git', "--git-dir=$self->{git_dir}", "log",
+ qw/--reverse --no-notes --no-color --raw -r
+ --no-abbrev/, $range);
+ my $pid = open(my $log, '-|', @cmd) or
+ die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
+
+ while (my $line = <$log>) {
+ if ($line =~ /$addmsg/o) {
+ $self->index_blob($git, $1);
+ } elsif ($line =~ /$delmsg/o) {
+ $self->unindex_blob($git, $1);
+ } elsif ($line =~ /^commit ($h40)/o) {
+ $latest = $1;
}
- $latest = $commit;
}
+ close $log;
+ $db->set_metadata('last_commit', $latest) if defined $latest;
+ };
+ if ($@) {
+ warn "indexing failed: $@\n";
+ $db->cancel_transaction;
+ } else {
+ $db->commit_transaction;
}
- close $log;
- $db->set_metadata('last_commit', $latest) if defined $latest;
}
1;
my $rw = PublicInbox::Search->new($git_dir, 1);
my $ro = PublicInbox::Search->new($git_dir);
+my $rw_commit = sub {
+ $rw = undef;
+ $rw = PublicInbox::Search->new($git_dir, 1);
+};
{
my $root = Email::MIME->create(
}
{
+ $rw_commit->();
$ro->reopen;
my $found = $ro->lookup_message('<root@s>');
ok($found, "message found");
# ghost vivication
{
- $rw->reopen;
+ $rw_commit->();
my $rmid = '<ghost-message@s>';
my $reply_to_ghost = Email::MIME->create(
header_str => [
# search thread on ghost
{
+ $rw_commit->();
$ro->reopen;
# Subject:
# long message-id
{
- $rw->reopen;
+ $rw_commit->();
$ro->reopen;
my $long_mid = 'last' . ('x' x 60). '@s';
my $long_midc = Digest::SHA::sha1_hex($long_mid);
my $long_id = $rw->add_message($long);
is($long_id, int($long_id), "long_id is an integer: $long_id");
+ $rw_commit->();
$ro->reopen;
my $res = $ro->query('references:root@s');
my @res = filter_mids($res);
body => "no References\n");
ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
+ $rw_commit->();
$ro->reopen;
my $t = $ro->get_thread('root@s');
is($t->{count}, 4, "got all 4 mesages in thread");
# quote prioritization
{
- $rw->reopen;
+ $rw_commit->();
$rw->add_message(Email::MIME->create(
header_str => [
Date => 'Sat, 02 Oct 2010 00:00:01 +0000',