blib/lib/LucyX/Search/Filter.pm

filter: Statementstmt 99.0% 103 / 104 Branchbran 66.6% 12 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 24 / 24 Podpod 66.6% 4 / 6 totaltotal 89.5% 145 / 162 CC 12 SCAR 7.4
CC
12
Cov
91%
equals
4.3
_fetch_cached_bits
4.0
new
3.1
CRAP 2.1
« blib/lib/LucyX/Search/MockMatcher.pm ↑ Summary blib/lib/LucyX/Index/LongFieldSim.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15  
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 package LucyX::Search::Filter;
20 2 ▶BEGIN { our @ISA = qw( Lucy::Search::Query ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 our $VERSION = '0.008000';
22 2 $VERSION = eval $VERSION;
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶no bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Inside-out member vars.
30 2 our %query;
31 2 our %cached_bits;
32  
33 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
34 2 my ( $either, %args ) = @_;
35 2 my $query = delete $args{query};
36 2 ▶ confess("required parameter query is not a Lucy::Search::Query")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($query) and $query->isa("Lucy::Search::Query") 0 2 2
Condition33%1/3
builtin::blessed($query) and $query->isa("Lucy::Search::Query")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($query) and $query->isa("Lucy::Search::Query")
builtin::blessed($query) $query->isa("Lucy::Search::Query")
Truth table33%1/3
A and B
A= builtin::blessed($query)B= $query->isa("Lucy::Search::Query")
ABresult
0X0
100
111
37 unless ( blessed($query)
38 && $query->isa('Lucy::Search::Query') );
39 2 my $self = $either->SUPER::new(%args);
40 2 $self->_init_cache;
41 2 $query{$$self} = $query;
42 2 $self->set_boost(0);
43 2 return $self;
44 }
45  
46 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
47 3 my $self = shift;
48 3 delete $query{$$self};
49 3 delete $cached_bits{$$self};
50 3 $self->SUPER::DESTROY;
51 }
52  
53 6 ▶sub make_compiler {
Subroutine make_compiler: called CC=2 SCAR=7.0
Pod documented
54 6 my ( $self, %args ) = @_;
55 6 my $subordinate = delete $args{subordinate};
56 6 my $compiler
57 = LucyX::Search::FilterCompiler->new( %args, parent => $self );
58 6 ▶ $compiler->normalize unless $subordinate;
Branch: 1 branch
BranchTrueFalseTotal
unless $subordinate 0 6 6
59 6 return $compiler;
60 }
61  
62 1 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod undocumented
63 1 my ( $self, $outstream ) = @_;
64 1 $self->SUPER::serialize($outstream);
65 1 my $frozen = nfreeze( $query{$$self} );
66 1 $outstream->write_cu32( bytes::length($frozen) );
67 1 $outstream->print($frozen);
68 }
69  
70 1 ▶sub deserialize {
Subroutine deserialize: called CC=1 SCAR=0.0
Pod undocumented
71 1 my ( $self, $instream ) = @_;
72 1 $self->SUPER::deserialize($instream);
73 1 my $len = $instream->read_cu32;
74 1 my $frozen;
75 1 $instream->read( $frozen, $len );
76 1 $query{$$self} = thaw($frozen);
77 1 return $self;
78 }
79  
80 2 ▶sub equals {
Subroutine equals: called CC=4 SCAR=14.6
Pod documented
81 2 my ( $self, $other ) = @_;
82 2 ▶ return 0 unless $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $other->isa(__PACKAGE__) 0 2 2
83 2 ▶ return 0 unless $query{$$self}->equals( $query{$$other} );
Branch: 1 branch
BranchTrueFalseTotal
unless $LucyX::Search::Filter::query{$$self}->equals($LucyX::Search::Filter::query{$$other}) 0 2 2
84 2 ▶ return 0 unless $self->get_boost == $other->get_boost;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_boost == $other->get_boost 0 2 2
85 2 return 1;
86 }
87  
88 1 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
89 1 my $self = shift;
90 1 return 'Filter(' . $query{$$self}->to_string . ')';
91 }
92  
93 12 ▶sub _bits {
Subroutine _bits: called CC=2 SCAR=6.9
94 12 my ( $self, $seg_reader ) = @_;
95  
96 12 my $cached_bits = $self->_fetch_cached_bits($seg_reader);
97  
98 # Fill the cache.
99 12 ▶ if ( !defined $cached_bits ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cached_bits) 3 9 12
100 3 $cached_bits = Lucy::Object::BitVector->new(
101 capacity => $seg_reader->doc_max + 1 );
102 3 $self->_store_cached_bits( $seg_reader, $cached_bits );
103  
104 3 my $collector = Lucy::Search::Collector::BitCollector->new(
105 bit_vector => $cached_bits );
106  
107 3 my $polyreader = Lucy::Index::PolyReader->new(
108 schema => $seg_reader->get_schema,
109 folder => $seg_reader->get_folder,
110 snapshot => $seg_reader->get_snapshot,
111 sub_readers => [$seg_reader],
112 );
113 3 my $searcher
114 = Lucy::Search::IndexSearcher->new( index => $polyreader );
115  
116 # Perform the search.
117 $searcher->collect(
118 3 query => $query{$$self},
119 collector => $collector,
120 );
121 }
122  
123 12 return $cached_bits;
124 }
125  
126 # Store a cached BitVector associated with a particular SegReader. Store a
127 # weak reference to the SegReader as an indicator of cache validity.
128 3 ▶sub _store_cached_bits {
Subroutine _store_cached_bits: called CC=1 SCAR=0.0
129 3 my ( $self, $seg_reader, $bits ) = @_;
130 3 my $pair = { seg_reader => $seg_reader, bits => $bits };
131 3 weaken( $pair->{seg_reader} );
132 3 $cached_bits{$$self}{$$seg_reader} = $pair;
133 }
134  
135 # Retrieve a cached BitVector associated with a particular SegReader. As a
136 # side effect, clear away any BitVectors which are no longer valid because
137 # their SegReaders have gone away.
138 12 ▶sub _fetch_cached_bits {
Subroutine _fetch_cached_bits: called CC=4 SCAR=14.0
139 12 my ( $self, $seg_reader ) = @_;
140 12 my $cached_bits = $cached_bits{$$self};
141  
142 # Sweep.
143 12 ▶ while ( my ( $addr, $pair ) = each %$cached_bits ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$cached_bits) 15 12 27
144 # If weak ref has decomposed into undef, SegReader is gone... so
145 # delete.
146 15 ▶ next if defined $pair->{seg_reader};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pair->{"seg_reader"} 15 0 15
147 0 delete $cached_bits->{$addr};
148 }
149  
150 # Fetch.
151 12 my $pair = $cached_bits->{$$seg_reader};
152 12 ▶ return $pair->{bits} if defined $pair;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pair 9 3 12
153 3 return;
154 }
155  
156 # Kill any existing cached filters.
157 2 ▶sub _init_cache {
Subroutine _init_cache: called CC=1 SCAR=0.0
158 2 my $self = shift;
159 2 $cached_bits{$$self} = {};
160 }
161  
162 # Testing only.
163 4 ▶sub _cached_count {
Subroutine _cached_count: called CC=1 SCAR=0.0
164 4 my $self = shift;
165 6 return scalar grep { defined $cached_bits{$$self}{$_}{seg_reader} }
166 4 keys %{ $cached_bits{$$self} };
167 }
168  
169 package LucyX::Search::FilterCompiler;
170 2 our $VERSION = '0.008000';
171 2 $VERSION = eval $VERSION;
172 2 ▶BEGIN { our @ISA = qw( Lucy::Search::Compiler ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
173  
174 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
175 6 my ( $class, %args ) = @_;
176 6 ▶ $args{similarity} ||= $args{searcher}->get_schema->get_similarity;
Condition33%1/3
$args{"similarity"} ||= $args{"searcher"}->get_schema->get_similarity
l!l&&r!l&&!r
060
MC/DC0%0/2
$args{"similarity"} ||= $args{"searcher"}->get_schema->get_similarity
$args{"similarity"} $args{"searcher"}->get_schema->get_similarity
Truth table33%1/3
A ||= B
A= $args{"similarity"}B= $args{"searcher"}->get_schema->get_similarity
ABresult
1X1
011
000
177 6 return $class->SUPER::new(%args);
178 }
179  
180 6 ▶sub make_matcher {
Subroutine make_matcher: called CC=1 SCAR=0.0
181 6 my ( $self, %args ) = @_;
182 6 my $seg_reader = $args{reader};
183 6 my $bits = $self->get_parent->_bits($seg_reader);
184 6 return LucyX::Search::FilterMatcher->new(
185 bits => $bits,
186 doc_max => $seg_reader->doc_max,
187 );
188 }
189  
190 package LucyX::Search::FilterMatcher;
191 2 our $VERSION = '0.008000';
192 2 $VERSION = eval $VERSION;
193 2 ▶BEGIN { our @ISA = qw( Lucy::Search::Matcher ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
194  
195 2 1;
196  
197 __END__
« blib/lib/LucyX/Search/MockMatcher.pm ↑ Summary blib/lib/LucyX/Index/LongFieldSim.pm »