File Coverage

blib/lib/Plucene/Store/InputStream.pm
Criterion Covered Total %
statement 42 48 87.5
branch 1 2 50.0
condition 2 3 66.6
subroutine 14 20 70.0
pod 15 15 100.0
total 74 88 84.0


line stmt bran cond sub pod time code
1             package Plucene::Store::InputStream;
2              
3             =head1 NAME
4              
5             Plucene::Store::InputStream - a random-access input stream
6              
7             =head1 SYNOPSIS
8              
9             # isa IO::File
10              
11             =head1 DESCRIPTION
12              
13             A random-access input stream.Used for all Plucene index input operations.
14              
15             =head1 METHODS
16              
17             =cut
18              
19 20     20   59584 use strict;
  20         38  
  20         641  
20 20     20   111 use warnings;
  20         32  
  20         632  
21              
22 20     20   31460 use Encode qw(_utf8_on); # Magic
  20         281944  
  20         4357  
23              
24             =head2 new
25              
26             my $inputstream = Plucene::Store::InputStream->new($file);
27              
28             Create a new input stream.
29              
30             =cut
31              
32             sub new {
33 5137     5137 1 9551 my ($self, $filename) = @_;
34 5137   66     28604 $self = ref $self || $self;
35 5137 50       249490 open my $fh, '<', $filename
36             or die "$self cannot open $filename for reading: $!";
37 5137         10851 binmode $fh;
38 5137         78736 bless [ $fh, $filename ], $self;
39             }
40              
41 4935     4935   448237 sub DESTROY { CORE::close $_[0]->[0] }
42              
43             =head2 fh / read / seek / tell / getc / print / eof / close
44              
45             File operations
46              
47             =cut
48              
49 20     20   179 use Carp 'croak';
  20         45  
  20         21175  
50 0     0 1 0 sub fh { croak "InputStream fh called" }
51 70617     70617 1 270891 sub read { CORE::read $_[0]->[0], $_[1], $_[2] }
52 2560     2560 1 24058 sub seek { CORE::seek $_[0]->[0], $_[1], $_[2] }
53 0     0 1 0 sub tell { CORE::tell $_[0]->[0] }
54 0     0 1 0 sub getc { CORE::getc $_[0]->[0] }
55 0     0 1 0 sub print { croak "InputStream print called" }
56 0     0 1 0 sub eof { CORE::eof $_[0]->[0] }
57 0     0 1 0 sub close { CORE::close $_[0]->[0] }
58              
59             =head2 clone
60              
61             This will return a clone of this stream.
62              
63             =cut
64              
65             sub clone {
66 937     937 1 1433 my $orig = shift;
67 937         33649 my $clone = $orig->new($orig->[1]);
68 937         5912 CORE::seek($clone->[0], CORE::tell($orig->[0]), 0);
69 937         4640 return $clone;
70             }
71              
72             =head2 read_byte
73              
74             This will read and return a single byte.
75              
76             =cut
77              
78             sub read_byte { # unpack C
79 1372     1372 1 12091 ord CORE::getc $_[0]->[0];
80             }
81              
82             =head2 read_int
83              
84             This will read four bytes and return an integer.
85              
86             =cut
87              
88             sub read_int { # unpack N
89 4350     4350 1 25287 my $buf;
90 4350         43509 CORE::read $_[0]->[0], $buf, 4;
91 4350         25918 return unpack("N", $buf);
92             }
93              
94             =head2 read_vint
95              
96             This will read an integer stored in a variable-length format.
97              
98             =cut
99              
100             sub read_vint { # unpack w
101 421902     421902 1 854757 my $b = ord CORE::getc($_[0]->[0]);
102 421902         520971 my $i = $b & 0x7F;
103 421902         1010917 for (my $s = 7 ; ($b & 0x80) != 0 ; $s += 7) {
104 11817         19699 $b = ord CORE::getc $_[0]->[0];
105 11817         30511 $i |= ($b & 0x7F) << $s;
106             }
107 421902         1018981 return $i;
108             }
109              
110             =head2 read_vlong
111              
112             This will read a long and stored in variable-length format
113              
114             =cut
115              
116             *read_vlong = *read_vint; # Perl is type-agnostic. ;)
117             # Yes, but most Perls don't handle 64bit integers!
118              
119             =head2 read_string
120              
121             This will read a string.
122              
123             =cut
124              
125             sub read_string { # unpack w/a
126 820     820 1 6904 my $length = $_[0]->read_vint();
127 820         1129 my $utf8;
128 820         5763 CORE::read $_[0]->[0], $utf8, $length;
129 820         2009 _utf8_on($utf8);
130 820         19453 return $utf8;
131             }
132              
133             =head2 read_long
134              
135             This will read eight bytes and return a long.
136              
137             =cut
138              
139             sub read_long { # unpack NN
140 499     499 1 2028 my $int_a = $_[0]->read_int;
141 499         1258 my $int_b = $_[0]->read_int; # Order is important!
142             # and size matters!
143 499         1466 return (($int_a << 32) | ($int_b & 0xFFFFFFFF));
144             }
145              
146             1;