r15625@rob-kinyons-computer (orig r9171): rkinyon | 2007-02-26 11:56:32 -0500
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Array.pm
CommitLineData
6fe26b29 1package DBM::Deep::Array;
2
2120a181 3use 5.006_000;
460b1067 4
6fe26b29 5use strict;
460b1067 6use warnings;
6fe26b29 7
e9b0b5f0 8our $VERSION = q(1.0000);
86867f3a 9
8fec41b9 10# This is to allow DBM::Deep::Array to handle negative indices on
11# its own. Otherwise, Perl would intercept the call to negative
12# indices for us. This was causing bugs for negative index handling.
460b1067 13our $NEGATIVE_INDICES = 1;
7910cf68 14
6fe26b29 15use base 'DBM::Deep';
16
e1b265cc 17use Scalar::Util ();
18
596e9574 19sub _get_self {
8db25060 20 eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
596e9574 21}
22
f9c33187 23sub _repr { shift;[ @_ ] }
24
25sub _import {
26 my $self = shift;
27 my ($struct) = @_;
28
2120a181 29 $self->push( @$struct );
f9c33187 30
31 return 1;
32}
2120a181 33
6fe26b29 34sub TIEARRAY {
6fe26b29 35 my $class = shift;
0ca7ea98 36 my $args = $class->_get_args( @_ );
504185fb 37
38 $args->{type} = $class->TYPE_ARRAY;
39
40 return $class->_init($args);
6fe26b29 41}
42
7f441181 43sub FETCH {
eea0d863 44 my $self = shift->_get_self;
45 my ($key) = @_;
7f441181 46
504185fb 47 $self->lock( $self->LOCK_SH );
9a187d8c 48
2120a181 49 if ( !defined $key ) {
50 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
51 }
52 elsif ( $key =~ /^-?\d+$/ ) {
7f441181 53 if ( $key < 0 ) {
54 $key += $self->FETCHSIZE;
9281d66b 55 unless ( $key >= 0 ) {
56 $self->unlock;
57 return;
58 }
7f441181 59 }
c3aafc14 60 }
2120a181 61 elsif ( $key ne 'length' ) {
62 $self->unlock;
63 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
7f441181 64 }
65
2120a181 66 my $rv = $self->SUPER::FETCH( $key );
9281d66b 67
68 $self->unlock;
69
70 return $rv;
7f441181 71}
72
cb79ec85 73sub STORE {
74 my $self = shift->_get_self;
75 my ($key, $value) = @_;
76
9281d66b 77 $self->lock( $self->LOCK_EX );
78
9ab67b8c 79 my $size;
c3aafc14 80 my $idx_is_numeric;
2120a181 81 if ( !defined $key ) {
82 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
83 }
84 elsif ( $key =~ /^-?\d+$/ ) {
c3aafc14 85 $idx_is_numeric = 1;
cb79ec85 86 if ( $key < 0 ) {
9ab67b8c 87 $size = $self->FETCHSIZE;
c3aafc14 88 if ( $key + $size < 0 ) {
89 die( "Modification of non-creatable array value attempted, subscript $key" );
baa27ab6 90 }
c3aafc14 91 $key += $size
cb79ec85 92 }
cb79ec85 93 }
2120a181 94 elsif ( $key ne 'length' ) {
95 $self->unlock;
96 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97 }
cb79ec85 98
2120a181 99 my $rv = $self->SUPER::STORE( $key, $value );
cb79ec85 100
c3aafc14 101 if ( $idx_is_numeric ) {
9ab67b8c 102 $size = $self->FETCHSIZE unless defined $size;
c3aafc14 103 if ( $key >= $size ) {
104 $self->STORESIZE( $key + 1 );
9ab67b8c 105 }
cb79ec85 106 }
107
9281d66b 108 $self->unlock;
109
cb79ec85 110 return $rv;
111}
112
baa27ab6 113sub EXISTS {
eea0d863 114 my $self = shift->_get_self;
115 my ($key) = @_;
baa27ab6 116
504185fb 117 $self->lock( $self->LOCK_SH );
9281d66b 118
2120a181 119 if ( !defined $key ) {
120 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
121 }
122 elsif ( $key =~ /^-?\d+$/ ) {
baa27ab6 123 if ( $key < 0 ) {
124 $key += $self->FETCHSIZE;
9281d66b 125 unless ( $key >= 0 ) {
126 $self->unlock;
127 return;
128 }
129 }
9281d66b 130 }
2120a181 131 elsif ( $key ne 'length' ) {
132 $self->unlock;
133 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
134 }
9281d66b 135
136 my $rv = $self->SUPER::EXISTS( $key );
137
138 $self->unlock;
139
140 return $rv;
141}
142
143sub DELETE {
eea0d863 144 my $self = shift->_get_self;
145 my ($key) = @_;
9281d66b 146
9281d66b 147 $self->lock( $self->LOCK_EX );
148
149 my $size = $self->FETCHSIZE;
2120a181 150 if ( !defined $key ) {
151 DBM::Deep->_throw_error( "Cannot use an undefined array index." );
152 }
153 elsif ( $key =~ /^-?\d+$/ ) {
9281d66b 154 if ( $key < 0 ) {
155 $key += $size;
156 unless ( $key >= 0 ) {
157 $self->unlock;
158 return;
159 }
baa27ab6 160 }
baa27ab6 161 }
2120a181 162 elsif ( $key ne 'length' ) {
163 $self->unlock;
164 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
165 }
baa27ab6 166
c3aafc14 167 my $rv = $self->SUPER::DELETE( $key );
9281d66b 168
c3aafc14 169 if ($rv && $key == $size - 1) {
2120a181 170 $self->STORESIZE( $key );
504185fb 171 }
9281d66b 172
173 $self->unlock;
174
175 return $rv;
baa27ab6 176}
177
2120a181 178# Now that we have a real Reference sector, we should store arrayzize there. However,
179# arraysize needs to be transactionally-aware, so a simple location to store it isn't
180# going to work.
6fe26b29 181sub FETCHSIZE {
9281d66b 182 my $self = shift->_get_self;
183
184 $self->lock( $self->LOCK_SH );
185
83371fe3 186 my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
187 $self->_storage->{filter_fetch_value} = undef;
504185fb 188
2120a181 189 my $size = $self->FETCH('length') || 0;
504185fb 190
83371fe3 191 $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
504185fb 192
9281d66b 193 $self->unlock;
194
2120a181 195 return $size;
6fe26b29 196}
197
198sub STORESIZE {
eea0d863 199 my $self = shift->_get_self;
504185fb 200 my ($new_length) = @_;
201
9281d66b 202 $self->lock( $self->LOCK_EX );
203
83371fe3 204 my $SAVE_FILTER = $self->_storage->{filter_store_value};
205 $self->_storage->{filter_store_value} = undef;
504185fb 206
2120a181 207 my $result = $self->STORE('length', $new_length, 'length');
504185fb 208
83371fe3 209 $self->_storage->{filter_store_value} = $SAVE_FILTER;
504185fb 210
9281d66b 211 $self->unlock;
212
504185fb 213 return $result;
6fe26b29 214}
215
216sub POP {
eea0d863 217 my $self = shift->_get_self;
9281d66b 218
219 $self->lock( $self->LOCK_EX );
220
504185fb 221 my $length = $self->FETCHSIZE();
222
223 if ($length) {
224 my $content = $self->FETCH( $length - 1 );
225 $self->DELETE( $length - 1 );
9281d66b 226
227 $self->unlock;
228
504185fb 229 return $content;
230 }
231 else {
9281d66b 232 $self->unlock;
504185fb 233 return;
234 }
6fe26b29 235}
236
237sub PUSH {
2ac02042 238 my $self = shift->_get_self;
504185fb 239
9281d66b 240 $self->lock( $self->LOCK_EX );
241
504185fb 242 my $length = $self->FETCHSIZE();
9281d66b 243
504185fb 244 while (my $content = shift @_) {
245 $self->STORE( $length, $content );
246 $length++;
247 }
8f6d6ed0 248
9281d66b 249 $self->unlock;
250
8f6d6ed0 251 return $length;
6fe26b29 252}
253
254sub SHIFT {
eea0d863 255 my $self = shift->_get_self;
9281d66b 256
257 $self->lock( $self->LOCK_EX );
258
504185fb 259 my $length = $self->FETCHSIZE();
260
261 if ($length) {
262 my $content = $self->FETCH( 0 );
263
264 for (my $i = 0; $i < $length - 1; $i++) {
265 $self->STORE( $i, $self->FETCH($i + 1) );
266 }
267 $self->DELETE( $length - 1 );
9281d66b 268
269 $self->unlock;
504185fb 270
271 return $content;
272 }
273 else {
9281d66b 274 $self->unlock;
504185fb 275 return;
276 }
6fe26b29 277}
278
279sub UNSHIFT {
2ac02042 280 my $self = shift->_get_self;
504185fb 281 my @new_elements = @_;
9281d66b 282
283 $self->lock( $self->LOCK_EX );
284
504185fb 285 my $length = $self->FETCHSIZE();
286 my $new_size = scalar @new_elements;
287
288 if ($length) {
289 for (my $i = $length - 1; $i >= 0; $i--) {
290 $self->STORE( $i + $new_size, $self->FETCH($i) );
291 }
292 }
293
294 for (my $i = 0; $i < $new_size; $i++) {
295 $self->STORE( $i, $new_elements[$i] );
296 }
8f6d6ed0 297
9281d66b 298 $self->unlock;
299
8f6d6ed0 300 return $length + $new_size;
6fe26b29 301}
302
303sub SPLICE {
2ac02042 304 my $self = shift->_get_self;
9281d66b 305
306 $self->lock( $self->LOCK_EX );
307
504185fb 308 my $length = $self->FETCHSIZE();
309
310 ##
311 # Calculate offset and length of splice
312 ##
313 my $offset = shift;
714618f0 314 $offset = 0 unless defined $offset;
504185fb 315 if ($offset < 0) { $offset += $length; }
316
317 my $splice_length;
318 if (scalar @_) { $splice_length = shift; }
319 else { $splice_length = $length - $offset; }
320 if ($splice_length < 0) { $splice_length += ($length - $offset); }
321
322 ##
323 # Setup array with new elements, and copy out old elements for return
324 ##
325 my @new_elements = @_;
326 my $new_size = scalar @new_elements;
327
df3c5701 328 my @old_elements = map {
329 $self->FETCH( $_ )
330 } $offset .. ($offset + $splice_length - 1);
504185fb 331
332 ##
333 # Adjust array length, and shift elements to accomodate new section.
334 ##
6fe26b29 335 if ( $new_size != $splice_length ) {
336 if ($new_size > $splice_length) {
337 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
338 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
339 }
340 }
341 else {
342 for (my $i = $offset + $splice_length; $i < $length; $i++) {
343 $self->STORE( $i + ($new_size - $splice_length), $self->FETCH($i) );
344 }
345 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
346 $self->DELETE( $length - 1 );
347 $length--;
348 }
349 }
504185fb 350 }
351
352 ##
353 # Insert new elements into array
354 ##
355 for (my $i = $offset; $i < $offset + $new_size; $i++) {
356 $self->STORE( $i, shift @new_elements );
357 }
358
9281d66b 359 $self->unlock;
360
504185fb 361 ##
362 # Return deleted section, or last element in scalar context.
363 ##
364 return wantarray ? @old_elements : $old_elements[-1];
6fe26b29 365}
366
2120a181 367# We don't need to populate it, yet.
460b1067 368# It will be useful, though, when we split out HASH and ARRAY
685e40f1 369sub EXTEND {
504185fb 370 ##
371 # Perl will call EXTEND() when the array is likely to grow.
372 # We don't care, but include it because it gets called at times.
373 ##
685e40f1 374}
6fe26b29 375
f9c33187 376sub _copy_node {
898fd1fd 377 my $self = shift;
f9c33187 378 my ($db_temp) = @_;
379
380 my $length = $self->length();
381 for (my $index = 0; $index < $length; $index++) {
382 my $value = $self->get($index);
383 $self->_copy_value( \$db_temp->[$index], $value );
384 }
385
386 return 1;
387}
388
6fe26b29 389##
390# Public method aliases
391##
f9c33187 392sub length { (shift)->FETCHSIZE(@_) }
393sub pop { (shift)->POP(@_) }
394sub push { (shift)->PUSH(@_) }
395sub unshift { (shift)->UNSHIFT(@_) }
396sub splice { (shift)->SPLICE(@_) }
397
398# This must be last otherwise we have to qualify all other calls to shift
399# as calls to CORE::shift
f75b719e 400sub shift { (CORE::shift)->SHIFT(@_) }
6fe26b29 401
4021;
403__END__