File Coverage

blib/lib/RDF/Query/Plan/Distinct.pm
Criterion Covered Total %
statement 44 60 73.3
branch 9 14 64.2
condition n/a
subroutine 11 15 73.3
pod 11 11 100.0
total 75 100 75.0


line stmt bran cond sub pod time code
1             # RDF::Query::Plan::Distinct
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Plan::Distinct - Executable query plan for Distincts.
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Plan::Distinct version 2.918.
11              
12             =head1 METHODS
13              
14             Beyond the methods documented below, this class inherits methods from the
15             L<RDF::Query::Plan> class.
16              
17             =over 4
18              
19             =cut
20              
21             package RDF::Query::Plan::Distinct;
22              
23 35     35   138 use strict;
  35         54  
  35         854  
24 35     35   122 use warnings;
  35         54  
  35         787  
25 35     35   123 use base qw(RDF::Query::Plan);
  35         62  
  35         2540  
26              
27             ######################################################################
28              
29             our ($VERSION);
30             BEGIN {
31 35     35   17399 $VERSION = '2.918';
32             }
33              
34             ######################################################################
35              
36             =item C<< new ( $plan ) >>
37              
38             =cut
39              
40             sub new {
41 10     10 1 19 my $class = shift;
42 10         14 my $plan = shift;
43 10         45 my $self = $class->SUPER::new( $plan );
44 10         40 $self->[0]{referenced_variables} = [ $plan->referenced_variables ];
45 10         32 return $self;
46             }
47              
48             =item C<< execute ( $execution_context ) >>
49              
50             =cut
51              
52             sub execute ($) {
53 10     10 1 17 my $self = shift;
54 10         15 my $context = shift;
55 10         33 $self->[0]{delegate} = $context->delegate;
56 10 50       41 if ($self->state == $self->OPEN) {
57 0         0 throw RDF::Query::Error::ExecutionError -text => "DISTINCT plan can't be executed while already open";
58             }
59 10         19 my $plan = $self->[1];
60 10         37 $plan->execute( $context );
61              
62 10 50       25 if ($plan->state == $self->OPEN) {
63 10         27 $self->[0]{seen} = {};
64 10         32 $self->state( $self->OPEN );
65             } else {
66 0         0 warn "could not execute plan in distinct";
67             }
68 10         20 $self;
69             }
70              
71             =item C<< next >>
72              
73             =cut
74              
75             sub next {
76 30     30 1 36 my $self = shift;
77 30 50       64 unless ($self->state == $self->OPEN) {
78 0         0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open DISTINCT";
79             }
80 30         37 my $plan = $self->[1];
81 30         31 while (1) {
82 33         88 my $row = $plan->next;
83 33 100       211 return undef unless ($row);
84 26 100       77 if (not $self->[0]{seen}{ $row->as_string }++) {
85 23 50       458 if (my $d = $self->delegate) {
86 0         0 $d->log_result( $self, $row );
87             }
88 23         65 return $row;
89             }
90             }
91             }
92              
93             =item C<< close >>
94              
95             =cut
96              
97             sub close {
98 10     10 1 15 my $self = shift;
99 10 50       28 unless ($self->state == $self->OPEN) {
100 0         0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open DISTINCT";
101             }
102 10         26 delete $self->[0]{seen};
103 10         36 $self->[1]->close();
104 10         35 $self->SUPER::close();
105             }
106              
107             =item C<< pattern >>
108              
109             Returns the query plan that will be used to produce the data to be made distinct.
110              
111             =cut
112              
113             sub pattern {
114 10     10 1 12 my $self = shift;
115 10         48 return $self->[1];
116             }
117              
118             =item C<< distinct >>
119              
120             Returns true if the pattern is guaranteed to return distinct results.
121              
122             =cut
123              
124             sub distinct {
125 10     10 1 35 return 1;
126             }
127              
128             =item C<< ordered >>
129              
130             Returns true if the pattern is guaranteed to return ordered results.
131              
132             =cut
133              
134             sub ordered {
135 10     10 1 17 my $self = shift;
136 10         30 return $self->pattern->ordered;
137             }
138              
139             =item C<< plan_node_name >>
140              
141             Returns the string name of this plan node, suitable for use in serialization.
142              
143             =cut
144              
145             sub plan_node_name {
146 0     0 1   return 'distinct';
147             }
148              
149             =item C<< plan_prototype >>
150              
151             Returns a list of scalar identifiers for the type of the content (children)
152             nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
153             identifiers.
154              
155             =cut
156              
157             sub plan_prototype {
158 0     0 1   my $self = shift;
159 0           return qw(P);
160             }
161              
162             =item C<< plan_node_data >>
163              
164             Returns the data for this plan node that corresponds to the values described by
165             the signature returned by C<< plan_prototype >>.
166              
167             =cut
168              
169             sub plan_node_data {
170 0     0 1   my $self = shift;
171 0           return ($self->pattern);
172             }
173              
174             =item C<< graph ( $g ) >>
175              
176             =cut
177              
178             sub graph {
179 0     0 1   my $self = shift;
180 0           my $g = shift;
181 0           my $c = $self->pattern->graph( $g );
182 0           $g->add_node( "$self", label => "Distinct" . $self->graph_labels );
183 0           $g->add_edge( "$self", $c );
184 0           return "$self";
185             }
186              
187              
188             1;
189              
190             __END__
191              
192             =back
193              
194             =head1 AUTHOR
195              
196             Gregory Todd Williams <gwilliams@cpan.org>
197              
198             =cut