1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # temporary stack for public-inbox-index
5 package PublicInbox::IdxStack;
9 use constant FMT => eval { pack('Q', 1) } ? 'A1QQH*' : 'A1IIH*';
11 # start off in write-only mode
13 open(my $io, '+>', undef) or die "open: $!";
14 bless { wr => $io, latest_cmt => $_[1] }, __PACKAGE__
19 my ($self, $file_char, $at, $ct, $blob_oid) = @_;
20 my $rec = pack(FMT, $file_char, $at, $ct, $blob_oid);
21 $self->{rec_size} //= length($rec);
22 print { $self->{wr} } $rec or die "print: $!";
23 $self->{tot_size} += length($rec);
28 $self->{rec_size} ? $self->{tot_size} / $self->{rec_size} : 0;
31 # switch into read-only mode and returns self
34 my $io = $self->{rd} = delete($self->{wr});
35 $io->flush or die "flush: $!";
41 my $sz = $self->{rec_size} or return;
42 my $rec_pos = $self->{tot_size} -= $sz;
43 return if $rec_pos < 0;
45 seek($io, $rec_pos, SEEK_SET) or die "seek: $!";
46 my $r = read($io, my $buf, $sz);
47 defined($r) or die "read: $!";
48 $r == $sz or die "read($r != $sz)";