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