KiokuDB/TypeMap/Entry/MOP.pm

filter: Statementstmt 93.3% 210 / 225 Branchbran 75.5% 68 / 90 Conditioncond 80.0% 12 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 35 / 35 Podpod 0.0% 0 / 14 totaltotal 85.0% 331 / 389 CC 60 SCAR 17.1
CC
60
Cov
88%
compile_collapse_body
12.0
__ANON__
10.8
__ANON__
8.1
CRAP 5.5
« KiokuDB/Collapser.pm ↑ Summary KiokuDB/Collapser/Buffer.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::TypeMap::Entry::MOP;
4 22 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use KiokuDB::Thunk;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub does_role {
14 13732 ▶ my ($meta, $role) = @_;
Subroutine does_role: called CC=2 SCAR=6.9
15 13732 ▶ return unless my $does = $meta->can('does_role');
Branch: 1 branch
BranchTrueFalseTotal
unless my $does = $meta->can("does_role") 12033 1699 13732
16 1699 return $meta->$does($role);
17 }
18  
19 22 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 22 with (
22 'KiokuDB::TypeMap::Entry::Std',
23 'KiokuDB::TypeMap::Entry::Std::Expand' => {
24 alias => { compile_expand => 'compile_expand_body' },
25 }
26 );
27  
28 22 has check_class_versions => (
29 isa => "Bool",
30 is => "ro",
31 default => 1,
32 );
33  
34 has version_table => (
35 isa => "HashRef[Str|CodeRef|HashRef]",
36 is => "ro",
37 86 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 22 );
39  
40 has class_version_table => (
41 isa => "HashRef[HashRef[Str|CodeRef|HashRef]]",
42 is => "ro",
43 31 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 22 );
45  
46 22 has write_upgrades => (
47 isa => "Bool",
48 is => "ro",
49 default => 0,
50 );
51  
52 # FIXME collapser and expaner should both be methods in Class::MOP::Class,
53 # apart from the visit call
54  
55 sub compile_collapse_body {
56 271 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_collapse_body: called CC=11 SCAR=24.8
Pod undocumented
57  
58 271 my $meta = Class::MOP::get_metaclass_by_name($class);
59  
60 my @attrs = grep {
61 271 ▶ !does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize')
Condition66%2/3
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') || does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
l!l&&r!l&&!r
201370
MC/DC50%1/2
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') || does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
Truth table66%2/3
A || B
A= does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize')B= does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
ABresult
1X1
011
000
62 and
63 !does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
64 } $meta->get_all_attributes;
65  
66 271 my %lazy;
67 271 foreach my $attr ( @attrs ) {
68 1370 $lazy{$attr->name} = does_role($attr->meta, "KiokuDB::Meta::Attribute::Lazy");
69 }
70  
71 271 my $meta_instance = $meta->get_meta_instance;
72  
73 271 my %attrs;
74  
75 271 ▶ if ( $meta->is_anon_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->is_anon_class) 35 236 271
76  
77 # FIXME ancestral roles all the way up to first non anon ancestor,
78 # at least check for additional attributes or other metadata which we
79 # should probably error on anything we can't store
80  
81 # theoretically this can do multiple inheritence too
82  
83 35 my $ancestor = $meta;
84 35 my @anon;
85  
86 search: {
87 35 push @anon, $ancestor;
88  
89 35 my @super = $ancestor->superclasses;
90  
91 35 ▶ if ( @super == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@super == 1) { } 35 0 35
elsif (@super > 1) { } 0 0 0
92 35 $ancestor = Class::MOP::get_metaclass_by_name($super[0]);
93 35 ▶ if ( $ancestor->is_anon_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor->is_anon_class) 0 35 35
94 0 redo search;
95 }
96 } elsif ( @super > 1 ) {
97 0 croak "Cannot resolve anonymous class with multiple inheritence: " . $meta->name;
98 } else {
99 0 croak "no super, ancestor: $ancestor (" . $ancestor->name . ")";
100 }
101 }
102  
103 35 my $class_meta = $ancestor->name;
104  
105 35 foreach my $anon ( reverse @anon ) {
106 $class_meta = {
107 roles => [
108 35 map { $_->name } map {
109 $_->isa("Moose::Meta::Role::Composite")
110 35 ▶ ? @{$_->get_roles}
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('Moose::Meta::Role::Composite') ? : 0 35 35
111 : $_
112 35 } @{ $anon->roles }
113 ],
114 superclasses => [ $class_meta ],
115 };
116 }
117  
118 35 ▶ if ( $class_meta->{superclasses}[0] eq $ancestor->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_meta->{"superclasses"}[0] eq $ancestor->name) 35 0 35
119 # no need for redundancy, expansion will provide this as the default
120 35 delete $class_meta->{superclasses};
121 }
122  
123 %attrs = (
124 35 class => $ancestor->name,
125 class_meta => $class_meta,
126 );
127 }
128  
129 271 my $immutable = does_role($meta, "KiokuDB::Role::Immutable");
130 271 my $content_id = does_role($meta, "KiokuDB::Role::ID::Content");
131  
132 271 my @extra_args;
133  
134 271 ▶ if ( defined( my $version = $meta->version ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $version = $meta->version)) 30 241 271
135 30 push @extra_args, class_version => "$version"; # force stringification for version objects
136 }
137  
138 return (
139 sub {
140 2938 ▶ my ( $self, %args ) = @_;
Subroutine __ANON__: called CC=10 SCAR=23.8
141  
142 2938 my $object = $args{object};
143  
144 2938 ▶ if ( $immutable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($immutable) 178 2760 2938
145 # FIXME this doesn't handle unset_root
146 178 ▶ if ( $self->live_objects->object_in_storage($object) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->live_objects->object_in_storage($object)) { } 27 151 178
elsif ($content_id) { } 149 2 151
147 27 return $self->make_skip_entry( %args, prev => $self->live_objects->object_to_entry($object) );
148 } elsif ( $content_id ) {
149 149 ▶ if ( ($self->backend->exists($args{id}))[0] ) { # exists works in list context
Branch: 1 branch
BranchTrueFalseTotal
if (($self->backend->exists($args{"id"}))[0]) 58 91 149
150 58 return $self->make_skip_entry(%args);
151 }
152 }
153 }
154  
155 2853 my %collapsed;
156  
157 2853 attr: foreach my $attr ( @attrs ) {
158 21062 my $name = $attr->name;
159 21062 ▶ if ( $attr->has_value($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_value($object)) 12316 8746 21062
160 12316 ▶ if ( $lazy{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lazy{$name}) 0 12316 12316
161 0 my $value = $meta_instance->Class::MOP::Instance::get_slot_value($object, $name); # FIXME fix KiokuDB::Meta::Instance to allow fetching thunk
162  
163 0 ▶ if ( ref $value eq 'KiokuDB::Thunk' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "KiokuDB::Thunk") 0 0 0
164 0 $collapsed{$name} = $value->collapsed;
165 0 next attr;
166 }
167 }
168  
169 12316 my $value = $attr->get_raw_value($object);
170 12316 ▶ $collapsed{$name} = ref($value) ? $self->visit($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 8373 3943 12316
171 }
172 }
173  
174 2848 return $self->make_entry(
175 @extra_args,
176 %args,
177 data => \%collapsed,
178 );
179 },
180 271 %attrs,
181 );
182 }
183  
184 sub compile_expand {
185 271 ▶ my ( $self, $class, $resolver, @args ) = @_;
Subroutine compile_expand: called CC=1 SCAR=0.0
Pod undocumented
186  
187 271 my $meta = Class::MOP::get_metaclass_by_name($class);
188  
189 271 my $typemap_entry = $self;
190  
191 271 my $anon = $meta->is_anon_class;
192  
193 271 my $inner = $self->compile_expand_body($class, $resolver, @args);
194  
195 271 my $version = $meta->version;
196  
197 return sub {
198 4077 ▶ my ( $linker, $entry, @args ) = @_;
Subroutine __ANON__: called CC=8 SCAR=20.9
199  
200 4077 ▶ if ( $entry->has_class_meta and !$anon ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->has_class_meta and not $anon) 27 4050 4077
Condition100%3/3
$entry->has_class_meta and not $anon
!ll&&!rl&&r
40232727
MC/DC100%2/2
$entry->has_class_meta and not $anon
$entry->has_class_meta not $anon
Truth table100%3/3
A and not(B)
A= $entry->has_class_metaB= not $anon
ABresult
0X0
100
111
201 # the entry is for an anonymous subclass of this class, we need to
202 # compile that entry and short circuit to it. if $anon is true then
203 # we're already compiled, and the class_meta is already handled
204 27 my $anon_meta = $self->reconstruct_anon_class($entry);
205  
206 27 my $anon_class = $anon_meta->name;
207  
208 27 ▶ unless ( $resolver->resolved($anon_class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resolver->resolved($anon_class)) 0 27 27
209 0 $resolver->compile_entry($anon_class, $typemap_entry);
210 }
211  
212 27 my $method = $resolver->expand_method($anon_class);
213 27 return $linker->$method($entry, @args);
214 }
215  
216 4050 ▶ if ( !$self->check_class_versions or $self->is_version_up_to_date($meta, $version, $entry->class_version) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->check_class_versions or $self->is_version_up_to_date($meta, $version, $entry->class_version)) { } 4037 13 4050
Condition100%3/3
not $self->check_class_versions or $self->is_version_up_to_date($meta, $version, $entry->class_version)
l!l&&r!l&&!r
393410313
MC/DC100%2/2
not $self->check_class_versions or $self->is_version_up_to_date($meta, $version, $entry->class_version)
not $self->check_class_versions $self->is_version_up_to_date($meta, $version, $entry->class_version)
Truth table100%3/3
not(A) or B
A= not $self->check_class_versionsB= $self->is_version_up_to_date($meta, $version, $entry->class_version)
ABresult
1X1
011
000
217 4037 $linker->$inner($entry, @args);
218 } else {
219 13 my $upgraded = $self->upgrade_entry( linker => $linker, meta => $meta, entry => $entry, expand_args => \@args);
220  
221 10 ▶ if ( $self->write_upgrades ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->write_upgrades) 5 5 10
222 5 ▶ croak "Upgraded entry can't be updated (mismatch in 'prev' chain)"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($entry) == builtin::refaddr($upgraded->root_prev) 0 5 5
223 unless refaddr($entry) == refaddr($upgraded->root_prev);
224  
225 5 $linker->backend->insert($upgraded);
226 }
227  
228 10 $linker->$inner($upgraded, @args);
229 }
230 }
231 271 }
232  
233 22 { my %cache;
234 sub is_version_up_to_date {
235 131 ▶ my ( $self, $meta, $version, $entry_version ) = @_;
Subroutine is_version_up_to_date: called CC=5 SCAR=16.1
Pod undocumented
236  
237 # no clever stuff, only if they are the same string they are the same version
238  
239 22 ▶ no warnings 'uninitialized'; # undef $VERSION is allowed
Subroutine BEGIN: called CC=1 SCAR=0.0
240 131 ▶ return 1 if $version eq $entry_version;
Branch: 1 branch
BranchTrueFalseTotal
if $version eq $entry_version 111 20 131
241  
242 20 my $key = join(":", $meta->name, $entry_version); # $VERSION isn't supposed to change at runtime
243  
244 20 ▶ return $cache{$key} if exists $cache{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache{$key} 4 16 20
245  
246 # check the version table for equivalent versions (recursively)
247 # ref handlers are upgrade hooks
248 16 foreach my $handler ( $self->find_version_handlers($meta, $entry_version) ) {
249 12 ▶ return $cache{$key} = $self->is_version_up_to_date( $meta, $version, $handler ) if not ref $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handler 6 6 12
250 }
251  
252 10 return $cache{$key} = undef;
253 }
254  
255 22 ▶sub clear_version_cache { %cache = () }
Subroutine clear_version_cache: called CC=1 SCAR=0.0
Pod undocumented
256 }
257  
258 22 sub find_version_handlers {
259 32 ▶ my ( $self, $meta, $version ) = @_;
Subroutine find_version_handlers: called CC=2 SCAR=6.9
Pod undocumented
260  
261 22 ▶ no warnings 'uninitialized'; # undef $VERSION is allowed
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 32 ▶ if ( does_role($meta, "KiokuDB::Role::Upgrade::Handlers") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (does_role($meta, 'KiokuDB::Role::Upgrade::Handlers')) { } 21 11 32
264 21 return $meta->name->kiokudb_upgrade_handler($version);
265 } else {
266 11 return grep { defined } map { $_->{$version} } $self->class_version_table->{$meta->name}, $self->version_table;
267 }
268 }
269  
270 sub upgrade_entry {
271 13 ▶ my ( $self, %args ) = @_;
Subroutine upgrade_entry: called CC=2 SCAR=6.9
Pod undocumented
272  
273 13 my ( $meta, $entry ) = @args{qw(meta entry)};
274  
275 13 ▶ if ( does_role($meta, "KiokuDB::Role::Upgrade::Data") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (does_role($meta, 'KiokuDB::Role::Upgrade::Data')) { } 1 12 13
276 1 return $meta->name->kiokudb_upgrade_data(%args);
277 } else {
278 12 return $self->upgrade_entry_from_version( %args, from_version => $entry->class_version );
279 }
280 }
281  
282 sub upgrade_entry_from_version {
283 16 ▶ my ( $self, %args ) = @_;
Subroutine upgrade_entry_from_version: called CC=6 SCAR=19.7
Pod undocumented
284  
285 16 my ( $meta, $from_version, $entry ) = @args{qw(meta from_version entry)};
286  
287 22 ▶ no warnings 'uninitialized'; # undef $VERSION is allowed
Subroutine BEGIN: called CC=1 SCAR=0.0
288  
289 16 foreach my $handler ( $self->find_version_handlers($meta, $from_version) ) {
290 13 ▶ if ( ref $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler) { } 9 4 13
291  
292 9 my $cb = $self->_process_upgrade_handler($handler);
293  
294 # apply handler
295 9 my $converted = $self->$cb(%args);
296  
297 9 ▶ if ( $self->is_version_up_to_date( $meta, $meta->version, $converted->class_version ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_version_up_to_date($meta, $meta->version, $converted->class_version)) { } 9 0 9
elsif ($entry->class_version eq $converted->class_version) { } 0 0 0
298 9 return $converted;
299 } elsif ( $entry->class_version eq $converted->class_version ) {
300 0 croak "Upgrade from " . $entry->class_version . " did change 'class_version' field";
301 } else {
302 # more error context
303 return try {
304 0 $self->upgrade_entry_from_version(%args, entry => $converted, from_version => $converted->class_version);
305 0 } catch {
306 0 croak "$_\n... when upgrading from $from_version";
307 };
308 }
309 } else {
310 # nonref is equivalent version, recursively search for handlers for that version
311 4 return $self->upgrade_entry_from_version( %args, from_version => $handler );
312 }
313 }
314  
315 3 ▶ croak "No handler found for " . $meta->name . " version $from_version" . ( $entry->class_version ne $from_version ? "(entry version is " . $entry->class_version . ")" : "" );
Branch: 1 branch
BranchTrueFalseTotal
$entry->class_version ne $from_version ? : 0 3 3
316 }
317  
318 sub _process_upgrade_handler {
319 9 ▶ my ( $self, $handler ) = @_;
Subroutine _process_upgrade_handler: called CC=5 SCAR=16.3
320  
321 9 ▶ if ( ref $handler eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "HASH") 4 5 9
322 croak "Data provided in upgrade handler must be a hash"
323 4 ▶ if ref $handler->{data} and ref $handler->{data} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $handler->{"data"} and ref $handler->{"data"} ne "HASH" 0 4 4
Condition66%2/3
ref $handler->{"data"} and ref $handler->{"data"} ne "HASH"
!ll&&!rl&&r
220
MC/DC0%0/2
ref $handler->{"data"} and ref $handler->{"data"} ne "HASH"
ref $handler->{"data"} ref $handler->{"data"} ne "HASH"
Truth table66%2/3
A and B
A= ref $handler->{"data"}B= ref $handler->{"data"} ne "HASH"
ABresult
0X0
100
111
324  
325 croak "No class_version provided in upgrade handler"
326 4 ▶ unless defined $handler->{class_version};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handler->{"class_version"} 0 4 4
327  
328 return sub {
329 4 ▶ my ( $self, %args ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
330  
331 4 my $entry = $args{entry};
332  
333 4 ▶ croak "Entry data not a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry->data eq "HASH" 0 4 4
334 unless ref $entry->data eq 'HASH';
335  
336 $entry->derive(
337 %$handler,
338 data => {
339 4 %{ $entry->data },
340 4 ▶ %{ $handler->{data} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$handler->{"data"} or {} 2 2 4
341 },
342 );
343 4 };
344 }
345  
346 5 return $handler;
347 }
348  
349 sub compile_create {
350 271 ▶ my ( $self, $class ) = @_;
Subroutine compile_create: called CC=2 SCAR=6.9
Pod undocumented
351  
352 271 my $meta = Class::MOP::get_metaclass_by_name($class);
353  
354 271 my $meta_instance = $meta->get_meta_instance;
355  
356 271 my $cache = does_role($meta, "KiokuDB::Role::Cacheable");
357  
358 271 ▶ my @register_args = (
Branch: 1 branch
BranchTrueFalseTotal
$cache ? : 33 238 271
359 ( $cache ? ( cache => 1 ) : () ),
360 );
361  
362 return sub {
363 4047 ▶ return ( $meta_instance->create_instance(), @register_args );
Subroutine __ANON__: called CC=1 SCAR=0.0
364 271 };
365 }
366  
367 sub compile_clear {
368 271 ▶ my ( $self, $class ) = @_;
Subroutine compile_clear: called CC=1 SCAR=0.0
Pod undocumented
369  
370 return sub {
371 27 ▶ my ( $linker, $obj ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
372 27 %$obj = (); # FIXME
373 }
374 271 }
375  
376 sub compile_expand_data {
377 542 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_expand_data: called CC=3 SCAR=11.1
Pod undocumented
378  
379 542 my $meta = Class::MOP::get_metaclass_by_name($class);
380  
381 542 my $meta_instance = $meta->get_meta_instance;
382  
383 542 my ( %attrs, %lazy );
384  
385 my @attrs = grep {
386 542 ▶ !does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize')
Condition66%2/3
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') || does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
l!l&&r!l&&!r
402740
MC/DC50%1/2
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') || does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize') does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
Truth table66%2/3
A || B
A= does_role($_->meta, 'KiokuDB::Meta::Attribute::DoNotSerialize')B= does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
ABresult
1X1
011
000
387 and
388 !does_role($_->meta, 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
389 } $meta->get_all_attributes;
390  
391 542 foreach my $attr ( @attrs ) {
392 2740 $attrs{$attr->name} = $attr;
393 2740 $lazy{$attr->name} = does_role($attr->meta, "KiokuDB::Meta::Attribute::Lazy");
394 }
395  
396 return sub {
397 4074 ▶ my ( $linker, $instance, $entry, @args ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.2
398  
399 4074 my $data = $entry->data;
400  
401 4074 my @values;
402  
403 4074 foreach my $name ( keys %$data ) {
404 17575 ▶ my $attr = $attrs{$name} or croak "Unknown attribute: $name";
Branch: 1 branch
BranchTrueFalseTotal
my $attr = $attrs{$name} or croak("Unknown attribute: $name") 0 17575 17575
405 17575 my $value = $data->{$name};
406  
407 17575 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 11744 5831 17575
408 11744 ▶ if ( $lazy{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lazy{$name}) { } 0 11744 11744
409 0 my $thunk = KiokuDB::Thunk->new( collapsed => $value, linker => $linker, attr => $attr );
410 0 $attr->set_raw_value($instance, $thunk);
411 } else {
412 11744 my @pair = ( $attr, undef );
413  
414 11744 ▶ $linker->inflate_data($value, \$pair[1]) if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 11744 0 11744
415 11744 push @values, \@pair;
416 }
417 } else {
418 5831 $attr->set_raw_value($instance, $value);
419 }
420 }
421  
422 $linker->queue_finalizer(sub {
423 4074 ▶ foreach my $pair ( @values ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
424 11744 my ( $attr, $value ) = @$pair;
425 11744 $attr->set_raw_value($instance, $value);
426 11744 ▶ $attr->_weaken_value($instance) if $attr->is_weak_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->is_weak_ref 647 11097 11744
427 }
428 4074 });
429  
430 4074 return $instance;
431 }
432 542 }
433  
434 sub reconstruct_anon_class {
435 27 ▶ my ( $self, $entry ) = @_;
Subroutine reconstruct_anon_class: called CC=1 SCAR=0.0
Pod undocumented
436  
437 $self->inflate_class_meta(
438 superclasses => [ $entry->class ],
439 27 %{ $entry->class_meta },
440 );
441 }
442  
443 sub inflate_class_meta {
444 27 ▶ my ( $self, %meta ) = @_;
Subroutine inflate_class_meta: called CC=3 SCAR=11.1
Pod undocumented
445  
446 27 foreach my $super ( @{ $meta{superclasses} } ) {
447 27 ▶ $super = $self->inflate_class_meta(%$super)->name if ref $super;
Branch: 1 branch
BranchTrueFalseTotal
if ref $super 0 27 27
448 }
449  
450 # FIXME should probably get_meta_by_name($entry->class)
451 Moose::Meta::Class->create_anon_class(
452 27 cache => 1,
453 %meta,
454 );
455 }
456  
457 sub compile_id {
458 271 ▶ my ( $self, $class ) = @_;
Subroutine compile_id: called CC=2 SCAR=6.9
Pod undocumented
459  
460 271 ▶ if ( does_role(Class::MOP::get_metaclass_by_name($class), "KiokuDB::Role::ID") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (does_role(Class::MOP::get_metaclass_by_name($class), 'KiokuDB::Role::ID')) { } 70 201 271
461 return sub {
462 202 ▶ my ( $self, $object ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
463 202 return $object->kiokudb_object_id;
464 }
465 70 } else {
466 201 return "generate_uuid";
467 }
468 }
469  
470 sub should_compile_intrinsic {
471 271 ▶ my ( $self, $class, @args ) = @_;
Subroutine should_compile_intrinsic: called CC=3 SCAR=11.0
Pod undocumented
472  
473 271 my $meta = Class::MOP::get_metaclass_by_name($class);
474  
475 271 ▶ if ( $self->has_intrinsic ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_intrinsic) { } 4 267 271
elsif (does_role($meta, 'KiokuDB::Role::Intrinsic')) { } 2 265 267
476 4 return $self->intrinsic;
477 } elsif ( does_role($meta, "KiokuDB::Role::Intrinsic") ) {
478 2 return 1;
479 } else {
480 265 return 0;
481 }
482 }
483  
484 22 __PACKAGE__->meta->make_immutable;
485  
486 22 __PACKAGE__
487  
488 __END__
489  
490 =pod
491  
492 =head1 NAME
493  
494 KiokuDB::TypeMap::Entry::MOP - A L<KiokuDB::TypeMap> entry for objects with a
495 metaclass.
496  
497 =head1 SYNOPSIS
498  
499 KiokuDB::TypeMap->new(
500 entries => {
501 'My::Class' => KiokuDB::TypeMap::Entry::MOP->new(
502 intrinsic => 1,
503 ),
504 },
505 );
506  
507 =head1 DESCRIPTION
508  
509 This typemap entry handles collapsing and expanding of L<Moose> based objects.
510  
511 It supports anonymous classes with runtime roles, the L<KiokuDB::Role::ID> role.
512  
513 Code for immutable classes is cached and performs several orders of magnitude
514 better, so make use of L<Moose::Meta::Class/make_immutable>.
515  
516 =head1 ATTRIBUTES
517  
518 =over 4
519  
520 =item intrinsic
521  
522 If true the object will be collapsed as part of its parent, without an ID.
523  
524 =item check_class_versions
525  
526 If true (the default) then class versions will be checked on load and if there
527 is a mismatch between the stored version number and the current version number,
528 the version upgrade handler tables will be used to convert the out of date
529 entry.
530  
531 =item version_table
532  
533 =item class_version_table
534  
535 Tables of handlers.
536  
537 See also L<KiokuDB::Role::Upgrade::Data> and
538 L<KiokuDB::Role::Upgrade::Handlers::Table> for convenience roles that do not
539 require a central table.
540  
541 The first is a global version table (useful when the typemap entry is only
542 handling one class) and the second is a table of tables keyed by the class name.
543  
544 The tables are keyed by version number (as a string, C<undef> and C<""> are
545 considered the same), and the value can be either a code reference that
546 processes the entry to bring it up to date, a hash reference of overridden
547 fields, or a string denoting a version number that this version is equivalent
548 to.
549  
550 Version numbers have no actual ordinal meaning, they are taken as simple string
551 identifiers.
552  
553 If we had 3 versions, C<1.0>, C<1.1> and C<2.0>, where C<1.1> is a minor update
554 to the class that requires no structural changes from C<1.0>, our table could
555 be written like this:
556  
557 {
558 '1.0' => '1.1', # upgrading the data from 1.0 to 1.1 is a noop
559 '1.1' => sub {
560 my ( $self, %args ) = @_;
561  
562 # manually convert the entry data
563 return $entry->clone(
564 class_version => '2.0',
565 prev => $entry,
566 data => ...,
567 ),
568 },
569 }
570  
571 When an object that was stored as version C<1.0> is retrieved from the
572 database, and the current definition of the class has C<$VERSION> C<2.0>,
573 table declares C<1.0> is the same as C<1.1>, so we search for the handler for
574 C<1.1> and apply it.
575  
576 The resulting class has the version C<2.0> which is the same as what we have
577 now, so this object can be thawed.
578  
579 The callback is invoked with the following arguments:
580  
581 =over 4
582  
583 =item entry
584  
585 The entry to upgrade.
586  
587 =item from_version
588  
589 The key under which the handler was found (not necessarily the same as
590 C<< $entry->class_version >>).
591  
592 =item meta
593  
594 The L<Class::MOP::Class> of the entry's class.
595  
596 =item linker
597  
598 The L<KiokuDB::Linker> instance that is inflating this object.
599  
600 Can be used to retrieve additional required objects (cycles are not a problem
601 but be aware that the objects might not be usable yet at the time of the
602 callback's invocation).
603  
604 =back
605  
606 When a hash is provided as a handler it'll be used to create an entry like
607 this:
608  
609 $entry->derive(
610 %$handler,
611 data => {
612 %{ $entry->data },
613 %{ $handler->{data} || {} },
614 },
615 );
616  
617 The field C<class_version> is required, and C<data> must contain a hash:
618  
619 KiokuDB->connect(
620 class_version_table => {
621 Foo => {
622 "0.02" => {
623 class_version => "0.03", # upgrade 0.02 to 0.03
624 data => {
625 a_new_field => "default_value",
626 },
627 },
628 },
629 },
630 );
631  
632 =item write_upgrades
633  
634 If true, after applying version upgrade handlers, the updated entry will be
635 written back to the database.
636  
637 Defaults to false but might default to true in future versions (unless the
638 database is in readonly mode).
639  
640 =back
« KiokuDB/Collapser.pm ↑ Summary KiokuDB/Collapser/Buffer.pm »