| 1 | # RDF::Query::Federate::Plan | ||
| 2 | # ----------------------------------------------------------------------------- | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | RDF::Query::Federate::Plan - Executable query plan nodes. | ||
| 7 | |||
| 8 | =head1 VERSION | ||
| 9 | |||
| 10 | This document describes RDF::Query::Federate::Plan version 2.919. | ||
| 11 | |||
| 12 | =head1 STATUS | ||
| 13 | |||
| 14 | This module's API and functionality should be considered deprecated. | ||
| 15 | If you need functionality that this module provides, | ||
| 16 | please L<get in touch|http://www.perlrdf.org/>. | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | =over 4 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | package RDF::Query::Federate::Plan; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | use base qw(RDF::Query::Plan); | ||
| 29 | |||
| 30 | use Data::Dumper; | ||
| 31 | use Set::Scalar; | ||
| 32 | use List::Util qw(reduce); | ||
| 33 | use Scalar::Util qw(blessed refaddr); | ||
| 34 | use RDF::Query::Error qw(:try); | ||
| 35 | |||
| 36 | ###################################################################### | ||
| 37 | |||
| 38 | our ($VERSION); | ||
| 39 | BEGIN { | ||
| 40 | $VERSION = '2.919'; | ||
| 41 | } | ||
| 42 | |||
| 43 | ###################################################################### | ||
| 44 | |||
| 45 | =item C<< generate_plans ( $algebra, $execution_context, %args ) >> | ||
| 46 | |||
| 47 | Returns a list of equivalent query plan objects for the given algebra object. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub generate_plans { | ||
| 52 | my $self = shift; | ||
| 53 | my $class = ref($self) || $self; | ||
| 54 | my $algebra = shift; | ||
| 55 | my $context = shift; | ||
| 56 | my %args = @_; | ||
| 57 | my $l = Log::Log4perl->get_logger('rdf.query.federate.plan'); | ||
| 58 | |||
| 59 | my $query = $context->query; | ||
| 60 | my $cache = ($query->{_query_cache}{federate_plans} ||= {}); | ||
| 61 | my $sse = $algebra->sse(); | ||
| 62 | |||
| 63 | if ($cache->{ $sse }) { | ||
| 64 | return @{ $cache->{ $sse } }; | ||
| 65 | } else { | ||
| 66 | my $aclass = ref($algebra); | ||
| 67 | my ($type) = ($aclass =~ m<::(\w+)$>); | ||
| 68 | |||
| 69 | if ($type eq 'BasicGraphPattern') { | ||
| 70 | # my ($plan) = $self->prune_plans( $context, $self->SUPER::generate_plans( $algebra, $context, %args ) ); | ||
| 71 | my ($plan) = $self->SUPER::generate_plans( $algebra, $context, %args ); | ||
| 72 | my @triples = $algebra->triples(); | ||
| 73 | my @fplans = map { $_->[0] } $self->_optimistic_triple_join_plans( $context, \@triples, %args, method => 'triples' ); | ||
| 74 | $l->debug("generating plans for federated query with algebra: $sse"); | ||
| 75 | my @optimistic_plans; | ||
| 76 | # foreach my $plan (@plans) { | ||
| 77 | if (@fplans) { | ||
| 78 | my $time = $context->optimistic_threshold_time || 0; | ||
| 79 | my $oplan = RDF::Query::Plan::ThresholdUnion->new( $time, @fplans, $plan ); | ||
| 80 | $oplan->label( services => $plan->label( 'services' ) ); | ||
| 81 | push(@optimistic_plans, $oplan); | ||
| 82 | } | ||
| 83 | unless (@optimistic_plans) { | ||
| 84 | push(@optimistic_plans, $plan); | ||
| 85 | } | ||
| 86 | # } | ||
| 87 | |||
| 88 | $cache->{ $sse } = \@optimistic_plans; | ||
| 89 | if ($l->is_debug) { | ||
| 90 | foreach my $i (0 .. $#optimistic_plans) { | ||
| 91 | my $p = $optimistic_plans[ $i ]; | ||
| 92 | my $sse = $p->sse({}, ''); | ||
| 93 | $l->debug("optimistic plan $i: $sse"); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | return @optimistic_plans; | ||
| 97 | } else { | ||
| 98 | return $self->SUPER::generate_plans( $algebra, $context, %args ); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _optimistic_triple_join_plans { | ||
| 104 | my $self = shift; | ||
| 105 | my $context = shift; | ||
| 106 | my $triples = shift; | ||
| 107 | my %args = @_; | ||
| 108 | my $l = Log::Log4perl->get_logger('rdf.query.federate.plan'); | ||
| 109 | |||
| 110 | my $method = $args{ method }; | ||
| 111 | my @join_types = RDF::Query::Plan::Join->join_classes; | ||
| 112 | my @triples = @$triples; | ||
| 113 | my %triples = map { refaddr($_) => $_ } @triples; | ||
| 114 | my %ids = map { refaddr($triples[$_]) => $_ } (0 .. $#triples); | ||
| 115 | my %tplans = map { refaddr($_) => [ $self->generate_plans( $_, $context, %args ) ] } @triples; | ||
| 116 | |||
| 117 | my %per_service; | ||
| 118 | my @service_plans; | ||
| 119 | foreach my $id (0 .. $#triples) { | ||
| 120 | my $r = refaddr($triples[$id]); | ||
| 121 | my $ps = $tplans{$r}; | ||
| 122 | my $t = $triples{ $r }; | ||
| 123 | foreach my $pid (0 .. $#{ $ps }) { | ||
| 124 | my $p = $ps->[ $pid ]; | ||
| 125 | $self->label_plan_with_services( $p, $context ); | ||
| 126 | push(@service_plans, { plan => $p, size => 1, coverage => [$id] }); | ||
| 127 | foreach my $service (@{ $p->label('services') || [] }) { | ||
| 128 | push( @{ $per_service{ $service }{ $r } }, $p ); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | foreach my $s (sort keys %per_service) { | ||
| 134 | $l->trace("SERVICE: $s"); | ||
| 135 | my $data = $per_service{ $s }; | ||
| 136 | my @triples; | ||
| 137 | my @ids; | ||
| 138 | foreach my $r (sort { $ids{ $a } <=> $ids{ $b } } keys %$data) { | ||
| 139 | my $t = $triples{ $r }; | ||
| 140 | push(@ids, $ids{ $r }); | ||
| 141 | push(@triples, $t); | ||
| 142 | $l->trace("\tTRIPLE $ids{$r}: " . $t->sse); | ||
| 143 | my @plans = @{ $data->{ $r } }; | ||
| 144 | foreach my $p (@plans) { | ||
| 145 | $l->trace("\t\tPLAN: " . $p->sse); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | my ($join) = $self->_triple_join_plans( $context, \@triples, %args ); | ||
| 149 | my ($plan, $algebras) = @$join; | ||
| 150 | my $size = scalar(@$algebras); | ||
| 151 | my $algebra = ($size > 1) ? RDF::Query::Algebra::BasicGraphPattern->new( @$algebras ) : $algebras->[0]; | ||
| 152 | $plan->label('algebra', $algebra); | ||
| 153 | my $service = RDF::Query::Plan::Service->new_from_plan( $s, $plan, $context ); | ||
| 154 | push(@service_plans, { service => $s, plan => $service, size => $size, coverage => [sort { $a <=> $b } @ids] }); | ||
| 155 | } | ||
| 156 | |||
| 157 | my %plans_by_coverage; | ||
| 158 | foreach my $sp (@service_plans) { | ||
| 159 | my @cover = @{ $sp->{ coverage } }; | ||
| 160 | my $data = \%plans_by_coverage; | ||
| 161 | while (@cover) { | ||
| 162 | my $c = shift(@cover); | ||
| 163 | $data = ($data->{ $c } ||= {}); | ||
| 164 | } | ||
| 165 | $data->{ '_service' } = $sp; | ||
| 166 | } | ||
| 167 | |||
| 168 | my @plans; | ||
| 169 | my $full_coverage = join('', 0..$#triples); | ||
| 170 | my @join_service_plans = sort { $b->{size} <=> $a->{size} } grep { $_->{size} >= 2 } @service_plans; | ||
| 171 | SP: foreach my $sp (@join_service_plans) { | ||
| 172 | $l->trace("----------------------->"); | ||
| 173 | my $plan = $sp->{plan}; | ||
| 174 | my $coverage = join('', @{$sp->{coverage}}); | ||
| 175 | $l->trace("trying service $sp->{service} with BGP coverage $coverage"); | ||
| 176 | while ($coverage ne $full_coverage) { | ||
| 177 | $l->trace("coverage ($coverage) isn't full yet (needs to be $full_coverage)"); | ||
| 178 | my $needed = $full_coverage; | ||
| 179 | foreach my $c (split(//, $coverage)) { | ||
| 180 | $needed =~ s/$c//; | ||
| 181 | } | ||
| 182 | my @needed = split('', $needed); | ||
| 183 | |||
| 184 | # XXX this is where things go naive. ideally, we would start with | ||
| 185 | # XXX any triple that yielded the optimal bin packing of plans to | ||
| 186 | # XXX produce full coverage, but instead we start with the lowest | ||
| 187 | # XXX numbered triple, and use a greedy search from there. | ||
| 188 | my $start = shift(@needed); | ||
| 189 | $l->trace("starting remote BGP with triple $start"); | ||
| 190 | $coverage .= $start; | ||
| 191 | my $access_key = $start; | ||
| 192 | my $data = $plans_by_coverage{ $start }; | ||
| 193 | while (@needed and ref($data->{ $needed[0] })) { | ||
| 194 | my $c = shift(@needed); | ||
| 195 | $access_key .= $c; | ||
| 196 | $l->trace("adding triple $c to the current remote BGP"); | ||
| 197 | $coverage .= $c; | ||
| 198 | $data = $data->{ $c }; | ||
| 199 | } | ||
| 200 | unless (exists $data->{ '_service' }) { | ||
| 201 | $l->trace("the current plan reached a dead end with key '$access_key': " . Dumper($data)); | ||
| 202 | next SP; | ||
| 203 | } | ||
| 204 | $l->trace("no more triples in this remote BGP"); | ||
| 205 | my $join_plan = $data->{ '_service' }{'plan'}; | ||
| 206 | Carp::confess Dumper($full_coverage, $coverage, $data) unless ref($join_plan); | ||
| 207 | $plan = RDF::Query::Plan::Join::NestedLoop->new( $plan, $join_plan, 0, {} ); | ||
| 208 | $coverage = join('', sort split(//, $coverage)); | ||
| 209 | } | ||
| 210 | push(@plans, $plan); | ||
| 211 | $l->trace("<-------------"); | ||
| 212 | } | ||
| 213 | if (@plans) { | ||
| 214 | my $count = scalar(@plans); | ||
| 215 | $l->debug("returning $count possible QEPs for optimistic BGP"); | ||
| 216 | return map {[$_, $triples]} @plans; | ||
| 217 | } else { | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | =item C<< label_plan_with_services ( $plan, $context ) >> | ||
| 223 | |||
| 224 | Labels the supplied plan object with the URIs of applicable services that are | ||
| 225 | capable of answering the query represented by the plan. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub label_plan_with_services { | ||
| 230 | my $self = shift; | ||
| 231 | my $plan = shift; | ||
| 232 | my $context = shift; | ||
| 233 | my $query = $context->query; | ||
| 234 | my @sds = $query->services; | ||
| 235 | my $l = Log::Log4perl->get_logger('rdf.query.federate.plan'); | ||
| 236 | |||
| 237 | if ($plan->isa('RDF::Query::Plan::Triple')) { | ||
| 238 | my @services; | ||
| 239 | foreach my $sd (@sds) { | ||
| 240 | if ($sd->answers_triple_pattern( $plan->triple )) { | ||
| 241 | push(@services, $sd); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | # $plan might have already been labeled with services, in which case | ||
| 246 | # we should just assume the existing label is correct, and save ourselves | ||
| 247 | # the work of re-labeling | ||
| 248 | if (@services and not($plan->label( 'services' ))) { | ||
| 249 | if ($l->is_debug) { | ||
| 250 | $l->debug( "SERVICES that can handle pattern: " . $plan->triple->sse . "\n\t" . join("\n\t", map { $_->url } @services) ); | ||
| 251 | } | ||
| 252 | $plan->label( services => [ map { $_->url } @services ] ); | ||
| 253 | } | ||
| 254 | } elsif ($plan->isa('RDF::Query::Plan::Join')) { | ||
| 255 | $self->label_plan_with_services($_, $context) for ($plan->lhs, $plan->rhs); | ||
| 256 | my $lhs = $plan->lhs->label( 'services' ) || []; | ||
| 257 | my $rhs = $plan->rhs->label( 'services' ) || []; | ||
| 258 | my $set = Set::Scalar->new(@$lhs)->intersection(Set::Scalar->new(@$rhs)); | ||
| 259 | if (my @members = $set->members) { | ||
| 260 | $plan->label( services => [ @members ] ); | ||
| 261 | } | ||
| 262 | } elsif ($plan->isa('RDF::Query::Plan::ThresholdUnion')) { | ||
| 263 | my $dplan = $plan->default; | ||
| 264 | $self->label_plan_with_services($dplan, $context); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | 1; | ||
| 269 | |||
| 270 | __END__ |