Model/UID.pm

filter: Statementstmt 37.1% 23 / 62 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.7% 4 / 13 Podpod 100% 7 / 7 totaltotal 40.4% 34 / 84 CC 4 SCAR 14.2
CC
4
Cov
36%
_params
12.0
update_from_store
6.0
new_from_store
2.0
CRAP 4.1
« Model/TypeMap/Moose.pm ↑ Summary Model/Namespace.pm »
1 package Elastic::Model::UID;
2 23 $Elastic::Model::UID::VERSION = '0.52';
3 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use MooseX::Types::Moose qw(Str Int Maybe Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #===================================
8 23 has index => (
9 #===================================
10 is => 'ro',
11 isa => Str,
12 required => 1,
13 writer => '_index'
14 );
15  
16 #===================================
17 23 has type => (
18 #===================================
19 is => 'ro',
20 isa => Str,
21 required => 1
22 );
23  
24 #===================================
25 23 has id => (
26 #===================================
27 is => 'ro',
28 isa => Str,
29 writer => '_id'
30 );
31  
32 #===================================
33 23 has version => (
34 #===================================
35 is => 'ro',
36 isa => Int,
37 writer => '_version'
38 );
39  
40 #===================================
41 23 has routing => (
42 #===================================
43 is => 'ro',
44 isa => Maybe [Str],
45 writer => '_routing'
46 );
47  
48 #===================================
49 23 has from_store => (
50 #===================================
51 is => 'ro',
52 isa => Bool,
53 writer => '_from_store'
54 );
55  
56 #===================================
57 23 has 'is_partial' => (
58 #===================================
59 is => 'ro',
60 isa => Bool,
61 );
62  
63 #===================================
64 23 has cache_key => (
65 #===================================
66 is => 'ro',
67 isa => Str,
68 lazy => 1,
69 builder => '_build_cache_key',
70 clearer => '_clear_cache_key',
71 );
72  
73 23 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 #===================================
76 0 ▶sub new_from_store {
Subroutine new_from_store: not called CC=1 SCAR=6.9
Pod documented
77 #===================================
78 0 my $class = shift;
79 0 my %params = %{ shift() };
80 $class->new(
81 from_store => 1,
82 routing => $params{fields}{_routing},
83 0 map { $_ => $params{"_$_"} } qw(index type id version)
84 );
85 }
86  
87 #===================================
88 0 ▶sub new_partial {
Subroutine new_partial: not called CC=1 SCAR=6.9
Pod documented
89 #===================================
90 0 my $class = shift;
91 0 my %params = %{ shift() };
92 $class->new(
93 from_store => 1,
94 is_partial => 1,
95 routing => $params{fields}{_routing},
96 0 map { $_ => $params{"_$_"} } qw(index type id version)
97 );
98 }
99  
100 #===================================
101 0 ▶sub update_from_store {
Subroutine update_from_store: not called CC=2 SCAR=17.9
Pod documented
102 #===================================
103 0 my $self = shift;
104 0 my $params = shift;
105 0 $self->$_( $params->{$_} ) for qw(_index _id _version);
106 0 $self->_from_store(1);
107 0 $self->_clear_cache_key;
108 0 $self;
109 }
110  
111 #===================================
112 0 ▶sub update_from_uid {
Subroutine update_from_uid: not called CC=1 SCAR=6.9
Pod documented
113 #===================================
114 0 my $self = shift;
115 0 my $uid = shift;
116 0 $self->_index( $uid->index );
117 0 $self->_routing( $uid->routing );
118 0 $self->_version( $uid->version );
119 0 $self->_from_store(1);
120 0 $self->_clear_cache_key;
121 0 $self;
122 }
123  
124 #===================================
125 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
126 #===================================
127 0 my $self = shift;
128 0 bless {%$self}, ref $self;
129 }
130  
131 #===================================
132 0 ▶sub read_params { shift->_params(qw(index type id routing)) }
Subroutine read_params: not called CC=1 SCAR=6.9
Pod documented
133 0 ▶sub write_params { shift->_params(qw(index type id routing version)) }
Subroutine write_params: not called CC=1 SCAR=6.9
Pod documented
134 #===================================
135  
136 #===================================
137 0 ▶sub _params {
Subroutine _params: not called CC=3 SCAR=24.8
138 #===================================
139 0 my $self = shift;
140 0 my %vals;
141 0 for (@_) {
142 0 ▶ my $val = $self->$_ or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $self->$_ or next 0 0 0
143 0 $vals{$_} = $val;
144 }
145 0 return \%vals;
146 }
147  
148 #===================================
149 0 ▶sub _build_cache_key {
Subroutine _build_cache_key: not called CC=1 SCAR=6.9
150 #===================================
151 0 my $self = shift;
152 0 return join ";", map { s/;/;;/g; $_ } map { $self->$_ } qw(type id);
153 }
154  
155 23 __PACKAGE__->meta->make_immutable;
156  
157 23 1;
158  
159 =pod
160  
161 =encoding UTF-8
162  
163 =head1 NAME
164  
165 Elastic::Model::UID - The Unique ID of a document in an Elasticsearch cluster
166  
167 =head1 VERSION
168  
169 version 0.52
170  
171 =head1 SYNOPSIS
172  
173 $doc = $domain->new_doc(
174 $type => {
175 id => $id, # optional
176 routing => $routing, # optional
177 ....
178 }
179 );
180  
181 $doc = $domain->get( $type => $id );
182 $doc = $domain->get( $type => $id, routing => $routing );
183  
184 $uid = $doc->uid;
185  
186 $index = $uid->index;
187 $type = $uid->type;
188 $id = $uid->id;
189 $version = $uid->version;
190 $routing = $uid->routing;
191  
192 =head1 DESCRIPTION
193  
194 To truly identify a document as unique in Elasticsearch, you need to know
195 the L<index|Elastic::Model::Terminology/Index> where it is stored, the
196 L<type|Elastic::Model::Terminology/Type> of the document, its
197 L<id|Elastic::Model::Terminology/ID>, and
198 possibly its L<routing|Elastic::Model::Terminology/Routing> value (which
199 defaults to the ID). Also, each object
200 has a L</version> number which is incremented on every change.
201 L<Elastic::Model::UID> wraps up all of these details into an object.
202  
203 =head1 ATTRIBUTES
204  
205 =head2 index
206  
207 The L<index|Elastic::Model::Terminology/Index> (or
208 L<domain|Elastic::Model::Terminology/Domain>) name. When you create a new
209 document, its UID will set C<index> to C<< $domain->name >>, which may be an index
210 or an L<index alias|Elastic::Manual::Terminology/Alias>. However, when you
211 save the document, the L</index> will be updated to reflect the actual index
212 name.
213  
214 =head2 type
215  
216 The L<type|Elastic::Model::Terminology/Type> of the document, eg C<user>.
217  
218 =head2 id
219  
220 The string L<id|Elastic::Model::Terminology/ID> of the document - if not set
221 when creating a new document, then a unique ID is auto-generated when the
222 document is saved.
223  
224 =head2 routing
225  
226 The L<routing|Elastic::Model::Terminology/Routing> string is used to determine
227 in which shard the document lives. If not specified, then Elasticsearch
228 generates a routing value using a hash of the ID. If you use a custom
229 routing value, then you can't change that value as the new routing B<may>
230 point to a new shard. Instead, you should delete the old doc, and create a
231 new doc with the new routing value.
232  
233 =head2 version
234  
235 The version is an integer representing the current version of the document.
236 Each write operation will increment the C<version>, and attempts to update
237 an older version of the document will throw an exception.
238  
239 =head2 from_store
240  
241 A boolean value indicating whether the C<UID> was loaded from Elasticsearch
242 (C<true>) or created via L</"new()">.
243  
244 =head2 is_partial
245  
246 A boolean value indicating whether the C<UID> represents a partial or full
247 object. Partial objects cannot be saved.
248  
249 =head2 cache_key
250  
251 A generated string combining the L</"type"> and the L</"id">
252  
253 =head1 METHODS
254  
255 =head2 new()
256  
257 $uid = Elastic::Model::Uid->new(
258 index => $domain->name, # required
259 type => $type, # required
260 id => $id, # optional
261 routing => $routing, # optional
262 );
263  
264 Creates a new UID with L</"from_store"> set to false.
265  
266 =head2 new_from_store()
267  
268 $uid = Elastic::Model::UID->new_from_store(
269 _index => $index,
270 _type => $type,
271 _id => $id,
272 _version => $version,
273 fields => { routing => $routing }
274 );
275  
276 This is called when creating a new UID for a doc that has been loaded
277 from Elasticsearch. You shouldn't need to use this method directly.
278  
279 =head2 new_partial()
280  
281 $uid = Elastic::Model::UID->new_partial(
282 _index => $index,
283 _type => $type,
284 _id => $id,
285 _version => $version,
286 fields => { routing => $routing }
287 );
288  
289 This is called when creating a new UID for a partial doc that has been loaded
290 from Elasticsearch. You shouldn't need to use this method directly.
291  
292 =head2 clone()
293  
294 $new_uid = $uid->clone();
295  
296 Clones an existing UID.
297  
298 =head2 update_from_store()
299  
300 $uid->update_from_store(
301 _index => $index,
302 _id => $id,
303 _version => $version,
304 );
305  
306 When a doc is saved, we update the UID to use the real index name (as opposed
307 to an alias or domain name), the ID (in case it has been auto-generated)
308 and the current version number. It also sets the L</"from_store"> attribute
309 to C<true>. You shouldn't need to use this method directly.
310  
311 =head2 update_from_uid()
312  
313 $uid->update_from_uid($new_uid);
314  
315 Updates the L</"index">, L</"routing"> and L</"id"> parameters of one UID
316 from a newer UID. You shouldn't need to use this method directly.
317  
318 =head2 read_params()
319  
320 $params = $uid->read_params()
321  
322 Returns a hashref containing L</"index">, L</"type">, L</"id"> and L</"routing">
323 values.
324  
325 =head2 write_params()
326  
327 $params = $uid->write_params()
328  
329 Returns a hashref containing L</"index">, L</"type">, L</"id">, L</"routing">
330 and L</"version"> values.
331  
332 =head1 AUTHOR
333  
334 Clinton Gormley <drtech@cpan.org>
335  
336 =head1 COPYRIGHT AND LICENSE
337  
338 This software is copyright (c) 2015 by Clinton Gormley.
339  
340 This is free software; you can redistribute it and/or modify it under
341 the same terms as the Perl 5 programming language system itself.
342  
343 =cut
344  
345 __END__
« Model/TypeMap/Moose.pm ↑ Summary Model/Namespace.pm »