AnyEvent-CouchDB
view release on metacpan or search on metacpan
lib/AnyEvent/CouchDB.pm view on Meta::CPAN
$cb
);
$cv;
}
sub config {
my ($self, $options) = @_;
my ($cv, $cb) = cvcb($options);
http_request(
GET => $self->{uri} . '_config',
headers => $self->_build_headers($options),
$cb
);
$cv;
}
sub replicate {
my ($self, $source, $target, $options) = @_;
my ($cv, $cb) = cvcb($options);
my $replication = {source => $source, target => $target};
if (my $continuous = delete $options->{continuous}) {
$replication->{continuous} = 1;
}
my $body = $default_json->encode($replication);
http_request(
POST => $self->{uri}.'_replicate',
headers => $self->_build_headers($options),
body => $body,
$cb
);
$cv;
}
1;
__END__
=head1 NAME
AnyEvent::CouchDB - a non-blocking CouchDB client based on jquery.couch.js
=head1 SYNOPSIS
Getting information about a CouchDB server:
use AnyEvent::CouchDB;
use Data::Dump 'pp';
my $couch = couch('http://localhost:5984/');
print pp( $couch->all_dbs->recv ), "\n";
print pp( $couch->info->recv ), "\n";
Get an object representing a CouchDB database:
my $db = $couch->db('database');
$db = couchdb('database');
$db = couchdb('http://somewhere.com:7777/database/');
With authentication:
# user is the username and s3cret is the password
$db = couchdb('http://user:s3cret@somewhere.com:7777/database');
Work with individual CouchDB documents;
my $user = $db->open_doc('~larry')->recv;
$user->{name} = "larry";
$db->save_doc($user)->recv;
Query a view:
$db->view('users/all', { startkey => 'b', endkey => 'bZZZ' })->recv
Finally, an asynchronous example:
# Calling cb allow you to set a callback that will run when results are available.
$db->all_docs->cb(sub {
my ($cv) = @_;
print pp( $cv->recv ), "\n";
});
# However, you have to be in an event loop at some point in time.
AnyEvent->condvar->recv;
=head1 DESCRIPTION
AnyEvent::CouchDB is a non-blocking CouchDB client implemented on top of the
L<AnyEvent> framework. Using this library will give you the ability to run
many CouchDB requests asynchronously, and it was intended to be used within
a L<Coro>+L<AnyEvent> environment. However, it can also be used synchronously
if you want.
Its API is based on jquery.couch.js, but we've adapted the API slightly so that
it makes sense in an asynchronous Perl environment.
=head2 AnyEvent condvars
The main thing you have to remember is that all the data retrieval methods
return an AnyEvent condvar, C<$cv>. If you want the actual data from the
request, there are a few things you can do.
You may have noticed that many of the examples in the SYNOPSIS call C<recv>
on the condvar. You're allowed to do this under 2 circumstances:
=over 4
=item Either you're in a main program,
Main programs are "allowed to call C<recv> blockingly", according to the
author of L<AnyEvent>.
=item or you're in a Coro + AnyEvent environment.
When you call C<recv> inside a coroutine, only that coroutine is blocked
while other coroutines remain active. Thus, the program as a whole is
still responsive.
=back
If you're not using Coro, and you don't want your whole program to block,
what you should do is call C<cb> on the condvar, and give it a coderef to
( run in 1.131 second using v1.01-cache-2.11-cpan-524268b4103 )