Couch-DB
view release on metacpan or search on metacpan
lib/Couch/DB.pm view on Meta::CPAN
sub new(%)
{ my ($class, %args) = @_;
$class ne __PACKAGE__
or panic "You have to instantiate extensions of this class";
(bless {}, $class)->init(\%args);
}
sub init($)
{ my ($self, $args) = @_;
my $v = delete $args->{api} or panic "Parameter 'api' is required";
$self->{CD_api} = blessed $v && $v->isa('version') ? $v : version->parse($v);
$self->{CD_clients} = [];
# explicit undef for server means: do not create
my $create_client = ! exists $args->{server} || defined $args->{server};
my $server = delete $args->{server};
my $external = $ENV{PERL_COUCH_DB_SERVER};
my %auth = ( auth => delete $args->{auth} || 'BASIC' );
if($server || ! $external)
{ $auth{username} = delete $args->{username};
$auth{password} = delete $args->{password};
}
elsif($external)
{ my $ext = URI->new($external);
if(my $userinfo = $ext->userinfo)
{ my ($username, $password) = split /:/, $userinfo;
$auth{username} = uri_unescape $username;
$auth{password} = uri_unescape $password;
$ext->userinfo(undef);
}
$server = "$ext";
}
$self->{CD_auth} = \%auth;
$self->createClient(server => $server || DEFAULT_SERVER, name => '_local')
if $create_client;
$self->{CD_toperl} = delete $args->{to_perl} || {};
$self->{CD_tojson} = delete $args->{to_json} || {};
$self->{CD_toquery} = delete $args->{to_query} || {};
$self;
}
#--------------------
sub api() { $_[0]->{CD_api} }
#--------------------
sub createClient(%)
{ my ($self, %args) = @_;
my $client = Couch::DB::Client->new(couch => $self, %{$self->{CD_auth}}, %args);
$client ? $self->addClient($client) : undef;
}
sub db($%)
{ my ($self, $name, %args) = @_;
Couch::DB::Database->new(name => $name, couch => $self, %args);
}
sub node($)
{ my ($self, $name) = @_;
$self->{CD_nodes}{$name} ||= Couch::DB::Node->new(name => $name, couch => $self);
}
sub cluster() { $_[0]->{CD_cluster} ||= Couch::DB::Cluster->new(couch => $_[0]) }
#--------------------
#XXX the API-doc might be mistaken, calling the "analyzer" parameter "field".
sub searchAnalyze($%)
{ my ($self, $config, %args) = @_;
exists $config->{analyzer} or panic "No analyzer specified.";
exists $config->{text} or panic "No text to inspect specified.";
$self->call(POST => '/_search_analyze',
introduced => '3.0',
send => $config,
$self->_resultsConfig(\%args),
);
}
sub requestUUIDs($%)
{ my ($self, $count, %args) = @_;
$self->call(GET => '/_uuids',
introduced => '2.0.0',
query => { count => $count },
$self->_resultsConfig(\%args),
);
}
sub freshUUIDs($%)
{ my ($self, $count, %args) = @_;
my $stock = $self->{CDC_uuids} || [];
my $bulk = delete $args{bulk} || 50;
while($count > @$stock)
{ my $result = $self->requestUUIDs($bulk, delay => 0) or last;
push @$stock, @{$result->values->{uuids} || []};
}
splice @$stock, 0, $count;
}
sub freshUUID(%) { my $s = shift; ($s->freshUUIDs(1, @_))[0] }
#--------------------
sub addClient($)
( run in 1.945 second using v1.01-cache-2.11-cpan-0b5f733616e )