ElasticSync/Role/ElasticSchema.pm

filter: Statementstmt 14.4% 12 / 83 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 17.6% 3 / 17 Podpod 76.9% 10 / 13 totaltotal 14.5% 25 / 172 CC 27 SCAR 27.8
CC
27
Cov
9%
es_drop_mapping
42.0
drop_indexes
20.0
es_create_snapshot
20.0
CRAP 16.0
« ElasticSync/ResultSet.pm ↑ Summary ElasticSync/Role/ElasticBlackholeResult.pm »
1 package DBIx::Class::ElasticSync::Role::ElasticSchema;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has es_store => (
9 is => 'rw',
10 isa => 'Maybe[Object]'
11 );
12  
13 has connect_elasticsearch => (
14 is => 'rw',
15 isa => 'HashRef',
16 required => 0,
17 0 ▶ default => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
18 1 );
19  
20 =head2 es
21  
22 Builds a Search::Elasticsearch Object based on the given Config. It holds the Object in Memory while Runtime
23  
24 $schema->connect_elasticsearch( host => "localhost", port => 9200, cxn => undef, debug => 0 );
25 my $es = $schema->es;
26  
27 =cut
28  
29 0 ▶sub es {
Subroutine es: not called CC=3 SCAR=24.8
Pod documented
30  
31 0 my ($self) = @_;
32  
33 0 ▶ if ( !$self->es_store ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->es_store) 0 0 0
34  
35 0 my $settings = $self->connect_elasticsearch;
36  
37 0 ▶ if ( $settings->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"debug"}) 0 0 0
38  
39 0 delete $settings->{debug};
40 0 $settings->{trace_to} = 'Stderr';
41 0 eval "use Log::Any::Adapter qw(Stderr);";
42 }
43  
44 0 $self->es_store( Search::Elasticsearch->new( %$settings ) );
45  
46 }
47  
48 0 return $self->es_store;
49 }
50  
51 =head2 es_dispatch
52  
53 Collects dispatching Informations from called ElasticResultSet
54  
55 =cut
56  
57 0 ▶sub es_dispatch {
Subroutine es_dispatch: not called CC=2 SCAR=17.9
Pod documented
58  
59 0 my $self = shift;
60 0 my $class = shift;
61  
62 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
63  
64 0 return $self->dispatcher->{$class};
65 }
66  
67 =head2 es_is_registered_rs
68  
69 checks if ElasticResultSet is active for the Application
70  
71 =cut
72  
73 0 ▶sub es_is_registered_rs {
Subroutine es_is_registered_rs: not called CC=2 SCAR=17.9
Pod documented
74  
75 0 my ( $self, $rs ) = @_;
76  
77 0 ▶ return 1 if grep { $_ eq $rs } @{ $self->connect_elasticsearch->{registered_elastic_rs} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $rs;} @{$self->connect_elasticsearch->{"registered_elastic_rs"};} 0 0 0
78 }
79  
80 0 ▶sub es_index_name {
Subroutine es_index_name: not called CC=2 SCAR=17.9
Pod undocumented
81  
82 0 my $self = shift;
83 0 ▶ return $self->connect_elasticsearch->{index} || ref $self;
Condition0%0/3
$self->connect_elasticsearch->{'index'} || ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->connect_elasticsearch->{'index'} || ref $self
$self->connect_elasticsearch->{'index'} ref $self
Truth table0%0/3
A || B
A= $self->connect_elasticsearch->{'index'}B= ref $self
ABresult
1X1
011
000
84 }
85  
86 =head2 es_index_all
87  
88 Batch indexes all data in RDMS Storage for the registered ElasticResultSets
89  
90 =cut
91  
92 0 ▶sub es_index_all {
Subroutine es_index_all: not called CC=3 SCAR=24.8
Pod documented
93  
94 0 my $self = shift;
95  
96 0 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
97  
98 0 foreach my $rs (@$registered_elastic_rs) {
99  
100 0 eval "use $rs";
101  
102 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
103  
104 0 $rs->es_batch_index;
105 }
106  
107 }
108  
109 =head2 es_index_obj
110  
111 push a Document from BlackholeResults to the Index
112  
113 =cut
114  
115 0 ▶sub es_index_obj {
Subroutine es_index_obj: not called CC=2 SCAR=17.9
Pod documented
116  
117 0 my $self = shift;
118 0 my $obj = shift;
119  
120 0 my $additional = {};
121  
122 0 ▶ if ( $obj->{_parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"_parent"}) 0 0 0
123  
124 0 $additional->{parent} = delete $obj->{_parent};
125 }
126  
127 $self->es->index(
128 {
129 index => $obj->{type},
130 id => $obj->{body}->{es_id},
131 type => $obj->{type},
132 body => $obj->{body},
133 0 %$additional,
134 }
135 );
136 }
137  
138 0 ▶sub es_create_index {
Subroutine es_create_index: not called CC=1 SCAR=6.9
Pod undocumented
139  
140 0 my $self = shift;
141  
142 0 $self->es->indices->create( index => $self->es_index_name, );
143 }
144  
145 =head2 es_collect_mappings
146  
147 Collects mapping templates from the registered Resultsets and push them to the Elasticsearch Storage
148  
149 =cut
150  
151 0 ▶sub es_collect_mappings {
Subroutine es_collect_mappings: not called CC=3 SCAR=24.8
Pod documented
152  
153 0 my ($self) = @_;
154  
155 0 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
156  
157 0 foreach my $rs (@$registered_elastic_rs) {
158  
159 0 eval "use $rs";
160  
161 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
162  
163 0 my $mapping = $rs->mapping;
164  
165 0 $self->es->indices->put_template(
166 name => $rs->type,
167 body => $rs->mapping,
168  
169 );
170 }
171  
172 }
173  
174 =head2 drop_indexes
175  
176 drops all Elastic Indexes
177  
178 =cut
179  
180 0 ▶sub drop_indexes {
Subroutine drop_indexes: not called CC=4 SCAR=30.0
Pod documented
181  
182 0 my ($self) = shift;
183  
184 0 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
185  
186 0 my $deleted_index = {};
187  
188 0 foreach my $rs (@$registered_elastic_rs) {
189  
190 0 eval "use $rs";
191  
192 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
193  
194 0 ▶ if ( !$deleted_index->{ $rs->index_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$deleted_index{$rs->index_name}) 0 0 0
195  
196 0 $deleted_index->{ $rs->index_name } = 1;
197 0 $self->es->indices->delete(
198 index => $rs->index_name,
199 ignore => 404,
200 );
201 }
202 }
203  
204 }
205  
206 0 ▶sub es_drop_mapping {
Subroutine es_drop_mapping: not called CC=6 SCAR=37.4
Pod undocumented
207  
208 0 my $self = shift;
209  
210 0 my $types = [];
211 0 my @sources = $self->sources;
212  
213 0 for my $source (@sources) {
214  
215 0 my $rs = $self->resultset($source);
216 0 my $source_info = $rs->result_source->source_info;
217  
218 0 ▶ next unless $source_info && $source_info->{es_index_type} eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
unless $source_info and $source_info->{"es_index_type"} eq "primary" 0 0 0
Condition0%0/3
$source_info and $source_info->{"es_index_type"} eq "primary"
!ll&&!rl&&r
000
MC/DC0%0/2
$source_info and $source_info->{"es_index_type"} eq "primary"
$source_info $source_info->{"es_index_type"} eq "primary"
Truth table0%0/3
A and B
A= $source_infoB= $source_info->{"es_index_type"} eq "primary"
ABresult
0X0
100
111
219  
220 0 ▶ next unless $rs->can('es_has_searchable') && $rs->es_has_searchable;
Branch: 1 branch
BranchTrueFalseTotal
unless $rs->can("es_has_searchable") and $rs->es_has_searchable 0 0 0
Condition0%0/3
$rs->can("es_has_searchable") and $rs->es_has_searchable
!ll&&!rl&&r
000
MC/DC0%0/2
$rs->can("es_has_searchable") and $rs->es_has_searchable
$rs->can("es_has_searchable") $rs->es_has_searchable
Truth table0%0/3
A and B
A= $rs->can("es_has_searchable")B= $rs->es_has_searchable
ABresult
0X0
100
111
221  
222 0 warn "delete mapping $source";
223  
224 0 $self->es->indices->delete_mapping(
225 index => $self->es_index_name,
226 type => $source,
227 ignore => 404,
228 );
229 }
230  
231 }
232  
233 =head2 es_create_repository
234  
235 Creates repository for Elastic Snapshot API
236  
237 =cut
238  
239 0 ▶sub es_create_repository {
Subroutine es_create_repository: not called CC=3 SCAR=24.8
Pod documented
240  
241 0 my ( $self, $repository, $body ) = @_;
242  
243 0 ▶ return unless $repository;
Branch: 1 branch
BranchTrueFalseTotal
unless $repository 0 0 0
244  
245 0 ▶ $body = {} unless ref $body;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 0 0 0
246  
247 0 $self->es->snapshot->create_repository(
248 repository => $repository,
249 body => $body,
250 );
251 }
252  
253 =head2 es_create_snapshot
254  
255 creates a snapshot
256  
257 =cut
258  
259 0 ▶sub es_create_snapshot {
Subroutine es_create_snapshot: not called CC=4 SCAR=30.0
Pod documented
260  
261 0 my ( $self, $repository, $snapshot, $body ) = @_;
262  
263 0 ▶ return unless $repository && $snapshot;
Branch: 1 branch
BranchTrueFalseTotal
unless $repository and $snapshot 0 0 0
Condition0%0/3
$repository and $snapshot
!ll&&!rl&&r
000
MC/DC0%0/2
$repository and $snapshot
$repository $snapshot
Truth table0%0/3
A and B
A= $repositoryB= $snapshot
ABresult
0X0
100
111
264  
265 0 ▶ $body = {} unless ref $body;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 0 0 0
266  
267 0 $self->es->snapshot->create(
268 repository => $repository,
269 snapshot => $snapshot,
270 body => $body,
271 );
272 }
273  
274 =head2 es_restore_snapshot
275  
276 restores a snapshot
277  
278 =cut
279  
280 0 ▶sub es_restore_snapshot {
Subroutine es_restore_snapshot: not called CC=4 SCAR=30.0
Pod documented
281  
282 0 my ( $self, $repository, $snapshot, $body ) = @_;
283  
284 0 ▶ return unless $repository && $snapshot;
Branch: 1 branch
BranchTrueFalseTotal
unless $repository and $snapshot 0 0 0
Condition0%0/3
$repository and $snapshot
!ll&&!rl&&r
000
MC/DC0%0/2
$repository and $snapshot
$repository $snapshot
Truth table0%0/3
A and B
A= $repositoryB= $snapshot
ABresult
0X0
100
111
285  
286 0 ▶ $body = {} unless ref $body;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 0 0 0
287  
288 0 $self->es->snapshot->restore(
289 repository => $repository,
290 snapshot => $snapshot,
291 body => $body,
292 );
293 }
294  
295 1 1;
« ElasticSync/ResultSet.pm ↑ Summary ElasticSync/Role/ElasticBlackholeResult.pm »