File Coverage

blib/lib/Pixie/LiveObjectManager.pm
Criterion Covered Total %
statement 67 100 67.0
branch 6 22 27.2
condition 2 12 16.6
subroutine 17 24 70.8
pod 0 15 0.0
total 92 173 53.1


line stmt bran cond sub pod time code
1             package Pixie::LiveObjectManager;
2              
3 16     16   82 use strict;
  16         28  
  16         526  
4              
5 16     16   85 use Pixie::Object;
  16         28  
  16         325  
6 16     16   83 use Pixie::FinalMethods;
  16         30  
  16         396  
7 16     16   79 use base 'Pixie::Object';
  16         27  
  16         1783  
8              
9 16     16   1576 use Pixie::ObjectInfo;
  16         31  
  16         341  
10 16     16   119 use Carp;
  16         32  
  16         1274  
11              
12             our $VERSION="2.06";
13              
14 16     16   99 use Scalar::Util qw/blessed weaken isweak/;
  16         34  
  16         2234  
15              
16             sub init {
17 24     24 0 50 my $self = shift;
18 24         131 $self->{_live_cache} = {};
19 24         64 return $self;
20             }
21              
22             sub cache_insert {
23 28     28 0 46 my $self = shift;
24 28         43 my $obj = shift;
25              
26 28 50       206 die if $obj->isa('Pixie::ObjectInfo');
27 28         149 my $info = $obj->PIXIE::get_info;
28 28         120 my $oid = $info->_oid;
29 16     16   98 no warnings 'uninitialized';
  16         26  
  16         15095  
30 28 50 33     304 if ( length($oid) && ! defined($self->{_live_cache}{$oid}) ) {
31 28         186 weaken($self->{_live_cache}{$oid} = $info);
32             # $info->set_lock_strategy(Pixie->get_the_current_lock_strategy);
33             }
34 28         90 return $oid => $obj;
35             }
36              
37             sub cache_get {
38 0     0 0 0 my $self = shift;
39 0         0 my($oid) = @_;
40              
41 0 0       0 defined $oid or return;
42 0 0       0 if ( defined $self->{_live_cache}{$oid} ) {
43 0         0 return $self->{_live_cache}{$oid}->the_object;
44             }
45             else {
46 0         0 return;
47             }
48             }
49              
50             sub cache_delete {
51 20     20 0 35 my $self = shift;
52 20         53 my($oid) = @_;
53 20 50       77 $oid = $self->get_oid_for($oid) if ref($oid);
54 20         96 delete $self->{_live_cache}{$oid};
55             }
56              
57             sub cache_size {
58 9     9 0 73 my $self = shift;
59 9         20 scalar keys %{$self->{_live_cache}};
  9         149  
60             }
61              
62             sub cache_keys {
63 0     0 0 0 my $self = shift;
64 0         0 keys %{$self->{_live_cache}};
  0         0  
65             }
66              
67             sub get_info_for {
68 0     0 0 0 my $self = shift;
69 0         0 my $thing = shift;
70 0 0       0 return ref($thing) ? $thing->PIXIE::get_info
71             : $self->get_info_for_oid($thing);
72             }
73              
74             sub get_info_for_oid {
75 0     0 0 0 my $self = shift;
76 0         0 my $oid = shift;
77 0         0 $self->{_live_cache}{$oid};
78             }
79              
80             sub set_pixie {
81 24     24 0 86 my $self = shift;
82 24         44 my $pixie = shift;
83 24         66 $self->{pixie} = $pixie;
84 24         112 weaken $self->{pixie};
85 24         83 return $self;
86             }
87              
88             sub bind_object_to_oid {
89 0     0 0 0 my $self = shift;
90 0         0 my($obj, $oid) = @_;
91 0   0     0 my $info = $self->get_info_for_oid($oid)
92             || $obj->PIXIE::get_info;
93              
94 0 0       0 $info->set_the_object($obj) unless defined($info->the_object);
95 0         0 $info->set__oid($oid);
96 0         0 $info->set_pixie($self->{pixie});
97 0   0     0 $info->set_lock_strategy( Pixie->get_the_current_lock_strategy ||
98             $self->{pixie}->lock_strategy );
99 0         0 $obj->PIXIE::set_info($info);
100             }
101              
102             sub lock_object {
103 0     0 0 0 my $self = shift;
104 0         0 my($obj) = @_;
105              
106 0         0 $self->assert_ownership_of($obj);
107 0         0 $self->{pixie}->store->lock_object_for(scalar($self->get_oid_for($obj)),
108             $self->{pixie});
109             }
110              
111             sub unlock_object {
112 5     5 0 7 my $self = shift;
113 5         6 my($obj) = @_;
114              
115 5         11 $self->assert_ownership_of($obj);
116 5         255 $self->{pixie}->store->unlock_object_for(scalar($self->get_oid_for($obj)),
117             $self->{pixie});
118             }
119              
120             sub assert_ownership_of {
121 5     5 0 6 my $self = shift;
122 5         6 my($obj) = @_;
123 5 50       27 die "The object is not managed by this pixie" unless
124             $self->{pixie}->manages_object($obj);
125             }
126              
127             sub lock_strategy_for {
128 0     0 0 0 my $self = shift;
129 0         0 my $oid = shift;
130              
131 0         0 my $info = $self->get_info_for($oid);
132 0 0       0 if (@_) {
133 0         0 $info->set_lock_strategy(@_);
134             }
135 0         0 $info->lock_strategy;
136             }
137              
138             sub get_oid_for {
139 5     5 0 7 my $self = shift;
140 5         8 my $obj = shift;
141              
142 5 50 33     35 return unless defined($obj) && blessed $obj;
143             Carp::confess "You should't call this on a Pixie::ObjectInfo" if
144 5 50       9 eval { $obj->isa('Pixie::ObjectInfo') };
  5         48  
145 5         20 $obj->PIXIE::oid
146             }
147              
148             sub DESTROY {
149 20     20   42 my $self = shift;
150 20         36 local $@; # protect $@
151 20         40 for (grep defined,
  20         225  
152             map $_->the_object,
153             grep defined,
154             values %{$self->{_live_cache}})
155             {
156 5         7 eval {$self->unlock_object($_)};
  5         14  
157 5         19 $_->PIXIE::set_info();
158             }
159             }
160              
161             1;