| 1 | package UR::DeletedRef; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | require UR; | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | # this is to workaround a Perl bug where the overload magic flag is not updated | ||
| 9 | # for references with a different RV (which happens anytime you do "my $object" | ||
| 10 | # https://rt.perl.org/rt3/Public/Bug/Display.html?id=9472 | ||
| 11 | if ($^V lt v5.8.9) { | ||
| 12 | eval "use overload fallback => 1"; | ||
| 13 | }; | ||
| 14 | }; | ||
| 15 | |||
| 16 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 17 | |||
| 18 | our $all_objects_deleted = {}; | ||
| 19 | |||
| 20 | sub bury { | ||
| 21 | my $class = shift; | ||
| 22 | |||
| 23 | for my $object (@_) { | ||
| 24 | if ($ENV{'UR_DEBUG_OBJECT_RELEASE'}) { | ||
| 25 | print STDERR "MEM BURY object $object class ",$object->class," id ",$object->id,"\n"; | ||
| 26 | } | ||
| 27 | my $original_class = ref($object); | ||
| 28 | my $original_id = $object->id; | ||
| 29 | |||
| 30 | %$object = (original_class => ref($object), original_data => {%$object}); | ||
| 31 | bless $object, 'UR::DeletedRef'; | ||
| 32 | |||
| 33 | $all_objects_deleted->{$original_class}->{$original_id} = $object; | ||
| 34 | Scalar::Util::weaken($all_objects_deleted->{$original_class}->{$original_id}); | ||
| 35 | } | ||
| 36 | |||
| 37 | return 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub resurrect { | ||
| 41 | shift unless (ref($_[0])); | ||
| 42 | |||
| 43 | foreach my $object (@_) { | ||
| 44 | my $original_class = $object->{'original_class'}; | ||
| 45 | bless $object, $original_class; | ||
| 46 | %$object = (%{$object->{original_data}}); | ||
| 47 | my $id = $object->id; | ||
| 48 | delete $all_objects_deleted->{$original_class}->{$id}; | ||
| 49 | $object->resurrect_object if ($object->can('resurrect_object')); | ||
| 50 | } | ||
| 51 | |||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | use Data::Dumper; | ||
| 56 | |||
| 57 | sub AUTOLOAD { | ||
| 58 | our $AUTOLOAD; | ||
| 59 | my $method = $AUTOLOAD; | ||
| 60 | $method =~ s/^.*:://g; | ||
| 61 | Carp::croak("Attempt to use a reference to an object which has been deleted. A call was made to method '$method'\nRessurrect it first.\n" . Dumper($_[0])); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub __rollback__ { | ||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub DESTROY { | ||
| 69 | if ($ENV{'UR_DEBUG_OBJECT_RELEASE'}) { | ||
| 70 | print STDERR "MEM DESTROY deletedref $_[0]\n"; | ||
| 71 | } | ||
| 72 | delete $all_objects_deleted->{"$_[0]"}; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | UR::DeletedRef - Represents an instance of a no-longer-existent object | ||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | my $obj = Some::Class->get(123); | ||
| 87 | |||
| 88 | $obj->delete; | ||
| 89 | print ref($obj),"\n"; # prints 'UR::DeletedRef' | ||
| 90 | $obj->some_method(); # generates an exception through Carp::confess | ||
| 91 | |||
| 92 | $obj->resurrect; | ||
| 93 | print ref($obj),"\n"; # prints 'Some::Class' | ||
| 94 | |||
| 95 | =head1 DESCRIPTION | ||
| 96 | |||
| 97 | Object instances become UR::DeletedRefs when some part of the application | ||
| 98 | calls delete() or unload() on them, meaning that they no longer exist | ||
| 99 | in that Context. The extant object reference is turned into a UR::DeletedRef | ||
| 100 | so that if that same reference is used in any capacity later in the program, | ||
| 101 | it will generate an exception through its AUTOLOAD to prevent using it by | ||
| 102 | mistake. | ||
| 103 | |||
| 104 | Note that UR::DeletedRef instances are different than Ghost objects. When a | ||
| 105 | UR-based object is deleted through delete(), a new Ghost object reference is | ||
| 106 | created from the data in the old object, and the old object reference is | ||
| 107 | re-blessed as a UR::DeletedRef. Any variables still referencing the original | ||
| 108 | object now hold a reference to this UR::DeletedRef. The Ghost object can be | ||
| 109 | retrieved by issuing a get() against the Ghost class. | ||
| 110 | |||
| 111 | Objects unloaded from the Context using unload(), or indirectly by rolling-back | ||
| 112 | a transaction which triggers unload of objects loaded during the transaction, | ||
| 113 | are also turned into UR::DeletedRefs. | ||
| 114 | |||
| 115 | You aren't likely to encounter UR::DeletedRefs in normal use. What usually | ||
| 116 | happens is that an object will be deleted with delete() (or unload()), the | ||
| 117 | lexical variable pointing to the DeletedRef will soon go out of scope and | ||
| 118 | the DeletedRef will then be garbage-colelcted. | ||
| 119 | |||
| 120 | =head1 SEE ALSO | ||
| 121 | |||
| 122 | UR::Object, UR::Object::Ghost, UR::Context | ||
| 123 | |||
| 124 | =cut | ||
| 125 |