lib/Metabase/Index/FlatFile.pm

filter: Statementstmt 80.9% 136 / 168 Branchbran 80.7% 21 / 26 Conditioncond 27.7% 5 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 67.3% 33 / 49 Podpod 0.0% 0 / 17 totaltotal 68.0% 196 / 288 CC 23 SCAR 10.6
CC
23
Cov
76%
query
7.0
__ANON__
6.0
__ANON__
6.0
CRAP 2.9
« lib/Metabase/Librarian.pm ↑ Summary blib/lib/Metabase/Test/Archive/Null.pm »
1 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Index::FlatFile;
6 # ABSTRACT: Metabase flat-file index
7 4 our $VERSION = '1.003'; # VERSION
8  
9 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Data::Stream::Bulk::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use List::AllUtils qw/any all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use JSON 2 qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Regexp::SQL::LIKE 0.001 qw/to_regexp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 with 'Metabase::Index';
23  
24 4 has 'index_file' => (
25 is => 'ro',
26 isa => 'Path::Class::File',
27 coerce => 1,
28 required => 1,
29 );
30  
31 0 ▶sub initialize {}
Subroutine initialize: not called CC=1 SCAR=0.0
Pod undocumented
32  
33 12 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod undocumented
34 12 my ($self, $fact) = @_;
35 12 ▶ Carp::confess( "can't index a Fact without a GUID" ) unless $fact->guid;
Branch: 1 branch
BranchTrueFalseTotal
unless $fact->guid 0 12 12
36  
37 12 my $metadata = $self->clone_metadata( $fact );
38 12 my $line = encode_json($metadata);
39 12 my $filename = $self->index_file;
40 12 ▶ my $fh = IO::File->new( $filename, "a+" )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($filename, "a+") or Carp::confess("Couldn't append to '${filename}': $!") 12 0 12
41 or Carp::confess( "Couldn't append to '$filename': $!" );
42 12 $fh->binmode(':raw');
43 12 flock $fh, LOCK_EX;
44 {
45 12 seek $fh, 2, 0; # end
46 12 print {$fh} $line, "\n";
47 }
48 12 $fh->close;
49 }
50  
51 41 ▶sub query {
Subroutine query: called CC=7 SCAR=19.5
Pod undocumented
52 41 my ($self, %spec) = @_;
53  
54 41 my $filename = $self->index_file;
55 41 ▶ return Data::Stream::Bulk::Array->new( array => [] )
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 41 41
56 unless -f $filename;
57  
58 41 my $query = $self->get_native_query( \%spec );
59 41 ▶ my $fh = IO::File->new( $filename, "r" )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($filename, "r") or Carp::confess("Couldn't read from '${filename}': $!") 41 0 41
60 or Carp::confess( "Couldn't read from '$filename': $!" );
61 41 $fh->binmode(':raw');
62 41 my @matches;
63 41 flock $fh, LOCK_SH;
64 {
65 41 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 63 41 104
66 63 my $parsed = decode_json($line);
67 63 ▶ push @matches, $parsed if $query->{-where}->($parsed);
Branch: 1 branch
BranchTrueFalseTotal
if $query->{"-where"}->($parsed) 39 24 63
68 }
69 }
70 41 $fh->close;
71  
72 # sort
73 41 ▶ if ( exists $spec{-order} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec{"-order"}) 3 38 41
74 3 @matches = sort { $spec{-order}->($a, $b) } @matches;
75 }
76  
77 # limit
78 41 ▶ if ( exists $spec{-limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec{"-limit"}) 1 40 41
79 1 @matches = splice(@matches, 0, $spec{-limit});
80 }
81  
82 return Data::Stream::Bulk::Array->new(
83 41 array => [ map { $_->{'core.guid'} } @matches ]
84 );
85 }
86  
87 15 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod undocumented
88 15 my ($self, %spec) = @_;
89 15 my $result = [ $self->query(%spec)->all ];
90 15 return scalar @$result;
91 }
92  
93 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.0
Pod undocumented
94 2 my ($self, $guid) = @_;
95  
96 2 my @index;
97 2 my $obj = tie @index, 'Tie::File', $self->index_file->stringify;
98 2 $obj->flock(LOCK_EX);
99 {
100 2 for my $i ( 0 .. $#index ) {
101 2 my $parsed = decode_json($index[$i]);
102 2 ▶ if ($parsed->{'core.guid'} eq $guid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->{"core.guid"} eq $guid) 2 0 2
103 2 splice @index, $i, 1; # delete that row
104 2 last;
105 }
106 }
107 }
108 2 undef $obj;
109 2 untie @index;
110  
111 2 return 1;
112 }
113  
114 #--------------------------------------------------------------------------#
115 # required by Metabase::Query
116 #
117 # ops return closures that define the necessary logic when called
118 # with hash of index fields
119 #--------------------------------------------------------------------------#
120  
121 41 ▶sub translate_query {
Subroutine translate_query: called CC=5 SCAR=16.1
Pod undocumented
122 41 my ( $self, $spec ) = @_;
123  
124 # translate search query into a coderef
125 41 ▶ if ( exists $spec->{-where} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{'-where'}) { } 30 11 41
126 30 $spec->{-where} = $self->dispatch_query_op( $spec->{-where} );
127 }
128 else {
129 11 ▶ $spec->{-where} = sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
130 }
131  
132 41 ▶ if ( exists $spec->{-order} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spec->{"-order"}) 3 38 41
133 3 ▶ my $sort_fcn = sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
134 3 my @order = @{$spec->{-order}};
135 3 ▶ while ( @order ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) 3 3 6
136 3 my ($dir, $field) = splice( @order, 0, 2);
137 3 my $old_fcn = $sort_fcn;
138 my $new_fcn = ($dir eq '-asc')
139 1 ▶ ? sub { my ($i, $j) = @_; return $i->{$field} cmp $j->{$field} }
Subroutine __ANON__: called CC=1 SCAR=0.0
140 2 ▶ : sub { my ($i, $j) = @_; return $j->{$field} cmp $i->{$field} }
Subroutine __ANON__: called CC=1 SCAR=0.0
141 3 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq '-asc' ? : 1 2 3
142 3 ▶ $sort_fcn = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
143 3 my ($i, $j) = @_;
144 3 ▶ return $old_fcn->($i, $j) || $new_fcn->($i, $j);
Condition33%1/3
&$old_fcn($i, $j) || &$new_fcn($i, $j)
l!l&&r!l&&!r
030
MC/DC0%0/2
&$old_fcn($i, $j) || &$new_fcn($i, $j)
&$old_fcn($i, $j) &$new_fcn($i, $j)
Truth table33%1/3
A || B
A= &$old_fcn($i, $j)B= &$new_fcn($i, $j)
ABresult
1X1
011
000
145 }
146 3 }
147 3 $spec->{-order} = $sort_fcn;
148 }
149  
150 41 return $spec;
151 }
152  
153 35 ▶sub op_eq {
Subroutine op_eq: called CC=1 SCAR=0.0
Pod undocumented
154 35 my ($self, $field, $val) = @_;
155 60 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
156 60 ▶ my $data = shift->{$field} || '';
Condition100%2/2
+(shift())->{$field} || ''
l!l
582
MC/DC100%1/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table100%2/2
A
A= +(shift())->{$field}
Aresult
11
00
157 60 return $data eq $val
158 35 };
159 }
160  
161 3 ▶sub op_ne {
Subroutine op_ne: called CC=1 SCAR=0.0
Pod undocumented
162 3 my ($self, $field, $val) = @_;
163 6 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
164 6 ▶ my $data = shift->{$field} || '';
Condition50%1/2
+(shift())->{$field} || ''
l!l
60
MC/DC0%0/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table50%1/2
A
A= +(shift())->{$field}
Aresult
11
00
165 6 return $data ne $val
166 3 };
167 }
168  
169 1 ▶sub op_gt {
Subroutine op_gt: called CC=1 SCAR=0.0
Pod undocumented
170 1 my ($self, $field, $val) = @_;
171 2 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
172 2 ▶ my $data = shift->{$field} || '';
Condition50%1/2
+(shift())->{$field} || ''
l!l
20
MC/DC0%0/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table50%1/2
A
A= +(shift())->{$field}
Aresult
11
00
173 2 return $data gt $val
174 1 };
175 }
176  
177 0 ▶sub op_lt {
Subroutine op_lt: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my ($self, $field, $val) = @_;
179 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
180 0 ▶ my $data = shift->{$field} || '';
Condition0%0/2
+(shift())->{$field} || ''
l!l
00
MC/DC0%0/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table0%0/2
A
A= +(shift())->{$field}
Aresult
11
00
181 0 return $data lt $val
182 0 };
183 }
184  
185 0 ▶sub op_ge {
Subroutine op_ge: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my ($self, $field, $val) = @_;
187 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 ▶ my $data = shift->{$field} || '';
Condition0%0/2
+(shift())->{$field} || ''
l!l
00
MC/DC0%0/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table0%0/2
A
A= +(shift())->{$field}
Aresult
11
00
189 0 return $data ge $val
190 0 };
191 }
192  
193 0 ▶sub op_le {
Subroutine op_le: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my ($self, $field, $val) = @_;
195 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
196 0 ▶ my $data = shift->{$field} || '';
Condition0%0/2
+(shift())->{$field} || ''
l!l
00
MC/DC0%0/1
+(shift())->{$field} || ''
+(shift())->{$field}
Truth table0%0/2
A
A= +(shift())->{$field}
Aresult
11
00
197 0 return $data le $val
198 0 };
199 }
200  
201 0 ▶sub op_between {
Subroutine op_between: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my ($self, $field, $low, $high) = @_;
203 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
204 0 my $data = shift->{$field};
205 0 ▶ return $data ge $low && $data le $high;
Condition0%0/3
$data ge $low && $data le $high
!ll&&!rl&&r
000
MC/DC0%0/2
$data ge $low && $data le $high
$data ge $low $data le $high
Truth table0%0/3
A && B
A= $data ge $lowB= $data le $high
ABresult
0X0
100
111
206 0 };
207 }
208  
209 0 ▶sub op_like {
Subroutine op_like: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my ($self, $field, $val) = @_;
211 0 my ($re) = to_regexp($val);
212 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 my $data = shift->{$field};
214 0 return $data =~ $re;
215 }
216 0 }
217  
218 0 ▶sub op_not {
Subroutine op_not: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my ($self, $pred) = @_;
220 0 my $clause = $self->dispatch_query_op($pred);
221 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 return ! $clause->(shift)
223 }
224 0 }
225  
226 0 ▶sub op_or {
Subroutine op_or: not called CC=1 SCAR=6.9
Pod undocumented
227 0 my ($self, @args) = @_;
228 0 my @predicates = map { $self->dispatch_query_op($_) } @args;
229 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 my $data = shift;
231 0 ▶ return any { $_->($data) } @predicates;
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 }
233 0 }
234  
235 12 ▶sub op_and {
Subroutine op_and: called CC=1 SCAR=0.0
Pod undocumented
236 12 my ($self, @args) = @_;
237 12 my @predicates = map { $self->dispatch_query_op($_) } @args;
238 23 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 23 my $data = shift;
240 23 ▶ return all { $_->($data) } @predicates;
Subroutine __ANON__: called CC=1 SCAR=0.0
241 }
242 12 }
243  
244 4 1;
245  
246 __END__
« lib/Metabase/Librarian.pm ↑ Summary blib/lib/Metabase/Test/Archive/Null.pm »