lib/Metabase/Librarian.pm

filter: Statementstmt 86.9% 80 / 92 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.3% 19 / 22 Podpod 71.4% 5 / 7 totaltotal 81.8% 113 / 138 CC 10 SCAR 11.1
CC
10
Cov
83%
store
8.4
_thaw_fact
3.0
extract
2.0
CRAP 3.0
« lib/Metabase/Query.pm ↑ Summary lib/Metabase/Index/FlatFile.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Librarian;
6 # ABSTRACT: Front-end interface to Metabase storage
7 3 our $VERSION = '1.003'; # VERSION
8  
9 3 ▶use Moose 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Class::MOP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use CPAN::DistnameInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Data::Stream::Bulk::Filter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Metabase::Archive;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Metabase::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Data::GUID ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use JSON 2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 has 'archive' => (
21 is => 'ro',
22 isa => 'Metabase::Archive',
23 required => 1,
24 );
25  
26 3 has 'index' => (
27 is => 'ro',
28 isa => 'Metabase::Index',
29 required => 1,
30 );
31  
32 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
33 2 my $self = shift;
34 2 $self->archive->initialize();
35 }
36  
37 # given fact, store it and return guid;
38 4 ▶sub store {
Subroutine store: called CC=7 SCAR=21.3
Pod documented
39 4 my ($self, $fact) = @_;
40  
41 # Facts must be assigned GUID at source
42 4 ▶ unless ( $fact->guid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fact->guid) 0 4 4
43 0 Carp::confess "Can't store: no GUID set for fact\n";
44 }
45  
46 # Don't store existing GUIDs; this should never happen, since we're just
47 # generating a new one, but... hey, can't be too safe, right?
48 # XXX maybe delegate this down to archive/index, because if they
49 # have unique constraint enforcement, then there's no reason to do
50 # this extra (negative) query
51 4 ▶ if ( $self->index->exists( $fact->guid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->index->exists($fact->guid)) 0 4 4
52 0 Carp::confess("GUID conflicts with an existing object");
53 }
54  
55 # Updated the "update_time" timestamp
56 4 $fact->touch;
57  
58 4 my $fact_struct = $fact->as_struct;
59  
60 # for Reports, store facts and replace content with GUID's
61 # XXX nasty tight coupling with as_struct() -- dagolden, 2009-04-09
62 4 ▶ if ( $fact->isa('Metabase::Report') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fact->isa("Metabase::Report")) 1 3 4
63 1 my @fact_guids;
64 1 for my $f ( $fact->facts ) {
65 2 push @fact_guids, $self->store( $f );
66 }
67 1 $fact_struct->{content} = JSON->new->ascii->encode(\@fact_guids);
68 }
69  
70 4 ▶ if ( $self->archive->store( $fact_struct )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->archive->store($fact_struct) and $self->index->add($fact)) { } 4 0 4
Condition33%1/3
$self->archive->store($fact_struct) and $self->index->add($fact)
!ll&&!rl&&r
004
MC/DC0%0/2
$self->archive->store($fact_struct) and $self->index->add($fact)
$self->archive->store($fact_struct) $self->index->add($fact)
Truth table33%1/3
A and B
A= $self->archive->store($fact_struct)B= $self->index->add($fact)
ABresult
0X0
100
111
71 && $self->index ->add ( $fact ) ) {
72 4 return $fact->guid;
73 } else {
74 0 Carp::confess("Error storing or indexing fact with guid: " . $fact->guid);
75 }
76 }
77  
78 3 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
79 3 my ($self, %spec) = @_;
80 3 return $self->index->search( %spec );
81 }
82  
83 4 ▶sub extract {
Subroutine extract: called CC=2 SCAR=7.0
Pod documented
84 4 my ($self, $guid) = @_;
85 4 my $fact_struct = $self->archive->extract( lc $guid );
86  
87 4 ▶ Carp::confess "Fact $guid does not exist" unless $fact_struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $fact_struct 0 4 4
88  
89 4 return $self->_thaw_fact( $fact_struct );
90 }
91  
92 4 ▶sub _thaw_fact {
Subroutine _thaw_fact: called CC=3 SCAR=11.0
93 4 my ($self, $fact_struct) = @_;
94 4 my $fact;
95  
96 # reconstruct fact meta and extract type to find the class
97 my $class = Metabase::Fact->class_from_type(
98 $fact_struct->{metadata}{core}{type}
99 4 );
100  
101 4 Class::MOP::load_class( $class );
102  
103 # XXX: The problem here is that what we get out of the librarian isn't
104 # exactly what we put in, it seems. We need to improve the specification
105 # for what goes in/out and then test it more thoroughly. *Clearly* the
106 # following block is a wretched hack. -- rjbs, 2009-06-24
107 4 ▶ if ($class->isa('Metabase::Report')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa('Metabase::Report')) { } 1 3 4
108 1 my @facts;
109 1 my $content = JSON->new->ascii->decode( $fact_struct->{content} );
110 1 for my $g ( @$content ) {
111 # XXX no error checking if extract() fails -- dagolden, 2009-04-09
112 2 push @facts, $self->extract( $g );
113 }
114  
115 1 my $bogus_content = [ map { $_->as_struct } @facts ];
116 1 my $bogus_string = JSON->new->ascii->encode( $bogus_content );
117  
118 $fact = $class->from_struct({
119 metadata => { core => $fact_struct->{metadata}{core} },
120 1 content => \$bogus_string,
121 });
122 1 $fact->close;
123 }
124 else {
125 3 $fact = $class->from_struct( $fact_struct );
126 }
127 4 return $fact;
128 }
129  
130 3 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
131 3 my ($self, $guid) = @_;
132 3 return $self->index->exists( lc $guid );
133 }
134  
135 0 ▶sub iterator {
Subroutine iterator: not called CC=1 SCAR=6.9
Pod documented
136 0 my ($self) = @_;
137 return Data::Stream::Bulk::Filter->new(
138 stream => $self->archive->iterator,
139 0 ▶ filter => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 return [ map { $self->_thaw_fact( $_ ) } @{ $_[0] } ];
141 },
142 0 );
143 }
144  
145 # DO NOT lc() the GUID -- we must allow deletion of improperly cased GUIDS from source
146 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my ($self, $guid) = @_;
148 0 $self->index->delete( $guid );
149 0 $self->archive->delete( $guid );
150 0 return 1;
151 }
152  
153 3 1;
154  
155 __END__
« lib/Metabase/Query.pm ↑ Summary lib/Metabase/Index/FlatFile.pm »