]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MiscIdx.pm
rewrite Linux nodatacow use in pure Perl w/o system
[public-inbox.git] / lib / PublicInbox / MiscIdx.pm
index ab5e029a4625c6b77f16c9166de1d0e9afacbf3b..dc15442d9acb9ba1b7d29c51512c58454c5512d5 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # like PublicInbox::SearchIdx, but for searching for non-mail messages.
@@ -16,11 +16,11 @@ use v5.10.1;
 use PublicInbox::InboxWritable;
 use PublicInbox::Search; # for SWIG Xapian and Search::Xapian compat
 use PublicInbox::SearchIdx qw(index_text term_generator add_val);
-use PublicInbox::Spawn qw(nodatacow_dir);
 use Carp qw(croak);
 use File::Path ();
 use PublicInbox::MiscSearch;
 use PublicInbox::Config;
+use PublicInbox::Syscall;
 my $json;
 
 sub new {
@@ -28,7 +28,7 @@ sub new {
        PublicInbox::SearchIdx::load_xapian_writable();
        my $mi_dir = "$eidx->{xpfx}/misc";
        File::Path::mkpath($mi_dir);
-       nodatacow_dir($mi_dir);
+       PublicInbox::Syscall::nodatacow_dir($mi_dir);
        my $flags = $PublicInbox::SearchIdx::DB_CREATE_OR_OPEN;
        $flags |= $PublicInbox::SearchIdx::DB_NO_SYNC if $eidx->{-no_fsync};
        $json //= PublicInbox::Config::json();
@@ -39,25 +39,30 @@ sub new {
        }, $class;
 }
 
-sub begin_txn {
+sub _begin_txn ($) {
        my ($self) = @_;
-       croak 'BUG: already in txn' if $self->{xdb}; # XXX make lazy?
        my $wdb = $PublicInbox::Search::X{WritableDatabase};
        my $xdb = eval { $wdb->new($self->{mi_dir}, $self->{flags}) };
        croak "Failed opening $self->{mi_dir}: $@" if $@;
-       $self->{xdb} = $xdb;
        $xdb->begin_transaction;
+       $xdb;
 }
 
 sub commit_txn {
        my ($self) = @_;
-       croak 'BUG: not in txn' unless $self->{xdb}; # XXX make lazy?
-       delete($self->{xdb})->commit_transaction;
+       my $xdb = delete $self->{xdb} or return;
+       $xdb->commit_transaction;
+}
+
+sub create_xdb {
+       my ($self) = @_;
+       $self->{xdb} //= _begin_txn($self);
+       commit_txn($self);
 }
 
 sub remove_eidx_key {
        my ($self, $eidx_key) = @_;
-       my $xdb = $self->{xdb};
+       my $xdb = $self->{xdb} //= _begin_txn($self);
        my $head = $xdb->postlist_begin('Q'.$eidx_key);
        my $tail = $xdb->postlist_end('Q'.$eidx_key);
        my @docids; # only one, unless we had bugs
@@ -74,7 +79,7 @@ sub remove_eidx_key {
 sub index_ibx {
        my ($self, $ibx) = @_;
        my $eidx_key = $ibx->eidx_key;
-       my $xdb = $self->{xdb};
+       my $xdb = $self->{xdb} //= _begin_txn($self);
        # Q = uniQue in Xapian terminology
        my $head = $xdb->postlist_begin('Q'.$eidx_key);
        my $tail = $xdb->postlist_end('Q'.$eidx_key);