Couchbase-Client
view release on metacpan or search on metacpan
lib/Couchbase/Client/Async.pm view on Meta::CPAN
package Couchbase::Client::Async;
use strict;
use warnings;
our $VERSION = '1.0.2';
use Couchbase::Client;
use Couchbase::Client::IDXConst;
use Log::Fu;
sub new {
my ($cls,$options) = @_;
my @async_keys = qw(
cb_update_event
cb_error
cb_waitdone
cb_update_timer
bless_events
);
my %async_opts;
@async_opts{@async_keys} = delete @{$options}{@async_keys};
my $arglist = Couchbase::Client::_MkCtorIDX($options);
$arglist->[CTORIDX_CBEVMOD] = delete $async_opts{cb_update_event}
and
$arglist->[CTORIDX_CBERR] = delete $async_opts{cb_error}
and
$arglist->[CTORIDX_CBWAITDONE] = delete $async_opts{cb_waitdone}
and
$arglist->[CTORIDX_CBTIMERMOD] = delete $async_opts{cb_update_timer}
or die "We require update_event, error, and wait_done callbacks";
if($async_opts{bless_events}) {
$arglist->[CTORIDX_BLESS_EVENT] = 1;
}
my $o = $cls->construct($arglist);
return $o;
}
#Establish proxy methods:
foreach my $subname (qw(
enable_compress
compression_settings
serialization_settings
conversion_settings
deconversion_settings
compress_threshold
timeout
)) {
no strict 'refs';
*{$subname} = sub {
my ($self,@args) = @_;
my $base = $self->_get_base_rv;
@_ = ($base, @args);
goto &{"Couchbase::Client::$subname"};
};
}
1;
__END__
=head1 NAME
Couchbase::Client::Async - Asynchronous system for couchbase clients.
=head1 DESCRIPTION
This is a module intended for use by other higher level components which interact
directly with Perl event frameworks.
libcouchbase allows for pluggable event loops, which drive and tell it about
file descriptor events, timeouts, and other such nicetis.
The purpose of this module is to provide a unified library for perl, which will
be compatible with L<POE>, L<AnyEvent>, L<IO::Async> and other event loops.
The module is divided into two components:
=head2 I/O Events
This part of the module provides an interface for event libraries to tell
libcouchbase about events, and conversely, have libcouchbase tell the event
library about new file descriptors and events.
=head2 Command Results and Events
This part of the module provides a framework in which event libraries can provide
interfaces to users, so that they can perform commands on a couchbase cluster, and
receive their results asynchronously.
=head1 EVENT MANAGEMENT
Event, Timer, and I/O management is the lower level of this module, and constitutes
the interface which event loop integrators would need to be most attune to.
At the most basic level, you are required to implement four callbacks. These
callbacks are indicated by values passed to their given hash keys in the object's
constructor.
=head3 C<cb_update_event>
cb_update_event => sub {
my ($evdata,$action,$flags) = @_;
if(ref($evdata) ne "Couchbase::Client::Event") {
bless $evdata, "Couchbase::Client::Event";
}
...
};
This callback is invoked to update and/or modify events. It receives three
arguments.
The first is C<$evdata> which is an array, which may be blessed into
L<Couchbase::Client::Async::Event>.
The C<$evdata> structure contains the state-'glue' for interaction with the
internal (C) event routines and their Perl dispatchers.
The following will mention some useful fields for the C<$evdata> structure. Note
this is not an exhaustive listing (see that class' documentation for that) but
a more practical guide as to what the fields are for, and how they may be used.
=over
=item C<fd>
This read-only field contains the numeric file descriptor on which the C library
will perform I/O functions. If your event loop supports watching file descriptor
numbers directly, you may simply watch this number; otherwise, look at the next
field
=item C<dupfh>
This mutatable field contains an optional dup'd Perl filehandle. Some Perl event
loops do not allow for watching a file descriptor directly and demand to be given
a 'PerlIO' filehandle (conforming to the L<IO::Handle> interface, or one of the
things returned by L<open>).
This filehandle should be a dup'd version of the C<fd> field, the reason being
that when the filehandle goes out of scope from perl, the underlying file
I<descriptor> will be C<close()>d. Since there is not necessarily a one-to-one
correlation between stream lifetimes as they exist in the Perl client, and their
lifetimes as they exist in libcouchbase, it is recommended that the file
descriptor be dup'd. In that way, close() on the dup'd file descriptor will not
affect the file descriptor in the C side.
The filehandle stored in the C<dupfh> field will remain active until the underlying
C<fd> is closed or changed.
The first time an event is created, the C<dupfh> field will be undef, and
the callback should check for this creation, and if true, create a new one,
using the following idiom:
open my $dupfh, ">&", $evdata->fd;
$evdata->dupfh($dupfh);
( run in 1.626 second using v1.01-cache-2.11-cpan-140bd7fdf52 )