Model/Namespace.pm

filter: Statementstmt 53.0% 26 / 49 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 53.8% 7 / 13 Podpod 100% 5 / 5 totaltotal 48.1% 38 / 79 CC 5 SCAR 12.4
CC
5
Cov
46%
all_domains
6.0
index
6.0
alias
6.0
CRAP 3.5
« Model/UID.pm ↑ Summary Model/Results.pm »
1 package Elastic::Model::Namespace;
2 23 $Elastic::Model::Namespace::VERSION = '0.52';
3 23 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use MooseX::Types::Moose qw(Str HashRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Elastic::Model::Index();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Elastic::Model::Alias();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #===================================
11 23 has 'name' => (
12 #===================================
13 is => 'ro',
14 isa => Str,
15 required => 1
16 );
17  
18 #===================================
19 23 has 'types' => (
20 #===================================
21 is => 'ro',
22 isa => HashRef,
23 traits => ['Hash'],
24 builder => '_build_types',
25 handles => {
26 class_for_type => 'get',
27 all_types => 'keys'
28 },
29 );
30  
31 #===================================
32 has 'fixed_domains' => (
33 #===================================
34 is => 'ro',
35 isa => ArrayRef [Str],
36 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 23 );
38  
39 23 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 #===================================
42 0 ▶sub all_domains {
Subroutine all_domains: not called CC=2 SCAR=17.9
Pod documented
43 #===================================
44 0 my $self = shift;
45 0 my @domains = ( $self->name, @{ $self->fixed_domains } );
46 0 my $aliases = $self->model->store->get_aliases( index => \@domains );
47 0 for ( keys %$aliases ) {
48 0 push @domains, ( $_, keys %{ $aliases->{$_}{aliases} } );
49 }
50 0 return uniq @domains;
51 }
52  
53 #===================================
54 0 ▶sub all_live_indices {
Subroutine all_live_indices: not called CC=1 SCAR=6.9
Pod documented
55 #===================================
56 0 my $self = shift;
57 0 my @domains = ( $self->name, @{ $self->fixed_domains } );
58 0 my $aliases = $self->model->store->get_aliases( index => \@domains );
59 0 return keys %$aliases;
60 }
61  
62 #===================================
63 0 ▶sub index {
Subroutine index: not called CC=2 SCAR=17.9
Pod documented
64 #===================================
65 0 my $self = shift;
66 0 ▶ Elastic::Model::Index->new(
Condition0%0/3
shift() || $self->name
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->name
shift() $self->name
Truth table0%0/3
A || B
A= shift()B= $self->name
ABresult
1X1
011
000
67 namespace => $self,
68 name => shift() || $self->name
69 );
70 }
71  
72 #===================================
73 0 ▶sub alias {
Subroutine alias: not called CC=2 SCAR=17.9
Pod documented
74 #===================================
75 0 my $self = shift;
76 0 ▶ Elastic::Model::Alias->new(
Condition0%0/3
shift() || $self->name
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->name
shift() $self->name
Truth table0%0/3
A || B
A= shift()B= $self->name
ABresult
1X1
011
000
77 namespace => $self,
78 name => shift() || $self->name
79 );
80 }
81  
82 #===================================
83 0 ▶sub mappings {
Subroutine mappings: not called CC=2 SCAR=17.9
Pod documented
84 #===================================
85 0 my $self = shift;
86 0 ▶ my @types = @_ == 0 ? $self->all_types : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 0 0 0
87 0 my $model = $self->model;
88 0 +{ map { $_ => $model->map_class( $self->class_for_type($_) ) } @types };
89 }
90  
91 23 1;
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 Elastic::Model::Namespace - Class-to-type map
100  
101 =head1 VERSION
102  
103 version 0.52
104  
105 =head1 SYNOPSIS
106  
107 =head2 Namespace declaration
108  
109 package MyApp;
110  
111 use Elastic::Model;
112  
113 has_namespace 'myapp' => {
114 user => 'MyApp::User',
115 post => 'MyApp::Post',
116 };
117  
118 no Elastic::Model;
119  
120 =head2 Using the namespace
121  
122 $namespace = $model->namespace('myapp');
123  
124 $index = $namespace->index($index_name);
125 $alias = $namespace->alias($alias_name);
126  
127 $name = $namespace->name;
128 @domains = $namespace->all_domains
129 \%types = $namespace->types;
130 @types = $namespace->all_types;
131 \%mappings = $namespace->mappings( @types );
132  
133 =head1 DESCRIPTION
134  
135 L<Elastic::Model::Namespace> maps your doc classes to
136 L<types|Elastic::Manual::Terminology/Type> (like a database table) in
137 Elasticsearch. For instance, you could map your class
138 C<MyApp::User> to type C<user>.
139  
140 This class <=> type mapping is applied to all
141 L<domains|Elastic::Manual::Terminology/Domain> known to the Namespace.
142 Each L<domain|Elastic::Model::Domain> has a single Namespace, and all documents
143 stored in that C<domain> (L<index|Elastic::Manual::Terminology/Index> or
144 L<index alias|Elastic::Manual::Terminology/Alias>) are handled by
145 the same Namespace. A C<namespace>/C<type>/C<id> combination must be
146 unique across all domains associated with a Namespace.
147  
148 A Namespace "knows" about Domains via the L</name> attribute. Either you can
149 have a single index called C<< $namespace->name >>, OR an alias called
150 C<< $namespace->name >> which points to multiple indices. Additional
151 domains (indices or aliases) can be specified with L</fixed_domains>
152  
153 Namespaces are also used to create, update or delete L<indices|/index()>
154 or L<aliases/alias()>.
155  
156 See L<Elastic::Manual::Intro>, L<Elastic::Model> for how to declare your
157 namespaces, and L<Elastic::Manual::Scaling> for more about
158 namespaces, indices and aliases.
159  
160 =head1 ATTRIBUTES
161  
162 =head2 name
163  
164 $name = $namespace->name
165  
166 The C<name> of the namespace. This attribute is important! It is used
167 in a couple of places:
168  
169 =head3 As the "main domain" name
170  
171 A L<domain|Elastic::Model::Domain> is like a database handle - you need
172 a domain to create, retrieve, update and deleted individual docs from
173 Elasticsearch. The L<domain name|Elastic::Model::Domain/name> can be an
174 L<index|Elastic::Manual::Terminology/Index> or an
175 L<alias|Elastic::Manual::Terminology/Alias>.
176 Several domains (indices/aliases) can be associated with a namespace.
177 The easiest way to do this it to make the "main domain name"
178 (ie the namespace L</name>) an alias which points to all the indices in that
179 namespace.
180  
181 See L<Elastic::Manual::Scaling/Namespaces, domains, aliases and indices>
182 L</fixed_domains> and L</all_domains()> for more.
183  
184 =head3 As the scope name
185  
186 A L<scope|Elastic::Model::Scope> is an optional in-memory cache. The cache ID
187 uses the object's L<type|Elastic::Manual::Terminology/Type>,
188 L<ID|Elastic::Manual::Terminology/ID> and namespace L</name> to group objects,
189 so the ID must be unique across all indices in a namespace.
190  
191 =head2 types
192  
193 =head3 all_types
194  
195 =head3 class_for_type
196  
197 \%types = $namespace->types
198 @type_names = $namespace->all_types
199 $class = $namespace->class_for_type($type_name)
200  
201 A hashref whose keys are the type names in Elasticsearch, and whose
202 values are wrapped doc classes, eg the class C<MyApp::User>
203 wrapped by L<Elastic::Model::Role::Model/wrap_doc_class()>.
204  
205 =head2 fixed_domains
206  
207 \@fixed_domains = $namespace->fixed_domains;
208  
209 While the preferred method for associating domains with a namespace is via
210 an alias named after the namespace L</name>, you can include a list of other
211 domains (indices or aliases) in the namespace declaration:
212  
213 has_namespace 'myapp' => {
214 user => 'MyApp::User'
215 },
216 fixed_domains => ['index_1','alias_2'];
217  
218 See L<Elastic::Manual::Scaling/Namespaces, domains, aliases and indices>
219 L</name> and L</all_domains()> for more.
220  
221 =head1 METHODS
222  
223 =head2 index()
224  
225 $index = $namespace->index;
226 $index = $namespace->index('index_name');
227  
228 Creates an L<Elastic::Model::Index> object for creating and administering indices
229 in the Elasticsearch cluster. The C<$index> L<name|Elastic::Model::Index/name>
230 is either the L</name> of the C<$namespace> or the value passed in to L</index()>.
231  
232 =head2 alias()
233  
234 $alias = $namespace->alias;
235 $alias = $namespace->alias('alias_name');
236  
237 Creates an L<Elastic::Model::Alias> object for creating and administering index
238 aliases in the Elasticsearch cluster. The C<$alias> L<name|Elastic::Model::Alias/name>
239 is either the L</name> of the C<$namespace> or the value passed in to L</alias()>.
240  
241 =head2 all_domains()
242  
243 @domains = $namespace->all_domains();
244  
245 Returns all domain names known to the namespace. It does this by retrieving
246 all indices and aliases associated with the namespace L</name> and the
247 L</fixed_domains> (if any).
248  
249 =head2 all_live_indices()
250  
251 @indices = $namespace->all_live_indices();
252  
253 Queries Elasticsearch to find all existing indices related to the namespace
254  
255 =head2 mappings()
256  
257 \%mapping = $namespace->mappings();
258 \%mapping = $namespace->mappings(@type_names);
259  
260 Generates the type mappings for the specified list of types, or, for all
261 types known to the namespace if not specified.
262  
263 =head1 SEE ALSO
264  
265 =over
266  
267 =item *
268  
269 L<Elastic::Manual::Intro>
270  
271 =item *
272  
273 L<Elastic::Model::Domain>
274  
275 =item *
276  
277 L<Elastic::Model::Index>
278  
279 =item *
280  
281 L<Elastic::Model::Alias>
282  
283 =back
284  
285 =head1 AUTHOR
286  
287 Clinton Gormley <drtech@cpan.org>
288  
289 =head1 COPYRIGHT AND LICENSE
290  
291 This software is copyright (c) 2015 by Clinton Gormley.
292  
293 This is free software; you can redistribute it and/or modify it under
294 the same terms as the Perl 5 programming language system itself.
295  
296 =cut
297  
298 __END__
« Model/UID.pm ↑ Summary Model/Results.pm »