| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package AtteanX::Query::Cache::Retriever; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 930107 | use 5.010001; | 
|  | 6 |  |  |  |  | 12 |  | 
| 4 | 6 |  |  | 6 |  | 20 | use strict; | 
|  | 6 |  |  |  |  | 8 |  | 
|  | 6 |  |  |  |  | 104 |  | 
| 5 | 6 |  |  | 6 |  | 20 | use warnings; | 
|  | 6 |  |  |  |  | 4 |  | 
|  | 6 |  |  |  |  | 246 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:KJETILK'; | 
| 8 |  |  |  |  |  |  | our $VERSION   = '0.002'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 6 |  |  | 6 |  | 17 | use Moo; | 
|  | 6 |  |  |  |  | 8 |  | 
|  | 6 |  |  |  |  | 26 |  | 
| 11 | 6 |  |  | 6 |  | 8456 | use Carp qw(croak); | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 247 |  | 
| 12 | 6 |  |  | 6 |  | 23 | use Attean::RDF; | 
|  | 6 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 377 |  | 
| 13 | 6 |  |  | 6 |  | 24 | use Types::Standard qw(InstanceOf); | 
|  | 6 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 55 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has model => (is => 'ro', | 
| 16 |  |  |  |  |  |  | isa => InstanceOf['AtteanX::Model::SPARQLCache'], | 
| 17 |  |  |  |  |  |  | required => 1); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | with 'MooX::Log::Any'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub fetch { | 
| 23 | 0 |  |  | 0 | 0 |  | my ($self, $triple) = @_; | 
| 24 | 0 |  |  |  |  |  | $triple = $triple->canonicalize; | 
| 25 | 0 |  |  |  |  |  | my $key = $triple->tuples_string; | 
| 26 | 0 |  |  |  |  |  | my @vars	= $triple->values_consuming_role('Attean::API::Variable'); | 
| 27 | 0 |  |  |  |  |  | my $use_hash = (scalar @vars) - 1; | 
| 28 | 0 | 0 |  |  |  |  | if ($use_hash < 0) { | 
|  |  | 0 |  |  |  |  |  | 
| 29 | 0 |  |  |  |  |  | croak "No variables in triple pattern $key"; | 
| 30 |  |  |  |  |  |  | } elsif ($use_hash > 1) { | 
| 31 | 0 |  |  |  |  |  | croak "Only triple patterns with one or two variables are supported, got $key"; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 | 0 |  |  |  |  | if ($self->model->isa('AtteanX::Model::SPARQLCache::LDF')) { | 
| 35 | 0 |  |  |  |  |  | my @terms = $triple->values; | 
| 36 | 0 |  |  |  |  |  | my @termswithvars; | 
| 37 | 0 |  |  |  |  |  | my %tmp = $triple->mapping; | 
| 38 | 0 |  |  |  |  |  | while (my ($pos, $term) = each (%tmp)) { | 
| 39 | 0 | 0 |  |  |  |  | if ($term->does('Attean::API::Variable')) { | 
| 40 | 0 |  |  |  |  |  | push(@termswithvars, $pos); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 | 0 |  |  |  |  |  | my $iter = $self->model->ldf_store->get_triples(@terms); | 
| 44 | 0 | 0 |  |  |  |  | if ($use_hash) { # Now, decide if we insert an array or a hash into the cache. | 
| 45 | 0 |  |  |  |  |  | @termswithvars = reverse sort @termswithvars; # Will ensure s, p, o order | 
| 46 | 0 |  |  |  |  |  | my $data; | 
| 47 | 0 |  |  |  |  |  | while (my $res = $iter->next) { | 
| 48 | 0 |  |  |  |  |  | push(@{$data->{$res->value($termswithvars[0])->ntriples_string}}, $res->value($termswithvars[1])->ntriples_string); | 
|  | 0 |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 0 |  |  |  |  |  | return $data; | 
| 51 |  |  |  |  |  |  | } else { | 
| 52 | 0 |  |  |  |  |  | my @data; | 
| 53 | 0 |  |  |  |  |  | while (my $res = $iter->next) { | 
| 54 | 0 |  |  |  |  |  | push(@data, $res->value($termswithvars[0])->ntriples_string); | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 0 |  |  |  |  |  | return \@data; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | } else { | 
| 59 | 0 |  |  |  |  |  | my $sparql = 'SELECT ' . join(' ', map { $_->ntriples_string } @vars) . | 
|  | 0 |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | " WHERE {\n\t" . $triple->as_sparql . '. }'; | 
| 61 | 0 |  |  |  |  |  | $self->log->debug("Running SPARQL query\n$sparql"); | 
| 62 | 0 |  |  |  |  |  | my $iter = $self->model->get_sparql($sparql); | 
| 63 | 0 | 0 |  |  |  |  | if ($use_hash) { # Now, decide if we insert an array or a hash into the cache. | 
| 64 | 0 |  |  |  |  |  | my $data; | 
| 65 | 0 |  |  |  |  |  | while (my $res = $iter->next) { | 
| 66 | 0 |  |  |  |  |  | push(@{$data->{$res->value($vars[0]->value)->ntriples_string}}, $res->value($vars[1]->value)->ntriples_string); | 
|  | 0 |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | } | 
| 68 | 0 |  |  |  |  |  | return $data; | 
| 69 |  |  |  |  |  |  | } else { | 
| 70 | 0 |  |  |  |  |  | my @data; | 
| 71 | 0 |  |  |  |  |  | while (my $res = $iter->next) { | 
| 72 | 0 |  |  |  |  |  | my ($value) = $res->values; | 
| 73 | 0 |  |  |  |  |  | push(@data, $value->ntriples_string); | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 0 |  |  |  |  |  | return \@data; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | 1; |