AnyMongo

 view release on metacpan or  search on metacpan

lib/AnyMongo/Connection.pm  view on Meta::CPAN

package AnyMongo::Connection;
BEGIN {
  $AnyMongo::Connection::VERSION = '0.03';
}
# ABSTRACT: Asynchronous MongoDB::Connection
use strict;
use warnings;
use constant {
    DEBUG => $ENV{ANYMONGO_DEBUG},
    # bson type
    BSON_INT32 => 4,
    BSON_INT64 => 8,
    # msg header size
    STANDARD_HEADER_SIZE => 16,
    RESPONSE_HEADER_SIZE => 20,
    # opcode
    OP_REPLY    => 1,
    OP_MSG      => 1000, #generic msg command followed by a string
    OP_UPDATE	=> 2001, #update document
    OP_INSERT	=> 2002, #insert new document
    RESERVED	=> 2003, #formerly used for OP_GET_BY_OID
    OP_QUERY	=> 2004, #query a collection
    OP_GET_MORE	=> 2005, #Get more data from a query. See Cursors
    OP_DELETE	=> 2006, #Delete documents
    OP_KILL_CURSORS  => 2007,
    # flags
    REPLY_CURSOR_NOT_FOUND     => 1,
    REPLY_QUERY_FAILURE        => 2,
    REPLY_SHARD_CONFIG_STALE   => 4,
    REPLY_AWAIT_CAPABLE        => 8,
};

use Carp qw(croak);
use Data::Dumper;
use AnyEvent::Socket;
use AnyEvent::Handle;
use AnyMongo::BSON qw(bson_decode);
use AnyMongo::MongoSupport qw(decode_bson_documents);
use AnyMongo::Cursor;
use namespace::autoclean;
use Any::Moose;

has find_master => (
    is       => 'ro',
    isa      => 'Bool',
    required => 1,
    default  => 0,
);

has master_handle => (
    isa => 'Maybe[AnyEvent::Handle]',
    is  => 'rw',
    clearer  => 'clear_master_handle',
);

has secondary_nodes => (
    isa => 'ArrayRef',
    is  => 'rw',
);

has secondary_nodes => (
    isa => 'ArrayRef',
    is  => 'rw',
    lazy_build => 1,
    clearer  => 'clear_secondary_nodes',

lib/AnyMongo/Connection.pm  view on Meta::CPAN

    );
}

sub authenticate {
    my ($self, $dbname, $username, $password, $is_digest) = @_;
    my $hash = $password;

    # create a hash if the password isn't yet encrypted
    if (!$is_digest) {
        $hash = Digest::MD5::md5_hex("${username}:mongo:${password}");
    }

    # get the nonce
    my $db = $self->get_database($dbname);
    my $result = $db->run_command({getnonce => 1});
    if (!$result->{'ok'}) {
        return $result;
    }

    my $nonce = $result->{'nonce'};
    my $digest = Digest::MD5::md5_hex($nonce.$username.$hash);

    # run the login command
    my $login = tie(my %hash, 'Tie::IxHash');
    %hash = (authenticate => 1,
             user => $username,
             nonce => $nonce,
             key => $digest);
    $result = $db->run_command($login);

    return $result;
}

sub admin { shift->get_database('admin') }

sub disconnect {
    my ($self) = @_;
    $self->clear_master_handle;
    my $guards = $self->{_guards};
    my $handles = $self->{_handles};
    map { delete $guards->{$_} } keys %{ $guards };
    map { (delete $handles->{$_})->destroy } keys %{$handles};
    $self->{mongo_servers} = {};
    $self->{_is_connected} = 0;
    $self->connected(0);
}

sub DEMOLISH {
    shift->disconnect;
}

__PACKAGE__->meta->make_immutable;

1;


=pod

=head1 NAME

AnyMongo::Connection - Asynchronous MongoDB::Connection

=head1 VERSION

version 0.03

=head1 SYNOPSIS

=head1 DESCRIPTION

=for connection flow
    1. create connection to seed list in parallel
    2. check every connection and do follow:
    2.1 call ismaster check the node
    2.2 if node is master, connect will be success
    2.3 if node is secondary
    2.3.1 update hosts(full member list) if possible
    2.3.2 double check primary ismaster
    3. if connection success, connection done.
    3.1 else, check if seed queue not empty, goto 1
    4. can't connect to master,failed.

=head1 AUTHORS

=over 4

=item *

Pan Fan(nightsailer) <nightsailer at gmail.com>

=item *

Kristina Chodorow <kristina at 10gen.com>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by Pan Fan(nightsailer).

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut


__END__



( run in 1.370 second using v1.01-cache-2.11-cpan-39bf76dae61 )