line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Store::File::Simple; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.14'; |
4
|
|
|
|
|
|
|
|
5
|
8
|
|
|
8
|
|
484601
|
use Catmandu::Sane; |
|
8
|
|
|
|
|
1072116
|
|
|
8
|
|
|
|
|
67
|
|
6
|
8
|
|
|
8
|
|
2523
|
use Moo; |
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
45
|
|
7
|
8
|
|
|
8
|
|
2955
|
use Carp; |
|
8
|
|
|
|
|
33
|
|
|
8
|
|
|
|
|
500
|
|
8
|
8
|
|
|
8
|
|
3474
|
use Catmandu; |
|
8
|
|
|
|
|
656809
|
|
|
8
|
|
|
|
|
57
|
|
9
|
8
|
|
|
8
|
|
1959
|
use Catmandu::Util; |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
325
|
|
10
|
8
|
|
|
8
|
|
4109
|
use Catmandu::Store::File::Simple::Index; |
|
8
|
|
|
|
|
29
|
|
|
8
|
|
|
|
|
313
|
|
11
|
8
|
|
|
8
|
|
3620
|
use Catmandu::Store::File::Simple::Bag; |
|
8
|
|
|
|
|
29
|
|
|
8
|
|
|
|
|
280
|
|
12
|
8
|
|
|
8
|
|
3905
|
use Catmandu::DirectoryIndex::UUID; |
|
8
|
|
|
|
|
36
|
|
|
8
|
|
|
|
|
377
|
|
13
|
8
|
|
|
8
|
|
4063
|
use Catmandu::DirectoryIndex::Number; |
|
8
|
|
|
|
|
27
|
|
|
8
|
|
|
|
|
323
|
|
14
|
8
|
|
|
8
|
|
60
|
use namespace::clean; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
38
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
with 'Catmandu::FileStore'; |
17
|
|
|
|
|
|
|
with 'Catmandu::Droppable'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has root => (is => 'ro', required => '1'); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#DEPRECATED |
22
|
|
|
|
|
|
|
has uuid => (is => 'ro'); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#DEPRECATED |
25
|
|
|
|
|
|
|
has keysize => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
isa => sub { |
28
|
|
|
|
|
|
|
Catmandu::Util::check_natural($_[0]); |
29
|
|
|
|
|
|
|
croak "keysize needs to be a multiple of 3" unless $_[0] % 3 == 0; |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
default => 9 |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
has directory_index_package => (is => "ro"); |
34
|
|
|
|
|
|
|
has directory_index_options => (is => "ro", lazy => 1, default => sub {+{};}); |
35
|
|
|
|
|
|
|
has directory_index => (is => "lazy"); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _build_directory_index { |
38
|
|
|
|
|
|
|
|
39
|
17
|
|
|
17
|
|
961
|
my $self = $_[0]; |
40
|
|
|
|
|
|
|
|
41
|
17
|
100
|
|
|
|
115
|
if ($self->directory_index_package()) { |
|
|
100
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Catmandu::Util::require_package($self->directory_index_package(), |
44
|
|
|
|
|
|
|
"Catmandu::DirectoryIndex") |
45
|
1
|
|
|
|
|
6
|
->new(%{$self->directory_index_options(),}, base_dir => $self->root()); |
|
1
|
|
|
|
|
50
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
elsif ($self->uuid()) { |
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
13
|
Catmandu::DirectoryIndex::UUID->new(base_dir => $self->root()); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
|
|
|
|
|
|
|
55
|
15
|
|
|
|
|
252
|
Catmandu::DirectoryIndex::Number->new( |
56
|
|
|
|
|
|
|
base_dir => $self->root(), |
57
|
|
|
|
|
|
|
keysize => $self->keysize() |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub drop { |
65
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
$self->index->delete_all; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=pod |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Catmandu::Store::File::Simple - A Catmandu::FileStore to store files on disk |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SYNOPSIS |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# From the command line |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Export a list of all file containers |
85
|
|
|
|
|
|
|
$ catmandu export File::Simple --root t/data to YAML |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Export a list of all files in container '1234' |
88
|
|
|
|
|
|
|
$ catmandu export File::Simple --root t/data --bag 1234 to YAML |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Add a file to the container '1234' |
91
|
|
|
|
|
|
|
$ catmandu stream /tmp/myfile.txt to File::Simple --root t/data --bag 1234 --id myfile.txt |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Download the file 'myfile.txt' from the container '1234' |
94
|
|
|
|
|
|
|
$ catmandu stream File::Simple --root t/data --bag 1234 --id myfile.txt to /tmp/output.txt |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Delete the file 'myfile.txt' from the container '1234' |
97
|
|
|
|
|
|
|
$ catmandu delete File::Simple --root t/data --bag 1234 --id myfile.txt |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Define a configuration file |
100
|
|
|
|
|
|
|
$ cat catmandu.yml |
101
|
|
|
|
|
|
|
--- |
102
|
|
|
|
|
|
|
store: |
103
|
|
|
|
|
|
|
mypaths: |
104
|
|
|
|
|
|
|
package: DBI |
105
|
|
|
|
|
|
|
options: |
106
|
|
|
|
|
|
|
data_source: dbi:sqlite:dbname=data/index.db |
107
|
|
|
|
|
|
|
myfiles: |
108
|
|
|
|
|
|
|
package: File::Simple |
109
|
|
|
|
|
|
|
options: |
110
|
|
|
|
|
|
|
root: data/files |
111
|
|
|
|
|
|
|
directory_index_package: Map |
112
|
|
|
|
|
|
|
directory_index_options: |
113
|
|
|
|
|
|
|
store_name: mypaths |
114
|
|
|
|
|
|
|
bag_name: data |
115
|
|
|
|
|
|
|
... |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Use the default 'catmandu.yml' configuraion file to add data to the FileStore |
118
|
|
|
|
|
|
|
$ catmandu stream /tmp/myfile.txt to myfiles --bag 1234 --id myfile.txt |
119
|
|
|
|
|
|
|
$ catmandu stream myfiles --bag 1234 --id myfile.txt to /tmp/myfile.txt |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# From Perl |
122
|
|
|
|
|
|
|
use Catmandu; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $store = Catmandu->store('File::Simple' , root => 't/data'); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $index = $store->index; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# List all folder |
129
|
|
|
|
|
|
|
$index->bag->each(sub { |
130
|
|
|
|
|
|
|
my $container = shift; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
print "%s\n" , $container->{_id}; |
133
|
|
|
|
|
|
|
}); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# Add a new folder |
136
|
|
|
|
|
|
|
$index->add({ _id => '1234' }); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# Get the folder |
139
|
|
|
|
|
|
|
my $files = $index->files('1234'); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Add a file to the folder |
142
|
|
|
|
|
|
|
$files->upload(IO::File->new('<foobar.txt'), 'foobar.txt'); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Retrieve a file |
145
|
|
|
|
|
|
|
my $file = $files->get('foobar.txt'); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Stream the contents of a file |
148
|
|
|
|
|
|
|
$files->stream(IO::File->new('>foobar.txt'), $file); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Delete a file |
151
|
|
|
|
|
|
|
$files->delete('foobar.txt'); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Delete a folder |
154
|
|
|
|
|
|
|
$index->delete('1234'); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 DESCRIPTION |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L<Catmandu::Store::File::Simple> is a L<Catmandu::FileStore> implementation to |
159
|
|
|
|
|
|
|
store files in a directory structure. Each L<Catmandu::FileBag> is |
160
|
|
|
|
|
|
|
a deeply nested directory based on the numeric identifier of the bag. E.g. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$store->bag(1234) |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
is stored as |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
${ROOT}/000/001/234 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
In this directory all the L<Catmandu::FileBag> items are stored as |
169
|
|
|
|
|
|
|
flat files. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 METHODS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 new(root => $path , [ keysize => NUM , uuid => 1]) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Create a new Catmandu::Store::File::Simple with the following configuration |
176
|
|
|
|
|
|
|
parameters: |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=over |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item root |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The root directory where to store all the files. Required. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item keysize |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
DEPRECATED: use directory_index_package and directory_index_options |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
By default the directory structure is 3 levels deep. With the keysize option |
189
|
|
|
|
|
|
|
a deeper nesting can be created. The keysize needs to be a multiple of 3. |
190
|
|
|
|
|
|
|
All the container keys of a L<Catmandu::Store::File::Simple> must be integers. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item uuid |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
DEPRECATED: use directory_index_package and directory_index_options |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
If the to a true value, then the Simple store will require UUID-s as keys |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item directory_index_package |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
package name that translates between id and a directory. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
prefix "Catmandu::DirectoryIndex::" can be omitted. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Default: L<Catmandu::DirectoryIndex::Number> |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item directory_index_options |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Constructor arguments for the directory_index_package (see above) |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=item directory_index |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
instance of L<Catmandu::DirectoryIndex>. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
When supplied, directory_index_package and directory_index_options are ignored. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
When not, this object is constructed from directory_index_package and directory_index_options. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=back |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 INHERITED METHODS |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
This Catmandu::FileStore implements: |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=over 3 |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item L<Catmandu::FileStore> |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=back |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
The index Catmandu::Bag in this Catmandu::Store implements: |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=over 3 |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item L<Catmandu::Bag> |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item L<Catmandu::FileBag::Index> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=back |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
The file Catmandu::Bag in this Catmandu::Store implements: |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=over 3 |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=item L<Catmandu::Bag> |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item L<Catmandu::FileBag> |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=item L<Catmandu::Droppable> |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=back |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 SEE ALSO |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
L<Catmandu::Store::File::Simple::Index>, |
259
|
|
|
|
|
|
|
L<Catmandu::Store::File::Simple::Bag>, |
260
|
|
|
|
|
|
|
L<Catmandu::Plugin::SideCar>, |
261
|
|
|
|
|
|
|
L<Catmandu::FileStore> |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |