line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CHI::Driver::Rethinkdb; |
2
|
|
|
|
|
|
|
$CHI::Driver::Rethinkdb::VERSION = '0.1.1'; |
3
|
1
|
|
|
1
|
|
942
|
use Moose; |
|
1
|
|
|
|
|
304093
|
|
|
1
|
|
|
|
|
5
|
|
4
|
1
|
|
|
1
|
|
4336
|
use Moose::Util::TypeConstraints; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
7
|
|
5
|
1
|
|
|
1
|
|
1182
|
use Carp qw(croak); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
50
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
694
|
use Rethinkdb; |
|
1
|
|
|
|
|
60043
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
47
|
use Rethinkdb::IO; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
extends 'CHI::Driver'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
CHI::Driver::Rethinkdb - Rethinkdb driver for CHI |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 VERSION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
version 0.1.1 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 SYNOPSIS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $cache = CHI->new( |
24
|
|
|
|
|
|
|
driver => 'Rethinkdb' |
25
|
|
|
|
|
|
|
host => 'lothal', # default localhost |
26
|
|
|
|
|
|
|
port => 32770, # default 28015 |
27
|
|
|
|
|
|
|
db_name => 'dev', # default test |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This driver uses the Rethinkdb module(s) to communicate |
33
|
|
|
|
|
|
|
with Rethinkdb database. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Rethinkdb is an open-source nosql database. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
L<https://www.rethinkdb.com/> |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 AUTHOR |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Dan Burke C<< dburke at addictmud.org >> |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 BUGS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
If you encounter any bugs, or have feature requests, please create an issue |
46
|
|
|
|
|
|
|
on github. https://github.com/dwburke/perl-CHI-Driver-Rethinkdb/issues |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Pull requests also welcome. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
L<http://www.perlfoundation.org/artistic_license_2_0> |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Should be 127, but CHI Driver tests failed. some value in between might be ok |
59
|
|
|
|
|
|
|
has '+max_key_length' => ( default => sub { 120 } ); |
60
|
|
|
|
|
|
|
has db_name => ( is => 'rw', isa => 'Str', default => sub { 'test' } ); |
61
|
|
|
|
|
|
|
has host => ( is => 'rw', isa => 'Str', default => sub { 'localhost' } ); |
62
|
|
|
|
|
|
|
has port => ( is => 'rw', isa => 'Int', default => sub { 28015 } ); |
63
|
|
|
|
|
|
|
has table_prefix => ( is => 'rw', isa => 'Str', default => sub { 'chi_' } ); |
64
|
|
|
|
|
|
|
has rethink_io => ( is => 'rw', isa => 'Rethinkdb::IO', lazy => 1, builder => '_build_rethink_io' ); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _build_rethink_io { |
70
|
1
|
|
|
1
|
|
1
|
my $self = shift; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
9
|
my $io = Rethinkdb::IO->new; |
73
|
1
|
|
|
|
|
32
|
$io->host( $self->host ); |
74
|
1
|
|
|
|
|
33
|
$io->port( $self->port ); |
75
|
1
|
|
|
|
|
27
|
$io->default_db( $self->db_name ); |
76
|
1
|
|
|
|
|
8
|
$io->connect; |
77
|
0
|
|
|
|
|
0
|
$io; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _table { |
81
|
1
|
|
|
1
|
|
246
|
my ($self) = @_; |
82
|
1
|
|
|
|
|
28
|
return $self->table_prefix . $self->namespace(); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub BUILD { |
87
|
1
|
|
|
1
|
0
|
3551
|
my ( $self, $args ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
5
|
my $response = r->db( $self->db_name ) |
90
|
|
|
|
|
|
|
->table_create( $self->_table ) |
91
|
|
|
|
|
|
|
->run($self->rethink_io); |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
0
|
|
|
|
if ($response && $response->type > 5) { |
94
|
0
|
0
|
|
|
|
|
unless ($response->response->[0] =~ /already exists/i) { |
95
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub fetch { |
103
|
0
|
|
|
0
|
0
|
|
my ( $self, $key ) = @_; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $response = r->table( $self->_table )->get( $key )->run($self->rethink_io); |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if ($response->type > 5) { |
108
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
if (my $data = $response->response) { |
112
|
0
|
|
|
|
|
|
return $data->{data}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub store { |
118
|
0
|
|
|
0
|
0
|
|
my ($self, $key, $data) = @_; |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my $response = r->table( $self->_table )->get( $key )->replace({ |
121
|
|
|
|
|
|
|
id => $key, |
122
|
|
|
|
|
|
|
data => $data |
123
|
|
|
|
|
|
|
})->run($self->rethink_io); |
124
|
0
|
0
|
|
|
|
|
if ($response->type > 5) { |
125
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
return; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub remove { |
132
|
0
|
|
|
0
|
0
|
|
my ( $self, $key ) = @_; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $response = r->table( $self->_table )->get( $key )->delete->run($self->rethink_io); |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if ($response->type > 5) { |
137
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
return; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub clear { |
144
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $response = r->table( $self->_table )->delete->run($self->rethink_io); |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if ($response->type > 5) { |
149
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
return; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub get_keys { |
156
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $response = r->table( $self->_table )->run($self->rethink_io); |
159
|
|
|
|
|
|
|
|
160
|
0
|
0
|
|
|
|
|
if ($response->type > 5) { |
161
|
0
|
|
|
|
|
|
croak $response->response->[0] . '(' . $response->type . ')'; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
my @keys = map { $_->{id} } @{ $response->response }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
return @keys; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub get_namespaces { |
169
|
0
|
|
|
0
|
0
|
|
croak 'not supported'; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |