summary refs log tree commit diff
path: root/lib/Net/Riak/Role/PBC.pm
blob: 775976e7f30ec1c04ab1d945ce08bbe31dcc8471 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Net::Riak::Role::PBC;

use Moose::Role;
use MooseX::Types::Moose qw/Str Int/;

with qw(
  Net::Riak::Role::PBC::Message
  Net::Riak::Role::PBC::Bucket
  Net::Riak::Role::PBC::MapReduce
  Net::Riak::Role::PBC::Link
  Net::Riak::Role::PBC::Meta
  Net::Riak::Role::PBC::Object);

use Net::Riak::Types 'Socket';
use IO::Socket::INET;

has [qw/r w dw/] => (
    is      => 'rw',
    isa     => Int,
    default => 2
);

has host => (
    is  => 'ro',
    isa => Str,
    required => 1,
);

has port => (
    is  => 'ro',
    isa => Int,
    required => 1,
);

has socket => (
    is => 'rw',
    isa => Socket,
    predicate => 'has_socket',
);

has timeout => (
    is => 'ro',
    isa => Int,
    default => 30,
);

sub is_alive {
    my $self = shift;
    return $self->send_message('PingReq');
}

sub connected {
    my $self = shift;
    return $self->has_socket && $self->socket->connected ? 1 : 0;
}

sub connect {
    my $self = shift;
    return if $self->has_socket && $self->connected;

    $self->socket(
        IO::Socket::INET->new(
            PeerAddr => $self->host,
            PeerPort => $self->port,
            Proto    => 'tcp',
            Timeout  => $self->timeout,
        )
    );
}

sub all_buckets {
    my $self = shift;
    my $resp = $self->send_message('ListBucketsReq');
    return ref ($resp->buckets) eq 'ARRAY' ? @{$resp->buckets} : ();
}

sub server_info {
    my $self = shift;
    my $resp = $self->send_message('GetServerInfoReq');
    return $resp;
}

sub stats { die "->stats is only avaliable through the REST interface" }

1;