Nano/Search.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 184 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Nano::Search;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Nano::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 our $VERSION = '0.06'; # VERSION
15  
16 # ATTRIBUTES
17  
18 has table => (
19 is => 'ro',
20 isa => 'Table',
21 new => 1,
22 );
23  
24 fun new_table($self) {
25 $self->nodes->nano->table($self->nodes->id)
26 }
27  
28 has nodes => (
29 is => 'ro',
30 isa => 'Nodes',
31 req => 1,
32 );
33  
34 has orders => (
35 is => 'ro',
36 isa => 'ArrayRef[CodeRef]',
37 new => 1,
38 );
39  
40 fun new_orders($self) {
41 []
42 }
43  
44 has scopes => (
45 is => 'ro',
46 isa => 'ArrayRef[CodeRef]',
47 new => 1,
48 );
49  
50 fun new_scopes($self) {
51 []
52 }
53  
54 # METHODS
55  
56 method all() {
57 $self->fetch($self->table->count)
58 }
59  
60 method count() {
61 @{$self->scopes} ? scalar(@{$self->all}) : $self->table->count
62 }
63  
64 method fetch(Int $size = 1) {
65 my $i = 0;
66 my $nano = $self->nodes->nano;
67 my $results = [];
68 if (!$size) {
69 return $results;
70 }
71 $self->reset;
72 while (my $keyval = $self->table->next) {
73 if (my $result = $self->scope($nano->object($keyval->recv))) {
74 $i = push @$results, $result;
75 }
76 if ($i >= $size) {
77 last;
78 }
79 }
80 return $self->order($results);
81 }
82  
83 method first() {
84 my $results;
85 my $nano = $self->nodes->nano;
86 $self->table->position(undef);
87 if (@{$self->orders}) {
88 $results = $self->all->[0];
89 }
90 else {
91 $self->reset;
92 while (my $keyval = $self->table->next) {
93 if (my $result = $self->scope($nano->object($keyval->recv))) {
94 $results = $result;
95 last;
96 }
97 }
98 }
99 return $results;
100 }
101  
102 method last() {
103 my $results;
104 my $nano = $self->nodes->nano;
105 $self->table->position($self->table->size);
106 if (@{$self->orders}) {
107 $results = $self->all->[-1];
108 }
109 else {
110 $self->table->position($self->table->size);
111 while (my $keyval = $self->table->prev) {
112 if (my $result = $self->scope($nano->object($keyval->recv))) {
113 $results = $result;
114 last;
115 }
116 }
117 }
118 return $results;
119 }
120  
121 method next() {
122 my $results = [];
123 my $nano = $self->nodes->nano;
124 if (@{$self->orders}) {
125 my $skip = 1;
126 for my $item (@{$self->all}) {
127 if (defined $self->{last_next}) {
128 if ($self->{last_next} == $item->id) {
129 $skip = 0;
130 }
131 next if $skip;
132 }
133 push @$results, $item;
134 $self->{last_next} = $item->id;
135 last;
136 }
137 }
138 else {
139 while (my $keyval = $self->table->next) {
140 if (my $result = $self->scope($nano->object($keyval->recv))) {
141 push @$results, $result;
142 last;
143 }
144 }
145 }
146 return $results->[0];
147 }
148  
149 method order(ArrayRef $result) {
150 if (@{$self->orders}) {
151 for my $order (@{$self->orders}) {
152 $result = [sort {$order->($a,$b)} @$result];
153 }
154 return $result;
155 }
156 else {
157 return $result;
158 }
159 }
160  
161 method prev() {
162 my $results = [];
163 my $nano = $self->nodes->nano;
164 if (@{$self->orders}) {
165 my $skip = 1;
166 for my $item (reverse(@{$self->all})) {
167 if (defined $self->{last_prev}) {
168 if ($self->{last_prev} == $item->id) {
169 $skip = 0;
170 }
171 next if $skip;
172 }
173 push @$results, $item;
174 $self->{last_prev} = $item->id;
175 last;
176 }
177 }
178 else {
179 while (my $keyval = $self->table->prev) {
180 if (my $result = $self->scope($nano->object($keyval->recv))) {
181 push @$results, $result;
182 last;
183 }
184 }
185 }
186 return $results->[0];
187 }
188  
189 method reset() {
190 $self->table->reset;
191 return $self;
192 }
193  
194 method scope(Object $object) {
195 if (@{$self->scopes}) {
196 return (grep {!$_->($object)} @{$self->scopes}) ? undef : $object;
197 }
198 else {
199 return $object;
200 }
201 }
202  
203 1;
204  
205 =encoding utf8
206  
207 =head1 NAME
208  
209 Nano::Search - Persisted Index Search
210  
211 =cut
212  
213 =head1 ABSTRACT
214  
215 Persisted Index Search
216  
217 =cut
218  
219 =head1 SYNOPSIS
220  
221 use Nano::Nodes;
222 use Nano::Search;
223  
224 my $nodes = Nano::Nodes->new(
225 type => 'Nano::Node',
226 );
227  
228 my $search = Nano::Search->new(
229 nodes => $nodes,
230 );
231  
232 # $search->count;
233  
234 =cut
235  
236 =head1 DESCRIPTION
237  
238 This package provides a mechanism for searching a prior persisted index.
239  
240 =cut
241  
242 =head1 LIBRARIES
243  
244 This package uses type constraints from:
245  
246 L<Nano::Types>
247  
248 =cut
249  
250 =head1 ATTRIBUTES
251  
252 This package has the following attributes:
253  
254 =cut
255  
256 =head2 nodes
257  
258 nodes(Nodes)
259  
260 This attribute is read-only, accepts C<(Nodes)> values, and is required.
261  
262 =cut
263  
264 =head2 orders
265  
266 orders(ArrayRef[CodeRef])
267  
268 This attribute is read-only, accepts C<(ArrayRef[CodeRef])> values, and is optional.
269  
270 =cut
271  
272 =head2 scopes
273  
274 scopes(ArrayRef[CodeRef])
275  
276 This attribute is read-only, accepts C<(ArrayRef[CodeRef])> values, and is optional.
277  
278 =cut
279  
280 =head2 table
281  
282 table(Table)
283  
284 This attribute is read-only, accepts C<(Table)> values, and is optional.
285  
286 =cut
287  
288 =head1 METHODS
289  
290 This package implements the following methods:
291  
292 =cut
293  
294 =head2 all
295  
296 all() : ArrayRef[Object]
297  
298 The all method returns all objects (qualified via scopes, when present) from
299 the index.
300  
301 =over 4
302  
303 =item all example #1
304  
305 # given: synopsis
306  
307 my $result = $search->all;
308  
309 =back
310  
311 =over 4
312  
313 =item all example #2
314  
315 # given: synopsis
316  
317 use Nano::Node;
318  
319 $search->nodes->set(Nano::Node->new);
320 $search->nodes->set(Nano::Node->new);
321  
322 my $result = $search->all;
323  
324 =back
325  
326 =cut
327  
328 =head2 count
329  
330 count() : Int
331  
332 The count method returns the count of objects (qualified via scopes, when
333 present) in the index.
334  
335 =over 4
336  
337 =item count example #1
338  
339 # given: synopsis
340  
341 my $count = $search->count;
342  
343 =back
344  
345 =over 4
346  
347 =item count example #2
348  
349 # given: synopsis
350  
351 use Nano::Node;
352  
353 $search->nodes->set(Nano::Node->new);
354 $search->nodes->set(Nano::Node->new);
355  
356 my $count = $search->count;
357  
358 =back
359  
360 =cut
361  
362 =head2 fetch
363  
364 fetch(Int $size = 1) : ArrayRef[Object]
365  
366 The fetch method returns a variable number of objects (qualified via scopes,
367 when present) from the index.
368  
369 =over 4
370  
371 =item fetch example #1
372  
373 # given: synopsis
374  
375 my $result = $search->fetch;
376  
377 =back
378  
379 =over 4
380  
381 =item fetch example #2
382  
383 # given: synopsis
384  
385 use Nano::Node;
386  
387 $search->nodes->set(Nano::Node->new);
388 $search->nodes->set(Nano::Node->new);
389  
390 my $result = $search->fetch;
391  
392 =back
393  
394 =over 4
395  
396 =item fetch example #3
397  
398 # given: synopsis
399  
400 use Nano::Node;
401  
402 $search->nodes->set(Nano::Node->new);
403 $search->nodes->set(Nano::Node->new);
404 $search->nodes->set(Nano::Node->new);
405  
406 my $result = $search->fetch(2);
407  
408 =back
409  
410 =cut
411  
412 =head2 first
413  
414 first() : Maybe[Object]
415  
416 The first method returns the first object (qualified via scopes, when present)
417 from the index.
418  
419 =over 4
420  
421 =item first example #1
422  
423 # given: synopsis
424  
425 my $first = $search->first;
426  
427 =back
428  
429 =over 4
430  
431 =item first example #2
432  
433 # given: synopsis
434  
435 use Nano::Node;
436  
437 $search->nodes->set(Nano::Node->new(id => '1st'));
438 $search->nodes->set(Nano::Node->new(id => '2nd'));
439 $search->nodes->set(Nano::Node->new(id => '3rd'));
440  
441 my $first = $search->first;
442  
443 =back
444  
445 =cut
446  
447 =head2 last
448  
449 last() : Maybe[Object]
450  
451 The last method returns the last object (qualified via scopes, when present)
452 from the index.
453  
454 =over 4
455  
456 =item last example #1
457  
458 # given: synopsis
459  
460 my $last = $search->last;
461  
462 =back
463  
464 =over 4
465  
466 =item last example #2
467  
468 # given: synopsis
469  
470 use Nano::Node;
471  
472 $search->nodes->set(Nano::Node->new(id => '1st'));
473 $search->nodes->set(Nano::Node->new(id => '2nd'));
474 $search->nodes->set(Nano::Node->new(id => '3rd'));
475  
476 my $last = $search->last;
477  
478 =back
479  
480 =cut
481  
482 =head2 next
483  
484 next() : Maybe[Object]
485  
486 The next method returns the next object based on the currently held cursor
487 (qualified via scopes, when present) from the index.
488  
489 =over 4
490  
491 =item next example #1
492  
493 # given: synopsis
494  
495 my $next = $search->next;
496  
497 =back
498  
499 =over 4
500  
501 =item next example #2
502  
503 # given: synopsis
504  
505 use Nano::Node;
506  
507 $search->nodes->set(Nano::Node->new(id => '1st'));
508 $search->nodes->set(Nano::Node->new(id => '2nd'));
509 $search->nodes->set(Nano::Node->new(id => '3rd'));
510  
511 my $next = $search->next;
512  
513 =back
514  
515 =over 4
516  
517 =item next example #3
518  
519 # given: synopsis
520  
521 use Nano::Node;
522  
523 $search->nodes->set(Nano::Node->new(id => '1st'));
524 $search->nodes->set(Nano::Node->new(id => '2nd'));
525 $search->nodes->set(Nano::Node->new(id => '3rd'));
526  
527 my $next;
528  
529 $next = $search->next;
530 $next = $search->next;
531  
532 =back
533  
534 =cut
535  
536 =head2 order
537  
538 order(ArrayRef[Object] $results) : ArrayRef[Object]
539  
540 The order method determines the sort order of the array of objects provided
541 based on the registered ordering routines.
542  
543 =over 4
544  
545 =item order example #1
546  
547 # given: synopsis
548  
549 use Nano::Node;
550  
551 my $results = [
552 Nano::Node->new(id => '1st'),
553 Nano::Node->new(id => '2nd'),
554 Nano::Node->new(id => '3rd'),
555 ];
556  
557 $search = Nano::Search->new(
558 nodes => $nodes,
559 orders => [sub {
560 my ($a, $b) = @_;
561 $a->id cmp $b->id
562 }],
563 );
564  
565 $results = $search->order($results);
566  
567 =back
568  
569 =over 4
570  
571 =item order example #2
572  
573 # given: synopsis
574  
575 use Nano::Node;
576  
577 my $results = [
578 Nano::Node->new(id => '1st'),
579 Nano::Node->new(id => '2nd'),
580 Nano::Node->new(id => '3rd'),
581 ];
582  
583 $search = Nano::Search->new(
584 nodes => $nodes,
585 orders => [sub {
586 my ($a, $b) = @_;
587 $b->id cmp $a->id
588 }],
589 );
590  
591 $results = $search->order($results);
592  
593 =back
594  
595 =cut
596  
597 =head2 prev
598  
599 prev() : Maybe[Object]
600  
601 The prev method returns the previous object based on the currently held cursor
602 (qualified via scopes, when present) from the index.
603  
604 =over 4
605  
606 =item prev example #1
607  
608 # given: synopsis
609  
610 my $prev = $search->prev;
611  
612 =back
613  
614 =over 4
615  
616 =item prev example #2
617  
618 # given: synopsis
619  
620 use Nano::Node;
621  
622 $search->table->position(3);
623  
624 $search->nodes->set(Nano::Node->new(id => '1st'));
625 $search->nodes->set(Nano::Node->new(id => '2nd'));
626 $search->nodes->set(Nano::Node->new(id => '3rd'));
627  
628 my $prev = $search->prev;
629  
630 =back
631  
632 =over 4
633  
634 =item prev example #3
635  
636 # given: synopsis
637  
638 use Nano::Node;
639  
640 $search->table->position(3);
641  
642 $search->nodes->set(Nano::Node->new(id => '1st'));
643 $search->nodes->set(Nano::Node->new(id => '2nd'));
644 $search->nodes->set(Nano::Node->new(id => '3rd'));
645  
646 my $prev;
647  
648 $prev = $search->prev;
649 $prev = $search->prev;
650  
651 =back
652  
653 =cut
654  
655 =head2 reset
656  
657 reset() : Object
658  
659 The reset method resets the position on the currently held cursor.
660  
661 =over 4
662  
663 =item reset example #1
664  
665 # given: synopsis
666  
667 $search = $search->reset;
668  
669 =back
670  
671 =cut
672  
673 =head2 scope
674  
675 scope(Object $object) : Maybe[Object]
676  
677 The scope method determines whether the object provided passes-through the
678 registered scopes and if-so returns the object provided.
679  
680 =over 4
681  
682 =item scope example #1
683  
684 # given: synopsis
685  
686 use Nano::Node;
687  
688 my $node = Nano::Node->new(id => '0000003');
689  
690 my $result = $search->scope($node);
691  
692 =back
693  
694 =over 4
695  
696 =item scope example #2
697  
698 # given: synopsis
699  
700 use Nano::Node;
701  
702 $search = Nano::Search->new(
703 nodes => $nodes,
704 scopes => [sub {
705 my ($node) = @_;
706 $node->id ne '0000003'
707 }],
708 );
709  
710 my $node = Nano::Node->new(id => '0000003');
711  
712 my $result = $search->scope($node);
713  
714 =back
715  
716 =over 4
717  
718 =item scope example #3
719  
720 # given: synopsis
721  
722 use Nano::Node;
723  
724 $search = Nano::Search->new(
725 nodes => $nodes,
726 scopes => [sub {
727 my ($node) = @_;
728 $node->id ne '0000003'
729 }],
730 );
731  
732 my $node = Nano::Node->new(id => '0000004');
733  
734 my $result = $search->scope($node);
735  
736 =back
737  
738 =cut
739  
740 =head1 AUTHOR
741  
742 Al Newkirk, C<awncorp@cpan.org>
743  
744 =head1 LICENSE
745  
746 Copyright (C) 2011-2019, Al Newkirk, et al.
747  
748 This is free software; you can redistribute it and/or modify it under the terms
749 of the The Apache License, Version 2.0, as elucidated in the L<"license
750 file"|https://github.com/cpanery/nano/blob/master/LICENSE>.
751  
752 =head1 PROJECT
753  
754 L<Wiki|https://github.com/cpanery/nano/wiki>
755  
756 L<Project|https://github.com/cpanery/nano>
757  
758 L<Initiatives|https://github.com/cpanery/nano/projects>
759  
760 L<Milestones|https://github.com/cpanery/nano/milestones>
761  
762 L<Contributing|https://github.com/cpanery/nano/blob/master/CONTRIBUTE.md>
763  
764 L<Issues|https://github.com/cpanery/nano/issues>
765  
766 =cut