File Coverage

blib/lib/Elastic/Model/UID.pm
Criterion Covered Total %
statement 12 51 23.5
branch 0 2 0.0
condition n/a
subroutine 4 13 30.7
pod 7 7 100.0
total 23 73 31.5


line stmt bran cond sub pod time code
1             package Elastic::Model::UID;
2             $Elastic::Model::UID::VERSION = '0.52';
3 23     23   131 use Moose;
  23         74  
  23         228  
4 23     23   152852 use MooseX::Types::Moose qw(Str Int Maybe Bool);
  23         55  
  23         233  
5 23     23   128896 use namespace::autoclean;
  23         45  
  23         265  
6              
7             #===================================
8             has index => (
9             #===================================
10                 is => 'ro',
11                 isa => Str,
12                 required => 1,
13                 writer => '_index'
14             );
15              
16             #===================================
17             has type => (
18             #===================================
19                 is => 'ro',
20                 isa => Str,
21                 required => 1
22             );
23              
24             #===================================
25             has id => (
26             #===================================
27                 is => 'ro',
28                 isa => Str,
29                 writer => '_id'
30             );
31              
32             #===================================
33             has version => (
34             #===================================
35                 is => 'ro',
36                 isa => Int,
37                 writer => '_version'
38             );
39              
40             #===================================
41             has routing => (
42             #===================================
43                 is => 'ro',
44                 isa => Maybe [Str],
45                 writer => '_routing'
46             );
47              
48             #===================================
49             has from_store => (
50             #===================================
51                 is => 'ro',
52                 isa => Bool,
53                 writer => '_from_store'
54             );
55              
56             #===================================
57             has 'is_partial' => (
58             #===================================
59                 is => 'ro',
60                 isa => Bool,
61             );
62              
63             #===================================
64             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     23   5881 no Moose;
  23         52  
  23         158  
74              
75             #===================================
76             sub new_from_store {
77             #===================================
78 0     0 1       my $class = shift;
79 0               my %params = %{ shift() };
  0            
80                 $class->new(
81                     from_store => 1,
82                     routing => $params{fields}{_routing},
83 0                   map { $_ => $params{"_$_"} } qw(index type id version)
  0            
84                 );
85             }
86              
87             #===================================
88             sub new_partial {
89             #===================================
90 0     0 1       my $class = shift;
91 0               my %params = %{ shift() };
  0            
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)
  0            
97                 );
98             }
99              
100             #===================================
101             sub update_from_store {
102             #===================================
103 0     0 1       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             sub update_from_uid {
113             #===================================
114 0     0 1       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             sub clone {
126             #===================================
127 0     0 1       my $self = shift;
128 0               bless {%$self}, ref $self;
129             }
130              
131             #===================================
132 0     0 1   sub read_params { shift->_params(qw(index type id routing)) }
133 0     0 1   sub write_params { shift->_params(qw(index type id routing version)) }
134             #===================================
135              
136             #===================================
137             sub _params {
138             #===================================
139 0     0         my $self = shift;
140 0               my %vals;
141 0               for (@_) {
142 0 0                 my $val = $self->$_ or next;
143 0                   $vals{$_} = $val;
144                 }
145 0               return \%vals;
146             }
147              
148             #===================================
149             sub _build_cache_key {
150             #===================================
151 0     0         my $self = shift;
152 0               return join ";", map { s/;/;;/g; $_ } map { $self->$_ } qw(type id);
  0            
  0            
  0            
153             }
154              
155             __PACKAGE__->meta->make_immutable;
156              
157             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__
346            
347             # ABSTRACT: The Unique ID of a document in an Elasticsearch cluster
348            
349