Filename | /Users/dde/perl5/perlbrew/perls/5.18.0t/lib/site_perl/5.18.0/MooseX/Storage/Engine.pm |
Statements | Executed 17 statements in 1.56ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 8µs | 8µs | BEGIN@5 | MooseX::Storage::Engine::
1 | 1 | 1 | 8µs | 6.17ms | BEGIN@8 | MooseX::Storage::Engine::
1 | 1 | 1 | 7µs | 63µs | BEGIN@365 | MooseX::Storage::Engine::
1 | 1 | 1 | 7µs | 26µs | BEGIN@9 | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:18] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:202] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:210] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:219] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:220] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:222] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:223] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:244] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:24] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:256] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:267] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | __ANON__[:279] | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | add_custom_type_handler | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | check_for_cycle_in_collapse | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | check_for_cycle_in_expansion | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | collapse_attribute | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | collapse_attribute_value | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | collapse_object | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | expand_attribute | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | expand_attribute_value | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | expand_object | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | find_type_handler | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | find_type_handler_for | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | map_attributes | MooseX::Storage::Engine::
0 | 0 | 0 | 0s | 0s | remove_custom_type_handler | MooseX::Storage::Engine::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package MooseX::Storage::Engine; | ||||
2 | { | ||||
3 | 2 | 700ns | $MooseX::Storage::Engine::VERSION = '0.39'; | ||
4 | } | ||||
5 | # spent 8µs within MooseX::Storage::Engine::BEGIN@5 which was called:
# once (8µs+0s) by MooseX::Storage::Basic::BEGIN@10 at line 7 | ||||
6 | 1 | 4µs | $MooseX::Storage::Engine::AUTHORITY = 'cpan:STEVAN'; | ||
7 | 1 | 17µs | 1 | 8µs | } # spent 8µs making 1 call to MooseX::Storage::Engine::BEGIN@5 |
8 | 2 | 32µs | 2 | 12.3ms | # spent 6.17ms (8µs+6.17) within MooseX::Storage::Engine::BEGIN@8 which was called:
# once (8µs+6.17ms) by MooseX::Storage::Basic::BEGIN@10 at line 8 # spent 6.17ms making 1 call to MooseX::Storage::Engine::BEGIN@8
# spent 6.17ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:519] |
9 | 2 | 1.43ms | 2 | 46µs | # spent 26µs (7+20) within MooseX::Storage::Engine::BEGIN@9 which was called:
# once (7µs+20µs) by MooseX::Storage::Basic::BEGIN@10 at line 9 # spent 26µs making 1 call to MooseX::Storage::Engine::BEGIN@9
# spent 20µs making 1 call to Exporter::import |
10 | |||||
11 | # the class marker when | ||||
12 | # serializing an object. | ||||
13 | 1 | 200ns | our $CLASS_MARKER = '__CLASS__'; | ||
14 | |||||
15 | has 'storage' => ( | ||||
16 | is => 'ro', | ||||
17 | isa => 'HashRef', | ||||
18 | default => sub {{}} | ||||
19 | 1 | 3µs | 1 | 1.92ms | ); # spent 1.92ms making 1 call to Moose::has |
20 | |||||
21 | has 'seen' => ( | ||||
22 | is => 'ro', | ||||
23 | isa => 'HashRef[Int]', # int is the refaddr | ||||
24 | default => sub {{}} | ||||
25 | 1 | 2µs | 1 | 2.68ms | ); # spent 2.68ms making 1 call to Moose::has |
26 | |||||
27 | 1 | 1µs | 1 | 6.38ms | has 'object' => (is => 'rw', isa => 'Object', predicate => '_has_object'); # spent 6.38ms making 1 call to Moose::has |
28 | 1 | 1µs | 1 | 1.42ms | has 'class' => (is => 'rw', isa => 'Str'); # spent 1.42ms making 1 call to Moose::has |
29 | |||||
30 | ## this is the API used by other modules ... | ||||
31 | |||||
32 | sub collapse_object { | ||||
33 | my ( $self, %options ) = @_; | ||||
34 | |||||
35 | # NOTE: | ||||
36 | # mark the root object as seen ... | ||||
37 | $self->seen->{refaddr $self->object} = undef; | ||||
38 | |||||
39 | $self->map_attributes('collapse_attribute', \%options); | ||||
40 | $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier; | ||||
41 | return $self->storage; | ||||
42 | } | ||||
43 | |||||
44 | sub expand_object { | ||||
45 | my ($self, $data, %options) = @_; | ||||
46 | |||||
47 | $options{check_version} = 1 unless exists $options{check_version}; | ||||
48 | $options{check_authority} = 1 unless exists $options{check_authority}; | ||||
49 | |||||
50 | # NOTE: | ||||
51 | # mark the root object as seen ... | ||||
52 | $self->seen->{refaddr $data} = undef; | ||||
53 | |||||
54 | $self->map_attributes('expand_attribute', $data, \%options); | ||||
55 | return $self->storage; | ||||
56 | } | ||||
57 | |||||
58 | ## this is the internal API ... | ||||
59 | |||||
60 | sub collapse_attribute { | ||||
61 | my ($self, $attr, $options) = @_; | ||||
62 | my $value = $self->collapse_attribute_value($attr, $options); | ||||
63 | return if !defined($value); | ||||
64 | $self->storage->{$attr->name} = $value; | ||||
65 | } | ||||
66 | |||||
67 | sub expand_attribute { | ||||
68 | my ($self, $attr, $data, $options) = @_; | ||||
69 | my $value = $self->expand_attribute_value($attr, $data->{$attr->name}, $options); | ||||
70 | $self->storage->{$attr->name} = defined $value ? $value : return; | ||||
71 | } | ||||
72 | |||||
73 | sub collapse_attribute_value { | ||||
74 | my ($self, $attr, $options) = @_; | ||||
75 | # Faster, but breaks attributes without readers, do we care? | ||||
76 | #my $value = $attr->get_read_method_ref->($self->object); | ||||
77 | my $value = $attr->get_value($self->object); | ||||
78 | |||||
79 | # NOTE: | ||||
80 | # this might not be enough, we might | ||||
81 | # need to make it possible for the | ||||
82 | # cycle checker to return the value | ||||
83 | $self->check_for_cycle_in_collapse($attr, $value) | ||||
84 | if ref $value; | ||||
85 | |||||
86 | if (defined $value && $attr->has_type_constraint) { | ||||
87 | my $type_converter = $self->find_type_handler($attr->type_constraint, $value); | ||||
88 | (defined $type_converter) | ||||
89 | || confess "Cannot convert " . $attr->type_constraint->name; | ||||
90 | $value = $type_converter->{collapse}->($value, $options); | ||||
91 | } | ||||
92 | return $value; | ||||
93 | } | ||||
94 | |||||
95 | sub expand_attribute_value { | ||||
96 | my ($self, $attr, $value, $options) = @_; | ||||
97 | |||||
98 | # NOTE: | ||||
99 | # (see comment in method above ^^) | ||||
100 | if( ref $value and not( | ||||
101 | $options->{disable_cycle_check} or | ||||
102 | $self->class->does('MooseX::Storage::Traits::DisableCycleDetection') | ||||
103 | )) { | ||||
104 | $self->check_for_cycle_in_collapse($attr, $value) | ||||
105 | } | ||||
106 | |||||
107 | if (defined $value && $attr->has_type_constraint) { | ||||
108 | my $type_converter = $self->find_type_handler($attr->type_constraint, $value); | ||||
109 | $value = $type_converter->{expand}->($value, $options); | ||||
110 | } | ||||
111 | return $value; | ||||
112 | } | ||||
113 | |||||
114 | # NOTE: | ||||
115 | # possibly these two methods will | ||||
116 | # be used by a cycle supporting | ||||
117 | # engine. However, I am not sure | ||||
118 | # if I can make a cycle one work | ||||
119 | # anyway. | ||||
120 | |||||
121 | sub check_for_cycle_in_collapse { | ||||
122 | my ($self, $attr, $value) = @_; | ||||
123 | (!exists $self->seen->{refaddr $value}) | ||||
124 | || confess "Basic Engine does not support cycles in class(" | ||||
125 | . ($attr->associated_class->name) . ").attr(" | ||||
126 | . ($attr->name) . ") with $value"; | ||||
127 | $self->seen->{refaddr $value} = undef; | ||||
128 | } | ||||
129 | |||||
130 | sub check_for_cycle_in_expansion { | ||||
131 | my ($self, $attr, $value) = @_; | ||||
132 | (!exists $self->seen->{refaddr $value}) | ||||
133 | || confess "Basic Engine does not support cycles in class(" | ||||
134 | . ($attr->associated_class->name) . ").attr(" | ||||
135 | . ($attr->name) . ") with $value"; | ||||
136 | $self->seen->{refaddr $value} = undef; | ||||
137 | } | ||||
138 | |||||
139 | # util methods ... | ||||
140 | |||||
141 | sub map_attributes { | ||||
142 | my ($self, $method_name, @args) = @_; | ||||
143 | map { | ||||
144 | $self->$method_name($_, @args) | ||||
145 | } grep { | ||||
146 | # Skip our special skip attribute :) | ||||
147 | !$_->does('MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize') | ||||
148 | } ($self->_has_object ? $self->object : $self->class)->meta->get_all_attributes; | ||||
149 | } | ||||
150 | |||||
151 | ## ------------------------------------------------------------------ | ||||
152 | ## This is all the type handler stuff, it is in a state of flux | ||||
153 | ## right now, so this may change, or it may just continue to be | ||||
154 | ## improved upon. Comments and suggestions are welcomed. | ||||
155 | ## ------------------------------------------------------------------ | ||||
156 | |||||
157 | # NOTE: | ||||
158 | # these are needed by the | ||||
159 | # ArrayRef and HashRef handlers | ||||
160 | # below, so I need easy access | ||||
161 | my %OBJECT_HANDLERS = ( | ||||
162 | expand => sub { | ||||
163 | my ($data, $options) = @_; | ||||
164 | (exists $data->{$CLASS_MARKER}) | ||||
165 | || confess "Serialized item has no class marker"; | ||||
166 | # check the class more thoroughly here ... | ||||
167 | my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER}); | ||||
168 | my $meta = eval { $class->meta }; | ||||
169 | confess "Class ($class) is not loaded, cannot unpack" if $@; | ||||
170 | |||||
171 | if ($options->{check_version}) { | ||||
172 | my $meta_version = $meta->version; | ||||
173 | if (defined $meta_version && $version) { | ||||
174 | if ($options->{check_version} eq 'allow_less_than') { | ||||
175 | ($meta_version <= $version) | ||||
176 | || confess "Class ($class) versions is not less than currently available." | ||||
177 | . " got=($version) available=($meta_version)"; | ||||
178 | } | ||||
179 | elsif ($options->{check_version} eq 'allow_greater_than') { | ||||
180 | ($meta->version >= $version) | ||||
181 | || confess "Class ($class) versions is not greater than currently available." | ||||
182 | . " got=($version) available=($meta_version)"; | ||||
183 | } | ||||
184 | else { | ||||
185 | ($meta->version == $version) | ||||
186 | || confess "Class ($class) versions don't match." | ||||
187 | . " got=($version) available=($meta_version)"; | ||||
188 | } | ||||
189 | } | ||||
190 | } | ||||
191 | |||||
192 | if ($options->{check_authority}) { | ||||
193 | my $meta_authority = $meta->authority; | ||||
194 | ($meta->authority eq $authority) | ||||
195 | || confess "Class ($class) authorities don't match." | ||||
196 | . " got=($authority) available=($meta_authority)" | ||||
197 | if defined $meta_authority && defined $authority; | ||||
198 | } | ||||
199 | |||||
200 | # all is well ... | ||||
201 | $class->unpack($data, %$options); | ||||
202 | }, | ||||
203 | collapse => sub { | ||||
204 | my ( $obj, $options ) = @_; | ||||
205 | # ($obj->can('does') && $obj->does('MooseX::Storage::Basic')) | ||||
206 | # || confess "Bad object ($obj) does not do MooseX::Storage::Basic role"; | ||||
207 | ($obj->can('pack')) | ||||
208 | || confess "Object ($obj) does not have a &pack method, cannot collapse"; | ||||
209 | $obj->pack(%$options); | ||||
210 | }, | ||||
211 | 1 | 5µs | ); | ||
212 | |||||
213 | |||||
214 | my %TYPES = ( | ||||
215 | # NOTE: | ||||
216 | # we need to make sure that we properly numify the numbers | ||||
217 | # before and after them being futzed with, because some of | ||||
218 | # the JSON engines are stupid/annoying/frustrating | ||||
219 | 'Int' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, | ||||
220 | 'Num' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, | ||||
221 | # These are boring ones, so they use the identity function ... | ||||
222 | 'Str' => { expand => sub { shift }, collapse => sub { shift } }, | ||||
223 | 'Bool' => { expand => sub { shift }, collapse => sub { shift } }, | ||||
224 | # These are the trickier ones, (see notes) | ||||
225 | # NOTE: | ||||
226 | # Because we are nice guys, we will check | ||||
227 | # your ArrayRef and/or HashRef one level | ||||
228 | # down and inflate any objects we find. | ||||
229 | # But this is where it ends, it is too | ||||
230 | # expensive to try and do this any more | ||||
231 | # recursively, when it is probably not | ||||
232 | # nessecary in most of the use cases. | ||||
233 | # However, if you need more then this, subtype | ||||
234 | # and add a custom handler. | ||||
235 | 'ArrayRef' => { | ||||
236 | expand => sub { | ||||
237 | my ( $array, @args ) = @_; | ||||
238 | foreach my $i (0 .. $#{$array}) { | ||||
239 | next unless ref($array->[$i]) eq 'HASH' | ||||
240 | && exists $array->[$i]->{$CLASS_MARKER}; | ||||
241 | $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args); | ||||
242 | } | ||||
243 | $array; | ||||
244 | }, | ||||
245 | collapse => sub { | ||||
246 | my ( $array, @args ) = @_; | ||||
247 | # NOTE: | ||||
248 | # we need to make a copy cause | ||||
249 | # otherwise it will affect the | ||||
250 | # other real version. | ||||
251 | [ map { | ||||
252 | blessed($_) | ||||
253 | ? $OBJECT_HANDLERS{collapse}->($_, @args) | ||||
254 | : $_ | ||||
255 | } @$array ] | ||||
256 | } | ||||
257 | }, | ||||
258 | 'HashRef' => { | ||||
259 | expand => sub { | ||||
260 | my ( $hash, @args ) = @_; | ||||
261 | foreach my $k (keys %$hash) { | ||||
262 | next unless ref($hash->{$k}) eq 'HASH' | ||||
263 | && exists $hash->{$k}->{$CLASS_MARKER}; | ||||
264 | $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args); | ||||
265 | } | ||||
266 | $hash; | ||||
267 | }, | ||||
268 | collapse => sub { | ||||
269 | my ( $hash, @args ) = @_; | ||||
270 | # NOTE: | ||||
271 | # we need to make a copy cause | ||||
272 | # otherwise it will affect the | ||||
273 | # other real version. | ||||
274 | +{ map { | ||||
275 | blessed($hash->{$_}) | ||||
276 | ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args)) | ||||
277 | : ($_ => $hash->{$_}) | ||||
278 | } keys %$hash } | ||||
279 | } | ||||
280 | }, | ||||
281 | 1 | 17µs | 'Object' => \%OBJECT_HANDLERS, | ||
282 | # NOTE: | ||||
283 | # The sanity of enabling this feature by | ||||
284 | # default is very questionable. | ||||
285 | # - SL | ||||
286 | #'CodeRef' => { | ||||
287 | # expand => sub {}, # use eval ... | ||||
288 | # collapse => sub {}, # use B::Deparse ... | ||||
289 | #} | ||||
290 | ); | ||||
291 | |||||
292 | sub add_custom_type_handler { | ||||
293 | my ($self, $type_name, %handlers) = @_; | ||||
294 | (exists $handlers{expand} && exists $handlers{collapse}) | ||||
295 | || confess "Custom type handlers need an expand *and* a collapse method"; | ||||
296 | $TYPES{$type_name} = \%handlers; | ||||
297 | } | ||||
298 | |||||
299 | sub remove_custom_type_handler { | ||||
300 | my ($self, $type_name) = @_; | ||||
301 | delete $TYPES{$type_name} if exists $TYPES{$type_name}; | ||||
302 | } | ||||
303 | |||||
304 | sub find_type_handler { | ||||
305 | my ($self, $type_constraint, $value) = @_; | ||||
306 | |||||
307 | # check if the type is a Maybe and | ||||
308 | # if its parent is not parameterized. | ||||
309 | # If both is true recurse this method | ||||
310 | # using ->type_parameter. | ||||
311 | return $self->find_type_handler($type_constraint->type_parameter, $value) | ||||
312 | if ($type_constraint->parent && $type_constraint->parent eq 'Maybe' | ||||
313 | and not $type_constraint->parent->can('type_parameter')); | ||||
314 | |||||
315 | # find_type_for is a method of a union type. If we can call that method | ||||
316 | # then we are dealign with a union and we need to ascertain which of | ||||
317 | # the union's types we need to use for the value we are serializing. | ||||
318 | if($type_constraint->can('find_type_for')) { | ||||
319 | my $tc = $type_constraint->find_type_for($value); | ||||
320 | return $self->find_type_handler($tc, $value) if defined($tc); | ||||
321 | } | ||||
322 | |||||
323 | # this should handle most type usages | ||||
324 | # since they they are usually just | ||||
325 | # the standard set of built-ins | ||||
326 | return $TYPES{$type_constraint->name} | ||||
327 | if exists $TYPES{$type_constraint->name}; | ||||
328 | |||||
329 | # the next possibility is they are | ||||
330 | # a subtype of the built-in types, | ||||
331 | # in which case this will DWIM in | ||||
332 | # most cases. It is probably not | ||||
333 | # 100% ideal though, but until I | ||||
334 | # come up with a decent test case | ||||
335 | # it will do for now. | ||||
336 | foreach my $type (keys %TYPES) { | ||||
337 | return $TYPES{$type} | ||||
338 | if $type_constraint->is_subtype_of($type); | ||||
339 | } | ||||
340 | |||||
341 | # NOTE: | ||||
342 | # the reason the above will work has to | ||||
343 | # do with the fact that custom subtypes | ||||
344 | # are mostly used for validation of | ||||
345 | # the guts of a type, and not for some | ||||
346 | # weird structural thing which would | ||||
347 | # need to be accomidated by the serializer. | ||||
348 | # Of course, mst or phaylon will probably | ||||
349 | # do something to throw this assumption | ||||
350 | # totally out the door ;) | ||||
351 | # - SL | ||||
352 | |||||
353 | # NOTE: | ||||
354 | # if this method hasnt returned by now | ||||
355 | # then we have no been able to find a | ||||
356 | # type constraint handler to match | ||||
357 | confess "Cannot handle type constraint (" . $type_constraint->name . ")"; | ||||
358 | } | ||||
359 | |||||
360 | sub find_type_handler_for { | ||||
361 | my ($self, $type_handler_name) = @_; | ||||
362 | $TYPES{$type_handler_name} | ||||
363 | } | ||||
364 | |||||
365 | 2 | 38µs | 2 | 118µs | # spent 63µs (7+55) within MooseX::Storage::Engine::BEGIN@365 which was called:
# once (7µs+55µs) by MooseX::Storage::Basic::BEGIN@10 at line 365 # spent 63µs making 1 call to MooseX::Storage::Engine::BEGIN@365
# spent 55µs making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:706] |
366 | |||||
367 | 1 | 11µs | 1; | ||
368 | |||||
369 | __END__ |