Plucene/Store/InputStream.pm

filter: Statementstmt 88.0% 44 / 50 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.0% 14 / 20 Podpod 100% 15 / 15 totaltotal 82.9% 78 / 94 CC 4 SCAR 4.8
CC
4
Cov
86%
new
3.1
fh
2.0
tell
2.0
CRAP 1.6
« Plucene/Search/Query.pm ↑ Summary Plucene/Search/Similarity.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 20 ▶use Encode qw(_utf8_on); # Magic
Subroutine BEGIN: called CC=1 SCAR=0.0
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 5464 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
33 5464 my ($self, $filename) = @_;
34 5464 ▶ $self = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
99144730
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
35 5464 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "$self cannot open $filename for reading: $!" 5464 0 5464
36 or die "$self cannot open $filename for reading: $!";
37 5464 binmode $fh;
38 5464 bless [ $fh, $filename ], $self;
39 }
40  
41 5172 ▶sub DESTROY { CORE::close $_[0]->[0] }
Subroutine DESTROY: called CC=1 SCAR=0.0
42  
43 =head2 fh / read / seek / tell / getc / print / eof / close
44  
45 File operations
46  
47 =cut
48  
49 20 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 0 ▶sub fh { croak "InputStream fh called" }
Subroutine fh: not called CC=1 SCAR=6.9
Pod documented
51 78949 ▶sub read { CORE::read $_[0]->[0], $_[1], $_[2] }
Subroutine read: called CC=1 SCAR=0.0
Pod documented
52 2962 ▶sub seek { CORE::seek $_[0]->[0], $_[1], $_[2] }
Subroutine seek: called CC=1 SCAR=0.0
Pod documented
53 0 ▶sub tell { CORE::tell $_[0]->[0] }
Subroutine tell: not called CC=1 SCAR=6.9
Pod documented
54 0 ▶sub getc { CORE::getc $_[0]->[0] }
Subroutine getc: not called CC=1 SCAR=6.9
Pod documented
55 0 ▶sub print { croak "InputStream print called" }
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
56 0 ▶sub eof { CORE::eof $_[0]->[0] }
Subroutine eof: not called CC=1 SCAR=6.9
Pod documented
57 0 ▶sub close { CORE::close $_[0]->[0] }
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
58  
59 =head2 clone
60  
61 This will return a clone of this stream.
62  
63 =cut
64  
65 991 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
66 991 my $orig = shift;
67 991 my $clone = $orig->new($orig->[1]);
68 991 CORE::seek($clone->[0], CORE::tell($orig->[0]), 0);
69 991 return $clone;
70 }
71  
72 =head2 read_byte
73  
74 This will read and return a single byte.
75  
76 =cut
77  
78 1678 ▶sub read_byte { # unpack C
Subroutine read_byte: called CC=1 SCAR=0.0
Pod documented
79 1678 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 4632 ▶sub read_int { # unpack N
Subroutine read_int: called CC=1 SCAR=0.0
Pod documented
89 4632 my $buf;
90 4632 CORE::read $_[0]->[0], $buf, 4;
91 4632 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 473620 ▶sub read_vint { # unpack w
Subroutine read_vint: called CC=2 SCAR=6.9
Pod documented
101 473620 my $b = ord CORE::getc($_[0]->[0]);
102 473620 my $i = $b & 0x7F;
103 473620 ▶ for (my $s = 7 ; ($b & 0x80) != 0 ; $s += 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (($b & 128) != 0) 15882 473620 489502
104 15882 $b = ord CORE::getc $_[0]->[0];
105 15882 $i |= ($b & 0x7F) << $s;
106 }
107 473620 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 20 *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 1126 ▶sub read_string { # unpack w/a
Subroutine read_string: called CC=1 SCAR=0.0
Pod documented
126 1126 my $length = $_[0]->read_vint();
127 1126 my $utf8;
128 1126 CORE::read $_[0]->[0], $utf8, $length;
129 1126 _utf8_on($utf8);
130 1126 return $utf8;
131 }
132  
133 =head2 read_long
134  
135 This will read eight bytes and return a long.
136  
137 =cut
138  
139 601 ▶sub read_long { # unpack NN
Subroutine read_long: called CC=1 SCAR=0.0
Pod documented
140 601 my $int_a = $_[0]->read_int;
141 601 my $int_b = $_[0]->read_int; # Order is important!
142 # and size matters!
143 601 return (($int_a << 32) | ($int_b & 0xFFFFFFFF));
144 }
145  
146 20 1;
« Plucene/Search/Query.pm ↑ Summary Plucene/Search/Similarity.pm »