line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::Hash; |
2
|
|
|
|
|
|
|
|
3
|
16
|
|
|
16
|
|
628577
|
use Catmandu::Sane; |
|
16
|
|
|
|
|
78
|
|
|
16
|
|
|
|
|
159
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
16
|
|
|
16
|
|
131
|
use Moo; |
|
16
|
|
|
|
|
37
|
|
|
16
|
|
|
|
|
129
|
|
8
|
16
|
|
|
16
|
|
6862
|
use Catmandu::Util qw(:is); |
|
16
|
|
|
|
|
47
|
|
|
16
|
|
|
|
|
5078
|
|
9
|
16
|
|
|
16
|
|
7423
|
use Catmandu::Store::Hash::Bag; |
|
16
|
|
|
|
|
58
|
|
|
16
|
|
|
|
|
623
|
|
10
|
16
|
|
|
16
|
|
116
|
use namespace::clean; |
|
16
|
|
|
|
|
37
|
|
|
16
|
|
|
|
|
81
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Store'; |
13
|
|
|
|
|
|
|
with 'Catmandu::Droppable'; |
14
|
|
|
|
|
|
|
with 'Catmandu::Transactional'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has _hashes => |
17
|
|
|
|
|
|
|
(is => 'ro', lazy => 1, init_arg => undef, default => sub {+{}}); |
18
|
|
|
|
|
|
|
has init_data => (is => 'ro'); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub BUILD { |
21
|
30
|
|
|
30
|
0
|
284
|
my ($self) = @_; |
22
|
30
|
100
|
|
|
|
306
|
if (my $data = $self->init_data) { |
23
|
6
|
|
|
|
|
31
|
$self->bag->add($_) for @$data; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub drop { |
28
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
29
|
0
|
|
|
|
|
0
|
$_->drop for values %{$self->bags}; |
|
0
|
|
|
|
|
0
|
|
30
|
0
|
|
|
|
|
0
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub transaction { |
34
|
2
|
|
|
2
|
0
|
7109
|
my ($self, $coderef) = @_; |
35
|
2
|
50
|
|
|
|
11
|
&{$coderef} if is_code_ref($coderef); |
|
2
|
|
|
|
|
7
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
1; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
__END__ |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=pod |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 NAME |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Catmandu::Store::Hash - An in-memory store |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 SYNOPSIS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use Catmandu; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $store = Catmandu->store('Hash'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $obj1 = $store->bag->add({ name => 'Patrick' }); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
printf "obj1 stored as %s\n" , $obj1->{_id}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Force an id in the store |
59
|
|
|
|
|
|
|
my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' }); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $obj3 = $store->bag->get('test123'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$store->bag->delete('test123'); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
$store->bag->delete_all; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# All bags are iterators |
68
|
|
|
|
|
|
|
$store->bag->each(sub { ... }); |
69
|
|
|
|
|
|
|
$store->bag->take(10)->each(sub { ... }); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
A Catmandu::Store::Hash is an in-memory L<Catmandu::Store> backed by a hash |
74
|
|
|
|
|
|
|
for fast retrieval combined with a doubly linked list for fast traversal. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 METHODS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head2 new([init_data => [...] ]) |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Create a new Catmandu::Store::Hash. Optionally provide as init_data an array |
81
|
|
|
|
|
|
|
ref of data: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $store = Catmandu->store('Hash', init_data => [ |
84
|
|
|
|
|
|
|
{ _id => 1, data => foo } , |
85
|
|
|
|
|
|
|
{ _id => 2, data => bar } |
86
|
|
|
|
|
|
|
]); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# or in a catmandu.yml configuration file: |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
--- |
91
|
|
|
|
|
|
|
store: |
92
|
|
|
|
|
|
|
hash: |
93
|
|
|
|
|
|
|
package: Hash |
94
|
|
|
|
|
|
|
options: |
95
|
|
|
|
|
|
|
init_data: |
96
|
|
|
|
|
|
|
- _id: 1 |
97
|
|
|
|
|
|
|
data: foo |
98
|
|
|
|
|
|
|
- _id: 2 |
99
|
|
|
|
|
|
|
data: bar |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 INHERITED METHODS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This Catmandu::Store implements: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=over 3 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item L<Catmandu::Store> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item L<Catmandu::Transactional> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Each Catmandu::Bag in this Catmandu::Store implements: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=over 3 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item L<Catmandu::Bag> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=back |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |