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

filter: Statementstmt 0.0% 0 / 84 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 / 162 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 { host => "localhost", port => 9200, cxn => undef, debug => 0 },
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 my $debug = {};
38  
39 if ( $settings->{debug} ) {
40 $debug->{trace_to} = 'Stderr';
41  
42 eval "use Log::Any::Adapter qw(Stderr);";
43 }
44  
45 $self->es_store( Search::Elasticsearch->new( nodes => sprintf( '%s:%s', $settings->{host}, $settings->{port} ) ), log_to => 'Stderr', cxn => $settings->{cxn}, %$debug );
46  
47 }
48  
49 return $self->es_store;
50 }
51  
52 =head2 es_dispatch
53  
54 Collects dispatching Informations from called ElasticResultSet
55  
56 =cut
57  
58 sub es_dispatch {
59  
60 my $self = shift;
61 my $class = shift;
62  
63 return unless $class;
64  
65 return $self->dispatcher->{$class};
66 }
67  
68 =head2 es_is_registered_rs
69  
70 checks if ElasticResultSet is active for the Application
71  
72 =cut
73  
74 sub es_is_registered_rs {
75  
76 my ( $self, $rs ) = @_;
77  
78 return 1 if grep { $_ eq $rs } @{ $self->connect_elasticsearch->{registered_elastic_rs} };
79 }
80  
81 sub es_index_name {
82  
83 my $self = shift;
84 return $self->connect_elasticsearch->{index} || ref $self;
85 }
86  
87 =head2 es_index_all
88  
89 Batch indexes all data in RDMS Storage for the registered ElasticResultSets
90  
91 =cut
92  
93 sub es_index_all {
94  
95 my $self = shift;
96  
97 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
98  
99 foreach my $rs (@$registered_elastic_rs) {
100  
101 eval "use $rs";
102  
103 warn $@ if $@;
104  
105 $rs->es_batch_index;
106 }
107  
108 }
109  
110 =head2 es_index_obj
111  
112 push a Document from BlackholeResults to the Index
113  
114 =cut
115  
116 sub es_index_obj {
117  
118 my $self = shift;
119 my $obj = shift;
120  
121 my $additional = {};
122  
123 if ( $obj->{_parent} ) {
124  
125 $additional->{parent} = delete $obj->{_parent};
126 }
127  
128 $self->es->index(
129 {
130 index => $obj->{type},
131 id => $obj->{body}->{es_id},
132 type => $obj->{type},
133 body => $obj->{body},
134 %$additional,
135 }
136 );
137 }
138  
139 sub es_create_index {
140  
141 my $self = shift;
142  
143 $self->es->indices->create( index => $self->es_index_name, );
144 }
145  
146 =head2 es_collect_mappings
147  
148 Collects mapping templates from the registered Resultsets and push them to the Elasticsearch Storage
149  
150 =cut
151  
152 sub es_collect_mappings {
153  
154 my ($self) = @_;
155  
156 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
157  
158 foreach my $rs (@$registered_elastic_rs) {
159  
160 eval "use $rs";
161  
162 warn $@ if $@;
163  
164 my $mapping = $rs->mapping;
165  
166 $self->es->indices->put_template(
167 name => $rs->type,
168 body => $rs->mapping,
169  
170 );
171 }
172  
173 }
174  
175 =head2 drop_indexes
176  
177 drops all Elastic Indexes
178  
179 =cut
180  
181 sub drop_indexes {
182  
183 my ($self) = shift;
184  
185 my $registered_elastic_rs = $self->connect_elasticsearch->{registered_elastic_rs};
186  
187 my $deleted_index = {};
188  
189 foreach my $rs (@$registered_elastic_rs) {
190  
191 eval "use $rs";
192  
193 warn $@ if $@;
194  
195 if ( !$deleted_index->{ $rs->index_name } ) {
196  
197 $deleted_index->{ $rs->index_name } = 1;
198 $self->es->indices->delete(
199 index => $rs->index_name,
200 ignore => 404,
201 );
202 }
203 }
204  
205 }
206  
207 sub es_drop_mapping {
208  
209 my $self = shift;
210  
211 my $types = [];
212 my @sources = $self->sources;
213  
214 for my $source (@sources) {
215  
216 my $rs = $self->resultset($source);
217 my $source_info = $rs->result_source->source_info;
218  
219 next unless $source_info && $source_info->{es_index_type} eq 'primary';
220  
221 next unless $rs->can('es_has_searchable') && $rs->es_has_searchable;
222  
223 warn "delete mapping $source";
224  
225 $self->es->indices->delete_mapping(
226 index => $self->es_index_name,
227 type => $source,
228 ignore => 404,
229 );
230 }
231  
232 }
233  
234 =head2 es_create_repository
235  
236 Creates repository for Elastic Snapshot API
237  
238 =cut
239  
240 sub es_create_repository {
241  
242 my ( $self, $repository, $body ) = @_;
243  
244 return unless $repository;
245  
246 $body = {} unless ref $body;
247  
248 $self->es->snapshot->create_repository(
249 repository => $repository,
250 body => $body,
251 );
252 }
253  
254 =head2 es_create_snapshot
255  
256 creates a snapshot
257  
258 =cut
259  
260 sub es_create_snapshot {
261  
262 my ( $self, $repository, $snapshot, $body ) = @_;
263  
264 return unless $repository && $snapshot;
265  
266 $body = {} unless ref $body;
267  
268 $self->es->snapshot->create(
269 repository => $repository,
270 snapshot => $snapshot,
271 body => $body,
272 );
273 }
274  
275 =head2 es_restore_snapshot
276  
277 restores a snapshot
278  
279 =cut
280  
281 sub es_restore_snapshot {
282  
283 my ( $self, $repository, $snapshot, $body ) = @_;
284  
285 return unless $repository && $snapshot;
286  
287 $body = {} unless ref $body;
288  
289 $self->es->snapshot->restore(
290 repository => $repository,
291 snapshot => $snapshot,
292 body => $body,
293 );
294 }
295  
296 1;