lib/CHI/Driver/Rethinkdb.pm

filter: Statementstmt 50.6% 38 / 75 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.8% 11 / 19 Podpod 0.0% 0 / 7 totaltotal 40.1% 49 / 122 SCAR 34.4
CC
10
Cov
40%
BUILD
13.7
fetch
12.0
store
6.0
CRAP 31.1
« blib/lib/CHI/Driver/Rethinkdb.pm ↑ Summary lib/CHI/Driver/Rethinkdb/t/CHIDriverTests.pm »
1 package CHI::Driver::Rethinkdb;
2 1 $CHI::Driver::Rethinkdb::VERSION = '0.1.1';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Rethinkdb;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Rethinkdb::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 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 1 ▶has '+max_key_length' => ( default => sub { 120 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶has db_name => ( is => 'rw', isa => 'Str', default => sub { 'test' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
61 0 ▶has host => ( is => 'rw', isa => 'Str', default => sub { 'localhost' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
62 0 ▶has port => ( is => 'rw', isa => 'Int', default => sub { 28015 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
63 1 ▶has table_prefix => ( is => 'rw', isa => 'Str', default => sub { 'chi_' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
64 1 has rethink_io => ( is => 'rw', isa => 'Rethinkdb::IO', lazy => 1, builder => '_build_rethink_io' );
65  
66  
67 1 __PACKAGE__->meta->make_immutable;
68  
69 sub _build_rethink_io {
70 1 ▶ my $self = shift;
Subroutine _build_rethink_io: called CC=1 SCAR=0.0
71
72 1 my $io = Rethinkdb::IO->new;
73 1 $io->host( $self->host );
74 1 $io->port( $self->port );
75 1 $io->default_db( $self->db_name );
76 1 $io->connect;
77 0 $io;
78 }
79  
80 sub _table {
81 1 ▶ my ($self) = @_;
Subroutine _table: called CC=1 SCAR=0.0
82 1 return $self->table_prefix . $self->namespace();
83 }
84  
85  
86 sub BUILD {
87 1 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: called CC=4 SCAR=26.2
Pod undocumented
88  
89 1 my $response = r->db( $self->db_name )
90 ->table_create( $self->_table )
91 ->run($self->rethink_io);
92  
93 0 ▶ if ($response && $response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and $response->type > 5) 0 0 0
Condition0%0/3
$response and $response->type > 5
!ll&&!rl&&r
000
MC/DC0%0/2
$response and $response->type > 5
$response $response->type > 5
Truth table0%0/3
A and B
A= $responseB= $response->type > 5
ABresult
0X0
100
111
94 0 ▶ unless ($response->response->[0] =~ /already exists/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->response->[0] =~ /already exists/i) 0 0 0
95 0 croak $response->response->[0] . '(' . $response->type . ')';
96 }
97 }
98  
99 0 return;
100 }
101  
102 sub fetch {
103 0 ▶ my ( $self, $key ) = @_;
Subroutine fetch: not called CC=3 SCAR=24.8
Pod undocumented
104  
105 0 my $response = r->table( $self->_table )->get( $key )->run($self->rethink_io);
106  
107 0 ▶ if ($response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->type > 5) 0 0 0
108 0 croak $response->response->[0] . '(' . $response->type . ')';
109 }
110  
111 0 ▶ if (my $data = $response->response) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $response->response) 0 0 0
112 0 return $data->{data};
113 }
114  
115 }
116  
117 sub store {
118 0 ▶ my ($self, $key, $data) = @_;
Subroutine store: not called CC=2 SCAR=17.9
Pod undocumented
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 ▶ if ($response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->type > 5) 0 0 0
125 0 croak $response->response->[0] . '(' . $response->type . ')';
126 }
127  
128 0 return;
129 }
130  
131 sub remove {
132 0 ▶ my ( $self, $key ) = @_;
Subroutine remove: not called CC=2 SCAR=17.9
Pod undocumented
133  
134 0 my $response = r->table( $self->_table )->get( $key )->delete->run($self->rethink_io);
135  
136 0 ▶ if ($response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->type > 5) 0 0 0
137 0 croak $response->response->[0] . '(' . $response->type . ')';
138 }
139  
140 0 return;
141 }
142  
143 sub clear {
144 0 ▶ my ($self) = @_;
Subroutine clear: not called CC=2 SCAR=17.9
Pod undocumented
145  
146 0 my $response = r->table( $self->_table )->delete->run($self->rethink_io);
147  
148 0 ▶ if ($response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->type > 5) 0 0 0
149 0 croak $response->response->[0] . '(' . $response->type . ')';
150 }
151  
152 0 return;
153 }
154  
155 sub get_keys {
156 0 ▶ my ($self) = @_;
Subroutine get_keys: not called CC=2 SCAR=17.9
Pod undocumented
157  
158 0 my $response = r->table( $self->_table )->run($self->rethink_io);
159  
160 0 ▶ if ($response->type > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->type > 5) 0 0 0
161 0 croak $response->response->[0] . '(' . $response->type . ')';
162 }
163  
164 0 my @keys = map { $_->{id} } @{ $response->response };
165 0 return @keys;
166 }
167  
168 sub get_namespaces {
169 0 ▶ croak 'not supported';
Subroutine get_namespaces: not called CC=1 SCAR=6.9
Pod undocumented
170 }
171  
172 1 1;
« blib/lib/CHI/Driver/Rethinkdb.pm ↑ Summary lib/CHI/Driver/Rethinkdb/t/CHIDriverTests.pm »