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