lib/UR/Context/AutoUnloadPool.pm

filter: Statementstmt 21.8% 14 / 64 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 23.0% 3 / 13 Podpod 100% 2 / 2 totaltotal 20.0% 19 / 95 CC 10 SCAR 26.6
CC
10
Cov
18%
_unload_objects
42.0
__ANON__
6.0
_is_printing_debug
6.0
CRAP 14.2
« blib/lib/UR/Namespace/Command/Old/ExportDbicClasses.pm ↑ Summary blib/lib/UR/DataSource/RDBMS/FkConstraint.pm »
1 package UR::Context::AutoUnloadPool;
2  
3 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 266 require UR;
7 266 our $VERSION = "0.47"; # UR $VERSION
8  
9 266 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # These are plain Perl objects that get garbage collected in the normal way,
12 # not UR::Objects
13  
14 266 our @CARP_NOT = qw( UR::Context );
15  
16 266 my $pool_count = 0;
17 0 ▶sub _pool_count { $pool_count }
Subroutine _pool_count: not called CC=1 SCAR=6.9
18  
19 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
20 0 my $class = shift;
21 0 my $self = bless { pool => {} }, $class;
22 0 $self->_attach_observer();
23 0 $pool_count++;
24 0 UR::Context::manage_objects_may_go_out_of_scope();
25 0 return $self;
26 }
27  
28 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
29 0 my $self = shift;
30 0 delete $self->{pool};
31 0 $self->_detach_observer();
32 0 $pool_count--;
33 0 UR::Context::manage_objects_may_go_out_of_scope();
34 0 return 1;
35 }
36  
37 0 ▶sub _attach_observer {
Subroutine _attach_observer: not called CC=1 SCAR=6.9
38 0 my $self = shift;
39 0 Scalar::Util::weaken($self);
40 my $o = UR::Object->add_observer(
41 aspect => 'load',
42 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
43 0 my $loaded = shift;
44  
45 0 ▶ return if ! $loaded->is_prunable();
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded->is_prunable 0 0 0
46 0 $self->_object_was_loaded($loaded);
47 }
48 0 );
49 0 $self->{observer} = $o;
50 }
51  
52 0 ▶sub _detach_observer {
Subroutine _detach_observer: not called CC=1 SCAR=6.9
53 0 my $self = shift;
54 0 delete($self->{observer})->delete();
55 }
56  
57 0 ▶sub _is_printing_debug {
Subroutine _is_printing_debug: not called CC=2 SCAR=17.9
58 0 ▶ $ENV{UR_DEBUG_OBJECT_PRUNING} || $ENV{'UR_DEBUG_OBJECT_RELEASE'};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"UR_DEBUG_OBJECT_PRUNING"} or $ENV{"UR_DEBUG_OBJECT_RELEASE"} 0 0 0
59 }
60  
61 0 ▶sub _object_was_loaded {
Subroutine _object_was_loaded: not called CC=2 SCAR=17.9
62 0 my($self, $o) = @_;
63 0 ▶ if (_is_printing_debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_printing_debug()) 0 0 0
64 0 my($class, $id) = ($o->class, $o->id);
65 0 print STDERR Carp::shortmess("MEM AUTORELEASE $class id $id loaded in pool $self\n");
66 }
67 0 $self->{pool}->{$o->class}->{$o->id} = undef;
68 }
69  
70 0 ▶sub _unload_objects {
Subroutine _unload_objects: not called CC=6 SCAR=37.4
71 0 my $self = shift;
72 0 ▶ return unless $self->{pool};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pool"} 0 0 0
73  
74 0 ▶ print STDERR Carp::shortmess("MEM AUTORELEASE pool $self draining\n") if _is_printing_debug();
Branch: 1 branch
BranchTrueFalseTotal
if _is_printing_debug() 0 0 0
75  
76 0 foreach my $class_name ( keys %{$self->{pool}} ) {
77 0 ▶ if (_is_printing_debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_printing_debug()) 0 0 0
78 printf STDERR "MEM AUTORELEASE class $class_name: %s\n",
79 0 join(', ', values %{ $self->{pool}->{$class_name}} );
80 }
81 0 my $objs_for_class = $UR::Context::all_objects_loaded->{$class_name};
82 0 ▶ next unless $objs_for_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $objs_for_class 0 0 0
83 0 my @objs_to_release = grep { ! $_->__changes__ }
84 0 @$objs_for_class{ keys %{$self->{pool}->{$class_name}}};
85 0 UR::Context->current->_weaken_references_for_objects(\@objs_to_release);
86 }
87 0 delete $self->{pool};
88 }
89  
90 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
91 0 local $@;
92  
93 0 my $self = shift;
94 0 ▶ return unless ($self->{pool});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pool"} 0 0 0
95 0 $self->_detach_observer();
96 0 $self->_unload_objects();
97 0 $pool_count--;
98 0 UR::Context::manage_objects_may_go_out_of_scope();
99 }
100  
101 266 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
« blib/lib/UR/Namespace/Command/Old/ExportDbicClasses.pm ↑ Summary blib/lib/UR/DataSource/RDBMS/FkConstraint.pm »