line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Hash::MongoDB; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
26336
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
84
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
375
|
use MongoDB::Connection; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use MongoDB::OID; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Tie::Hash::MongoDB - Tie a hash to a MongoDB document |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Every single action to the hash is directly processed |
14
|
|
|
|
|
|
|
on the MongoDB server, remember this while using this |
15
|
|
|
|
|
|
|
module! |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 VERSION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Version 0.01 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use Tie::Hash::MongoDB; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
tie %foo,'Tie::Hash::MongoDB',$id,{ |
30
|
|
|
|
|
|
|
server => 'localhost', |
31
|
|
|
|
|
|
|
database => 'default', |
32
|
|
|
|
|
|
|
collection => 'default', |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Tie a MongoDB document to a Perl hash. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
tie arguments: |
38
|
|
|
|
|
|
|
1. the hash to be tied |
39
|
|
|
|
|
|
|
2. this modules name |
40
|
|
|
|
|
|
|
3. the document id or undef for a new document |
41
|
|
|
|
|
|
|
4. connection arguments for the MongoDB interface |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 METHODS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 TIEHASH |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
tie %foo,'Tie::Hash::MongoDB',$id,{ |
48
|
|
|
|
|
|
|
server => 'localhost', |
49
|
|
|
|
|
|
|
database => 'default', |
50
|
|
|
|
|
|
|
collection => 'default', |
51
|
|
|
|
|
|
|
}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Tie the MongoDB document or create a new one. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub TIEHASH { |
58
|
|
|
|
|
|
|
my $class = shift; |
59
|
|
|
|
|
|
|
my $id = shift; |
60
|
|
|
|
|
|
|
my $args = shift; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Fill in some defaults |
63
|
|
|
|
|
|
|
my $server_name = $args->{server} || 'localhost'; |
64
|
|
|
|
|
|
|
my $port = $args->{port} || 27017; |
65
|
|
|
|
|
|
|
my $database_name = $args->{database} || 'default'; |
66
|
|
|
|
|
|
|
my $collection_name = $args->{collection} || 'default'; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Create the object |
69
|
|
|
|
|
|
|
my $self = bless {}, $class; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Connect to the server, select the database and get a collection handle |
72
|
|
|
|
|
|
|
my $connection = MongoDB::Connection->new( |
73
|
|
|
|
|
|
|
host => $server_name, |
74
|
|
|
|
|
|
|
port => $port |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
my $database = $connection->$database_name; |
77
|
|
|
|
|
|
|
$self->{collection} = $database->collection_name; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
if ( defined($id) ) { |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Create an id object |
82
|
|
|
|
|
|
|
$self->{id} = MongoDB::OID->new( value => $id ); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Create a new document using the given id unless it's already on the server |
85
|
|
|
|
|
|
|
$self->{id} = $self->{collection}->insert( { _id => $self->{id} } ) |
86
|
|
|
|
|
|
|
unless $self->{collection}->query( { _id => $self->{id} } )->count; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Create an empty document if no id was found |
91
|
|
|
|
|
|
|
$self->{id} = $self->{collection}->insert( {} ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return $self; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 DELETE |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$object->DELETE($key) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Remove a key from the document. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
No return value. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub DELETE { |
108
|
|
|
|
|
|
|
my $self = shift; |
109
|
|
|
|
|
|
|
my $key = shift; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
$self->{collection} |
112
|
|
|
|
|
|
|
->update( { _id => $self->{id} }, { '$unset' => { $key => 1 } } ); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 EXISTS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$object->EXISTS($key) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Returns true if the key exists or false otherwise. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub EXISTS { |
125
|
|
|
|
|
|
|
my $self = shift; |
126
|
|
|
|
|
|
|
my $key = shift; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return $self->{collection} |
129
|
|
|
|
|
|
|
->query( { _id => $self->{id}, $key => { '$exists' => 'true' } } )->count |
130
|
|
|
|
|
|
|
? 1 |
131
|
|
|
|
|
|
|
: 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 FETCH |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
$object->FETCH($key) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns the current value of a key. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Returns undef if the key doesn't exist or has an undef value. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub FETCH { |
145
|
|
|
|
|
|
|
my $self = shift; |
146
|
|
|
|
|
|
|
my $key = shift; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $doc = |
149
|
|
|
|
|
|
|
$self->{collection}->find_one( { _id => $self->{id} }, { $key => 1 } ); |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
return $doc->{_id}->value if $key eq '_id' and ref( $doc->{$key} ); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
return $doc ? $doc->{$key} : undef; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 FIRSTKEY |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
$object->FIRSTKEY |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Returns the first key of the document. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub FIRSTKEY { |
165
|
|
|
|
|
|
|
my $self = shift; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $doc = $self->{collection}->find_one( { _id => $self->{id} } ); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
return unless $doc; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$self->{keylist} = [ keys(%$doc) ]; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
return $self->NEXTKEY; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 NEXTKEY |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
$object->NEXTKEY |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Returns the next key of the document or nothing at the end of the list. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub NEXTKEY { |
185
|
|
|
|
|
|
|
my $self = shift; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
return if $#{$self->{keylist}} == -1; |
188
|
|
|
|
|
|
|
return shift( @{ $self->{keylist} } ); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 STORE |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
$object->STORE($key,$value) |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Add or update a key. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub STORE { |
200
|
|
|
|
|
|
|
my $self = shift; |
201
|
|
|
|
|
|
|
my $key = shift; |
202
|
|
|
|
|
|
|
my $value = shift; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
$self->{collection} |
205
|
|
|
|
|
|
|
->update( { _id => $self->{id} }, { '$set' => { $key => $value } } ); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
return $value; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head2 UNTIE |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
$object->UNTIE |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Unties the hash from the document. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub UNTIE { |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 AUTHOR |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Sebastian Willing, C<< <sewi at cpan.org> >> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 BUGS |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-tie-hash-mongodb at rt.cpan.org>, or through |
228
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tie-Hash-MongoDB>. I will be notified, and then you'll |
229
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 SUPPORT |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
perldoc Tie::Hash::MongoDB |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
You can also look for information at: |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=over 4 |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item * Author's blog: |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
L<http://www.pal-blog.de/entwicklung/perl/2011/creating-tiehashmongodb-from-scratch-using-padre.html> |
245
|
|
|
|
|
|
|
L<http://www.pal-blog.de/entwicklung/perl/2011/finishing-tiehashmongodb.html> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tie-Hash-MongoDB> |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Tie-Hash-MongoDB> |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item * CPAN Ratings |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Tie-Hash-MongoDB> |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item * Search CPAN |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Tie-Hash-MongoDB/> |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=back |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
Copyright 2011 Sebastian Willing. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
272
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
273
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=cut |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
1; # End of Tie::Hash::MongoDB |