File Coverage

blib/lib/Couch/DB/Cluster.pm
Criterion Covered Total %
statement 21 97 21.6
branch 0 10 0.0
condition 0 8 0.0
subroutine 7 34 20.5
pod 15 16 93.7
total 43 165 26.0


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Couch-DB version 0.201.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2024-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oorestyle: not found P for method shardsForDoc($db)
12             #oorestyle: not found P for method shardsForDoc($db)
13              
14             package Couch::DB::Cluster;{
15             our $VERSION = '0.201';
16             }
17              
18              
19 1     1   1423 use warnings;
  1         3  
  1         86  
20 1     1   8 use strict;
  1         3  
  1         33  
21              
22 1     1   7 use Log::Report 'couch-db';
  1         2  
  1         8  
23              
24 1     1   344 use Couch::DB::Util qw/flat/;;
  1         3  
  1         9  
25              
26 1     1   8 use Scalar::Util qw/weaken/;
  1         3  
  1         76  
27 1     1   8 use URI::Escape qw/uri_escape/;
  1         2  
  1         55  
28 1     1   7 use Storable qw/dclone/;
  1         2  
  1         2411  
29              
30             #--------------------
31              
32 0     0 1   sub new(@) { my ($class, %args) = @_; (bless {}, $class)->init(\%args) }
  0            
33              
34             sub init($)
35 0     0 0   { my ($self, $args) = @_;
36              
37 0 0         $self->{CDC_couch} = delete $args->{couch} or panic "Requires couch";
38 0           weaken $self->{CDC_couch};
39              
40 0           $self;
41             }
42              
43              
44             #--------------------
45              
46 0     0 1   sub couch() { $_[0]->{CDC_couch} }
47              
48             #--------------------
49              
50             sub clusterState(%)
51 0     0 1   { my ($self, %args) = @_;
52              
53 0           my %query;
54 0           my @need = flat delete $args{ensure_dbs_exists};
55 0 0         $query{ensure_dbs_exists} = $self->couch->jsonText(\@need, compact => 1)
56             if @need;
57              
58 0           $self->couch->call(GET => '/_cluster_setup',
59             introduced => '2.0.0',
60             query => \%query,
61             $self->couch->_resultsConfig(\%args),
62             );
63             }
64              
65              
66             sub clusterSetup($%)
67 0     0 1   { my ($self, $config, %args) = @_;
68              
69 0           $self->couch->toJSON($config, int => qw/port node_count/);
70              
71 0           $self->couch->call(POST => '/_cluster_setup',
72             introduced => '2.0.0',
73             send => $config,
74             $self->couch->_resultsConfig(\%args),
75             );
76             }
77              
78             #--------------------
79              
80             sub reshardStatus(%)
81 0     0 1   { my ($self, %args) = @_;
82 0           my $path = '/_reshard';
83 0 0         $path .= '/state' unless delete $args{counts};
84              
85 0           $self->couch->call(GET => $path,
86             introduced => '2.4.0',
87             $self->couch->_resultsConfig(\%args),
88             );
89             }
90              
91              
92             sub resharding(%)
93 0     0 1   { my ($self, %args) = @_;
94              
95             my %send = (
96             state => (delete $args{state} or panic "Requires 'state'"),
97             reason => delete $args{reason},
98 0   0       );
99              
100 0           $self->couch->call(PUT => '/_reshard/state',
101             introduced => '2.4.0',
102             send => \%send,
103             $self->couch->_resultsConfig(\%args),
104             );
105             }
106              
107              
108             sub __jobValues($$)
109 0     0     { my ($self, $couch, $job) = @_;
110              
111 0           $couch->toPerl($job, isotime => qw/start_time update_time/)
112             ->toPerl($job, node => qw/node/);
113              
114             $couch->toPerl($_, isotime => qw/timestamp/)
115 0 0         for @{$job->{history} || []};
  0            
116             }
117              
118             sub __reshardJobsValues($$)
119 0     0     { my ($self, $result, $data) = @_;
120 0           my $couch = $result->couch;
121              
122 0           my $values = dclone $data;
123 0 0         $self->__jobValues($couch, $_) for @{$values->{jobs} || []};
  0            
124 0           $values;
125             }
126              
127             sub reshardJobs(%)
128 0     0 1   { my ($self, %args) = @_;
129              
130             $self->couch->call(GET => '/_reshard/jobs',
131             introduced => '2.4.0',
132             $self->couch->_resultsConfig(\%args,
133 0     0     on_values => sub { $self->__reshardJobsValues(@_) },
134 0           ),
135             );
136             }
137              
138              
139             sub __reshardStartValues($$)
140 0     0     { my ($self, $result, $data) = @_;
141 0           my $values = dclone $data;
142             $result->couch->toPerl($_, node => 'node')
143 0           for @$values;
144              
145 0           $values;
146             }
147              
148             sub reshardStart($%)
149 0     0 1   { my ($self, $create, %args) = @_;
150              
151             $self->couch->call(POST => '/_reshard/jobs',
152             introduced => '2.4.0',
153             send => $create,
154             $self->couch->_resultsConfig(\%args,
155 0     0     on_values => sub { $self->__reshardStartValues(@_) },
156 0           ),
157             );
158             }
159              
160              
161             sub __reshardJobValues($$)
162 0     0     { my ($self, $result, $data) = @_;
163 0           my $couch = $result->couch;
164              
165 0           my $values = dclone $data;
166 0           $self->__jobValues($couch, $values);
167 0           $values;
168             }
169              
170             sub reshardJob($%)
171 0     0 1   { my ($self, $jobid, %args) = @_;
172              
173             $self->couch->call(GET => "/_reshard/jobs/$jobid",
174             introduced => '2.4.0',
175             $self->couch->_resultsConfig(\%args,
176 0     0     on_values => sub { $self->__reshardJobValues(@_) }),
  0            
177             );
178             }
179              
180              
181             sub reshardJobRemove($%)
182 0     0 1   { my ($self, $jobid, %args) = @_;
183              
184 0           $self->couch->call(DELETE => "/_reshard/jobs/$jobid",
185             introduced => '2.4.0',
186             $self->couch->_resultsConfig(\%args),
187             );
188             }
189              
190              
191             sub reshardJobState($%)
192 0     0 1   { my ($self, $jobid, %args) = @_;
193              
194 0           $self->couch->call(GET => "/_reshard/job/$jobid/state",
195             introduced => '2.4.0',
196             $self->couch->_resultsConfig(\%args),
197             );
198             }
199              
200              
201             sub reshardJobChange($%)
202 0     0 1   { my ($self, $jobid, %args) = @_;
203              
204             my %send = (
205             state => (delete $args{state} or panic "Requires 'state'"),
206             reason => delete $args{reason},
207 0   0       );
208              
209 0           $self->couch->call(PUT => "/_reshard/job/$jobid/state",
210             introduced => '2.4.0',
211             send => \%send,
212             $self->couch->_resultsConfig(\%args),
213             );
214             }
215              
216              
217             sub __dbshards($$)
218 0     0     { my ($self, $result, $data) = @_;
219 0           my $couch = $result->couch;
220              
221 0           my %values = %$data;
222 0   0       my $shards = delete $values{shards} || {};
223 0           $values{shards} = [ map +($_ => $couch->listToPerl($_, node => $shards->{$_}) ), keys %$shards ];
224 0           \%values;
225             }
226              
227             sub shardsForDB($%)
228 0     0 1   { my ($self, $db, %args) = @_;
229              
230             $self->couch->call(GET => $db->_pathToDB('_shards'),
231             introduced => '2.0.0',
232             $self->couch->_resultsConfig(\%args,
233 0     0     on_values => sub { $self->__dbshards(@_) },
234 0           ),
235             );
236             }
237              
238              
239             sub __docshards($$)
240 0     0     { my ($result, $data) = @_;
241 0           my $values = +{ %$data };
242 0           $values->{nodes} = [ $result->couch->listToPerl($values, node => delete $values->{nodes}) ];
243 0           $values;
244             }
245              
246             sub shardsForDoc($%)
247 0     0 1   { my ($self, $doc, %args) = @_;
248 0           my $db = $doc->db;
249              
250             $self->couch->call(GET => $db->_pathToDB('_shards/'.$doc->id),
251             introduced => '2.0.0',
252             $self->couch->_resultsConfig(\%args,
253 0     0     on_values => sub { $self->__docshards(@_) },
254 0           ),
255             );
256             }
257              
258              
259             sub syncShards($%)
260 0     0 1   { my ($self, $db, %args) = @_;
261              
262 0           $self->couch->call(POST => $db->_pathToDB('_sync_shards'),
263             send => {},
264             introduced => '2.3.1',
265             $self->couch->_resultsConfig(\%args),
266             );
267             }
268              
269             1;