MongoDB.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« MongoDB/Pool.pm ↑ Summary
1 ## Copyright (C) Graham Barr
2 ## vim: ts=8:sw=2:expandtab:shiftround
3 ## ABSTRACT: Asynchronous MongoDB client using AnyEvent
4  
5 ## WARNING!!!!
6 ## WARNING!!!! This code is still very much a work in progress,
7 ## WARNING!!!! do not depend on anything not changing
8 ## WARNING!!!!
9  
10 package AnyEvent::MongoDB;
11  
12 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Moose::Util::TypeConstraints;
14  
15 use aliased 'AnyEvent::MongoDB::Pool';
16  
17 use AnyEvent;
18 use Tie::IxHash;
19 use boolean;
20  
21 use namespace::autoclean;
22  
23 BEGIN {
24 my $hostport = '[\-\.a-zA-Z0-9]+ (?: : \d+ )?';
25 my $userpass = '[-_.\w\d]+:[-_\w\d]+@';
26 my $dbname = '[-\d\w]+';
27  
28 subtype 'MongoDB_URI' ##
29 => as 'Str' ##
30 => where {
31 $_ =~ m{
32 ^mongodb://
33 (?:
34 (?: $userpass ) $hostport (?: , $hostport)* (?: /$dbname )
35 | $hostport (?: , $hostport)*
36 )
37 $}x;
38 };
39 }
40  
41 has uri => (
42 is => 'ro',
43 isa => 'MongoDB_URI',
44 );
45  
46 has host => (
47 is => 'ro',
48 isa => 'Str',
49 default => '127.0.0.1',
50 );
51  
52 has port => (
53 is => 'ro',
54 isa => 'Int',
55 default => 27017,
56 );
57  
58 has servers => (
59 is => 'ro',
60 isa => 'ArrayRef',
61 lazy_build => 1,
62 auto_deref => 1,
63 );
64  
65 # XXX need more callbacks, on_connect, on_connect_error, on_reconect??
66 has on_error => (
67 is => 'rw',
68 isa => 'CodeRef',
69 default => sub {
70 sub { confess(@_) }
71 },
72 );
73  
74 has on_auth_error => (
75 is => 'rw',
76 isa => 'CodeRef',
77 );
78  
79  
80 has timeout => (
81 is => 'ro',
82 isa => 'Int',
83 default => 20000,
84 );
85  
86 has w => (
87 is => 'rw',
88 isa => 'Int',
89 default => 1,
90 );
91  
92 has wtimeout => (
93 is => 'rw',
94 isa => 'Int',
95 default => 1000,
96 );
97  
98 has username => (
99 is => 'rw',
100 isa => 'Str',
101 );
102  
103 has password => (
104 is => 'rw',
105 isa => 'Str',
106 );
107  
108 has auth_db => (
109 is => 'rw',
110 isa => 'Str',
111 default => 'admin',
112 );
113  
114 has query_timeout => (
115 is => 'rw',
116 isa => 'Int',
117 default => 30000,
118 );
119  
120 has pool => (
121 is => 'rw',
122 isa => Pool,
123 lazy_build => 1,
124 handles => ['get_database'],
125 );
126  
127 sub _build_servers {
128 my $self = shift;
129 my @nodes;
130 if (my $uri = $self->uri) {
131 my ($user, $pass, $db);
132 $uri =~ s,^mongodb://,,;
133 ($user, $pass) = ($1, $2) if $uri =~ s,([-_.\w\d]+):([-_\w\d]+)@,,;
134 ($db) = $1 if $uri =~ s,/([^/]+)$,,;
135 $self->add_auth($user, $pass, $db) if defined $user;
136 foreach my $part (split(/,/, $uri)) {
137 my ($host, $port) = split(/:/, $part);
138 push @nodes, {host => $host, port => $port || 27017};
139 }
140 }
141 else {
142 @nodes = ({host => $self->host, port => $self->port});
143 }
144  
145 return \@nodes;
146 }
147  
148 sub BUILD {
149 my $self = shift;
150  
151 $self->clear_servers;
152 $self->servers;
153  
154 my ($user, $pass, $db) = ($self->username, $self->password, $self->auth_db);
155 $self->add_auth($user, $pass, $db) if defined $user;
156  
157 return;
158 }
159  
160 sub _build_pool {
161 my $self = shift;
162  
163 return Pool->new(mongo => $self);
164 }
165  
166 sub add_auth {
167  
168 #XXX TODO
169 }
170  
171 __PACKAGE__->meta->make_immutable;
172  
173 __END__
« MongoDB/Pool.pm ↑ Summary