blib/lib/DBIx/Class/ElasticSync/Role/ElasticSchema.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 161 CC 18 SCAR 21.6
CC
18
Cov
0%
es
12.0
drop_indexes
12.0
es_drop_mapping
12.0
CRAP 8.7
1 package DBIx::Class::ElasticSync::Role::ElasticSchema;
2  
3 use strict;
4 use warnings;
5  
6 use Moose::Role;
7  
8 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 default => sub { },
18 );
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 sub es {
30  
31 my ($self) = @_;
32  
33 if ( !$self->es_store ) {
34  
35 my $settings = $self->connect_elasticsearch;
36  
37 if ( $settings->{debug} ) {
38  
39 delete $settings->{debug};
40 $settings->{trace_to} = 'Stderr';
41 eval "use Log::Any::Adapter qw(Stderr);";
42 }
43  
44 $self->es_store( Search::Elasticsearch->new( %$settings );
45  
46 }
47  
48 return $self->es_store;
49 }
50  
51 =head2 es_dispatch
52  
53 Collects dispatching Informations from called ElasticResultSet
54  
55 =cut
56  
57 sub es_dispatch {
58  
59 my $self = shift;
60 my $class = shift;
61  
62 return unless $class;
63  
64 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 sub es_is_registered_rs {
74  
75 my ( $self, $rs ) = @_;
76  
77 return 1 if grep { $_ eq $rs } @{ $self->connect_elasticsearch->{registered_elastic_rs} };
78 }
79  
80 sub es_index_name {
81  
82 my $self = shift;
83 return $self->connect_elasticsearch->{index} || ref $self;
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 sub es_index_all {
93  
94 my $self = shift;
95  
96 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
97  
98 foreach my $rs (@$registered_elastic_rs) {
99  
100 eval "use $rs";
101  
102 warn $@ if $@;
103  
104 $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 sub es_index_obj {
116  
117 my $self = shift;
118 my $obj = shift;
119  
120 my $additional = {};
121  
122 if ( $obj->{_parent} ) {
123  
124 $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 %$additional,
134 }
135 );
136 }
137  
138 sub es_create_index {
139  
140 my $self = shift;
141  
142 $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 sub es_collect_mappings {
152  
153 my ($self) = @_;
154  
155 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
156  
157 foreach my $rs (@$registered_elastic_rs) {
158  
159 eval "use $rs";
160  
161 warn $@ if $@;
162  
163 my $mapping = $rs->mapping;
164  
165 $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 sub drop_indexes {
181  
182 my ($self) = shift;
183  
184 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
185  
186 my $deleted_index = {};
187  
188 foreach my $rs (@$registered_elastic_rs) {
189  
190 eval "use $rs";
191  
192 warn $@ if $@;
193  
194 if ( !$deleted_index->{ $rs->index_name } ) {
195  
196 $deleted_index->{ $rs->index_name } = 1;
197 $self->es->indices->delete(
198 index => $rs->index_name,
199 ignore => 404,
200 );
201 }
202 }
203  
204 }
205  
206 sub es_drop_mapping {
207  
208 my $self = shift;
209  
210 my $types = [];
211 my @sources = $self->sources;
212  
213 for my $source (@sources) {
214  
215 my $rs = $self->resultset($source);
216 my $source_info = $rs->result_source->source_info;
217  
218 next unless $source_info && $source_info->{es_index_type} eq 'primary';
219  
220 next unless $rs->can('es_has_searchable') && $rs->es_has_searchable;
221  
222 warn "delete mapping $source";
223  
224 $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 sub es_create_repository {
240  
241 my ( $self, $repository, $body ) = @_;
242  
243 return unless $repository;
244  
245 $body = {} unless ref $body;
246  
247 $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 sub es_create_snapshot {
260  
261 my ( $self, $repository, $snapshot, $body ) = @_;
262  
263 return unless $repository && $snapshot;
264  
265 $body = {} unless ref $body;
266  
267 $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 sub es_restore_snapshot {
281  
282 my ( $self, $repository, $snapshot, $body ) = @_;
283  
284 return unless $repository && $snapshot;
285  
286 $body = {} unless ref $body;
287  
288 $self->es->snapshot->restore(
289 repository => $repository,
290 snapshot => $snapshot,
291 body => $body,
292 );
293 }
294  
295 1;