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 59     59   396 use strict;
  59         134  
  59         1716  
16 59     59   306 use warnings;
  59         129  
  59         2069  
17             package MongoDB::QueryResult::Filtered;
18              
19             # ABSTRACT: An iterator for Mongo query results with client-side filtering
20              
21 59     59   350 use version;
  59         146  
  59         335  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4595 use Moo;
  59         166  
  59         350  
25 59         464 use Types::Standard qw(
26             CodeRef
27 59     59   18955 );
  59         163  
28              
29             extends 'MongoDB::QueryResult';
30              
31 59     59   33597 use namespace::clean;
  59         145  
  59         403  
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__