blib/lib/DBM/Deep/Sector/DBI/Reference.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 168 CC 13 SCAR 25.1
CC
13
Cov
0%
data
42.0
free
12.0
_init
6.0
CRAP 12.3
1 package DBM::Deep::Sector::DBI::Reference;
2  
3 use 5.008_004;
4  
5 use strict;
6 use warnings FATAL => 'all';
7  
8 use base 'DBM::Deep::Sector::DBI';
9  
10 use Scalar::Util;
11  
12 sub table { 'refs' }
13  
14 sub _init {
15 my $self = shift;
16  
17 my $e = $self->engine;
18  
19 unless ( $self->offset ) {
20 my $classname = Scalar::Util::blessed( delete $self->{data} );
21 $self->{offset} = $self->engine->storage->write_to(
22 refs => undef,
23 ref_type => $self->type,
24 classname => $classname,
25 );
26 }
27 else {
28 my ($rows) = $self->engine->storage->read_from(
29 refs => $self->offset,
30 qw( ref_type ),
31 );
32  
33 $self->{type} = $rows->[0]{ref_type};
34 }
35  
36 return;
37 }
38  
39 sub get_data_for {
40 my $self = shift;
41 my ($args) = @_;
42  
43 my ($rows) = $self->engine->storage->read_from(
44 datas => { ref_id => $self->offset, key => $args->{key} },
45 qw( id ),
46 );
47  
48 return unless $rows->[0]{id};
49  
50 $self->load(
51 $self->engine,
52 $rows->[0]{id},
53 'datas',
54 );
55 }
56  
57 sub write_data {
58 my $self = shift;
59 my ($args) = @_;
60  
61 if ( ( $args->{value}->type || 'S' ) eq 'S' ) {
62 $args->{value}{offset} = $self->engine->storage->write_to(
63 datas => $args->{value}{offset},
64 ref_id => $self->offset,
65 data_type => 'S',
66 key => $args->{key},
67 value => $args->{value}{data},
68 );
69  
70 $args->{value}->reload;
71 }
72 else {
73 # Write the Scalar of the Reference
74 $self->engine->storage->write_to(
75 datas => undef,
76 ref_id => $self->offset,
77 data_type => 'R',
78 key => $args->{key},
79 value => $args->{value}{offset},
80 );
81 }
82 }
83  
84 sub delete_key {
85 my $self = shift;
86 my ($args) = @_;
87  
88 my $old_value = $self->get_data_for({
89 key => $args->{key},
90 });
91  
92 my $data;
93 if ( $old_value ) {
94 $data = $old_value->data({ export => 1 });
95  
96 $self->engine->storage->delete_from(
97 'datas',
98 { ref_id => $self->offset,
99 key => $args->{key}, },
100 );
101 $old_value->free;
102 }
103  
104 return $data;
105 }
106  
107 sub get_classname {
108 my $self = shift;
109 my ($rows) = $self->engine->storage->read_from(
110 'refs', $self->offset,
111 qw( classname ),
112 );
113 return unless @$rows;
114 return $rows->[0]{classname};
115 }
116  
117 # Look to hoist this method into a ::Reference trait
118 sub data {
119 my $self = shift;
120 my ($args) = @_;
121 $args ||= {};
122  
123 my $engine = $self->engine;
124 my $cache = $engine->cache;
125 my $off = $self->offset;
126 my $obj;
127 if ( !defined $cache->{ $off } ) {
128 $obj = DBM::Deep->new({
129 type => $self->type,
130 base_offset => $self->offset,
131 storage => $engine->storage,
132 engine => $engine,
133 });
134  
135 $cache->{$off} = $obj;
136 Scalar::Util::weaken($cache->{$off});
137 }
138 else {
139 $obj = $cache->{$off};
140 }
141  
142 # We're not exporting, so just return.
143 unless ( $args->{export} ) {
144 if ( $engine->storage->{autobless} ) {
145 my $classname = $self->get_classname;
146 if ( defined $classname ) {
147 bless $obj, $classname;
148 }
149 }
150  
151 return $obj;
152 }
153  
154 # We shouldn't export if this is still referred to.
155 if ( $self->get_refcount > 1 ) {
156 return $obj;
157 }
158  
159 return $obj->export;
160 }
161  
162 sub free {
163 my $self = shift;
164  
165 # We're not ready to be removed yet.
166 return if $self->decrement_refcount > 0;
167  
168 # Rebless the object into DBM::Deep::Null.
169 # In external_refs mode, this will already have been removed from
170 # the cache, so we can skip this.
171 my $e = $self->engine;
172 if(!$e->{external_refs}) {
173 eval { %{ $e->cache->{ $self->offset } } = (); };
174 eval { @{ $e->cache->{ $self->offset } } = (); };
175 bless $e->cache->{ $self->offset }, 'DBM::Deep::Null';
176 delete $e->cache->{ $self->offset };
177 }
178  
179 $e->storage->delete_from(
180 'datas', { ref_id => $self->offset },
181 );
182  
183 $e->storage->delete_from(
184 'datas', { value => $self->offset, data_type => 'R' },
185 );
186  
187 $self->SUPER::free( @_ );
188 }
189  
190 sub increment_refcount {
191 my $self = shift;
192 my $refcount = $self->get_refcount;
193 $refcount++;
194 $self->write_refcount( $refcount );
195 return $refcount;
196 }
197  
198 sub decrement_refcount {
199 my $self = shift;
200 my $refcount = $self->get_refcount;
201 $refcount--;
202 $self->write_refcount( $refcount );
203 return $refcount;
204 }
205  
206 sub get_refcount {
207 my $self = shift;
208 my ($rows) = $self->engine->storage->read_from(
209 'refs', $self->offset,
210 qw( refcount ),
211 );
212 return $rows->[0]{refcount};
213 }
214  
215 sub write_refcount {
216 my $self = shift;
217 my ($num) = @_;
218 $self->engine->storage->{dbh}->do(
219 "UPDATE refs SET refcount = ? WHERE id = ?", undef,
220 $num, $self->offset,
221 );
222 }
223  
224 sub clear {
225 my $self = shift;
226  
227 DBM::Deep->new({
228 type => $self->type,
229 base_offset => $self->offset,
230 storage => $self->engine->storage,
231 engine => $self->engine,
232 })->_clear;
233  
234 return;
235 }
236  
237 1;
238 __END__