line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mango::Database; |
2
|
9
|
|
|
9
|
|
59
|
use Mojo::Base -base; |
|
9
|
|
|
|
|
22
|
|
|
9
|
|
|
|
|
81
|
|
3
|
|
|
|
|
|
|
|
4
|
9
|
|
|
9
|
|
1838
|
use Carp 'croak'; |
|
9
|
|
|
|
|
20
|
|
|
9
|
|
|
|
|
382
|
|
5
|
9
|
|
|
9
|
|
50
|
use Mango::BSON qw(bson_code bson_doc); |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
332
|
|
6
|
9
|
|
|
9
|
|
3931
|
use Mango::Collection; |
|
9
|
|
|
|
|
28
|
|
|
9
|
|
|
|
|
69
|
|
7
|
9
|
|
|
9
|
|
3901
|
use Mango::GridFS; |
|
9
|
|
|
|
|
27
|
|
|
9
|
|
|
|
|
71
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has [qw(mango name)]; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub build_write_concern { |
12
|
0
|
|
|
0
|
1
|
|
my $mango = shift->mango; |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
0
|
|
|
|
|
j => $mango->j ? \1 : \0, |
15
|
|
|
|
|
|
|
w => $mango->w, |
16
|
|
|
|
|
|
|
wtimeout => $mango->wtimeout |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub collection { |
21
|
0
|
|
|
0
|
1
|
|
my ($self, $name) = @_; |
22
|
0
|
|
|
|
|
|
return Mango::Collection->new(db => $self, name => $name); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub collection_names { |
26
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
27
|
0
|
0
|
|
|
|
|
my $cb = ref $_[-1] eq 'CODE' ? pop : undef; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Non-blocking |
30
|
|
|
|
|
|
|
return $self->list_collections(@_ => sub { |
31
|
0
|
|
|
0
|
|
|
my ($self, $err, $cursor) = @_; |
32
|
0
|
0
|
|
|
|
|
return $self->$cb($err, []) if $err; |
33
|
|
|
|
|
|
|
$cursor->all(sub { |
34
|
0
|
|
|
|
|
|
my ($cursor, $err, $docs) = @_; |
35
|
0
|
|
|
|
|
|
@$docs = map { $_->{name} } @$docs; |
|
0
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$self->$cb($err, $docs); |
37
|
0
|
|
|
|
|
|
}); |
38
|
0
|
0
|
|
|
|
|
}) if $cb; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Blocking |
41
|
0
|
|
|
|
|
|
my $docs = $self->list_collections(@_)->all; |
42
|
0
|
|
|
|
|
|
@$docs = map { $_->{name} } @$docs; |
|
0
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
return $docs; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub command { |
47
|
0
|
|
|
0
|
1
|
|
my ($self, $command) = (shift, shift); |
48
|
0
|
0
|
|
|
|
|
my $cb = ref $_[-1] eq 'CODE' ? pop : undef; |
49
|
0
|
0
|
|
|
|
|
$command = ref $command ? $command : bson_doc($command => 1, @_); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Non-blocking |
52
|
0
|
|
|
|
|
|
my $mango = $self->mango; |
53
|
0
|
|
|
|
|
|
my $name = $self->name; |
54
|
0
|
|
|
|
|
|
my $protocol = $mango->protocol; |
55
|
|
|
|
|
|
|
return $mango->query( |
56
|
|
|
|
|
|
|
("$name.\$cmd", {}, 0, -1, $command, {}) => sub { |
57
|
0
|
|
|
0
|
|
|
my ($collection, $err, $reply) = @_; |
58
|
0
|
|
|
|
|
|
my $doc = $reply->{docs}[0]; |
59
|
0
|
|
0
|
|
|
|
$err ||= $protocol->command_error($doc); |
60
|
0
|
|
|
|
|
|
$self->$cb($err, $doc); |
61
|
|
|
|
|
|
|
} |
62
|
0
|
0
|
|
|
|
|
) if $cb; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Blocking |
65
|
0
|
|
|
|
|
|
my $doc = $mango->query("$name.\$cmd", {}, 0, -1, $command, {})->{docs}[0]; |
66
|
0
|
0
|
|
|
|
|
if (my $err = $protocol->command_error($doc)) { croak $err } |
|
0
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $doc; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub dereference { |
71
|
0
|
|
|
0
|
1
|
|
my ($self, $dbref, $cb) = @_; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Non-blocking |
74
|
0
|
|
|
|
|
|
my $collection = $self->collection($dbref->{'$ref'}); |
75
|
0
|
|
|
0
|
|
|
return $collection->find_one($dbref->{'$id'} => sub { shift; $self->$cb(@_) } |
|
0
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
) if $cb; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Blocking |
79
|
0
|
|
|
|
|
|
return $collection->find_one($dbref->{'$id'}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
0
|
1
|
|
sub gridfs { Mango::GridFS->new(db => shift) } |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub list_collections { |
85
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
86
|
0
|
0
|
|
|
|
|
my $cb = ref $_[-1] eq 'CODE' ? pop : undef; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $command = bson_doc(listCollections => 1, @_); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Non-blocking |
91
|
|
|
|
|
|
|
return $self->command($command => sub { |
92
|
0
|
|
|
0
|
|
|
my ($self, $err, $res) = @_; |
93
|
0
|
|
|
|
|
|
$res = $res->{cursor}; |
94
|
|
|
|
|
|
|
my $cursor = Mango::Cursor->new(collection => $self->collection, |
95
|
0
|
|
|
|
|
|
id => $res->{id}, ns => $res->{ns})->add_batch($res->{firstBatch}); |
96
|
0
|
|
|
|
|
|
$self->$cb($err, $cursor); |
97
|
0
|
0
|
|
|
|
|
}) if $cb; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Blocking |
100
|
0
|
|
|
|
|
|
my $cursor = $self->command($command)->{cursor}; |
101
|
|
|
|
|
|
|
return Mango::Cursor->new(collection => $self->collection, |
102
|
|
|
|
|
|
|
id => $cursor->{id}, ns => $cursor->{ns}) |
103
|
0
|
|
|
|
|
|
->add_batch($cursor->{firstBatch}); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
0
|
1
|
|
sub stats { shift->command(bson_doc(dbstats => 1), @_) } |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=encoding utf8 |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 NAME |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Mango::Database - MongoDB database |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SYNOPSIS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
use Mango::Database; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $db = Mango::Database->new(mango => $mango); |
121
|
|
|
|
|
|
|
my $collection = $db->collection('foo'); |
122
|
|
|
|
|
|
|
my $gridfs = $db->gridfs; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L is a container for MongoDB databases used by L. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L implements the following attributes. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 mango |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $mango = $db->mango; |
135
|
|
|
|
|
|
|
$db = $db->mango(Mango->new); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L object this database belongs to. Note that this reference is usually |
138
|
|
|
|
|
|
|
weakened, so the L object needs to be referenced elsewhere as well. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 name |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my $name = $db->name; |
143
|
|
|
|
|
|
|
$db = $db->name('bar'); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Name of this database. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
L inherits all methods from L and implements the |
150
|
|
|
|
|
|
|
following new ones. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 build_write_concern |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $concern = $db->build_write_concern; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Build write concern based on l"mango"> settings. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 collection |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $collection = $db->collection('foo'); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Build L object for collection. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 collection_names |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $names = $db->collection_names; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Names of all collections in this database. You can filter the results by using |
169
|
|
|
|
|
|
|
the same arguments as for C. You can also append a callback |
170
|
|
|
|
|
|
|
to perform operation non-blocking. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
$db->collection_names(sub { |
173
|
|
|
|
|
|
|
my ($db, $err, $names) = @_; |
174
|
|
|
|
|
|
|
... |
175
|
|
|
|
|
|
|
}); |
176
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 command |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
my $doc = $db->command(bson_doc(text => 'foo.bar', search => 'test')); |
181
|
|
|
|
|
|
|
my $doc = $db->command(bson_doc(getLastError => 1, w => 2)); |
182
|
|
|
|
|
|
|
my $doc = $db->command('getLastError', w => 2); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Run command against database. You can also append a callback to run command |
185
|
|
|
|
|
|
|
non-blocking. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
$db->command(('getLastError', w => 2) => sub { |
188
|
|
|
|
|
|
|
my ($db, $err, $doc) = @_; |
189
|
|
|
|
|
|
|
... |
190
|
|
|
|
|
|
|
}); |
191
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 dereference |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
my $doc = $db->dereference($dbref); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Resolve database reference. You can also append a callback to perform |
198
|
|
|
|
|
|
|
operation non-blocking. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
$db->dereference($dbref => sub { |
201
|
|
|
|
|
|
|
my ($db, $err, $doc) = @_; |
202
|
|
|
|
|
|
|
... |
203
|
|
|
|
|
|
|
}); |
204
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 gridfs |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
my $gridfs = $db->gridfs; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Build L object. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 list_collections |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# return a cursor for all collections |
215
|
|
|
|
|
|
|
my $cursor = $db->list_collections; |
216
|
|
|
|
|
|
|
# only collections which name matchs a regex |
217
|
|
|
|
|
|
|
my $cursor = $db->list_collections(filter => { name => qr{^prefix} }); |
218
|
|
|
|
|
|
|
# only capped collections |
219
|
|
|
|
|
|
|
my $cursor = $db->list_collections(filter => { 'options.capped' => 1 }); |
220
|
|
|
|
|
|
|
# only the first 10 collections |
221
|
|
|
|
|
|
|
my $cursor = $db->list_collections(cursor => { batchSize => 10 }); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Returns a L of all collections in this database. Each collection |
224
|
|
|
|
|
|
|
is represented by a document containing at least the keys C and |
225
|
|
|
|
|
|
|
C. You can also append a callback to perform operation non-blocking. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
$db->list_collections(sub { |
228
|
|
|
|
|
|
|
my ($db, $err, $cursor) = @_; |
229
|
|
|
|
|
|
|
... |
230
|
|
|
|
|
|
|
}); |
231
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 stats |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
my $stats = $db->stats; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Get database statistics. You can also append a callback to perform operation |
238
|
|
|
|
|
|
|
non-blocking. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
$db->stats(sub { |
241
|
|
|
|
|
|
|
my ($db, $err, $stats) = @_; |
242
|
|
|
|
|
|
|
... |
243
|
|
|
|
|
|
|
}); |
244
|
|
|
|
|
|
|
Mojo::IOLoop->start unless Mojo::IOLoop->is_running; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 SEE ALSO |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
L, L, L. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |