| 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::Database; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | use Digest::MD5 qw(md5_hex); | ||
| 13 | use Scalar::Util qw(weaken); | ||
| 14 | |||
| 15 | use aliased 'AnyEvent::MongoDB::Pool'; | ||
| 16 | use aliased 'AnyEvent::MongoDB::Collection'; | ||
| 17 | use aliased 'AnyEvent::MongoDB::Connection'; | ||
| 18 | use aliased 'AnyEvent::MongoDB::Request'; | ||
| 19 | use aliased 'AnyEvent::MongoDB::GridFS'; | ||
| 20 | |||
| 21 | use namespace::autoclean; | ||
| 22 | |||
| 23 | has pool => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Pool, | ||
| 26 | required => 1, | ||
| 27 | handles => [qw/ mongo /], | ||
| 28 | ); | ||
| 29 | |||
| 30 | has name => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Str', | ||
| 33 | required => 1, | ||
| 34 | ); | ||
| 35 | |||
| 36 | has connection => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => Connection, | ||
| 39 | clearer => 'clear_connection', | ||
| 40 | ); | ||
| 41 | |||
| 42 | has pending => ( | ||
| 43 | traits => ['Array'], | ||
| 44 | default => sub { [] }, | ||
| 45 | handles => { | ||
| 46 | shift_pending => 'shift', | ||
| 47 | push_pending => 'push', | ||
| 48 | has_pending => 'count', | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | sub _exec { | ||
| 53 | my ($self, $op, $options) = @_; | ||
| 54 | |||
| 55 | $options->{safe} ||= 1 | ||
| 56 | if $op ne 'op_query' and $options->{cb}; | ||
| 57 | |||
| 58 | if ($options->{safe}) { | ||
| 59 | my $m = $self->mongo; | ||
| 60 | $options->{w} ||= $m->w; | ||
| 61 | $options->{wtimeout} ||= $m->wtimeout; | ||
| 62 | } | ||
| 63 | |||
| 64 | $options->{timeout} = $self->mongo->query_timeout | ||
| 65 | if !exists $options->{timeout} and ($op eq 'op_query' or $options->{safe}); | ||
| 66 | |||
| 67 | my $req = Request->$op($options); | ||
| 68 | |||
| 69 | # XXX TODO slave_ok | ||
| 70 | |||
| 71 | my $conn = $self->connection; | ||
| 72 | if ($conn and $conn->connected) { | ||
| 73 | $conn->push_request($req); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $self->push_pending($req); | ||
| 77 | if ($self->has_pending == 1) { | ||
| 78 | $self->pool->connect( | ||
| 79 | sub { | ||
| 80 | my $conn = shift; | ||
| 81 | $self->connection($conn); | ||
| 82 | while (my $r = $self->shift_pending) { | ||
| 83 | $conn->push_request($r); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return $req; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub get_collection { | ||
| 94 | my ($self, $name) = @_; | ||
| 95 | |||
| 96 | return Collection->new( | ||
| 97 | database => $self, | ||
| 98 | name => $name, | ||
| 99 | ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub authenticate { | ||
| 103 | my ($self, $user, $pass, %options) = @_; | ||
| 104 | |||
| 105 | my $save = exists $options{save} ? $options{save} : 1; | ||
| 106 | my $cb = $options{cb}; | ||
| 107 | my $on_error = $options{on_error} || $self->mongo->on_auth_error || $self->mongo->on_error; | ||
| 108 | |||
| 109 | # create a hash if the password isn't yet encrypted | ||
| 110 | $pass = md5_hex("${user}:mongo:${pass}") | ||
| 111 | unless $options{is_digest}; | ||
| 112 | |||
| 113 | $self->run_command( | ||
| 114 | {getnonce => 1}, | ||
| 115 | sub { | ||
| 116 | my $doc = shift; | ||
| 117 | unless ($doc->{ok} and $doc->{nonce}) { | ||
| 118 | $on_error->($doc) if $on_error; | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | my $nonce = $doc->{nonce}; | ||
| 122 | $self->run_command( | ||
| 123 | Tie::IxHash->new( | ||
| 124 | authenticate => 1, | ||
| 125 | user => $user, | ||
| 126 | nonce => $nonce, | ||
| 127 | key => md5_hex($nonce . $user . $pass), | ||
| 128 | ), | ||
| 129 | sub { | ||
| 130 | my $doc = shift; | ||
| 131 | $self->mongo->save_auth($self->name, $user => $pass) if $save and $doc->{ok}; | ||
| 132 | my $cb = $options{cb}; | ||
| 133 | $cb->($doc) if $cb; | ||
| 134 | } | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | ); | ||
| 138 | |||
| 139 | weaken(my $_self = $self); # avoid loops | ||
| 140 | } | ||
| 141 | |||
| 142 | sub last_error { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | my %arg; | ||
| 146 | $arg{cb} = shift if ref($_[0]); | ||
| 147 | |||
| 148 | my %options = (@_, %arg); | ||
| 149 | if (my $conn = $self->connection) { | ||
| 150 | $conn->last_error(\%options); | ||
| 151 | } | ||
| 152 | elsif (my $cb = $arg{cb}) { | ||
| 153 | $cb->({err => "no connection"}); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub run_command { | ||
| 159 | my $self = shift; | ||
| 160 | |||
| 161 | my %arg = (limit => -1, skip => 0, ns => $self->name . '.$cmd'); | ||
| 162 | $arg{command} = shift if ref($_[0]); | ||
| 163 | $arg{cb} = shift if ref($_[0]); | ||
| 164 | |||
| 165 | my %options = (@_, %arg); | ||
| 166 | $self->_exec(op_query => \%options); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub gridfs { | ||
| 170 | my $self = shift; | ||
| 171 | GridFS->new(@_, database => $self); | ||
| 172 | } | ||
| 173 | |||
| 174 | __PACKAGE__->meta->make_immutable; |