| 1 | package Elastic::Model::Domain; | ||
| 2 | $Elastic::Model::Domain::VERSION = '0.52'; | ||
| 3 | use Carp; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use MooseX::Types::Moose qw(Maybe Str); | ||
| 7 | |||
| 8 | #=================================== | ||
| 9 | has 'name' => ( | ||
| 10 | #=================================== | ||
| 11 | isa => Str, | ||
| 12 | is => 'rw', | ||
| 13 | required => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | #=================================== | ||
| 17 | has 'namespace' => ( | ||
| 18 | #=================================== | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'Elastic::Model::Namespace', | ||
| 21 | required => 1, | ||
| 22 | handles => ['class_for_type'], | ||
| 23 | ); | ||
| 24 | |||
| 25 | #=================================== | ||
| 26 | has '_default_routing' => ( | ||
| 27 | #=================================== | ||
| 28 | isa => Maybe [Str], | ||
| 29 | is => 'ro', | ||
| 30 | lazy => 1, | ||
| 31 | builder => '_get_default_routing', | ||
| 32 | clearer => 'clear_default_routing', | ||
| 33 | ); | ||
| 34 | |||
| 35 | no Moose; | ||
| 36 | |||
| 37 | #=================================== | ||
| 38 | sub _get_default_routing { | ||
| 39 | #=================================== | ||
| 40 | my $self = shift; | ||
| 41 | my $name = $self->name; | ||
| 42 | my $aliases = $self->model->store->get_aliases( index => $name ); | ||
| 43 | |||
| 44 | croak "Domain ($name) doesn't exist either as an index or an alias" | ||
| 45 | unless %$aliases; | ||
| 46 | |||
| 47 | my @indices = keys %$aliases; | ||
| 48 | croak "Domain ($name) is an alias pointing at more than one index: " | ||
| 49 | . join( ", ", @indices ) | ||
| 50 | if @indices > 1; | ||
| 51 | |||
| 52 | my $index = shift @indices; | ||
| 53 | return '' if $index eq $name; | ||
| 54 | return $aliases->{$index}{aliases}{$name}{index_routing} || ''; | ||
| 55 | } | ||
| 56 | |||
| 57 | #=================================== | ||
| 58 | sub new_doc { | ||
| 59 | #=================================== | ||
| 60 | my $self = shift; | ||
| 61 | my $type = shift or croak "No type passed to new_doc"; | ||
| 62 | my $class = $self->class_for_type($type) or croak "Unknown type ($type)"; | ||
| 63 | |||
| 64 | my %params = ref $_[0] ? %{ shift() } : @_; | ||
| 65 | |||
| 66 | my $uid = Elastic::Model::UID->new( | ||
| 67 | index => $self->name, | ||
| 68 | type => $type, | ||
| 69 | routing => $self->_default_routing, | ||
| 70 | %params | ||
| 71 | ); | ||
| 72 | |||
| 73 | return $class->new( %params, uid => $uid, ); | ||
| 74 | } | ||
| 75 | |||
| 76 | #=================================== | ||
| 77 | sub create { shift->new_doc(@_)->save } | ||
| 78 | #=================================== | ||
| 79 | |||
| 80 | #=================================== | ||
| 81 | sub get { | ||
| 82 | #=================================== | ||
| 83 | my $self = shift; | ||
| 84 | |||
| 85 | my $type = shift or croak "No type passed to get()"; | ||
| 86 | my $id = shift or croak "No id passed to get()"; | ||
| 87 | my %args = @_; | ||
| 88 | my $uid = Elastic::Model::UID->new( | ||
| 89 | index => $self->name, | ||
| 90 | type => $type, | ||
| 91 | id => $id, | ||
| 92 | routing => delete $args{routing} || $self->_default_routing, | ||
| 93 | ); | ||
| 94 | $self->model->get_doc( uid => $uid, %args ); | ||
| 95 | } | ||
| 96 | |||
| 97 | #=================================== | ||
| 98 | sub try_get { shift->get( @_, ignore => 404 ) } | ||
| 99 | #=================================== | ||
| 100 | |||
| 101 | #=================================== | ||
| 102 | sub exists { | ||
| 103 | #=================================== | ||
| 104 | my $self = shift; | ||
| 105 | my $type = shift or croak "No type passed to exists()"; | ||
| 106 | my $id = shift or croak "No id passed to exists()"; | ||
| 107 | my %args = @_; | ||
| 108 | my $uid = Elastic::Model::UID->new( | ||
| 109 | index => $self->name, | ||
| 110 | type => $type, | ||
| 111 | id => $id, | ||
| 112 | routing => delete $args{routing} || $self->_default_routing, | ||
| 113 | ); | ||
| 114 | $self->model->doc_exists( uid => $uid, %args ); | ||
| 115 | } | ||
| 116 | |||
| 117 | #=================================== | ||
| 118 | sub delete { | ||
| 119 | #=================================== | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | my $type = shift or croak "No type passed to delete()"; | ||
| 123 | my $id = shift or croak "No id passed to delete()"; | ||
| 124 | my %args = @_; | ||
| 125 | |||
| 126 | my $uid = Elastic::Model::UID->new( | ||
| 127 | index => $self->name, | ||
| 128 | type => $type, | ||
| 129 | id => $id, | ||
| 130 | routing => delete $args{routing} || $self->_default_routing, | ||
| 131 | ); | ||
| 132 | $self->model->delete_doc( uid => $uid, %args ); | ||
| 133 | } | ||
| 134 | |||
| 135 | #=================================== | ||
| 136 | sub try_delete { shift->delete( @_, ignore => 404 ) } | ||
| 137 | #=================================== | ||
| 138 | |||
| 139 | #=================================== | ||
| 140 | sub view { | ||
| 141 | #=================================== | ||
| 142 | my $self = shift; | ||
| 143 | $self->model->view( domain => $self->name, @_ ); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | =pod | ||
| 149 | |||
| 150 | =encoding UTF-8 | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | Elastic::Model::Domain - The domain (index or alias) where your docs are stored. | ||
| 155 | |||
| 156 | =head1 VERSION | ||
| 157 | |||
| 158 | version 0.52 | ||
| 159 | |||
| 160 | =head1 SYNOPSIS | ||
| 161 | |||
| 162 | =head2 Get a domain instance | ||
| 163 | |||
| 164 | $domain = $model->domain('myapp'); | ||
| 165 | |||
| 166 | =head2 Create a new doc/object | ||
| 167 | |||
| 168 | $user = $domain->new_doc( user => \%args ); | ||
| 169 | $user->save; | ||
| 170 | |||
| 171 | # or: | ||
| 172 | |||
| 173 | $user = $domain->create( user => \%args); | ||
| 174 | |||
| 175 | =head2 Retrieve a doc by ID | ||
| 176 | |||
| 177 | $user = $domain->get( $type => $id ); | ||
| 178 | |||
| 179 | $user = $domain->try_get( $type => $id ); # return undef if missing | ||
| 180 | |||
| 181 | =head2 Check if a doc exists | ||
| 182 | |||
| 183 | $bool = $domain->exists( $type => $id ); | ||
| 184 | |||
| 185 | =head2 Delete a doc by ID | ||
| 186 | |||
| 187 | $uid = $domain->delete( $type => $id ); | ||
| 188 | |||
| 189 | $user = $domain->try_delete( $type => $id ); # return undef if missing | ||
| 190 | |||
| 191 | =head2 Create a view on the current domain | ||
| 192 | |||
| 193 | $view = $domain->view(%args); | ||
| 194 | |||
| 195 | =head1 DESCRIPTION | ||
| 196 | |||
| 197 | A "domain" is like a database handle used for CRUD (creating, updating or deleting) | ||
| 198 | individual objects or L<documents|Elastic::Manual::Terminology/Document>. | ||
| 199 | The C<< $domain->name >> can be the name of an | ||
| 200 | L<index|Elastic::Manual::Terminology/Index> or an | ||
| 201 | L<index alias|Elastic::Manual::Terminology/Alias>. A domain can only belong to | ||
| 202 | a single L<namespace|Elastic::Manual::Terminology/Namespace>. | ||
| 203 | |||
| 204 | B<NOTE:> If C<< $domain->name >> is an alias, it can only point to a single | ||
| 205 | index. | ||
| 206 | |||
| 207 | =head1 ATTRIBUTES | ||
| 208 | |||
| 209 | =head2 name | ||
| 210 | |||
| 211 | A C<< $domain->name >> must be either the name of an | ||
| 212 | L<index|Elastic::Manual::Terminology/Index> or of an | ||
| 213 | L<index alias|Elastic::Manual::Terminology/Alias> which points to a single | ||
| 214 | index. The index or alias must exist, and must be known to the | ||
| 215 | L<namespace|Elastic::Model::Namespace>. | ||
| 216 | |||
| 217 | =head2 namespace | ||
| 218 | |||
| 219 | The L<Elastic::Model::Namespace> object to which this domain belongs. | ||
| 220 | |||
| 221 | =head1 INSTANTIATOR | ||
| 222 | |||
| 223 | =head2 new() | ||
| 224 | |||
| 225 | $domain = $model->domain_class->new({ | ||
| 226 | name => $domain_name, | ||
| 227 | namespace => $namespace, | ||
| 228 | }); | ||
| 229 | |||
| 230 | Although documented here, you shouldn't need to call C<new()> yourself. | ||
| 231 | Instead you should use L<Elastic::Model::Role::Model/"domain()">: | ||
| 232 | |||
| 233 | $domain = $model->domain($domain_name); | ||
| 234 | |||
| 235 | =head1 METHODS | ||
| 236 | |||
| 237 | =head2 new_doc() | ||
| 238 | |||
| 239 | $doc = $domain->new_doc( $type => \%args ); | ||
| 240 | |||
| 241 | C<new_doc()> will create a new object in the class that maps to type C<$type>, | ||
| 242 | passing C<%args> to C<new()> in the associated class. For instance: | ||
| 243 | |||
| 244 | $user = $domain->new_doc( | ||
| 245 | user => { | ||
| 246 | id => 1, | ||
| 247 | name => 'Clint', | ||
| 248 | } | ||
| 249 | ); | ||
| 250 | |||
| 251 | =head2 create() | ||
| 252 | |||
| 253 | $doc = $domain->create( $type => \%args ); | ||
| 254 | |||
| 255 | This is the equivalent of: | ||
| 256 | |||
| 257 | $doc = $domain->new_doc( $type => \%args )->save(); | ||
| 258 | |||
| 259 | =head2 get() | ||
| 260 | |||
| 261 | $doc = $domain->get( $type => $id ); | ||
| 262 | $doc = $domain->get( $type => $id, routing => $routing, ... ); | ||
| 263 | |||
| 264 | Retrieves a doc of type C<$type> with ID C<$id> from index C<< $domain->name >> | ||
| 265 | or throws an exception if the doc doesn't exist. See | ||
| 266 | L<Elastic::Model::Role::Store/get_doc()> for more parameters which can be passed. | ||
| 267 | |||
| 268 | =head2 try_get() | ||
| 269 | |||
| 270 | $doc = $domain->try_get( $type => $id ); | ||
| 271 | $doc = $domain->try_get( $type => $id, routing => $routing, ... ); | ||
| 272 | |||
| 273 | Retrieves a doc of type C<$type> with ID C<$id> from index C<< $domain->name >> | ||
| 274 | or returns undef if the doc doesn't exist. | ||
| 275 | |||
| 276 | =head2 exists() | ||
| 277 | |||
| 278 | $bool = $domain->exists( $type => $id ); | ||
| 279 | $bool = $domain->exists( $type => $id, routing => $routing, ... ); | ||
| 280 | |||
| 281 | Checks if a doc of type C<$type> with ID C<$id> exists in | ||
| 282 | index C<< $domain->name >>. See L<Elastic::Model::Role::Store/doc_exists()> | ||
| 283 | for more parameters which can be passed. | ||
| 284 | |||
| 285 | =head2 delete() | ||
| 286 | |||
| 287 | $uid = $domain->delete( $type => $id ); | ||
| 288 | $uid = $domain->delete( $type => $id, routing => $routing, ... ); | ||
| 289 | |||
| 290 | Deletes a doc of type C<$type> with ID C<$id> from index C<< $domain->name >> | ||
| 291 | or throws an exception if the doc doesn't exist. See | ||
| 292 | L<Elastic::Model::Role::Store/delete_doc()> for more parameters which can be passed. | ||
| 293 | |||
| 294 | =head2 try_delete() | ||
| 295 | |||
| 296 | $uid = $domain->try_delete( $type => $id ); | ||
| 297 | $uid = $domain->try_delete( $type => $id, routing => $routing, ... ); | ||
| 298 | |||
| 299 | Deletes a doc of type C<$type> with ID C<$id> from index C<< $domain->name >> | ||
| 300 | or returns undef if the doc doesn't exist. | ||
| 301 | |||
| 302 | =head2 view() | ||
| 303 | |||
| 304 | $view = $domain->view(%args) | ||
| 305 | |||
| 306 | Creates a L<view|Elastic::Model::View> with the L<Elastic::Model::View/"domain"> | ||
| 307 | set to C<< $domain->name >>. A C<view> is used for searching docs in a | ||
| 308 | C<$domain>. | ||
| 309 | |||
| 310 | =head1 AUTHOR | ||
| 311 | |||
| 312 | Clinton Gormley <drtech@cpan.org> | ||
| 313 | |||
| 314 | =head1 COPYRIGHT AND LICENSE | ||
| 315 | |||
| 316 | This software is copyright (c) 2015 by Clinton Gormley. | ||
| 317 | |||
| 318 | This is free software; you can redistribute it and/or modify it under | ||
| 319 | the same terms as the Perl 5 programming language system itself. | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | __END__ |