File Coverage

blib/lib/MongoDB/QueryResult/Filtered.pm
Criterion Covered Total %
statement 18 35 51.4
branch 0 4 0.0
condition 0 6 0.0
subroutine 6 8 75.0
pod 2 2 100.0
total 26 55 47.2


line stmt bran cond sub pod time code
1             # Copyright 2014 - present MongoDB, Inc.
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14              
15 58     58   390 use strict;
  58         136  
  58         1685  
16 58     58   300 use warnings;
  58         128  
  58         2129  
17             package MongoDB::QueryResult::Filtered;
18              
19             # ABSTRACT: An iterator for Mongo query results with client-side filtering
20              
21 58     58   354 use version;
  58         132  
  58         338  
22             our $VERSION = 'v2.2.0';
23              
24 58     58   4540 use Moo;
  58         154  
  58         342  
25 58         543 use Types::Standard qw(
26             CodeRef
27 58     58   18475 );
  58         166  
28              
29             extends 'MongoDB::QueryResult';
30              
31 58     58   34088 use namespace::clean;
  58         148  
  58         422  
32              
33             # N.B.: _post_filter may also munge documents in addition to filtering;
34             # it *must* be run on all documents
35             has _post_filter => (
36             is => 'ro',
37             isa => CodeRef,
38             required => 1,
39             );
40              
41             sub has_next {
42 0     0 1   my ($self) = @_;
43 0           my $limit = $self->_limit;
44 0 0 0       if ( $limit > 0 && ( $self->cursor_at + 1 ) > $limit ) {
45 0           $self->_kill_cursor;
46 0           return 0;
47             }
48 0   0       while ( !$self->_drained || $self->_get_more ) {
49 0           my $peek = $self->_docs->[0];
50 0 0         if ( $self->_post_filter->($peek) ) {
51             # if meets criteria, has_next is true
52 0           return 1;
53             }
54             else {
55             # otherwise throw it away and repeat
56 0           $self->_inc_cursor_at;
57 0           $self->_next_doc;
58             }
59             }
60             # ran out of docs, so nothing left
61 0           return 0;
62             }
63              
64             sub all {
65 0     0 1   my ($self) = @_;
66 0           my @ret;
67 0           push @ret, grep { $self->_post_filter->($_) } $self->_drain_docs
  0            
68             while $self->has_next;
69 0           return @ret;
70             }
71              
72             1;
73              
74             __END__