MongoDB/Collection.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 188 CC 23 SCAR 28.3
CC
23
Cov
0%
ensure_index
42.0
find
20.0
insert
20.0
CRAP 16.9
1 ## Copyright (C) Graham Barr
2 ## vim: ts=8:sw=2:expandtab:shiftround
3  
4 ## WARNING!!!!
5 ## WARNING!!!! This code is still very much a work in progress,
6 ## WARNING!!!! do not depend on anything not changing
7 ## WARNING!!!!
8  
9 package AnyEvent::MongoDB::Collection;
10  
11 use Moose;
12 use boolean;
13  
14 use aliased 'AnyEvent::MongoDB::Database';
15  
16 use namespace::autoclean;
17  
18 has database => (
19 is => 'ro',
20 isa => Database,
21 required => 1,
22 handles => [qw/ _exec /],
23 );
24  
25 has ns => (
26 is => 'ro',
27 lazy_build => 1,
28 );
29  
30 has name => (
31 is => 'ro',
32 isa => 'Str',
33 required => 1,
34 );
35  
36 sub _build_ns {
37 my $self = shift;
38 join ".", $self->database->name, $self->name;
39 }
40  
41 sub find {
42 my $self = shift;
43  
44 my %arg = (ns => $self->ns);
45 $arg{query} = shift if ref($_[0]);
46 $arg{fields} = shift if ref($_[0]);
47 $arg{cb} = shift if ref($_[0]);
48  
49 my %options = (@_, %arg);
50 # XXX if slave_okay then we could pick a slave
51 $self->_exec(op_query => \%options);
52 }
53  
54 sub find_one {
55 shift->find(@_, limit => -1, skip => 0);
56 }
57  
58 sub insert {
59 my $self = shift;
60  
61 my %arg = (ns => $self->ns);
62 $arg{documents} = shift if ref($_[0]);
63 $arg{cb} = shift if ref($_[0]);
64  
65 my %options = (@_, %arg);
66  
67 $options{documents} ||= delete $options{document};
68  
69 $options{documents} = [$options{documents}]
70 unless ref($options{documents}) eq 'ARRAY';
71  
72 $self->_exec(op_insert => \%options);
73 }
74  
75 sub save {
76 my $self = shift;
77  
78 my %arg = (ns => $self->ns);
79 $arg{document} = shift if ref($_[0]);
80 $arg{cb} = shift if ref($_[0]);
81  
82 my %options = (@_, %arg);
83  
84 my $id = $options{document}{_id};
85 if (defined $id) {
86 $options{query} = {_id => $id};
87 $options{upsert} = true;
88 $self->_exec(op_update => \%options);
89 }
90 else {
91 $self->_exec(op_insert => \%options);
92 }
93 }
94  
95 sub update {
96 my $self = shift;
97  
98 my %arg = (ns => $self->ns);
99 $arg{query} = shift if ref($_[0]);
100 $arg{doc} = shift if ref($_[0]);
101 $arg{cb} = shift if ref($_[0]);
102  
103 my %options = (@_, %arg);
104  
105 $self->_exec(op_update => \%options);
106 }
107  
108 sub remove {
109 my $self = shift;
110  
111 my %arg = (ns => $self->ns);
112 $arg{query} = shift if ref($_[0]);
113 $arg{cb} = shift if ref($_[0]);
114  
115 my %options = (@_, %arg);
116  
117 $self->_exec(op_remove => \%options);
118 }
119  
120 sub ensure_index {
121 my $self = shift;
122  
123 my %arg = (ns => $self->ns);
124 $arg{key} = shift if ref($_[0]);
125 $arg{cb} = shift if ref($_[0]);
126  
127 my %options = (@_, %arg);
128  
129 $options{name} ||= do {
130 my @k = $arg{key}->Keys;
131 my @v = $arg{key}->Values;
132 join("_", map { $k[$_], $v[$_] } 0 .. $#k);
133 };
134  
135 my $doc = Tie::IxHash->new(
136 name => delete $options{name},
137 ns => delete $options{ns},
138 key => delete $options{key},
139 unique => delete $options{unique} ? true : false,
140 dropDups => delete $options{drop_dups} ? true : false,
141 background => delete $options{background} ? true : false,
142 );
143  
144 $options{documents} = [ $doc ];
145 $options{ns} = "system.indexes";
146  
147 $self->_exec(op_insert => \%options);
148 }
149  
150 sub drop_index {
151 my $self = shift;
152 my $index = shift;
153  
154 my $cmd = Tie::IxHash->new(
155 deleteIndexes => $self->name,
156 index => $index,
157 );
158  
159 $self->database->run_command($cmd, @_);
160 }
161  
162 sub drop_indexes {
163 shift->drop_index('*', @_);
164 }
165  
166 sub drop {
167 my $self = shift;
168  
169 $self->database->run_command({drop => $self->name}, @_);
170 }
171  
172 sub validate {
173 my $self = shift;
174  
175 $self->database->run_command({validate => $self->name}, @_);
176 }
177  
178 sub get_indexes {
179 my $self = shift;
180  
181 my %arg = (ns => "system.indexes");
182 $arg{cb} = shift if ref($_[0]);
183  
184 my %options = (@_, %arg);
185 $options{query} = { ns => $self->ns };
186  
187 $self->_exec(op_query => \%options);
188 }
189  
190 sub count {
191 my $self = shift;
192  
193 my %arg = (ns => $self->ns);
194 $arg{query} = shift if ref($_[0]);
195 $arg{cb} = shift if ref($_[0]);
196  
197 my %options = (@_, %arg);
198  
199 my $cmd = Tie::IxHash->new(
200 count => $self->name,
201 query => delete $options{query} || {},
202 );
203  
204 $self->database->run_command($cmd, %options);
205 }
206  
207 __PACKAGE__->meta->make_immutable;
208