blib/lib/GraphQL/Houtou/DataLoader.pm

filter: Statementstmt 61.3% 81 / 132 Branchbran 55.3% 31 / 56 Conditioncond 52.9% 9 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 50.0% 18 / 36 Podpod 0.0% 0 / 9 totaltotal 54.7% 143 / 261 CC 38 SCAR 17.6
CC
38
Cov
59%
_race_adapter
20.0
_after
12.0
_adapt_values
12.0
CRAP 5.8
« blib/lib/GraphQL/Houtou/Runtime/ExecState.pm ↑ Summary blib/lib/GraphQL/Houtou/Directive.pm »
1 package GraphQL::Houtou::DataLoader;
2  
3 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use GraphQL::Houtou ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use GraphQL::Houtou::Async::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '0.08';
12  
13 10 GraphQL::Houtou::_bootstrap_xs();
14  
15 # Reference batching loader for GraphQL::Houtou, following the semantics of
16 # Facebook's dataloader. It is written strictly against the public batching
17 # contract (execute's on_stall hook): loads queue a key and return an
18 # XS-backed await ticket; dispatch() sends every queued key to the batch
19 # function in one call and settles the tickets.
20  
21 1528 ▶sub new {
Subroutine new: called CC=9 SCAR=22.6
Pod undocumented
22 1528 my ($class, %args) = @_;
23 1528 my $batch = $args{batch};
24 1528 ▶ die "GraphQL::Houtou::DataLoader requires a 'batch' function\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $batch ne "CODE" 0 1528 1528
25 if ref($batch) ne 'CODE';
26 my $async_adapter = exists $args{async_adapter}
27 ? GraphQL::Houtou::Async::Adapter->coerce($args{async_adapter})
28 1528 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'async_adapter'} ? : 0 1528 1528
29 return bless {
30 batch => $batch,
31 max_batch_size => $args{max_batch_size} || 0,
32 cache => exists $args{cache} ? ($args{cache} ? 1 : 0) : 1,
33 cache_key => ref($args{cache_key}) eq 'CODE' ? $args{cache_key} : undef,
34 1528 ▶ _batch_plan => $args{batch_plan} ? 1 : 0,
Branch: 5 branches
BranchTrueFalseTotal
exists $args{'cache'} ? : 3 1525 1528
$args{'cache'} ? : 0 3 3
ref $args{'cache_key'} eq 'CODE' ? : 2 1526 1528
$args{'batch_plan'} ? : 1 1527 1528
$async_adapter ? : 0 1528 1528
Condition100%2/2
$args{'max_batch_size'} || 0
l!l
11527
MC/DC100%1/1
$args{'max_batch_size'} || 0
$args{'max_batch_size'}
Truth table100%2/2
A
A= $args{'max_batch_size'}
Aresult
11
00
35 ($async_adapter ? (_async_adapter => $async_adapter) : ()),
36 _promises => {},
37 _queue => [],
38 }, $class;
39 }
40  
41 2481 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod undocumented
42 2481 my ($self, $key) = @_;
43 2481 ▶ die "GraphQL::Houtou::DataLoader::load requires a defined key\n" if !defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2481 2481
44 my $cache_key = $self->{cache}
45 2481 ▶ ? ($self->{cache_key} ? $self->{cache_key}->($key) : $key)
Branch: 2 branches
BranchTrueFalseTotal
$self->{'cache'} ? : 2429 52 2481
$self->{'cache_key'} ? : 2 2427 2429
46 : undef;
47 2481 ▶ if ($self->{cache} && exists $self->{_promises}{$cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"} and exists $self->{"_promises"}{$cache_key}) 464 2017 2481
Condition100%3/3
$self->{"cache"} and exists $self->{"_promises"}{$cache_key}
!ll&&!rl&&r
521965464
MC/DC100%2/2
$self->{"cache"} and exists $self->{"_promises"}{$cache_key}
$self->{"cache"} exists $self->{"_promises"}{$cache_key}
Truth table100%3/3
A and B
A= $self->{"cache"}B= exists $self->{"_promises"}{$cache_key}
ABresult
0X0
100
111
48 464 return $self->{_promises}{$cache_key};
49 }
50 2017 return $self->_enqueue_load_miss($key, $cache_key, $self->{cache});
51 }
52  
53 # dataloader-js semantics: takes an arrayref of keys, returns one promise
54 # that resolves with an arrayref of values in key order. Per-key failures
55 # do not reject the promise; the failed slots hold
56 # GraphQL::Houtou::DataLoader::Error objects instead.
57 28 ▶sub load_many {
Subroutine load_many: called CC=5 SCAR=16.2
Pod undocumented
58 28 my ($self, @args) = @_;
59 28 ▶ if (@args != 1 || ref($args[0]) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args != 1 or ref $args[0] ne "ARRAY") 1 27 28
Condition66%2/3
@args != 1 or ref $args[0] ne "ARRAY"
l!l&&r!l&&!r
1027
MC/DC50%1/2
@args != 1 or ref $args[0] ne "ARRAY"
@args != 1 ref $args[0] ne "ARRAY"
Truth table66%2/3
A or B
A= @args != 1B= ref $args[0] ne "ARRAY"
ABresult
1X1
011
000
60 1 warnings::warnif(deprecated =>
61 'GraphQL::Houtou::DataLoader::load_many(LIST) is deprecated; '
62 . 'pass an arrayref of keys to get a single promise');
63 1 return map { $self->load($_) } @args;
64 }
65 27 my $keys = $args[0];
66 return GraphQL::Houtou::DataLoader::Ticket->resolved(
67 [], $self->{_async_adapter},
68 27 ▶ ) if !@$keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @$keys 1 26 27
69  
70 26 my @results;
71 26 my $remaining = @$keys;
72 my $ticket = GraphQL::Houtou::DataLoader::Ticket->new(
73 $self->{_async_adapter},
74 26 );
75 26 for my $i (0 .. $#$keys) {
76 89 my $slot = $i;
77 $self->load($keys->[$slot])->_subscribe(
78 88 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
79 88 $results[$slot] = $_[0];
80 88 ▶ $ticket->_resolve(\@results) if !--$remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless --$remaining 26 62 88
81 88 return;
82 },
83 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
84 1 my ($reason) = @_;
85 1 ▶ $results[$slot] =
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($reason) && $reason->isa('GraphQL::Houtou::DataLoader::Error') ? : 0 1 1
Condition33%1/3
builtin::blessed($reason) and $reason->isa("GraphQL::Houtou::DataLoader::Error")
!ll&&!rl&&r
100
MC/DC0%0/2
builtin::blessed($reason) and $reason->isa("GraphQL::Houtou::DataLoader::Error")
builtin::blessed($reason) $reason->isa("GraphQL::Houtou::DataLoader::Error")
Truth table33%1/3
A and B
A= builtin::blessed($reason)B= $reason->isa("GraphQL::Houtou::DataLoader::Error")
ABresult
0X0
100
111
86 blessed($reason) && $reason->isa('GraphQL::Houtou::DataLoader::Error')
87 ? $reason
88 : GraphQL::Houtou::DataLoader::Error->new($reason);
89 1 ▶ $ticket->_resolve(\@results) if !--$remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless --$remaining 0 1 1
90 1 return;
91 },
92 89 );
93 }
94 26 return $ticket;
95 }
96  
97 3 ▶sub prime {
Subroutine prime: called CC=4 SCAR=15.2
Pod undocumented
98 3 my ($self, $key, $value) = @_;
99 3 ▶ my $cache_key = $self->{cache_key} ? $self->{cache_key}->($key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache_key'} ? : 0 3 3
100 3 ▶ return $self if !$self->{cache} || exists $self->{_promises}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"cache"} or exists $self->{"_promises"}{$cache_key} 0 3 3
Condition33%1/3
not $self->{"cache"} or exists $self->{"_promises"}{$cache_key}
l!l&&r!l&&!r
003
MC/DC0%0/2
not $self->{"cache"} or exists $self->{"_promises"}{$cache_key}
not $self->{"cache"} exists $self->{"_promises"}{$cache_key}
Truth table33%1/3
not(A) or B
A= not $self->{"cache"}B= exists $self->{"_promises"}{$cache_key}
ABresult
1X1
011
000
101 $self->{_promises}{$cache_key} =
102 GraphQL::Houtou::DataLoader::Ticket->resolved(
103 $value, $self->{_async_adapter},
104 3 );
105 3 return $self;
106 }
107  
108 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my ($self, $key) = @_;
110 0 ▶ my $cache_key = $self->{cache_key} ? $self->{cache_key}->($key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cache_key'} ? : 0 0 0
111 0 delete $self->{_promises}{$cache_key};
112 0 return $self;
113 }
114  
115 0 ▶sub clear_all {
Subroutine clear_all: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self) = @_;
117 0 $self->{_promises} = {};
118 0 return $self;
119 }
120  
121 7 ▶sub pending_count { return scalar @{ $_[0]{_queue} } }
Subroutine pending_count: called CC=1 SCAR=0.0
Pod undocumented
122  
123 # Send every queued key to the batch function and settle the promises.
124 # Returns the number of keys dispatched (the on_stall progress signal).
125 # Settling promises runs GraphQL continuations synchronously, which may
126 # queue new loads; those land in a fresh queue for the next dispatch.
127 2998 ▶sub dispatch {
Subroutine dispatch: called CC=2 SCAR=6.9
Pod undocumented
128 2998 my ($self) = @_;
129 2998 my $queue = $self->{_queue};
130 2998 ▶ return 0 if !@$queue;
Branch: 1 branch
BranchTrueFalseTotal
unless @$queue 1501 1497 2998
131 1497 $self->{_queue} = [];
132 1497 return $self->_dispatch_queue($queue);
133 }
134  
135 # Build an on_stall callback that keeps dispatching a set of loaders until
136 # a full pass makes no progress. Loaders may feed each other: settling one
137 # loader's promises can queue loads on another within the same stall.
138 1480 ▶sub on_stall_for {
Subroutine on_stall_for: called CC=1 SCAR=0.0
Pod undocumented
139 1480 my ($class, @loaders) = @_;
140 1470 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
141 1470 my $total = 0;
142 1470 my $round;
143 1470 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$round and do { do { $round = 0; $round += $_->dispatch foreach (@loaders); $total += $round } } 1475 1470 2945
144 2945 $round = 0;
145 2945 $round += $_->dispatch for @loaders;
146 2945 $total += $round;
147 } while ($round);
148 1470 return $total;
149 1480 };
150 }
151  
152 package GraphQL::Houtou::DataLoader::Error;
153  
154 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
155 8 my ($class, $message) = @_;
156 8 return bless { message => $message }, $class;
157 }
158  
159 1 ▶sub message { return $_[0]{message} }
Subroutine message: called CC=1 SCAR=0.0
160  
161 package GraphQL::Houtou::DataLoader::Ticket;
162  
163 0 ▶sub _new_pending {
Subroutine _new_pending: not called CC=1 SCAR=6.9
164 0 my ($adapter) = @_;
165 0 return @{ $adapter->new_pending };
166 }
167  
168 0 ▶sub _as_promise {
Subroutine _as_promise: not called CC=1 SCAR=6.9
169 0 my ($self) = @_;
170 0 return GraphQL::Houtou::Async::Adapter::ticket_promise($self);
171 }
172  
173 10 *then = \&GraphQL::Houtou::Async::Adapter::ticket_then;
174  
175 0 ▶sub _catch_adapter {
Subroutine _catch_adapter: not called CC=1 SCAR=6.9
176 0 my ($adapter, $self, $callback) = @_;
177 0 ▶ return $adapter->then($self->_as_promise, sub { $_[0] }, $callback);
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 }
179  
180 10 *catch = \&GraphQL::Houtou::Async::Adapter::ticket_catch;
181  
182 0 ▶sub _after {
Subroutine _after: not called CC=3 SCAR=24.8
183 0 my ($adapter, $callback, $done, $reject) = @_;
184 0 my $result;
185 0 ▶ eval { $result = $callback->(); 1 } or return $reject->($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $result = &$callback(); 1 } } or return &$reject($@) 0 0 0
186 0 ▶ return $adapter->then($result, sub { $done->() }, $reject)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $adapter->is_promise($result) 0 0 0
187 if $adapter->is_promise($result);
188 0 return $done->();
189 }
190  
191 0 ▶sub _finally_adapter {
Subroutine _finally_adapter: not called CC=1 SCAR=6.9
192 0 my ($adapter, $self, $callback) = @_;
193 0 my ($promise, $resolve, $reject) = _new_pending($adapter);
194 $self->_subscribe_native(
195 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 my $value = $_[0];
197 0 ▶ _after($adapter, $callback, sub { $resolve->($value) }, $reject);
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 },
199 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 my $reason = $_[0];
201 0 ▶ _after($adapter, $callback, sub { $reject->($reason) }, $reject);
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 },
203 0 );
204 0 return $promise;
205 }
206  
207 10 *finally = \&GraphQL::Houtou::Async::Adapter::ticket_finally;
208  
209 0 ▶sub _adapt_values {
Subroutine _adapt_values: not called CC=3 SCAR=24.8
210 return [ map {
211 0 ▶ Scalar::Util::blessed($_)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('GraphQL::Houtou::DataLoader::Ticket') ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("GraphQL::Houtou::DataLoader::Ticket")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("GraphQL::Houtou::DataLoader::Ticket")
builtin::blessed($_) $_->isa("GraphQL::Houtou::DataLoader::Ticket")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("GraphQL::Houtou::DataLoader::Ticket")
ABresult
0X0
100
111
212 && $_->isa('GraphQL::Houtou::DataLoader::Ticket')
213 ? $_->_as_promise : $_
214 } @_ ];
215 }
216  
217 0 ▶sub _all_adapter {
Subroutine _all_adapter: not called CC=1 SCAR=6.9
218 0 my ($adapter, @values) = @_;
219 0 return $adapter->all(_adapt_values(@values));
220 }
221  
222 10 *all = \&GraphQL::Houtou::Async::Adapter::ticket_all;
223  
224 0 ▶sub _race_adapter {
Subroutine _race_adapter: not called CC=4 SCAR=30.0
225 0 my ($adapter, @values) = @_;
226 0 my ($promise, $resolve, $reject) = _new_pending($adapter);
227 0 my $settled = 0;
228 0 for (@{ _adapt_values(@values) }) {
229 0 ▶ if (!$adapter->is_promise($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($adapter->is_promise($_)) 0 0 0
230 0 ▶ $resolve->($_) if !$settled++;
Branch: 1 branch
BranchTrueFalseTotal
unless $settled++ 0 0 0
231 0 next;
232 }
233 $adapter->then(
234 $_,
235 0 ▶ sub { $resolve->(@_) if !$settled++; return },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $settled++ 0 0 0
236 0 ▶ sub { $reject->(@_) if !$settled++; return },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $settled++ 0 0 0
237 0 );
238 }
239 0 return $promise;
240 }
241  
242 10 *race = \&GraphQL::Houtou::Async::Adapter::ticket_race;
243  
244 package GraphQL::Houtou::DataLoader;
245  
246 10 1;
247 __END__
« blib/lib/GraphQL/Houtou/Runtime/ExecState.pm ↑ Summary blib/lib/GraphQL/Houtou/Directive.pm »