Files.pm

filter: Statementstmt 97.7% 129 / 132 Branchbran 72.7% 16 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 91.4% 32 / 35 Podpod 0.0% 0 / 21 totaltotal 84.6% 182 / 215 CC 18 SCAR 8.6
CC
18
Cov
94%
insert_entry
5.0
_build__txn_manager
4.8
delete
4.0
CRAP 2.4
↑ Summary JSPON.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Files;
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path qw(remove_tree make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Directory::Transactional;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Data::Stream::Bulk::Util qw(bulk);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use MooseX::Types::Path::Class qw(Dir File);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub _file_to_id_stream; # cleanup
20  
21 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $VERSION = "0.06";
24  
25 1 with qw(
26 KiokuDB::Backend
27 KiokuDB::Backend::Serialize::Delegate
28 KiokuDB::Backend::Role::Clear
29 KiokuDB::Backend::Role::Scan
30 KiokuDB::Backend::Role::Query::Simple::Linear
31 KiokuDB::Backend::Role::TXN
32 KiokuDB::Backend::Role::TXN::Nested
33 KiokuDB::Backend::Role::Concurrency::POSIX
34 );
35  
36 4 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.4
Pod undocumented
37 4 my $self = shift;
38  
39 4 ▶ unless ( $self->create ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->create) 4 0 4
40 4 my $dir = $self->dir;
41 4 ▶ $dir->open || croak("$dir: $!");
Branch: 1 branch
BranchTrueFalseTotal
$dir->open or croak("${dir}: $!") 4 0 4
42 }
43 }
44  
45 1 has dir => (
46 isa => Dir,
47 is => "ro",
48 required => 1,
49 coerce => 1,
50 );
51  
52 1 has create => (
53 isa => "Bool",
54 is => "ro",
55 default => 0,
56 );
57  
58 1 has object_dir => (
59 isa => "Str",
60 is => "ro",
61 default => "all",
62 );
63  
64 1 has root_set_dir => (
65 isa => "Str",
66 is => "ro",
67 default => "root",
68 );
69  
70 1 has trie => (
71 isa => "Bool",
72 is => "ro",
73 default => 0,
74 );
75  
76 # how many hex nybbles per trie level
77 1 has trie_nybbles => (
78 isa => "Int",
79 is => "rw",
80 default => 3, # default 4096 entries per level
81 );
82  
83 # /dec/afb/decafbad
84 1 has trie_levels => (
85 isa => "Int",
86 is => "rw",
87 default => 2,
88 );
89  
90 1 has _txn_manager => (
91 isa => "Directory::Transactional",
92 is => "ro",
93 lazy_build => 1,
94 );
95  
96 1 for (qw(nfs global_lock auto_commit)) {
97 3 has $_ => (
98 isa => "Bool",
99 is => "ro",
100 predicate => "has_$_",
101 );
102 }
103  
104 4 ▶sub _build__txn_manager {
Subroutine _build__txn_manager: called CC=4 SCAR=15.8
105 4 my $self = shift;
106  
107 4 ▶ Directory::Transactional->new(
Branch: 3 branches
BranchTrueFalseTotal
$self->has_nfs ? : 0 4 4
$self->has_global_lock ? : 4 0 4
$self->has_auto_commit ? : 0 4 4
108 root => $self->dir,
109 ( $self->has_nfs ? ( nfs => $self->nfs ) : () ),
110 ( $self->has_global_lock ? ( global_lock => $self->global_lock ) : () ),
111 ( $self->has_auto_commit ? ( auto_commit => $self->auto_commit ) : () ),
112 );
113 }
114  
115 274 ▶sub txn_do { shift->_txn_manager->txn_do(@_) }
Subroutine txn_do: called CC=1 SCAR=0.0
Pod undocumented
116 0 ▶sub txn_begin { shift->_txn_manager->txn_begin(@_) }
Subroutine txn_begin: not called CC=1 SCAR=6.9
Pod undocumented
117 0 ▶sub txn_commit { shift->_txn_manager->txn_commit(@_) }
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod undocumented
118 0 ▶sub txn_rollback { shift->_txn_manager->txn_rollback(@_) }
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod undocumented
119  
120 356 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
121 356 my ( $self, @uids ) = @_;
122  
123 356 my $t = $self->_txn_manager->_auto_txn;
124  
125 356 local $@;
126 356 return eval { map { $self->get_entry($_) } @uids };
127 }
128  
129 165 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
130 165 my ( $self, @entries ) = @_;
131  
132 # in case we're not in a transaction, make sure it's scoped for the entire insert
133 165 my $t = $self->_txn_manager->_auto_txn;
134  
135 # we sort so that locks are taken in a consistent order, reducing chance of deadlocks
136 165 foreach my $entry ( sort { $a->id cmp $b->id } @entries ) {
137 4481 $self->insert_entry($entry);
138 }
139 }
140  
141 16 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod undocumented
142 16 my ( $self, @ids_or_entries ) = @_;
143  
144 16 ▶ my @uids = map { ref($_) ? $_->id : $_ } @ids_or_entries;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 6 10 16
145  
146 16 my $t = $self->_txn_manager;
147  
148 16 my $g = $t->_auto_txn;
149  
150 16 foreach my $uid ( @uids ) {
151 16 foreach my $file ( $self->object_file($uid), $self->root_set_file($uid) ) {
152 31 $t->unlink($file);
153 }
154 }
155  
156 15 return;
157 }
158  
159 60 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
160 60 my ( $self, @uids ) = @_;
161  
162 60 my $t = $self->_txn_manager;
163  
164 60 my $g = $t->_auto_txn;
165  
166 60 map { $t->exists($self->object_file($_)) } @uids;
167 }
168  
169 4681 ▶sub get_entry {
Subroutine get_entry: called CC=1 SCAR=0.0
Pod undocumented
170 4681 my ( $self, $uid ) = @_;
171  
172 4681 my $fh = $self->open_entry($uid);
173  
174 4675 return $self->serializer->deserialize_from_stream($fh);
175 }
176  
177 4681 ▶sub open_entry {
Subroutine open_entry: called CC=1 SCAR=0.0
Pod undocumented
178 4681 my ( $self, $id ) = @_;
179  
180 4681 $self->_txn_manager->openr( $self->object_file($id) );
181 }
182  
183 4481 ▶sub insert_entry {
Subroutine insert_entry: called CC=5 SCAR=16.1
Pod undocumented
184 4481 my ( $self, $entry ) = @_;
185  
186 4481 my $id = $entry->id;
187  
188 4481 my $file = $self->object_file($id);
189  
190 4481 my $t = $self->_txn_manager;
191  
192 4481 $t->lock_path_write($file);
193  
194 4481 ▶ if ( not($entry->has_prev) and $t->exists($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $entry->has_prev and $t->exists($file)) 3 4478 4481
Condition100%3/3
not $entry->has_prev and $t->exists($file)
!ll&&!rl&&r
9343853
MC/DC100%2/2
not $entry->has_prev and $t->exists($file)
not $entry->has_prev $t->exists($file)
Truth table100%3/3
not(A) and B
A= not $entry->has_prevB= $t->exists($file)
ABresult
0X0
100
111
195 # this is a new entry
196 3 croak "Entry $id already exists";
197 }
198  
199 4478 my $fh = $t->openw($file);
200  
201 4478 $self->serializer->serialize_to_stream($fh, $entry);
202  
203 4478 ▶ close $fh || croak "Couldn't store: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Couldn't store: $!") 4478 0 4478
204  
205 4478 my $root_file = $self->root_set_file($id);
206  
207 4478 ▶ if ( $entry->root ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->root) { } 4324 154 4478
208 # just create a file with the same name... this used to be a hardlink
209 # but that doesn't play nice with Directory::Transactional
210 # maybe it will return eventually
211 4324 $t->openw($root_file);
212 } else {
213 154 $t->unlink($root_file);
214 }
215 }
216  
217 13736 ▶sub _trie_path {
Subroutine _trie_path: called CC=3 SCAR=11.0
218 13736 my ( $self, @path ) = @_;
219  
220 13736 ▶ return File::Spec->catfile(@path) unless $self->trie;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->trie 10246 3490 13736
221  
222 3490 my $uid = pop @path;
223  
224 3490 my $id_hex = unpack("H*", $uid);
225  
226 3490 my $nyb = $self->trie_nybbles;
227  
228 3490 for ( 1 .. $self->trie_levels ) {
229 6980 push @path, substr($id_hex, 0, $nyb, '');
230 }
231  
232 3490 File::Spec->catfile( @path, $uid );
233 }
234  
235 9242 ▶sub object_file {
Subroutine object_file: called CC=1 SCAR=0.0
Pod undocumented
236 9242 my ( $self, $uid ) = @_;
237  
238 9242 $self->_trie_path( $self->object_dir, $uid);
239 }
240  
241 4494 ▶sub root_set_file {
Subroutine root_set_file: called CC=1 SCAR=0.0
Pod undocumented
242 4494 my ( $self, $uid ) = @_;
243  
244 4494 $self->_trie_path( $self->root_set_dir, $uid );
245 }
246  
247 27 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod undocumented
248 27 my $self = shift;
249  
250 27 my $m = $self->_txn_manager;
251  
252 27 my $stream = $m->file_stream( only_files => 1 );
253  
254 27 ▶ while ( my $block = $stream->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $block = $stream->next) 59 27 86
255 59 $m->unlink($_) for @$block;
256 }
257 }
258  
259 46 ▶sub all_entry_files {
Subroutine all_entry_files: called CC=1 SCAR=0.0
Pod undocumented
260 46 my $self = shift;
261  
262 46 $self->_txn_manager->file_stream( only_files => 1, dir => $self->object_dir );
263 }
264  
265 59 ▶sub root_entry_files {
Subroutine root_entry_files: called CC=1 SCAR=0.0
Pod undocumented
266 59 my $self = shift;
267  
268 59 $self->_txn_manager->file_stream( only_files => 1, dir => $self->root_set_dir );
269 }
270  
271 68 ▶sub _file_to_id_stream {
Subroutine _file_to_id_stream: called CC=1 SCAR=0.0
272 68 my $stream = shift;
273  
274 $stream->filter(sub {[
275 map {
276 my ( undef, undef, $file ) = File::Spec->splitpath($_);
277 $file;
278 } @$_
279 68 ]});
280 }
281  
282 9 ▶sub all_entry_ids {
Subroutine all_entry_ids: called CC=1 SCAR=0.0
Pod undocumented
283 9 my $self = shift;
284  
285 9 _file_to_id_stream($self->all_entry_files);
286 }
287  
288 59 ▶sub root_entry_ids {
Subroutine root_entry_ids: called CC=1 SCAR=0.0
Pod undocumented
289 59 my $self = shift;
290  
291 59 _file_to_id_stream($self->root_entry_files);
292 }
293  
294 37 ▶sub all_entries {
Subroutine all_entries: called CC=1 SCAR=0.0
Pod undocumented
295 37 my $self = shift;
296  
297 37 my $ser = $self->serializer;
298 37 my $t = $self->_txn_manager;
299  
300 37 my $stream = $self->all_entry_files;
301  
302 37 ▶ $stream->filter(sub { [ map { $ser->deserialize_from_stream($t->openr($_)) } @$_ ]});
Subroutine __ANON__: called CC=1 SCAR=0.0
303 }
304  
305 # FIXME when we're no longer using empty files this should be fixed
306 38 ▶sub root_entries {
Subroutine root_entries: called CC=1 SCAR=0.0
Pod undocumented
307 38 my $self = shift;
308 38 ▶ $self->root_entry_ids->filter(sub{[ $self->get(@$_) ]});
Subroutine __ANON__: called CC=1 SCAR=0.0
309 }
310  
311 1 __PACKAGE__->meta->make_immutable;
312  
313 1 __PACKAGE__
314  
315 __END__
↑ Summary JSPON.pm »