blib/lib/DBIx/Class/TopoSort.pm

filter: Statementstmt 75.3% 55 / 73 Branchbran 55.5% 10 / 18 Conditioncond 27.2% 3 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 65.6% 82 / 125 CC 18 SCAR 21.3
CC
18
Cov
67%
__ANON__
20.0
enable_toposort_memoize
12.0
toposort_graph
10.0
CRAP 8.4
↑ Summary
1 # vim: set noai ts=4 sw=4:
2 package DBIx::Class::TopoSort;
3  
4 7 ▶use 5.008_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.050100';
10  
11 7 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Even though JSON::MaybeXS is recommended, DBIx::Class already uses JSON::Any,
14 # so we can depend on it existing in the world.
15 # Note: We cannot use JSON::DWIW because it does not provide a canonical()
16 # method. We need this in order to ensure the same hash is encoded the same way
17 # every time. Otherwise, preserve the order provided.
18 7 ▶use JSON::Any qw(CPANEL XS JSON PP);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Memoize qw(memoize unmemoize);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 my $MEMOIZED = 0;
23 sub enable_toposort_memoize {
24 0 ▶ disable_toposort_memoize() if $MEMOIZED;
Subroutine enable_toposort_memoize: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $MEMOIZED 0 0 0
25  
26 0 shift;
27 0 my ($normalizer) = @_;
28 $normalizer ||= sub {
29 0 ▶ my @keys = (
Subroutine __ANON__: not called CC=4 SCAR=30.0
30 $$,
31 );
32  
33 # toposort() could be called either as $schema->toposort(%opts) or as
34 # DBIx::Class::TopoSort->toposort($schema, %opts)
35 0 my $schema = shift;
36 0 ▶ unless (ref($schema) && $schema->isa('DBIx::Class::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema and $schema->isa("DBIx::Class::Schema")) 0 0 0
Condition0%0/3
ref $schema and $schema->isa("DBIx::Class::Schema")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $schema and $schema->isa("DBIx::Class::Schema")
ref $schema $schema->isa("DBIx::Class::Schema")
Truth table0%0/3
A and B
A= ref $schemaB= $schema->isa("DBIx::Class::Schema")
ABresult
0X0
100
111
37 0 $schema = shift;
38 }
39  
40 # If you give me a $schema object, the graph is going to be the same
41 # for every instance of that schema, so we can use the schema's class
42 # for the anchor.
43 0 push @keys, reftype($schema);
44  
45 # We need to track the %opts provided because invocations with different
46 # parameters may result in different outputs.
47 0 my %opts = @_;
48 0 ▶ if (%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opts) 0 0 0
49 0 push @keys, JSON::Any->new->canonical(1)->encode(\%opts);
50 }
51  
52 0 return join ':', @keys;
53 0 ▶ };
Condition0%0/3
$normalizer ||= sub {
l!l&&r!l&&!r
000
MC/DC0%0/2
$normalizer ||= sub { my(@keys) = $$; my $schema = shift(); unless (ref $schema and $schema->isa("DBIx::Class::Schema")) { $schema = shift(); } ; push @keys, builtin::reftype($schema); my(%opts) = @_; if (%opts) { push @keys, "JSON::Any"->new->canonical(1)->encode(\%opts); } ; return join(":", @keys); }
$normalizer sub { my(@keys) = $$; my $schema = shift(); unless (ref $schema and $schema->isa("DBIx::Class::Schema")) { $schema = shift(); } ; push @keys, builtin::reftype($schema); my(%opts) = @_; if (%opts) { push @keys, "JSON::Any"->new->canonical(1)->encode(\%opts); } ; return join(":", @keys); }
Truth table0%0/3
A ||= B
A= $normalizerB= sub { my(@keys) = $$; my $schema = shift(); unless (ref $schema and $schema->isa("DBIx::Class::Schema")) { $schema = shift(); } ; push @keys, builtin::reftype($schema); my(%opts) = @_; if (%opts) { push @keys, "JSON::Any"->new->canonical(1)->encode(\%opts); } ; return join(":", @keys); }
ABresult
1X1
011
000
54  
55 0 $MEMOIZED = 1;
56 0 memoize('DBIx::Class::TopoSort::toposort',
57 NORMALIZER => $normalizer,
58 );
59 }
60  
61 sub disable_toposort_memoize {
62 0 ▶ return unless $MEMOIZED;
Subroutine disable_toposort_memoize: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $MEMOIZED 0 0 0
63  
64 0 $MEMOIZED = 0;
65 0 unmemoize('DBIx::Class::TopoSort::toposort');
66 }
67  
68 sub toposort_graph {
69 9 ▶ my $self = shift;
Subroutine toposort_graph: called CC=10 SCAR=23.0
Pod documented
70 9 my ($schema, %opts) = @_;
71  
72 9 my $g = Graph->new;
73  
74 9 my @source_names = $schema->sources;
75  
76 9 my %table_sources;
77 9 foreach my $name ( @source_names ) {
78 20 my $table_name = $schema->source($name)->name;
79 20 ▶ $table_sources{$table_name} //= [];
Condition50%1/2
$table_sources{$table_name} //= []
l!l
020
MC/DC0%0/1
$table_sources{$table_name} //= []
$table_sources{$table_name}
Truth table50%1/2
A
A= $table_sources{$table_name}
Aresult
11
00
80 20 push @{ $table_sources{$table_name} }, $name;
81 }
82  
83 9 foreach my $name ( @source_names ) {
84 20 my $source = $schema->source($name);
85 20 $g->add_vertex($name);
86  
87 20 foreach my $rel_name ( $source->relationships ) {
88 24 ▶ next if grep { $_ eq $rel_name } @{$opts{skip}{$name}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $rel_name;} @{$opts{"skip"}{$name};} 2 22 24
89 22 my $rel_info = $source->relationship_info($rel_name);
90  
91 22 ▶ if ( $rel_info->{attrs}{is_foreign_key_constraint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_info->{"attrs"}{"is_foreign_key_constraint"}) 12 10 22
92 12 my $sources = $table_sources{$schema->source($rel_info->{source})->name};
93 12 foreach my $source ( @$sources ) {
94 12 $g->add_edge($source, $name);
95 }
96 }
97 }
98 }
99  
100 9 ▶ if ($opts{detect_cycle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"detect_cycle"}) 2 7 9
101 2 my @cycle = $g->find_a_cycle;
102 2 ▶ die 'Found circular relationships between [' . join(', ', @cycle) . ']'
Branch: 1 branch
BranchTrueFalseTotal
if @cycle 1 1 2
103 if @cycle;
104 }
105  
106 8 return $g;
107 }
108  
109 sub toposort {
110 9 ▶ my $self = shift;
Subroutine toposort: called CC=3 SCAR=11.0
Pod documented
111 9 my $schema;
112 9 ▶ if (ref($self) && $self->isa('DBIx::Class::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->isa('DBIx::Class::Schema')) { } 8 1 9
Condition66%2/3
ref $self and $self->isa("DBIx::Class::Schema")
!ll&&!rl&&r
108
MC/DC50%1/2
ref $self and $self->isa("DBIx::Class::Schema")
ref $self $self->isa("DBIx::Class::Schema")
Truth table66%2/3
A and B
A= ref $selfB= $self->isa("DBIx::Class::Schema")
ABresult
0X0
100
111
113 8 $schema = $self;
114 }
115 else {
116 1 $schema = shift(@_);
117 }
118 9 return $self->toposort_graph($schema, @_)->toposort();
119 }
120  
121 7 1;
122 __END__
123  
124 =head1 NAME
125  
126 DBIx::Class::TopoSort - The addition of topological sorting to DBIx::Class
127  
128 =head1 SYNOPSIS
129  
130 Within your schema class:
131  
132 __PACKAGE__->load_components('TopoSort');
133  
134 Later:
135  
136 my $schema = Your::App::Schema->connect(...);
137 my @toposorted_sourcenames = $schema->toposort();
138  
139 If you have a cycle in your relationships
140  
141 my @toposorted_sourcenames = $schema->toposort(
142 skip => {
143 Artist => [qw/ first_album /],
144 },
145 );
146  
147 Alternately:
148  
149 my @toposorted_sourcenames = DBIx::Class::TopoSort->toposort($schema);
150  
151 =head1 DESCRIPTION
152  
153 This adds a method to L<DBIx::Class::Schema> which returns the full list of
154 sources (similar to L<DBIx::Class::Schema/sources>) in topological-sorted order.
155  
156 =head2 TOPOLOGICAL SORT
157  
158 A topological sort of the tables returns the list of tables such that any table
159 with a foreign key relationship appears after any table it has a foreign key
160 relationship to.
161  
162 =head1 METHODS
163  
164 This class is not instantiable nor does it provide any methods of its own. All
165 methods are added to the L<DBIx::Class::Schema> class and are callable on
166 objects instantiated of that class.
167  
168 =head2 toposort
169  
170 This is sugar for:
171  
172 $self->toposort_graph(@_)->toposort();
173  
174 Calling this method multiple times may return the list of source names in
175 different order. Each order will conform to the gurantee described in the
176 section on TOPOLOGICAL SORT.
177  
178 This method will throw an error if there are any cycles in your tables. You will
179 need to specify the skip parameter (described below) to break those cycles.
180  
181 =head2 toposort (Class method)
182  
183 Alternately, if you do not wish to use TopoSort as a component, you can call it
184 as a class method on this class. The toposort() method is smart enough to
185 distinguish.
186  
187 Note: toposort_graph() does B<not> distinguish - it assumes it will be called
188 with the C<$schema> object passed in.
189  
190 =head2 toposort_graph
191  
192 This returns a L<Graph> object with a vertex for every source and an edge for
193 every foreign key relationship.
194  
195 It takes the following parameters.
196  
197 =over 4
198  
199 =item skip
200  
201 This describes the list of relationships that should be ignored by the toposort
202 algorithm. This is generally used if you have cycles in your schema (though it
203 could possibly be useful in other ways, I guess). The value is a hashref. The
204 keys of this hashref are source names and the values are arrays of relationship
205 names.
206  
207 skip => {
208 Artist => [ qw/ first_album / ],
209 },
210  
211 =item detect_cycle
212  
213 If this is true, then L<Graph/find_a_cycle> will be called and, if a cycle is
214 found, this will die detailing the cycle found.
215  
216 This is useful because the L<Graph/toposort> method dies with a cyclic
217 graph, but doesn't tell you what any of the cycles are that killed it.
218  
219 B<NOTE>: Finding cycles can be expensive. Don't do this on a regular basis.
220  
221 =back
222  
223 =head2 enable_toposort_memoize (Class method)
224  
225 This will L<Memoize/memoize> the L</toposort> function. By default, it uses a
226 normalizer function that concatenates the following (in order):
227  
228 =over 4
229  
230 =item * The PID of this process
231  
232 =item * The class of the schema
233  
234 =item * The canonicalized JSON of any options provided.
235  
236 =back
237  
238 You may pass in a different function if you need to.
239  
240 =head2 disable_toposort_memoize (Class method)
241  
242 This will disable any memoize on L</toposort>. Unlike L<Memoize/unmemoize>, this
243 will not croak if you haven't already memoized.
244  
245 =head1 SEE ALSO
246  
247 L<Graph/toposort>
248  
249 =head1 AUTHOR
250  
251 =over 4
252  
253 =item * Rob Kinyon <rob.kinyon@gmail.com>
254  
255 =back
256  
257 =head1 CONTRIBUTIONS
258  
259 Contributions have been generously donated by ZipRecruiter.
260  
261 =head1 LICENSE
262  
263 Copyright (c) 2013 Rob Kinyon. All Rights Reserved.
264 This is free software, you may use it and distribute it under the same terms
265 as Perl itself.
266  
267 =cut
↑ Summary