blib/lib/UR/Context/AutoUnloadPool.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 104 SCAR 42.8
CC
8
Cov
0%
_unload_objects
30.0
_attach_observer
6.0
_object_was_loaded
6.0
CRAP 72.0
1 package UR::Context::AutoUnloadPool;
2  
3 use strict;
4 use warnings;
5  
6 require UR;
7 our $VERSION = "0.46"; # UR $VERSION
8  
9 use Scalar::Util qw();
10  
11 # These are plain Perl objects that get garbage collected in the normal way,
12 # not UR::Objects
13  
14 our @CARP_NOT = qw( UR::Context );
15  
16 my $pool_count = 0;
17 sub _pool_count { $pool_count }
18  
19 sub create {
20 my $class = shift;
21 my $self = bless { pool => {} }, $class;
22 $self->_attach_observer();
23 $pool_count++;
24 UR::Context::manage_objects_may_go_out_of_scope();
25 return $self;
26 }
27  
28 sub delete {
29 my $self = shift;
30 delete $self->{pool};
31 $self->_detach_observer();
32 $pool_count--;
33 UR::Context::manage_objects_may_go_out_of_scope();
34 return 1;
35 }
36  
37 sub _attach_observer {
38 my $self = shift;
39 Scalar::Util::weaken($self);
40 my $o = UR::Object->add_observer(
41 aspect => 'load',
42 callback => sub {
43 my $loaded = shift;
44  
45 return if ! $loaded->is_prunable();
46 $self->_object_was_loaded($loaded);
47 }
48 );
49 $self->{observer} = $o;
50 }
51  
52 sub _detach_observer {
53 my $self = shift;
54 delete($self->{observer})->delete();
55 }
56  
57 sub _is_printing_debug {
58 $ENV{UR_DEBUG_OBJECT_PRUNING} || $ENV{'UR_DEBUG_OBJECT_RELEASE'};
59 }
60  
61 sub _object_was_loaded {
62 my($self, $o) = @_;
63 if (_is_printing_debug()) {
64 my($class, $id) = ($o->class, $o->id);
65 print STDERR Carp::shortmess("MEM AUTORELEASE $class id $id loaded in pool $self\n");
66 }
67 $self->{pool}->{$o->class}->{$o->id} = undef;
68 }
69  
70 sub _unload_objects {
71 my $self = shift;
72 return unless $self->{pool};
73  
74 print STDERR Carp::shortmess("MEM AUTORELEASE pool $self draining\n") if _is_printing_debug();
75  
76 foreach my $class_name ( keys %{$self->{pool}} ) {
77 if (_is_printing_debug()) {
78 printf STDERR "MEM AUTORELEASE class $class_name: %s\n",
79 join(', ', values %{ $self->{pool}->{$class_name}} );
80 }
81 my $objs_for_class = $UR::Context::all_objects_loaded->{$class_name};
82 next unless $objs_for_class;
83 my @objs_to_release = grep { ! $_->__changes__ }
84 @$objs_for_class{ keys %{$self->{pool}->{$class_name}}};
85 UR::Context->current->_weaken_references_for_objects(\@objs_to_release);
86 }
87 delete $self->{pool};
88 }
89  
90 sub DESTROY {
91 local $@;
92  
93 my $self = shift;
94 return unless ($self->{pool});
95 $self->_detach_observer();
96 $self->_unload_objects();
97 $pool_count--;
98 UR::Context::manage_objects_may_go_out_of_scope();
99 }
100  
101 1;
102  
103 =pod
104  
105 =head1 NAME
106  
107 UR::Context::AutoUnloadPool - Automatically unload objects when scope ends
108  
109 =head1 SYNOPSIS
110  
111 my $not_unloaded = Some::Class->get(...);
112 do {
113 my $guard = UR::Context::AutoUnloadPool->create();
114 my $object = Some::Class->get(...); # load an object from the database
115 ... # load more things
116 }; # $guard goes out of scope - unloads objects
117  
118 =head1 DESCRIPTION
119  
120 UR Objects retrieved from the database normally live in the object cache for
121 the life of the program. When a UR::Context::AutoUnloadPool is instantiated,
122 it tracks every object loaded during its life. The Pool's destructor calls
123 unload() on those objects.
124  
125 Changed objects and objects loaded before before the Pool is created will not
126 get unloaded.
127  
128 =head1 METHODS
129  
130 =over 4
131  
132 =item create
133  
134 my $guard = UR::Context::AutoUnloadPool->create();
135  
136 Creates a Pool object. All UR Objects loaded from the database during this
137 object's lifetime will get unloaded when the Pool goes out of scope.
138  
139 =item delete
140  
141 $guard->delete();
142  
143 Invalidates the Pool object. No objects are unloaded. When the Pool later
144 goes out of scope, no objects will be unloaded.
145  
146 =back
147  
148 =head1 SEE ALSO
149  
150 UR::Object, UR::Context
151  
152 =cut