Giddy/Collection.pm

filter: Statementstmt 11.8% 26 / 220 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 131 MC/DCmcdc 0.0% 0 / 70 Subroutinesub 18.7% 6 / 32 Podpod 100% 21 / 21 totaltotal 8.5% 53 / 618 CC 127 SCAR 61.4
CC
127
Cov
5%
sort
1482.0
find
342.0
grep
272.0
CRAP 465.3
« Giddy/Role/DocumentUpdater.pm ↑ Summary Giddy/Database.pm »
1 package Giddy::Collection;
2  
3 # ABSTRACT: A Giddy collection.
4  
5 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Giddy::Collection::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Giddy::StaticDirectory;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = "0.013_001";
14 2 $VERSION = eval $VERSION;
15  
16 2 has 'path' => (is => 'ro', isa => 'Str', default => '');
17  
18 2 has 'db' => (is => 'ro', isa => 'Giddy::Database', required => 1);
19  
20 2 has 'cached' => (is => 'ro', isa => 'Bool', lazy_build => 1);
21  
22 2 has '_loc' => (is => 'ro', isa => 'Int', default => 0, writer => '_set_loc');
23  
24 2 with 'Giddy::Role::DocumentLoader',
25 'Giddy::Role::DocumentMatcher',
26 'Giddy::Role::DocumentStorer',
27 'Giddy::Role::DocumentUpdater';
28  
29 =head1 NAME
30  
31 Giddy::Collection - A Giddy collection.
32  
33 =head1 VERSION
34  
35 version 0.013_001
36  
37 =head1 SYNOPSIS
38  
39 my $coll = $db->get_collection('articles');
40  
41 =head1 DESCRIPTION
42  
43 This class represents Giddy collections, which are directories holding documents
44 in the database. Collections are hierarchical in Giddy (see L<Giddy::Manual> for
45 more information).
46  
47 You will find most of your interaction with the Giddy database are performed on
48 this class' objects. The class provides methods for finding documents, creating
49 documents, updating documents, removing documents, iterating over query results,
50 etc.
51  
52 =head1 CONSUMES
53  
54 =over
55  
56 =item * L<Giddy::Role::DocumentLoader>
57  
58 =item * L<Giddy::Role::DocumentMatcher>
59  
60 =item * L<Giddy::Role::DocumentStorer>
61  
62 =item * L<Giddy::Role::DocumentUpdater>
63  
64 =back
65  
66 =head1 ATTRIBUTES
67  
68 =head2 path
69  
70 The relative path of the collection. Defaults to the empty string (''), which is
71 the root directory of the database. Never has a starting slash.
72  
73 =head2 db
74  
75 The L<Giddy::Database> object the collection belongs to. Required.
76  
77 =head2 cached
78  
79 Holds a boolean value indicating whether the collection exists in the database
80 index (i.e. has already been staged and commited), or isn't (i.e. a new one).
81 Automatically calculated.
82  
83 =head2 _loc
84  
85 An integer representing the current location of the iterator in the results array.
86 Not to be used externally.
87  
88 =head1 OBJECT METHODS
89  
90 =head2 DOCUMENT QUERYING
91  
92 =head3 find( [ \%query, \%options ] )
93  
94 Searches the collection for documents that match the provided query.
95 If no query is given, every document in the collection will be matched. See
96 L<Giddy::Manual/"FINDING DOCUMENTS"> for more information on queries.
97  
98 =head3 find( [ $name, \%options ] )
99  
100 Searches the collection for documents (more correctly "a document")
101 whos name equals C<$name>. This is a shortcut for C<< find({ _name => $name }, $options) >>.
102  
103 =head2 find( [ $regex, \%options ] )
104  
105 Searches the collection for documents whose name matches the regular
106 expression provided. This is a shortcut for C<< find({ _name => qr/some_regex/ }, $options >>.
107  
108 All version of the method return a L<Giddy::Collection::InMemory> object, which
109 inherits from this class.
110  
111 Searching just by name (either for equality or with a regex) is much faster
112 than searching by other attributes, as Giddy isn't forced to load and deserialize
113 every document.
114  
115 In any of the above versions of the method, if you don't provide a query, or provide
116 an empty string or empty hash-ref, every document in the collection will be matched.
117  
118 You can also pass a hash-ref of options. Currently, only the 'skip_binary' option
119 is available. If provided with a true value, binary attributes of documents will
120 be ignored. See L<Giddy::Manual/"BINARY ATTRIBUTES"> for info on binary attributes.
121  
122 =cut
123  
124 0 ▶sub find {
Subroutine find: not called CC=18 SCAR=58.3
Pod documented
125 0 my ($self, $query, $opts) = @_;
126  
127 0 ▶ croak "find() expects either a scalar, a regex or a hash-ref for the query."
Branch: 1 branch
BranchTrueFalseTotal
if defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp" 0 0 0
Condition0%0/9
defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp"
!ll&&!rl&&r
000
defined $query and ref $query and ref $query ne "HASH"
!ll&&!rl&&r
000
defined $query and ref $query
!ll&&!rl&&r
000
MC/DC0%0/4
defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp"
defined $query ref $query ref $query ne "HASH" ref $query ne "Regexp"
Truth table0%0/5
A and B and C and D
A= defined $queryB= ref $queryC= ref $query ne "HASH"D= ref $query ne "Regexp"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
128 if defined $query && ref $query && ref $query ne 'HASH' && ref $query ne 'Regexp';
129  
130 0 ▶ croak "find() expected a hash-ref of options."
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts and ref $opts ne "HASH" 0 0 0
Condition0%0/3
defined $opts and ref $opts ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts and ref $opts ne "HASH"
defined $opts ref $opts ne "HASH"
Truth table0%0/3
A and B
A= defined $optsB= ref $opts ne "HASH"
ABresult
0X0
100
111
131 if defined $opts && ref $opts ne 'HASH';
132  
133 0 ▶ $query ||= '';
Condition0%0/2
$query ||= ""
l!l
00
MC/DC0%0/1
$query ||= ""
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
134 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
135  
136 0 ▶ $query = { _name => $query } if !ref $query || ref $query eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $query or ref $query eq "Regexp" 0 0 0
Condition0%0/3
not ref $query or ref $query eq "Regexp"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $query or ref $query eq "Regexp"
not ref $query ref $query eq "Regexp"
Truth table0%0/3
not(A) or B
A= not ref $queryB= ref $query eq "Regexp"
ABresult
1X1
011
000
137  
138 # stage 1: create an in-memory collection
139 0 my $coll = Giddy::Collection::InMemory->new(
140 path => $self->path,
141 db => $self->db,
142 _query => { find => $query, coll => $self, opts => $opts }
143 );
144  
145 # we want to make finds by name equality fast, so we're not gonna
146 # give the in-memory collection all documents of the current
147 # collection if that is the case (but only when the current collection
148 # is not an in-memory collection, queries on in-memory collections
149 # will already be fast enough
150 $coll->_set_documents($self->_documents)
151 0 ▶ unless (exists $query->{_name} && (!ref $query->{_name} || (ref $query->{_name} eq 'HASH' && scalar keys %{$query->{_name}} == 1 && $query->{_name}->{'$eq'})));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $query->{"_name"} and !ref($query->{"_name"}) || ref $query->{"_name"} eq "HASH" && scalar keys %{$$query{"_name"};} == 1 && $query->{"_name"}{"\$eq"} 0 0 0
Condition0%0/12
exists $query->{"_name"} and !ref($query->{"_name"}) || ref $query->{"_name"} eq "HASH" && scalar keys %{$$query{"_name"};} == 1 && $query->{"_name"}{"\$eq"}
!ll&&!rl&&r
000
not ref $query->{"_name"} or ref $query->{"_name"} eq "HASH" and scalar keys %{$$query{"_name"};} == 1 and $query->{"_name"}{"\$eq"}
l!l&&r!l&&!r
000
ref $query->{"_name"} eq "HASH" and scalar keys %{$$query{"_name"};} == 1 and $query->{"_name"}{"\$eq"}
!ll&&!rl&&r
000
ref $query->{"_name"} eq "HASH" and scalar keys %{$$query{"_name"};} == 1
!ll&&!rl&&r
000
MC/DC0%0/5
exists $query->{"_name"} and !ref($query->{"_name"}) || ref $query->{"_name"} eq "HASH" && scalar keys %{$$query{"_name"};} == 1 && $query->{"_name"}{"\$eq"}
exists $query->{"_name"} not ref $query->{"_name"} ref $query->{"_name"} eq "HASH" scalar keys %{$$query{"_name"};} == 1 $query->{"_name"}{"\$eq"}
Truth table0%0/6
A and not(B) or C and D and E
A= exists $query->{"_name"}B= not ref $query->{"_name"}C= ref $query->{"_name"} eq "HASH"D= scalar keys %{$$query{"_name"};} == 1E= $query->{"_name"}{"\$eq"}
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
152  
153 # stage 2: are we matching by name? we do if query is a scalar
154 # or if the query hash-ref has the _name key
155 0 ▶ if (exists $query->{_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query->{"_name"}) 0 0 0
156 # let's find documents that match this name
157 0 $coll->_set_documents($self->_match_by_name(delete($query->{_name}), $opts));
158 }
159  
160 # stage 3: are we querying by document attributes too?
161 0 ▶ if (scalar keys %$query) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$query) 0 0 0
162 0 my ($docs, $loaded) = $coll->_match_by_query($query, $opts);
163 0 $coll->_set_documents($docs);
164 0 $coll->_set_loaded($loaded);
165 }
166  
167 0 return $coll;
168 }
169  
170 =head3 find_one( [ $query, \%options ] )
171  
172 Same as calling C<< find($query, $options)->first() >>.
173  
174 =cut
175  
176 0 ▶sub find_one {
Subroutine find_one: not called CC=1 SCAR=6.9
Pod documented
177 0 shift->find(@_)->first;
178 }
179  
180 =head3 grep( [ \@strings, \%options ] )
181  
182 Finds documents whose file contents (I<including> attribute names and database YAML
183 structure) match all (or any, depending on C<\%options>) of the provided
184 strings. This is much faster than using C<find()> as it simply uses the
185 C<git grep> command, but is obviously less useful.
186  
187 The only option supported is 'or'. If passed with a true value, documents that
188 have at least one of the provided strings will be matched. Otherwise only documents
189 that have all strings are matched.
190  
191 If the strings array is empty, all documents in the collection will match.
192  
193 =head3 grep( [ $string ] )
194  
195 Finds documents whose file contents (I<including> attribute names and database YAML
196 structure) match the provided string. If string is empty, all documents in the
197 collection will match.
198  
199 Both methods return a L<Giddy::Collection::InMemory> object.
200  
201 =cut
202  
203 0 ▶sub grep {
Subroutine grep: not called CC=16 SCAR=56.1
Pod documented
204 0 my ($self, $query, $opts) = @_;
205  
206 0 ▶ croak "grep() expected a hash-ref of options."
Branch: 1 branch
BranchTrueFalseTotal
if $opts and ref $opts ne "HASH" 0 0 0
Condition0%0/3
$opts and ref $opts ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$opts and ref $opts ne "HASH"
$opts ref $opts ne "HASH"
Truth table0%0/3
A and B
A= $optsB= ref $opts ne "HASH"
ABresult
0X0
100
111
207 if $opts && ref $opts ne 'HASH';
208  
209 0 ▶ $query ||= [];
Condition0%0/2
$query ||= []
l!l
00
MC/DC0%0/1
$query ||= []
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
210 0 ▶ $query = [$query] if !ref $query;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $query 0 0 0
211 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
212  
213 0 ▶ return $self->find('', $opts) unless scalar @$query;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$query 0 0 0
214  
215 0 my $coll = Giddy::Collection::InMemory->new(
216 path => $self->path,
217 db => $self->db,
218 _query => { grep => $query, coll => $self, opts => $opts }
219 );
220  
221 0 my @cmd = ('grep', '-I', '--name-only', '--max-depth', 1, '--cached');
222 0 ▶ push(@cmd, '--all-match') unless $opts->{'or'}; # that's how we do an 'and' search'
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"or"} 0 0 0
223  
224 0 foreach (@$query) {
225 0 push(@cmd, '-e', $_);
226 }
227  
228 0 ▶ push(@cmd, { cwd => $self->db->_repo->work_tree.'/'.$self->path })
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 0 0 0
229 if $self->path;
230  
231 0 my $docs = Tie::IxHash->new;
232 0 foreach ($self->db->_repo->run(@cmd)) {
233 0 ▶ if (m!/!) { # there'll at most be one slash since --max-depth is 1
Branch: 1 branch
BranchTrueFalseTotal
if (m[/]) { } 0 0 0
234 0 my $name = $`;
235 # if this is an in-memory collection, we have to ignore
236 # documents in the actual filesystem collection but not here
237 0 ▶ next unless $self->_documents->EXISTS($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_documents->EXISTS($name) 0 0 0
238 0 ▶ next if $docs->EXISTS($name);
Branch: 1 branch
BranchTrueFalseTotal
if $docs->EXISTS($name) 0 0 0
239 0 $docs->STORE($name => 'dir');
240 } else {
241 # if this is an in-memory collection, we have to ignore
242 # documents in the actual filesystem collection but not here
243 0 ▶ next unless $self->_documents->EXISTS($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_documents->EXISTS($_) 0 0 0
244 0 ▶ next if $docs->EXISTS($_);
Branch: 1 branch
BranchTrueFalseTotal
if $docs->EXISTS($_) 0 0 0
245 0 $docs->STORE($_ => 'file');
246 }
247 }
248  
249 # sort the documents alphabetically by name
250 0 $docs->SortByKey;
251  
252 0 $coll->_set_documents($docs);
253  
254 0 return $coll;
255 }
256  
257 =head3 grep_one( [ $string, \%options ] )
258  
259 =head3 grep_one( [ \@strings, \%options ] )
260  
261 Same as calling C<< grep( $string(s), $options)->first >>.
262  
263 =cut
264  
265 0 ▶sub grep_one {
Subroutine grep_one: not called CC=1 SCAR=6.9
Pod documented
266 0 shift->grep(@_)->first;
267 }
268  
269 =head2 DOCUMENT MANIPULATION
270  
271 =head3 insert( $name, \%attributes )
272  
273 Creates a new document in the collection. You must provide a name for the document
274 (keep in mind that the name will be the document's file/directory's name, so try
275 not to use fancy characters), and a hash-ref of the document's attributes. This hash-ref
276 doesn't have to be flat, attributes can be nestable (i.e. they can have array and
277 hash references themselves).
278  
279 If C<$attributes> has the '_body' key, the document created will be a document
280 file. Otherwise a document directory will be created. See L<Giddy::Manual/"CREATING DOCUMENTS">
281 for more information on documents.
282  
283 Returns the path of the document created relative to the database root directory
284 (including a starting slash). Croaks if a document or a sub-collection named C<$name>
285 already exists in the collection.
286  
287 =cut
288  
289 0 ▶sub insert {
Subroutine insert: not called CC=3 SCAR=24.8
Pod documented
290 0 my ($self, $filename, $attrs) = @_;
291  
292 0 ▶ croak "You must provide a filename for the new document (that doesn't start with a slash)."
Branch: 1 branch
BranchTrueFalseTotal
unless $filename and not $filename =~ m[^/] 0 0 0
Condition0%0/3
$filename and not $filename =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$filename and not $filename =~ m[^/]
$filename not $filename =~ m[^/]
Truth table0%0/3
A and not(B)
A= $filenameB= not $filename =~ m[^/]
ABresult
0X0
100
111
293 unless $filename && $filename !~ m!^/!;
294  
295 0 return ($self->batch_insert([$filename => $attrs]))[0];
296 }
297  
298 =head3 batch_insert( [ $name1 => \%attrs1, $name2 => \%attrs2, ... ] )
299  
300 Inserts a series of documents one after another. Returns a list with the names of
301 all documents created. If even one document cannot be created (mostly since a similarly
302 named document/collection already exists), none will be created.
303  
304 =cut
305  
306 0 ▶sub batch_insert {
Subroutine batch_insert: not called CC=10 SCAR=47.0
Pod documented
307 0 my ($self, $docs) = @_;
308  
309 # first, make sure the document array is valid
310 0 ▶ croak "batch_insert() expects an array-ref of documents."
Branch: 1 branch
BranchTrueFalseTotal
unless $docs and ref $docs eq "ARRAY" 0 0 0
Condition0%0/3
$docs and ref $docs eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$docs and ref $docs eq "ARRAY"
$docs ref $docs eq "ARRAY"
Truth table0%0/3
A and B
A= $docsB= ref $docs eq "ARRAY"
ABresult
0X0
100
111
311 unless $docs && ref $docs eq 'ARRAY';
312 0 ▶ croak "Odd number of elements in document array, batch_insert() expects an even-numberd array."
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$docs % 2 == 0 0 0 0
313 unless scalar @$docs % 2 == 0;
314  
315 0 my $hash = Tie::IxHash->new(@$docs);
316  
317 # make sure array is valid and we can actually create all the documents (i.e. they
318 # don't already exist) - if even one document is invalid, we don't create any
319 0 foreach my $filename ($hash->Keys) {
320 0 ▶ croak "A document called $filename already exists."
Branch: 1 branch
BranchTrueFalseTotal
if $self->cached and $self->db->_path_exists($self->_path_to($filename)) 0 0 0
Condition0%0/3
$self->cached and $self->db->_path_exists($self->_path_to($filename))
!ll&&!rl&&r
000
MC/DC0%0/2
$self->cached and $self->db->_path_exists($self->_path_to($filename))
$self->cached $self->db->_path_exists($self->_path_to($filename))
Truth table0%0/3
A and B
A= $self->cachedB= $self->db->_path_exists($self->_path_to($filename))
ABresult
0X0
100
111
321 if $self->cached && $self->db->_path_exists($self->_path_to($filename));
322  
323 0 my $attrs = $hash->FETCH($filename);
324  
325 0 ▶ croak "You must provide document ${filename}'s attributes as a hash-ref."
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs and ref $attrs eq "HASH" 0 0 0
Condition0%0/3
$attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs and ref $attrs eq "HASH"
$attrs ref $attrs eq "HASH"
Truth table0%0/3
A and B
A= $attrsB= ref $attrs eq "HASH"
ABresult
0X0
100
111
326 unless $attrs && ref $attrs eq 'HASH';
327 }
328  
329 0 my @names; # will hold names of all documents created
330  
331 # store the documents in the filesystem
332 0 foreach my $filename ($hash->Keys) {
333 0 $self->_store_document($filename, $hash->FETCH($filename));
334  
335 # return the document's path
336 0 push(@names, $filename);
337 }
338  
339 0 return @names;
340 }
341  
342 =head3 update( $name, \%object, [ \%options ] )
343  
344 =head3 update( \%query, \%object, [ \%options ] )
345  
346 Performs a query on the collection, and updates the first document found according
347 to the update object (C<\%object> above). You must provide a query, but this
348 can be an empty string or hash-ref, in which case all documents in the collection
349 will be matched. An options hash-ref can be provided, with any of the following options:
350  
351 =over
352  
353 =item * skip_binary - See L</"BINARY ATTRIBUTES"> for info.
354  
355 =item * multiple - Update all documents you find, not just the first one.
356  
357 =item * upsert - If you don't find any document that matches the query, create one
358  
359 =back
360  
361 Returns a hash-ref with two keys: 'n' - with the number of documents updated (0
362 if none) and 'docs' - an array-ref with the names of all documents updated (empty if none).
363  
364 See L<Giddy::Manual/"UPDATING DOCUMENTS"> for more information on updating.
365  
366 =cut
367  
368 0 ▶sub update {
Subroutine update: not called CC=14 SCAR=53.5
Pod documented
369 0 my ($self, $query, $obj, $options) = @_;
370  
371 0 ▶ croak "update() requires a query string (can be empty) or hash-ref (can also be empty)."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 0 0
372 unless defined $query;
373 0 ▶ croak "update() requires a hash-ref object to update according to."
Branch: 1 branch
BranchTrueFalseTotal
unless $obj and ref $obj eq "HASH" 0 0 0
Condition0%0/3
$obj and ref $obj eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and ref $obj eq "HASH"
$obj ref $obj eq "HASH"
Truth table0%0/3
A and B
A= $objB= ref $obj eq "HASH"
ABresult
0X0
100
111
374 unless $obj && ref $obj eq 'HASH';
375 0 ▶ croak "update() expects a hash-ref of options."
Branch: 1 branch
BranchTrueFalseTotal
if $options and ref $options ne "HASH" 0 0 0
Condition0%0/3
$options and ref $options ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$options and ref $options ne "HASH"
$options ref $options ne "HASH"
Truth table0%0/3
A and B
A= $optionsB= ref $options ne "HASH"
ABresult
0X0
100
111
376 if $options && ref $options ne 'HASH';
377  
378 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
379 0 $options->{skip_binary} = 1;
380  
381 0 my $cursor = $self->find($query, $options);
382  
383 0 my $updated = { docs => [], n => 0 }; # will be returned to the caller
384  
385 # have we found anything? if not, are we upserting?
386 0 ▶ if ($cursor->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cursor->count) { } 0 0 0
elsif ($options->{'upsert'} and ref $query eq 'HASH' and $query->{'_name'} and not ref $query->{'_name'}) { } 0 0 0
387 0 ▶ my @docs = $options->{multiple} ? $cursor->all : ($cursor->first); # the documents we're updating
Branch: 1 branch
BranchTrueFalseTotal
$options->{'multiple'} ? : 0 0 0
388  
389 0 foreach (@docs) {
390 0 my $name = $_->{_name};
391  
392 # update the document object
393 0 $self->_update_document($obj, $_);
394  
395 # store the document in the file system
396 0 $self->_store_document($name, $_);
397  
398 # add info about this update to the $updated hash
399 0 $updated->{n} += 1;
400 0 push(@{$updated->{docs}}, $name);
401 }
402 ▶ } elsif ($options->{upsert} && ref $query eq 'HASH' && $query->{_name} && !ref $query->{_name}) {
Condition0%0/9
$options->{"upsert"} and ref $query eq "HASH" and $query->{"_name"} and not ref $query->{"_name"}
!ll&&!rl&&r
000
$options->{"upsert"} and ref $query eq "HASH" and $query->{"_name"}
!ll&&!rl&&r
000
$options->{"upsert"} and ref $query eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
$options->{"upsert"} and ref $query eq "HASH" and $query->{"_name"} and not ref $query->{"_name"}
$options->{"upsert"} ref $query eq "HASH" $query->{"_name"} not ref $query->{"_name"}
Truth table0%0/5
A and B and C and not(D)
A= $options->{"upsert"}B= ref $query eq "HASH"C= $query->{"_name"}D= not ref $query->{"_name"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
403 # we can create one document
404 0 my $doc = {};
405 0 $self->_update_document($obj, $doc);
406  
407 # store the document in the fs
408 0 $self->_store_document($query->{_name}, $doc);
409  
410 # add info about this upsert to the $updated hash
411 0 $updated->{n} = 1;
412 0 $updated->{docs} = [$query->{_name}];
413 }
414  
415 0 return $updated;
416 }
417  
418 =head3 remove( [ $name, \%options ] )
419  
420 =head3 remove( [ \%query, \%options ] )
421  
422 Performs a query on the collection, and removes every document matched. If a query
423 is not provided (or if empty string or hash-ref), every document in the collection
424 is removed. If you pass an options hash-ref with the 'just_one' key holding a true
425 value, only one document will be removed (the first matched, if any).
426  
427 =cut
428  
429 0 ▶sub remove {
Subroutine remove: not called CC=12 SCAR=50.5
Pod documented
430 0 my ($self, $query, $options) = @_;
431  
432 0 ▶ croak "remove() expects a query string (can be empty) or hash-ref (can also be empty)."
Branch: 1 branch
BranchTrueFalseTotal
if defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp" 0 0 0
Condition0%0/9
defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp"
!ll&&!rl&&r
000
defined $query and ref $query and ref $query ne "HASH"
!ll&&!rl&&r
000
defined $query and ref $query
!ll&&!rl&&r
000
MC/DC0%0/4
defined $query and ref $query and ref $query ne "HASH" and ref $query ne "Regexp"
defined $query ref $query ref $query ne "HASH" ref $query ne "Regexp"
Truth table0%0/5
A and B and C and D
A= defined $queryB= ref $queryC= ref $query ne "HASH"D= ref $query ne "Regexp"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
433 if defined $query && ref $query && ref $query ne 'HASH' && ref $query ne 'Regexp';
434 0 ▶ croak "remove() expects a hash-ref of options."
Branch: 1 branch
BranchTrueFalseTotal
if $options and ref $options ne "HASH" 0 0 0
Condition0%0/3
$options and ref $options ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$options and ref $options ne "HASH"
$options ref $options ne "HASH"
Truth table0%0/3
A and B
A= $optionsB= ref $options ne "HASH"
ABresult
0X0
100
111
435 if $options && ref $options ne 'HASH';
436  
437 0 ▶ $query ||= '';
Condition0%0/2
$query ||= ""
l!l
00
MC/DC0%0/1
$query ||= ""
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
438  
439 0 my $cursor = $self->find($query, $options);
440  
441 0 my $deleted = { docs => [], n => 0 };
442  
443 # assuming query was a name search and not an attribute search,
444 # i don't want to unnecessarily load all document just so i could
445 # delete them, so I'm gonna just iterate through the cursor's
446 # _documents array:
447 0 ▶ foreach ($options->{just_one} && $cursor->count ? ($cursor->_documents->Keys(0)) : $cursor->count ? $cursor->_documents->Keys : ()) {
Branch: 2 branches
BranchTrueFalseTotal
$options->{'just_one'} && $cursor->count ? : 0 0 0
$cursor->count ? : 0 0 0
Condition0%0/3
$options->{"just_one"} and $cursor->count
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"just_one"} and $cursor->count
$options->{"just_one"} $cursor->count
Truth table0%0/3
A and B
A= $options->{"just_one"}B= $cursor->count
ABresult
0X0
100
111
448 # remove the document
449 0 $self->db->_repo->run('rm', '-r', '-f', $_); # the -r switch is here in case this is a document directory
450  
451 # add some info about this deletion
452 0 $deleted->{n} += 1;
453 0 push(@{$deleted->{docs}}, $_);
454 }
455  
456 0 return $deleted;
457 }
458  
459 =head2 DOCUMENTS ITERATION
460  
461 =head3 count()
462  
463 Returns the number of documents in the collection.
464  
465 =cut
466  
467 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
468 0 shift->_documents->Length;
469 }
470  
471 =head3 sort( [ $order ] )
472  
473 Sorts the collection's documents. If C<$order> isn't provided, documents
474 will be sorted alphabetically by name (documents are already sorted by
475 name by default, so this is only useful for re-sorting).
476  
477 C<$order> can be any of the following:
478  
479 =over
480  
481 =item * An ordered L<Tie::IxHash> object.
482  
483 =item * An even-numbered array-ref (such as C<< [ 'attr1' => 1, 'attr2' => -1 ] >>).
484  
485 =back
486  
487 Of course we are sorting by attributes, so you can still use the '_name'
488 attribute in C<$order>. When you give an attribute a positive true value,
489 it will be sorted ascendingly. When you give a negative value, it will
490 be sorted descendingly. So, for example:
491  
492 $coll->sort([ 'birth_date' => -1, '_name' => 1 ])
493  
494 Will sort the documents in the collection descendingly by the 'birth_date'
495 attribute, and then ascendingly by the document's name.
496  
497 Documents that miss any attributes from the C<$order> object always lean
498 towards the end. If, for example, C<$order> is C<< [ date => 1 ] >>, then
499 the documents will be sorted ascendingly by the 'date' attribute, and all
500 documents that don't have the 'date' attribute will propagate to the end.
501  
502 =cut
503  
504 0 ▶sub sort {
Subroutine sort: not called CC=38 SCAR=73.0
Pod documented
505 0 my ($self, $order) = @_;
506  
507 0 ▶ croak "sort() expects a Tie::IxHash object or an even-numbered array-ref."
Branch: 1 branch
BranchTrueFalseTotal
if $order and !ref($order) || ref $order eq "ARRAY" && scalar @$order % 2 != 0 || builtin::blessed($order) && !$order->isa("Tie::IxHash") 0 0 0
Condition0%0/15
$order and !ref($order) || ref $order eq "ARRAY" && scalar @$order % 2 != 0 || builtin::blessed($order) && !$order->isa("Tie::IxHash")
!ll&&!rl&&r
000
not ref $order or ref $order eq "ARRAY" and scalar @$order % 2 != 0 or builtin::blessed($order) and not $order->isa("Tie::IxHash")
l!l&&r!l&&!r
000
not ref $order or ref $order eq "ARRAY" and scalar @$order % 2 != 0
l!l&&r!l&&!r
000
ref $order eq "ARRAY" and scalar @$order % 2 != 0
!ll&&!rl&&r
000
builtin::blessed($order) and not $order->isa("Tie::IxHash")
!ll&&!rl&&r
000
MC/DC0%0/6
$order and !ref($order) || ref $order eq "ARRAY" && scalar @$order % 2 != 0 || builtin::blessed($order) && !$order->isa("Tie::IxHash")
$order not ref $order ref $order eq "ARRAY" scalar @$order % 2 != 0 builtin::blessed($order) not $order->isa("Tie::IxHash")
Truth table0%0/9
A and not(B) or C and D or E and not(F)
A= $orderB= not ref $orderC= ref $order eq "ARRAY"D= scalar @$order % 2 != 0E= builtin::blessed($order)F= not $order->isa("Tie::IxHash")
ABCDEFresult
0XXXXX0
100X0X0
100X100
10100X0
1010100
11XXXX1
1011XX1
100X111
1010111
508 if $order && (
509 !ref $order ||
510 (ref $order eq 'ARRAY' && scalar @$order % 2 != 0) ||
511 (blessed $order && !$order->isa('Tie::IxHash'))
512 );
513  
514 # no need to do anything if we have no documents (or only have 1)
515 0 ▶ return 1 unless $self->count > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count > 1 0 0 0
516  
517 0 ▶ $order ||= [ '_name' => 1 ];
Condition0%0/2
$order ||= ["_name", 1]
l!l
00
MC/DC0%0/1
$order ||= ["_name", 1]
$order
Truth table0%0/2
A
A= $order
Aresult
11
00
518  
519 0 ▶ $order = Tie::IxHash->new(@$order)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($order) and $order->isa("Tie::IxHash") 0 0 0
Condition0%0/3
builtin::blessed($order) and $order->isa("Tie::IxHash")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($order) and $order->isa("Tie::IxHash")
builtin::blessed($order) $order->isa("Tie::IxHash")
Truth table0%0/3
A and B
A= builtin::blessed($order)B= $order->isa("Tie::IxHash")
ABresult
0X0
100
111
520 unless blessed $order && $order->isa('Tie::IxHash');
521  
522 # if $order doesn't have sorting by _name, add it explicitly to
523 # the end of it as a convention
524 0 ▶ $order->Push('_name' => 1)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $order->Indices("_name") 0 0 0
525 unless defined $order->Indices('_name');
526  
527 0 ▶ if ($order->Length == 1 && $order->Keys(0) eq '_name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($order->Length == 1 and $order->Keys(0) eq '_name') { } 0 0 0
Condition0%0/3
$order->Length == 1 and $order->Keys(0) eq "_name"
!ll&&!rl&&r
000
MC/DC0%0/2
$order->Length == 1 and $order->Keys(0) eq "_name"
$order->Length == 1 $order->Keys(0) eq "_name"
Truth table0%0/3
A and B
A= $order->Length == 1B= $order->Keys(0) eq "_name"
ABresult
0X0
100
111
528 # if we're only sorting by name, there's no need to load
529 # the documents, so we can just go ahead and sort
530 0 $self->_documents->OrderByKey;
531 0 ▶ $self->_documents->Reorder(reverse $self->_documents->Keys)
Branch: 1 branch
BranchTrueFalseTotal
if $order->FETCH("_name") < 0 0 0 0
532 if $order->FETCH('_name') < 0;
533 } else {
534 # we're gonna have to load the documents (if they're not
535 # already loaded).
536 $self->_documents->Reorder(sort {
537 # load the documents
538 0 my $doc_a = $self->_load_document($self->_documents->Indices($a));
539 0 my $doc_b = $self->_load_document($self->_documents->Indices($b));
540
541 # start comparing according to $order
542 0 foreach my $attr ($order->Keys) {
543 0 my $dir = $order->FETCH($attr);
544 0 ▶ if (defined $doc_a->{$attr} && !ref $doc_a->{$attr} && defined $doc_b->{$attr} && !ref $doc_b->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc_a->{$attr} and not ref $doc_a->{$attr} and defined $doc_b->{$attr} and not ref $doc_b->{$attr}) { } 0 0 0
Condition0%0/9
defined $doc_a->{$attr} and not ref $doc_a->{$attr} and defined $doc_b->{$attr} and not ref $doc_b->{$attr}
!ll&&!rl&&r
000
defined $doc_a->{$attr} and not ref $doc_a->{$attr} and defined $doc_b->{$attr}
!ll&&!rl&&r
000
defined $doc_a->{$attr} and not ref $doc_a->{$attr}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $doc_a->{$attr} and not ref $doc_a->{$attr} and defined $doc_b->{$attr} and not ref $doc_b->{$attr}
defined $doc_a->{$attr} not ref $doc_a->{$attr} defined $doc_b->{$attr} not ref $doc_b->{$attr}
Truth table0%0/5
A and not(B) and C and not(D)
A= defined $doc_a->{$attr}B= not ref $doc_a->{$attr}C= defined $doc_b->{$attr}D= not ref $doc_b->{$attr}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
545 # are we comparing numerically or alphabetically?
546 0 ▶ if ($doc_a->{$attr} =~ m/^\d+(\.\d+)?$/ && $doc_b->{$attr} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc_a->{$attr} =~ /^\d+(\.\d+)?$/ and $doc_b->{$attr} =~ /^\d+(\.\d+)?$/) { } 0 0 0
Condition0%0/3
$doc_a->{$attr} =~ /^\d+(\.\d+)?$/ and $doc_b->{$attr} =~ /^\d+(\.\d+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$doc_a->{$attr} =~ /^\d+(\.\d+)?$/ and $doc_b->{$attr} =~ /^\d+(\.\d+)?$/
$doc_a->{$attr} =~ /^\d+(\.\d+)?$/ $doc_b->{$attr} =~ /^\d+(\.\d+)?$/
Truth table0%0/3
A and B
A= $doc_a->{$attr} =~ /^\d+(\.\d+)?$/B= $doc_b->{$attr} =~ /^\d+(\.\d+)?$/
ABresult
0X0
100
111
547 # numerically
548 0 ▶ if ($dir > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir > 0) { } 0 0 0
elsif ($dir < 0) { } 0 0 0
549 # when $dir is positive, we want $a to be larger than $b
550 0 ▶ return 1 if $doc_a->{$attr} > $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} > $doc_b->{$attr} 0 0 0
551 0 ▶ return -1 if $doc_a->{$attr} < $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} < $doc_b->{$attr} 0 0 0
552 } elsif ($dir < 0) {
553 # when $dir is negative, we want $a to be smaller than $b
554 0 ▶ return -1 if $doc_a->{$attr} > $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} > $doc_b->{$attr} 0 0 0
555 0 ▶ return 1 if $doc_a->{$attr} < $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} < $doc_b->{$attr} 0 0 0
556 }
557 } else {
558 # alphabetically
559 0 ▶ if ($dir > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir > 0) { } 0 0 0
elsif ($dir < 0) { } 0 0 0
560 # when $dir is positive, we want $a to be larger than $b
561 0 ▶ return 1 if $doc_a->{$attr} gt $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} gt $doc_b->{$attr} 0 0 0
562 0 ▶ return -1 if $doc_a->{$attr} lt $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} lt $doc_b->{$attr} 0 0 0
563 } elsif ($dir < 0) {
564 # when $dir is negative, we want $a to be smaller than $b
565 0 ▶ return -1 if $doc_a->{$attr} gt $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} gt $doc_b->{$attr} 0 0 0
566 0 ▶ return 1 if $doc_a->{$attr} lt $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_a->{$attr} lt $doc_b->{$attr} 0 0 0
567 }
568 }
569 } else {
570 # documents cannot be compared for this attribute
571 # we want documents that have the attribute appear
572 # earlier in the collection, so let's find out if
573 # one of the documents has the attribute
574 0 ▶ return -1 if defined $doc_a->{$attr} && !defined $doc_b->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc_a->{$attr} and not defined $doc_b->{$attr} 0 0 0
Condition0%0/3
defined $doc_a->{$attr} and not defined $doc_b->{$attr}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc_a->{$attr} and not defined $doc_b->{$attr}
defined $doc_a->{$attr} not defined $doc_b->{$attr}
Truth table0%0/3
A and not(B)
A= defined $doc_a->{$attr}B= not defined $doc_b->{$attr}
ABresult
0X0
100
111
575 0 ▶ return 1 if defined $doc_b->{$attr} && !defined $doc_a->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc_b->{$attr} and not defined $doc_a->{$attr} 0 0 0
Condition0%0/3
defined $doc_b->{$attr} and not defined $doc_a->{$attr}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc_b->{$attr} and not defined $doc_a->{$attr}
defined $doc_b->{$attr} not defined $doc_a->{$attr}
Truth table0%0/3
A and not(B)
A= defined $doc_b->{$attr}B= not defined $doc_a->{$attr}
ABresult
0X0
100
111
576
577 # if we're here, either both documents have the
578 # attribute but it's non comparable (since it's a
579 # reference) or both documents don't have that
580 # attribute at all. in both cases, we consider them
581 # to be equal when comparing these attributes,
582 # so we don't return anything and just continue to
583 # the next attribute to sort according to (if any)
584 }
585 }
586  
587 # if we've reached this point, the documents compare entirely
588 # so we need to return zero
589 0 return 0;
590 } $self->_documents->Keys);
591 }
592  
593 0 $self->rewind;
594  
595 0 return $self;
596 }
597  
598 =head3 all()
599  
600 Returns an array of all the documents in the collection (after loading).
601  
602 =cut
603  
604 0 ▶sub all {
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
605 0 my $self = shift;
606 0 my @results;
607 0 ▶ while ($self->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next) 0 0 0
608 0 push(@results, $self->next);
609 }
610 0 $self->rewind;
611 0 return @results;
612 }
613  
614 =head3 has_next()
615  
616 Returns a true value if the iterator hasn't reached the last of the documents
617 (and thus C<next()> can be called).
618  
619 =cut
620  
621 0 ▶sub has_next {
Subroutine has_next: not called CC=1 SCAR=6.9
Pod documented
622 0 $_[0]->_loc < $_[0]->count;
623 }
624  
625 =head3 next()
626  
627 Returns the document currently pointed to by the iterator, and increases the
628 iterator to point to the next document.
629  
630 =cut
631  
632 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
Pod documented
633 0 my $self = shift;
634  
635 0 ▶ return unless $self->has_next;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_next 0 0 0
636  
637 0 my $next = $self->_load_document($self->_loc);
638 0 $self->_inc_loc;
639 0 return $next;
640 }
641  
642 =head2 rewind()
643  
644 Resets to iterator to point to the first document.
645  
646 =cut
647  
648 0 ▶sub rewind {
Subroutine rewind: not called CC=1 SCAR=6.9
Pod documented
649 0 $_[0]->_set_loc(0);
650 }
651  
652 =head2 first()
653  
654 Returns the first document in the collection (or C<undef> if none exist),
655 regardless of the iterator's current position (which will not change).
656  
657 =cut
658  
659 0 ▶sub first {
Subroutine first: not called CC=2 SCAR=17.9
Pod documented
660 0 my $self = shift;
661  
662 0 ▶ return unless $self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 0 0 0
663  
664 0 return $self->_load_document(0);
665 }
666  
667 =head2 last()
668  
669 Returns the last document in the collection (or C<undef> if none exist),
670 regardless of the iterator's current position (which will not change).
671  
672 =cut
673  
674 0 ▶sub last {
Subroutine last: not called CC=2 SCAR=17.9
Pod documented
675 0 my $self = shift;
676  
677 0 ▶ return unless $self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 0 0 0
678  
679 0 return $self->_load_document($self->count - 1);
680 }
681  
682 =head2 COLLECTION OPERATIONS
683  
684 =head3 get_parent()
685  
686 Returns a L<Giddy::Collection> object tied to the parent collection of the collection.
687 If this method is called on the root collection, C<undef> will be returned.
688  
689 =cut
690  
691 0 ▶sub get_parent {
Subroutine get_parent: not called CC=2 SCAR=17.9
Pod documented
692 0 my $self = shift;
693  
694 0 ▶ return unless $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->path 0 0 0
695  
696 0 return $self->db->get_collection($self->db->_up($self->path));
697 }
698  
699 =head3 get_collection( $name )
700  
701 Returns a L<Giddy::Collection> object tied to a child-collection named C<$name>.
702 If the collection does not exist, it will be created. If C<$name> exists in the
703 collection, but isn't a child collection, this method will croak. C<$name> must
704 not start with a slash.
705  
706 =cut
707  
708 0 ▶sub get_collection {
Subroutine get_collection: not called CC=2 SCAR=17.9
Pod documented
709 0 my ($self, $name) = @_;
710  
711 0 ▶ croak "You must provide the name of the child-collection to get."
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
712 unless $name;
713  
714 0 return $self->db->get_collection($self->_path_to($name));
715 }
716  
717 =head3 list_static_dirs()
718  
719 Returns a list of all the static-file directories in the collection (if any).
720  
721 =cut
722  
723 0 ▶sub list_static_dirs {
Subroutine list_static_dirs: not called CC=1 SCAR=6.9
Pod documented
724 0 my $self = shift;
725  
726 0 return map { $self->db->_is_static_dir($self->_path_to($_)) } $self->db->_list_dirs($self->path);
727 }
728  
729 =head3 get_static_dir( $name )
730  
731 Returns a L<Giddy::StaticDirectory> object for a directory of static files named
732 C<$name>, residing in the collection's directory. If the directory does not exist,
733 it will be created and marked as a static directory with an empty '.static' file.
734 If the directory exists but is not a static directory (or a file named C<$name>
735 exists), this method will croak.
736  
737 =cut
738  
739 0 ▶sub get_static_dir {
Subroutine get_static_dir: not called CC=4 SCAR=30.0
Pod documented
740 0 my ($self, $path) = @_;
741  
742 0 ▶ croak "You must provide the name of the static directory to load."
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
743 unless $path;
744  
745 0 my $fpath = $self->_path_to($path);
746  
747 # try to find such a directory
748 0 ▶ if ($self->db->_path_exists($fpath)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db->_path_exists($fpath)) { } 0 0 0
749 0 ▶ croak "Path $fpath already exists but isn't a static-file directory."
Branch: 1 branch
BranchTrueFalseTotal
unless $self->db->_is_static_dir($fpath) 0 0 0
750 unless $self->db->_is_static_dir($fpath);
751 } else {
752 # okay, let's create the directory
753 0 $self->db->_create_dir($fpath);
754 0 $self->db->_mark_dir_as_static($fpath);
755 0 $self->db->stage($fpath);
756 }
757  
758 0 return Giddy::StaticDirectory->new(path => $fpath, coll => $self);
759 }
760  
761 =head3 drop()
762  
763 Removes the collection from the database. Will not work (and croak) on
764 the root collection. Every document and sub-collection in the collection will
765 be removed. This method is not available on L<Giddy::Collection::InMemory> objects.
766  
767 =cut
768  
769 0 ▶sub drop {
Subroutine drop: not called CC=2 SCAR=17.9
Pod documented
770 0 my $self = shift;
771  
772 0 ▶ croak "You cannot drop the root collection."
Branch: 1 branch
BranchTrueFalseTotal
if $self->path eq "" 0 0 0
773 if $self->path eq '';
774  
775 0 $self->db->_repo->run('rm', '-r', '-f', $self->path);
776 }
777  
778 =head1 INTERNAL METHODS
779  
780 The following methods are only to be used internally.
781  
782 =head2 _documents()
783  
784 Returns a sorted Tie::IxHash object of all documents in the collection.
785  
786 =cut
787  
788 0 ▶sub _documents {
Subroutine _documents: not called CC=5 SCAR=34.0
789 0 my $self = shift;
790  
791 0 my $docs = Tie::IxHash->new;
792 0 foreach ($self->db->_list_contents($self->path)) {
793 0 my $full_path = $self->_path_to($_);
794  
795 # we're only looking for document directories and document files
796 0 ▶ if ($self->db->_is_directory($full_path) && $self->db->_is_document_dir($full_path)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->db->_is_directory($full_path) and $self->db->_is_document_dir($full_path)) { } 0 0 0
elsif ($self->db->_is_file($full_path)) { } 0 0 0
Condition0%0/3
$self->db->_is_directory($full_path) and $self->db->_is_document_dir($full_path)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->db->_is_directory($full_path) and $self->db->_is_document_dir($full_path)
$self->db->_is_directory($full_path) $self->db->_is_document_dir($full_path)
Truth table0%0/3
A and B
A= $self->db->_is_directory($full_path)B= $self->db->_is_document_dir($full_path)
ABresult
0X0
100
111
797 0 $docs->STORE($_ => 'dir');
798 } elsif ($self->db->_is_file($full_path)) {
799 0 $docs->STORE($_ => 'file');
800 }
801 }
802  
803 0 return $docs;
804 }
805  
806 =head2 _inc_loc()
807  
808 =cut
809  
810 0 ▶sub _inc_loc {
Subroutine _inc_loc: not called CC=1 SCAR=6.9
811 0 my $self = shift;
812  
813 0 $self->_set_loc($self->_loc + 1);
814 }
815  
816 =head2 _load_document( $index )
817  
818 =cut
819  
820 0 ▶sub _load_document {
Subroutine _load_document: not called CC=7 SCAR=40.3
821 0 my ($self, $index) = @_;
822  
823 0 ▶ return unless $index >= 0 && $index < $self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $index >= 0 and $index < $self->count 0 0 0
Condition0%0/3
$index >= 0 and $index < $self->count
!ll&&!rl&&r
000
MC/DC0%0/2
$index >= 0 and $index < $self->count
$index >= 0 $index < $self->count
Truth table0%0/3
A and B
A= $index >= 0B= $index < $self->count
ABresult
0X0
100
111
824  
825 0 my $name = $self->_documents->Keys($index);
826 0 ▶ if (exists $self->_loaded->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_loaded->{$name}) { } 0 0 0
827 0 return $self->_loaded->{$name};
828 } else {
829 0 my $t = $self->_documents->FETCH($name);
830 0 my $doc;
831 0 ▶ if ($t eq 'file') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'file') { } 0 0 0
elsif ($t eq 'dir') { } 0 0 0
832 0 $doc = $self->_query->{coll}->_load_document_file($name);
833 } elsif ($t eq 'dir') {
834 0 $doc = $self->_query->{coll}->_load_document_dir($name, $self->_query->{opts}->{skip_binary});
835 }
836 0 ▶ croak "Failed to load document $name." unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
837 0 $self->_loaded->{$name} = $doc;
838 0 return $doc;
839 }
840 }
841  
842 =head2 _path_to( @names )
843  
844 Returns an internal path created by joining the collection's path and
845 everything in C<@names> with '/' as a separator. If the collection is the root
846 collection (and thus has the empty path) than this method will behave correctly
847 and not return a string that starts with a slash.
848  
849 =cut
850  
851 0 ▶sub _path_to {
Subroutine _path_to: not called CC=2 SCAR=17.9
852 0 my ($self, @names) = @_;
853  
854 0 ▶ unshift(@names, $self->path) if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 0 0 0
855 0 return join('/', @names);
856 }
857  
858 =head2 _build_cached()
859  
860 =cut
861  
862 0 ▶sub _build_cached {
Subroutine _build_cached: not called CC=2 SCAR=17.9
863 0 my $self = shift;
864  
865 0 ▶ return $self->db->_path_exists($self->path) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->db->_path_exists($self->path) ? : 0 0 0
866 }
867  
868 =head1 AUTHOR
869  
870 Ido Perlmuter, C<< <ido at ido50.net> >>
871  
872 =head1 BUGS
873  
874 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
875 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
876 automatically be notified of progress on your bug as I make changes.
877  
878 =head1 SUPPORT
879  
880 You can find documentation for this module with the perldoc command.
881  
882 perldoc Giddy::Collection
883  
884 You can also look for information at:
885  
886 =over 4
887  
888 =item * RT: CPAN's request tracker
889  
890 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
891  
892 =item * AnnoCPAN: Annotated CPAN documentation
893  
894 L<http://annocpan.org/dist/Giddy>
895  
896 =item * CPAN Ratings
897  
898 L<http://cpanratings.perl.org/d/Giddy>
899  
900 =item * Search CPAN
901  
902 L<http://search.cpan.org/dist/Giddy/>
903  
904 =back
905  
906 =head1 LICENSE AND COPYRIGHT
907  
908 Copyright 2011 Ido Perlmuter.
909  
910 This program is free software; you can redistribute it and/or modify it
911 under the terms of either: the GNU General Public License as published
912 by the Free Software Foundation; or the Artistic License.
913  
914 See http://dev.perl.org/licenses/ for more information.
915  
916 =cut
917  
918 2 __PACKAGE__->meta->make_immutable;
« Giddy/Role/DocumentUpdater.pm ↑ Summary Giddy/Database.pm »