File Coverage

blib/lib/AtteanX/Store/SimpleTripleStore.pm
Criterion Covered Total %
statement 44 44 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 59 59 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             AtteanX::Store::SimpleTripleStore - SimpleTripleStore, unindexed, in-memory RDF store
4              
5             =head1 VERSION
6              
7             This document describes AtteanX::Store::SimpleTripleStore version 0.032
8              
9             =head1 SYNOPSIS
10              
11             use AtteanX::Store::SimpleTripleStore;
12              
13             =head1 DESCRIPTION
14              
15             AtteanX::Store::SimpleTripleStore provides an in-memory triple-store.
16              
17             =cut
18              
19 2     2   6091 use v5.14;
  2         8  
20 2     2   11 use warnings;
  2         5  
  2         97  
21              
22             use Moo;
23 2     2   12 use Type::Tiny::Role;
  2         5  
  2         18  
24 2     2   745 use Types::Standard qw(Int ArrayRef HashRef ConsumerOf InstanceOf);
  2         6  
  2         105  
25 2     2   13 use Encode;
  2         5  
  2         34  
26 2     2   2162 use Set::Scalar;
  2         7  
  2         173  
27 2     2   14 use Digest::SHA;
  2         5  
  2         96  
28 2     2   12 use List::Util qw(first);
  2         4  
  2         80  
29 2     2   14 use Scalar::Util qw(refaddr reftype blessed);
  2         10  
  2         134  
30 2     2   18 use namespace::clean;
  2         5  
  2         107  
31 2     2   12  
  2         4  
  2         20  
32             with 'Attean::API::MutableTripleStore';
33              
34             my @pos_names = Attean::API::Quad->variables;
35              
36             =head1 METHODS
37              
38             Beyond the methods documented below, this class inherits methods from the
39             L<Attean::API::QuadStore> class.
40              
41             =over 4
42              
43             =item C<< new ( triples => \@triples ) >>
44              
45             Returns a new memory-backed storage object.
46              
47             =cut
48              
49             has triples => (is => 'rw', isa => ArrayRef[ConsumerOf['Attean::API::Triple']], default => sub { [] });
50              
51             =item C<< get_triples ( $subject, $predicate, $object ) >>
52              
53             Returns a stream object of all statements matching the specified subject,
54             predicate and objects. Any of the arguments may be undef to match any value.
55              
56             =cut
57              
58             my $self = shift;
59             my @nodes = @_;
60             my %bound;
61             foreach my $pos (0 .. 2) {
62             my $n = $nodes[ $pos ];
63             if (blessed($n) and $n->does('Attean::API::Variable')) {
64             $n = undef;
65             $nodes[$pos] = undef;
66             }
67             if (blessed($n)) {
68             $bound{ $pos_names[$pos] } = $n;
69             }
70             }
71            
72             my $triples = $self->triples;
73             my $iter = Attean::ListIterator->new( values => $triples, item_type => 'Attean::API::Triple' );
74             return $iter->grep(sub {
75             my $q = shift;
76             foreach my $key (keys %bound) {
77             my $term = $q->$key();
78             unless ($term->equals( $bound{$key} )) {
79             return 0;
80             }
81             }
82             return 1;
83             });
84             return $iter;
85             }
86            
87             =item C<< add_triple( $t ) >>
88              
89             =cut
90              
91             my $self = shift;
92             my $t = shift;
93             push(@{ $self->triples }, $t);
94             }
95            
96             =item C<< remove_triple( $t ) >>
97              
98             =cut
99              
100             my $self = shift;
101             my $t = shift;
102             my @remove;
103             my $triples = $self->triples;
104 1     1 1 28 foreach my $i (0 .. $#{ $triples }) {
105 1         2 my $u = $triples->[$i];
106 1         1 if ($u->as_string eq $t->as_string) {
107 1         22 push(@remove, $i);
108 1         9 }
  1         6  
109 4         7 }
110 4 100       13 while (scalar(@remove)) {
111 1         5 my $i = pop(@remove);
112             splice(@$triples, $i, 1, ());
113             }
114 1         5 }
115 1         4 }
116 1         15  
117             1;
118              
119              
120             =back
121              
122             =head1 BUGS
123              
124             Please report any bugs or feature requests to through the GitHub web interface
125             at L<https://github.com/kasei/perlrdf2/issues>.
126              
127             =head1 AUTHOR
128              
129             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
130              
131             =head1 COPYRIGHT
132              
133             Copyright (c) 2014--2022 Gregory Todd Williams. This
134             program is free software; you can redistribute it and/or modify it under
135             the same terms as Perl itself.
136              
137             =cut