| 1 | package Zing::Search; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Entity'; | ||
| 15 | |||
| 16 | our $VERSION = '0.26'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has 'handle' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | def => '*', | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'symbol' => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'Str', | ||
| 29 | def => '*', | ||
| 30 | ); | ||
| 31 | |||
| 32 | has 'bucket' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Str', | ||
| 35 | def => '*', | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'system' => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Name', | ||
| 41 | def => 'zing', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has 'target' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Str', | ||
| 47 | def => '*', | ||
| 48 | ); | ||
| 49 | |||
| 50 | has 'store' => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => 'Store', | ||
| 53 | new => 1, | ||
| 54 | ); | ||
| 55 | |||
| 56 | fun new_store($self) { | ||
| 57 | $self->app->store | ||
| 58 | } | ||
| 59 | |||
| 60 | # METHODS | ||
| 61 | |||
| 62 | method any() { | ||
| 63 | return $self->where( | ||
| 64 | bucket => '*', | ||
| 65 | symbol => '*', | ||
| 66 | handle => '*', | ||
| 67 | target => '*', | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | method for(Str $type) { | ||
| 72 | return $self->where( | ||
| 73 | bucket => '*', | ||
| 74 | symbol => lc($type), | ||
| 75 | handle => $self->env->handle || '*', | ||
| 76 | target => $self->env->target || '*', | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | method objects() { | ||
| 81 | my $result = []; | ||
| 82 | |||
| 83 | $self->process(fun($term) { | ||
| 84 | push @$result, $self->app->term($term)->object; | ||
| 85 | }); | ||
| 86 | |||
| 87 | return $result; | ||
| 88 | } | ||
| 89 | |||
| 90 | method process(CodeRef $callback) { | ||
| 91 | for my $term (@{$self->results}) { | ||
| 92 | $callback->($term); | ||
| 93 | } | ||
| 94 | |||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | method query() { | ||
| 99 | my $system = $self->system || '*'; | ||
| 100 | my $handle = $self->handle || '*'; | ||
| 101 | my $target = $self->target || '*'; | ||
| 102 | my $symbol = $self->symbol || '*'; | ||
| 103 | my $bucket = $self->bucket || '*'; | ||
| 104 | |||
| 105 | return lc join ':', $system, $handle, $target, $symbol, $bucket; | ||
| 106 | } | ||
| 107 | |||
| 108 | method results() { | ||
| 109 | return $self->store->keys($self->query); | ||
| 110 | } | ||
| 111 | |||
| 112 | method using(Repo $repo) { | ||
| 113 | my $term = $self->app->term($repo); | ||
| 114 | |||
| 115 | return $self->where( | ||
| 116 | bucket => $term->bucket, | ||
| 117 | handle => $term->handle, | ||
| 118 | symbol => $term->symbol, | ||
| 119 | system => $term->system, | ||
| 120 | target => $term->target, | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | method where(Str %args) { | ||
| 125 | $self->$_($args{$_}) for keys %args; | ||
| 126 | |||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | =encoding utf8 | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | Zing::Search - Search Abstraction | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | =head1 ABSTRACT | ||
| 141 | |||
| 142 | Storage Search Abstraction | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | =head1 SYNOPSIS | ||
| 147 | |||
| 148 | use Zing::Search; | ||
| 149 | |||
| 150 | my $search = Zing::Search->new; | ||
| 151 | |||
| 152 | # $search->query; | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | =head1 DESCRIPTION | ||
| 157 | |||
| 158 | This package provides a storage search abstraction. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | =head1 INHERITS | ||
| 163 | |||
| 164 | This package inherits behaviors from: | ||
| 165 | |||
| 166 | L<Zing::Entity> | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | =head1 LIBRARIES | ||
| 171 | |||
| 172 | This package uses type constraints from: | ||
| 173 | |||
| 174 | L<Zing::Types> | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | =head1 ATTRIBUTES | ||
| 179 | |||
| 180 | This package has the following attributes: | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | =head2 bucket | ||
| 185 | |||
| 186 | bucket(Str) | ||
| 187 | |||
| 188 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | =head2 handle | ||
| 193 | |||
| 194 | handle(Str) | ||
| 195 | |||
| 196 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | =head2 store | ||
| 201 | |||
| 202 | store(Store) | ||
| 203 | |||
| 204 | This attribute is read-only, accepts C<(Store)> values, and is optional. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | =head2 symbol | ||
| 209 | |||
| 210 | symbol(Str) | ||
| 211 | |||
| 212 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | =head2 system | ||
| 217 | |||
| 218 | system(Name) | ||
| 219 | |||
| 220 | This attribute is read-only, accepts C<(Name)> values, and is optional. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | =head2 target | ||
| 225 | |||
| 226 | target(Str) | ||
| 227 | |||
| 228 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | =head1 METHODS | ||
| 233 | |||
| 234 | This package implements the following methods: | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | =head2 any | ||
| 239 | |||
| 240 | any() : Object | ||
| 241 | |||
| 242 | The any method returns a search to query for any C<handle>, C<target>, | ||
| 243 | C<symbol> and C<bucket>. | ||
| 244 | |||
| 245 | =over 4 | ||
| 246 | |||
| 247 | =item any example #1 | ||
| 248 | |||
| 249 | # given: synopsis | ||
| 250 | |||
| 251 | $search = $search->any; | ||
| 252 | |||
| 253 | =back | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | =head2 for | ||
| 258 | |||
| 259 | for(Str $type) : Object | ||
| 260 | |||
| 261 | The for method returns a search to query for any object of the given type | ||
| 262 | within the defined C<handle> and C<target>. | ||
| 263 | |||
| 264 | =over 4 | ||
| 265 | |||
| 266 | =item for example #1 | ||
| 267 | |||
| 268 | # given: synopsis | ||
| 269 | |||
| 270 | $search = $search->for('queue'); | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | =head2 objects | ||
| 277 | |||
| 278 | objects() : ArrayRef[Object] | ||
| 279 | |||
| 280 | The objects method returns a collection of objects derived from the query | ||
| 281 | criteria. | ||
| 282 | |||
| 283 | =over 4 | ||
| 284 | |||
| 285 | =item objects example #1 | ||
| 286 | |||
| 287 | # given: synopsis | ||
| 288 | |||
| 289 | my $objects = $search->objects; | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =over 4 | ||
| 294 | |||
| 295 | =item objects example #2 | ||
| 296 | |||
| 297 | # given: synopsis | ||
| 298 | |||
| 299 | use Zing::KeyVal; | ||
| 300 | use Zing::PubSub; | ||
| 301 | |||
| 302 | my $keyval = Zing::KeyVal->new(name => rand); | ||
| 303 | $keyval->send({ sent => 1 }); | ||
| 304 | |||
| 305 | my $pubsub = Zing::PubSub->new(name => rand); | ||
| 306 | $pubsub->send({ sent => 1 }); | ||
| 307 | |||
| 308 | my $objects = $search->objects; | ||
| 309 | |||
| 310 | =back | ||
| 311 | |||
| 312 | =cut | ||
| 313 | |||
| 314 | =head2 process | ||
| 315 | |||
| 316 | process(CodeRef $callback) : Object | ||
| 317 | |||
| 318 | The process method executes the C<callback> for each term in the search | ||
| 319 | results. | ||
| 320 | |||
| 321 | =over 4 | ||
| 322 | |||
| 323 | =item process example #1 | ||
| 324 | |||
| 325 | # given: synopsis | ||
| 326 | |||
| 327 | $search = $search->process(sub { | ||
| 328 | my ($term) = @_; | ||
| 329 | }); | ||
| 330 | |||
| 331 | =back | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | =head2 query | ||
| 336 | |||
| 337 | query() : Str | ||
| 338 | |||
| 339 | The query method returns the query string used to produce search results. | ||
| 340 | |||
| 341 | =over 4 | ||
| 342 | |||
| 343 | =item query example #1 | ||
| 344 | |||
| 345 | # given: synopsis | ||
| 346 | |||
| 347 | my $query = $search->query; | ||
| 348 | |||
| 349 | =back | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | =head2 results | ||
| 354 | |||
| 355 | results() : ArrayRef[Str] | ||
| 356 | |||
| 357 | The results method performs a search and returns a collection of terms that | ||
| 358 | meet the criteria. | ||
| 359 | |||
| 360 | =over 4 | ||
| 361 | |||
| 362 | =item results example #1 | ||
| 363 | |||
| 364 | # given: synopsis | ||
| 365 | |||
| 366 | my $results = $search->results; | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | =over 4 | ||
| 371 | |||
| 372 | =item results example #2 | ||
| 373 | |||
| 374 | # given: synopsis | ||
| 375 | |||
| 376 | use Zing::KeyVal; | ||
| 377 | use Zing::PubSub; | ||
| 378 | |||
| 379 | my $keyval = Zing::KeyVal->new(name => rand); | ||
| 380 | $keyval->send({ sent => 1 }); | ||
| 381 | |||
| 382 | my $pubsub = Zing::PubSub->new(name => rand); | ||
| 383 | $pubsub->send({ sent => 1 }); | ||
| 384 | |||
| 385 | my $results = $search->results; | ||
| 386 | |||
| 387 | =back | ||
| 388 | |||
| 389 | =cut | ||
| 390 | |||
| 391 | =head2 using | ||
| 392 | |||
| 393 | using(Repo $repo) : Object | ||
| 394 | |||
| 395 | The using method modifies the search criteria to match the term of the | ||
| 396 | provided repo or L<Zing::Repo> derived object. | ||
| 397 | |||
| 398 | =over 4 | ||
| 399 | |||
| 400 | =item using example #1 | ||
| 401 | |||
| 402 | # given: synopsis | ||
| 403 | |||
| 404 | use Zing::Queue; | ||
| 405 | |||
| 406 | my $tasks = Zing::Queue->new(name => 'tasks'); | ||
| 407 | |||
| 408 | $search = $search->using($tasks); | ||
| 409 | |||
| 410 | =back | ||
| 411 | |||
| 412 | =cut | ||
| 413 | |||
| 414 | =head2 where | ||
| 415 | |||
| 416 | where(Str %args) : Object | ||
| 417 | |||
| 418 | The where method modifies the search criteria based on the arguments | ||
| 419 | provided. | ||
| 420 | |||
| 421 | =over 4 | ||
| 422 | |||
| 423 | =item where example #1 | ||
| 424 | |||
| 425 | # given: synopsis | ||
| 426 | |||
| 427 | $search = $search->where( | ||
| 428 | handle => 'myapp', | ||
| 429 | target => 'us-west', | ||
| 430 | ); | ||
| 431 | |||
| 432 | =back | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | =head1 AUTHOR | ||
| 437 | |||
| 438 | Al Newkirk, C<awncorp@cpan.org> | ||
| 439 | |||
| 440 | =head1 LICENSE | ||
| 441 | |||
| 442 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 443 | |||
| 444 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 445 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 446 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 447 | |||
| 448 | =head1 PROJECT | ||
| 449 | |||
| 450 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 451 | |||
| 452 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 453 | |||
| 454 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 455 | |||
| 456 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 457 | |||
| 458 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 459 | |||
| 460 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 461 | |||
| 462 | =cut |