File Coverage

blib/lib/Pixie/LiveObjectManager.pm
Criterion Covered Total %
statement 99 99 100.0
branch 16 22 72.7
condition 5 12 41.6
subroutine 24 24 100.0
pod 0 15 0.0
total 144 172 83.7


line stmt bran cond sub pod time code
1             ##
2             # NAME
3             # Pixie::LiveObjectManager - manages in-memory cache of objects
4             #
5             # SYNOPSIS
6             # use Pixie::LiveObjectManager;
7             #
8             # my $obj_manager = Pixie::LiveObjectManager->new->set_pixie( $pixie );
9             # my $obj = Foo->new;
10             #
11             # # insert weak copy of $obj into the cache:
12             # my ($oid) = $obj_manager->cache_insert( $obj );
13             # my $obj2 = $obj_manager->cache_get( $oid );
14             # print $obj_manager->cache_size;
15             # ... for $obj_manager->cache_keys;
16             # $obj_manager->cache_delete( $oid );
17             #
18             # # still dunno about this one:
19             # $obj_manager->bind_object_to_oid( $obj_holder => $oid );
20             #
21             # $obj_manager->lock_strategy_for
22             # $obj_manager->lock_object / $obj_manager->unlock_object
23             #
24             ##
25              
26             package Pixie::LiveObjectManager;
27              
28 19     19   54323 use strict;
  19         36  
  19         920  
29              
30 19     19   132 use Carp qw( confess );
  19         35  
  19         3432  
31 19     19   1600 use Scalar::Util qw( blessed weaken isweak );
  19         46  
  19         1289  
32 19     19   779 use Pixie::Object;
  19         1409  
  19         499  
33 19     19   652 use Pixie::ObjectInfo;
  19         40  
  19         514  
34 19     19   723 use Pixie::FinalMethods;
  19         118  
  19         442  
35              
36 19     19   112 use base qw( Pixie::Object );
  19         36  
  19         3866  
37              
38             our $VERSION = "2.08_02";
39              
40             sub init {
41 26     26 0 54 my $self = shift;
42 26         148 $self->{_live_cache} = {};
43 26         76 return $self;
44             }
45              
46             #-----------------------------------------------------------------------------
47             # Cache methods
48             #-----------------------------------------------------------------------------
49              
50             sub cache_insert {
51 32     32 0 1019 my $self = shift;
52 32         52 my $obj = shift;
53              
54 32 50       238 confess( "can't insert a Pixie::ObjectInfo into the cache!" )
55             if $obj->isa('Pixie::ObjectInfo');
56              
57 32         133 my $info = $obj->PIXIE::get_info;
58 32         125 my $oid = $info->_oid;
59              
60 19     19   116 no warnings 'uninitialized';
  19         40  
  19         18839  
61 32 50 33     418 if ( length($oid) && ! defined($self->{_live_cache}{$oid}) ) {
62 32         164 weaken( $self->{_live_cache}{$oid} = $info );
63             # $info->set_lock_strategy(Pixie->get_the_current_lock_strategy);
64             }
65              
66 32         111 return $oid => $obj;
67             }
68              
69             sub cache_get {
70 3     3 0 6210 my $self = shift;
71 3         7 my($oid) = @_;
72              
73 3 50       23 defined $oid or return;
74 3 100       24 if ( defined $self->{_live_cache}{$oid} ) {
75 2         16 return $self->{_live_cache}{$oid}->the_object;
76             }
77             else {
78 1         4 return;
79             }
80             }
81              
82             sub cache_delete {
83 24     24 0 1009 my $self = shift;
84 24         54 my($oid) = @_;
85 24 50       104 $oid = $self->get_oid_for($oid) if blessed( $oid );
86 24         108 delete $self->{_live_cache}{$oid};
87             }
88              
89             sub cache_size {
90 11     11 0 1909 my $self = shift;
91 11         15 scalar keys %{$self->{_live_cache}};
  11         62  
92             }
93              
94             sub cache_keys {
95 2     2 0 5 my $self = shift;
96 2         3 keys %{$self->{_live_cache}};
  2         11  
97             }
98              
99             #-----------------------------------------------------------------------------
100             # Live Object Management
101             #-----------------------------------------------------------------------------
102              
103             ## TODO: use an accessor for pixie
104             sub set_pixie {
105 26     26 0 849 my $self = shift;
106 26         74 $self->{pixie} = shift;
107 26         141 weaken $self->{pixie};
108 26         91 return $self;
109             }
110              
111             sub bind_object_to_oid {
112 4     4 0 59 my $self = shift;
113 4         10 my($obj, $oid) = @_;
114 4   66     14 my $info = $self->get_info_for_oid($oid) || $obj->PIXIE::get_info;
115              
116             # TODO: what happens if $obj != $info->the_object ?
117 4 50       21 $info->set_the_object($obj) unless defined($info->the_object);
118 4         28 $info->set__oid($oid);
119 4         18 $info->set_pixie($self->{pixie});
120 4   33     19 $info->set_lock_strategy( Pixie->get_the_current_lock_strategy ||
121             $self->{pixie}->lock_strategy );
122 4         22 $obj->PIXIE::set_info($info);
123             }
124              
125             sub lock_object {
126 1     1 0 1201 my $self = shift;
127 1         2 my($obj) = @_;
128              
129 1         3 $self->assert_ownership_of($obj);
130 1         68 $self->{pixie}->store->lock_object_for(scalar($self->get_oid_for($obj)),
131             $self->{pixie});
132             }
133              
134             sub unlock_object {
135 3     3 0 655 my $self = shift;
136 3         5 my($obj) = @_;
137              
138 3         9 $self->assert_ownership_of($obj);
139 3         232 $self->{pixie}->store->unlock_object_for(scalar($self->get_oid_for($obj)),
140             $self->{pixie});
141             }
142              
143             sub assert_ownership_of {
144 6     6 0 114 my $self = shift;
145 6         11 my($obj) = @_;
146 6 100       39 confess( "The object is not managed by this pixie!" ) unless
147             $self->{pixie}->manages_object($obj);
148             }
149              
150             sub lock_strategy_for {
151 2     2 0 548 my $self = shift;
152 2         4 my $oid = shift;
153 2         6 my $info = $self->get_info_for($oid);
154              
155             # TODO: eek! this a bad place to act as an accessor...
156             # maybe set_lock_strategy_for(), or just let caller do it?
157 2 100       7 if (@_) {
158 1         6 $info->set_lock_strategy(@_);
159             }
160              
161 2         8 $info->lock_strategy;
162             }
163              
164             sub get_info_for {
165 5     5 0 437 my $self = shift;
166 5         7 my $thing = shift;
167 5 100       28 return blessed($thing)
168             ? $thing->PIXIE::get_info
169             : $self->get_info_for_oid( $thing );
170             }
171              
172             sub get_info_for_oid {
173 7     7 0 11 my $self = shift;
174 7         10 my $oid = shift;
175 7         42 $self->{_live_cache}{$oid};
176             }
177              
178             sub get_oid_for {
179 6     6 0 594 my $self = shift;
180 6         9 my $obj = shift;
181              
182 6 50 33     51 return unless defined( $obj ) && blessed $obj;
183             confess( "Can't get oid for a Pixie::ObjectInfo!" )
184 6 100       11 if eval { $obj->isa('Pixie::ObjectInfo') };
  6         74  
185              
186 5         24 $obj->PIXIE::oid
187             }
188              
189             sub DESTROY {
190 23     23   99 my $self = shift;
191 23         49 local $@; # protect $@
192 23         47 foreach my $obj (grep defined,
  23         244  
193             map $_->the_object,
194             grep defined,
195             values %{ $self->{_live_cache} })
196             {
197 2         6 eval { $self->unlock_object( $obj ) };
  2         7  
198 2         70 $obj->PIXIE::set_info( undef );
199             }
200             }
201              
202             1;