| 1 | package Catmandu::Store::Multi::Bag; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '1.2025'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Hash::Merge::Simple qw(merge); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | with 'Catmandu::Bag'; | |||||||||||||||||||||||||||
| 12 | 2 | with 'Catmandu::Droppable'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub generator { | ||||||||||||||||||||||||||
|
Subroutine
generator: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Loop of all stores and find the first one that implements the bag | ||||||||||||||||||||||||||||
| 18 | # and can create a generator | ||||||||||||||||||||||||||||
| 19 | 4 | my $gen; | |||||||||||||||||||||||||||
| 20 | 4 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 21 | 4 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 22 | 4 | ▶ | $gen = $bag ? $bag->generator : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | last if defined($gen); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | return undef unless $gen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 6 | my $item = $gen->(); | |||||||||||||||||||||||||||
| 30 | 6 | ▶ | return undef unless $item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 3 | return $item; | |||||||||||||||||||||||||||
| 32 | 4 | }; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Loop over all the bags and merge the results of the records found | ||||||||||||||||||||||||||||
| 39 | # Required in case of Store/FileStore combinations where each part | ||||||||||||||||||||||||||||
| 40 | # can contain different metadata | ||||||||||||||||||||||||||||
| 41 | 1 | my $found = 0; | |||||||||||||||||||||||||||
| 42 | 1 | my $result = {}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 45 | 2 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | my $item = $bag ? $bag->get($id) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | if ($item) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | $found = 1; | |||||||||||||||||||||||||||
| 49 | 2 | $result = merge $result, $item; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | return $found ? $result : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 4 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # By default try to add the data to all the stores | ||||||||||||||||||||||||||||
| 60 | 4 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 61 | 8 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 62 | 8 | ▶ | $bag->add($data) if $bag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | 1; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # By default try to delete the data from all the stores | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 74 | 2 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | $bag->delete($id) if $bag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | sub delete_all { | ||||||||||||||||||||||||||
|
Subroutine
delete_all: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # By default try to drop the data from all the stores | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 87 | 2 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 88 | 2 | ▶ | $bag->delete_all if $bag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub drop { | ||||||||||||||||||||||||||
|
Subroutine
drop: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # By default try to delete the data from all the stores | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | for my $store (@{$self->store->stores}) { | |||||||||||||||||||||||||||
| 100 | 2 | my $bag = $store->bag($self->name); | |||||||||||||||||||||||||||
| 101 | 2 | ▶ | $bag->drop if $bag && $bag->does('Catmandu::Droppable'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$bag and $bag->does("Catmandu::Droppable")
MC/DC0%0/2
$bag and $bag->does("Catmandu::Droppable")
$bag
$bag->does("Catmandu::Droppable")
Truth table33%1/3
A and B
A= $bagB= $bag->does("Catmandu::Droppable")
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1 | 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub commit { | ||||||||||||||||||||||||||||
| 108 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # By default try to commit the data to all the stores | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | for my $store (@{$self->store->stores}) { | ||||||||||||||||||||||||||||
| 113 | my $bag = $store->bag($self->name); | ||||||||||||||||||||||||||||
| 114 | $bag->commit if $bag; | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1; | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||