]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/AltId.pm
support altid mechanism for v2
[public-inbox.git] / lib / PublicInbox / AltId.pm
index 6fdc3a2d4df6c141a8b76181ad30829bf43abeb3..f8aa4cb8cf71a244d369fc671bf3639d635acdb4 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 package PublicInbox::AltId;
@@ -9,7 +9,7 @@ use URI::Escape qw(uri_unescape);
 # spec: TYPE:PREFIX:param1=value1&param2=value2&...
 # Example: serial:gmane:file=/path/to/altmsgmap.sqlite3
 sub new {
-       my ($class, $inbox, $spec) = @_;
+       my ($class, $inbox, $spec, $writable) = @_;
        my ($type, $prefix, $query) = split(/:/, $spec, 3);
        $type eq 'serial' or die "non-serial not supported, yet\n";
 
@@ -22,17 +22,31 @@ sub new {
        } split(/[&;]/, $query);
        my $f = $params{file} or die "file: required for $type spec $spec\n";
        unless (index($f, '/') == 0) {
-               $f = "$inbox->{mainrepo}/public-inbox/$f";
+               if (($inbox->{version} || 1) == 1) {
+                       $f = "$inbox->{mainrepo}/public-inbox/$f";
+               } else {
+                       $f = "$inbox->{mainrepo}/$f";
+               }
        }
        bless {
-               mm_alt => PublicInbox::Msgmap->new_file($f),
+               filename => $f,
+               writable => $writable,
                xprefix => 'X'.uc($prefix),
        }, $class;
 }
 
+sub mm_alt {
+       my ($self) = @_;
+       $self->{mm_alt} ||= eval {
+               my $f = $self->{filename};
+               my $writable = $self->{filename};
+               PublicInbox::Msgmap->new_file($f, $writable);
+       };
+}
+
 sub mid2alt {
        my ($self, $mid) = @_;
-       $self->{mm_alt}->num_for($mid);
+       $self->mm_alt->num_for($mid);
 }
 
 1;