line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::File::Memory::Bag; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.16'; |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
21
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
21
|
|
6
|
3
|
|
|
3
|
|
540
|
use Moo; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
21
|
|
7
|
3
|
|
|
3
|
|
1083
|
use Clone 'clone'; |
|
3
|
|
|
|
|
48
|
|
|
3
|
|
|
|
|
215
|
|
8
|
3
|
|
|
3
|
|
18
|
use Catmandu::Util qw(content_type); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
139
|
|
9
|
3
|
|
|
3
|
|
55
|
use namespace::clean; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
17
|
|
10
|
|
|
|
|
|
|
require bytes; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Bag'; |
13
|
|
|
|
|
|
|
with 'Catmandu::FileBag'; |
14
|
|
|
|
|
|
|
with 'Catmandu::Droppable'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub generator { |
17
|
4
|
|
|
4
|
0
|
1606
|
my ($self) = @_; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
|
|
13
|
my $name = $self->name; |
20
|
4
|
|
50
|
|
|
91
|
my $files = $self->store->_files->{$name} // {}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub { |
23
|
11
|
|
|
11
|
|
101
|
state $ids = [keys %$files]; |
24
|
|
|
|
|
|
|
|
25
|
11
|
|
|
|
|
20
|
my $id = pop @$ids; |
26
|
|
|
|
|
|
|
|
27
|
11
|
100
|
|
|
|
31
|
return undef unless $id; |
28
|
|
|
|
|
|
|
|
29
|
7
|
|
|
|
|
131
|
return $self->get($id); |
30
|
4
|
|
|
|
|
51
|
}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub exists { |
34
|
3
|
|
|
3
|
0
|
1771
|
my ($self, $id) = @_; |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
|
|
11
|
my $name = $self->name; |
37
|
3
|
|
50
|
|
|
76
|
my $files = $self->store->_files->{$name} // {}; |
38
|
|
|
|
|
|
|
|
39
|
3
|
|
|
|
|
39
|
exists $files->{$id}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub get { |
43
|
|
|
|
|
|
|
my ($self, $id) = @_; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $name = $self->name; |
46
|
|
|
|
|
|
|
my $files = $self->store->_files->{$name} // {}; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$files->{$id}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub add { |
52
|
|
|
|
|
|
|
my ($self, $data) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $id = $data->{_id}; |
55
|
|
|
|
|
|
|
my $io = $data->{_stream}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
delete $data->{_stream}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $name = $self->name; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $str = Catmandu::Util::read_io($io); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$data->{_id} = $id; |
64
|
|
|
|
|
|
|
$data->{size} = bytes::length($str); |
65
|
|
|
|
|
|
|
$data->{md5} = ''; |
66
|
|
|
|
|
|
|
$data->{content_type} = content_type($id); |
67
|
|
|
|
|
|
|
$data->{created} = time; |
68
|
|
|
|
|
|
|
$data->{modified} = time; |
69
|
|
|
|
|
|
|
$data->{_stream} = sub { |
70
|
|
|
|
|
|
|
my $io = $_[0]; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Catmandu::Error->throw("no io defined or not writable") |
73
|
|
|
|
|
|
|
unless defined($io); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$io->write($str); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
bytes::length($str); |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$self->store->_files->{$name}->{$id} = clone($data); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub delete { |
86
|
|
|
|
|
|
|
my ($self, $id) = @_; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $name = $self->name; |
89
|
|
|
|
|
|
|
my $files = $self->store->_files->{$name} // {}; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
delete $files->{$id}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub delete_all { |
95
|
|
|
|
|
|
|
my ($self) = @_; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$self->each( |
98
|
|
|
|
|
|
|
sub { |
99
|
|
|
|
|
|
|
my $key = shift->{_id}; |
100
|
|
|
|
|
|
|
$self->delete($key); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub drop { |
108
|
0
|
|
|
0
|
0
|
|
$_[0]->delete_all; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub commit { |
112
|
|
|
|
|
|
|
return 1; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=pod |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 NAME |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Catmandu::Store::File::Memory::Bag - Index of all "files" in a Catmandu::Store::File::Memory "folder" |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 SYNOPSIS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
use Catmandu; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $store = Catmandu->store('File::Memory'); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $index = $store->index; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# List all containers |
134
|
|
|
|
|
|
|
$index->each(sub { |
135
|
|
|
|
|
|
|
my $container = shift; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
print "%s\n" , $container->{_id}; |
138
|
|
|
|
|
|
|
}); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Add a new folder |
141
|
|
|
|
|
|
|
$index->add({_id => '1234'}); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Delete a folder |
144
|
|
|
|
|
|
|
$index->delete(1234); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# Get a folder |
147
|
|
|
|
|
|
|
my $folder = $index->get(1234); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Get the files in an folder |
150
|
|
|
|
|
|
|
my $files = $index->files(1234); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$files->each(sub { |
153
|
|
|
|
|
|
|
my $file = shift; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my $name = $file->{_id}; |
156
|
|
|
|
|
|
|
my $size = $file->{size}; |
157
|
|
|
|
|
|
|
my $content_type = $file->{content_type}; |
158
|
|
|
|
|
|
|
my $created = $file->{created}; |
159
|
|
|
|
|
|
|
my $modified = $file->{modified}; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$file->stream(IO::File->new(">/tmp/$name"), file); |
162
|
|
|
|
|
|
|
}); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Add a file |
165
|
|
|
|
|
|
|
$files->upload(IO::File->new("<data.dat"),"data.dat"); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Retrieve a file |
168
|
|
|
|
|
|
|
my $file = $files->get("data.dat"); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# Stream a file to an IO::Handle |
171
|
|
|
|
|
|
|
$files->stream(IO::File->new(">data.dat"),$file); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Delete a file |
174
|
|
|
|
|
|
|
$files->delete("data.dat"); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Delete a folders |
177
|
|
|
|
|
|
|
$index->delete("1234"); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 DESCRIPTION |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
A L<Catmandu::Store::File::Memory::Bag> contains all "files" available in a |
182
|
|
|
|
|
|
|
L<Catmandu::Store::File::Memory> FileStore "folder". All methods of L<Catmandu::Bag>, |
183
|
|
|
|
|
|
|
L<Catmandu::FileBag::Index> and L<Catmandu::Droppable> are |
184
|
|
|
|
|
|
|
implemented. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Every L<Catmandu::Bag> is also an L<Catmandu::Iterable>. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 FOLDERS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
All files in a L<Catmandu::Store::File::Memory> are organized in "folders". To add |
191
|
|
|
|
|
|
|
a "folder" a new record needs to be added to the L<Catmandu::Store::File::Memory::Index> : |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
$index->add({_id => '1234'}); |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
The C<_id> field is the only metadata available in Memory stores. To add more |
196
|
|
|
|
|
|
|
metadata fields to a Memory store a L<Catmandu::Plugin::SideCar> is required. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 FILES |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Files can be accessed via the "folder" identifier: |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
my $files = $index->files('1234'); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Use the C<upload> method to add new files to a "folder". Use the C<download> method |
205
|
|
|
|
|
|
|
to retrieve files from a "folder". |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
$files->upload(IO::File->new("</tmp/data.txt"),'data.txt'); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
my $file = $files->get('data.txt'); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
$files->download(IO::File->new(">/tmp/data.txt"),$file); |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 INHERITED METHODS |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This Catmandu::Bag implements: |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=over 3 |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item L<Catmandu::Bag> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item L<Catmandu::FileBag> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=back |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |