New branch plus fix for shift/unshift/splice with references being moved. The fix...
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Array.pm
CommitLineData
6fe26b29 1package DBM::Deep::Array;
2
b48ae6ec 3use 5.006_000;
460b1067 4
6fe26b29 5use strict;
460b1067 6use warnings;
6fe26b29 7
fbd470e4 8our $VERSION = q(1.0001);
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
9a63e1f2 29 $self->push( @$struct );
f9c33187 30
31 return 1;
32}
9a63e1f2 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
9a63e1f2 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 }
9a63e1f2 61 elsif ( $key ne 'length' ) {
62 $self->unlock;
63 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
7f441181 64 }
65
9a63e1f2 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;
9a63e1f2 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 }
9a63e1f2 94 elsif ( $key ne 'length' ) {
95 $self->unlock;
96 DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97 }
cb79ec85 98
9a63e1f2 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
9a63e1f2 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 }
9a63e1f2 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;
9a63e1f2 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 }
9a63e1f2 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) {
9a63e1f2 170 $self->STORESIZE( $key );
504185fb 171 }
9281d66b 172
173 $self->unlock;
174
175 return $rv;
baa27ab6 176}
177
9a63e1f2 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
9a63e1f2 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
9a63e1f2 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
9a63e1f2 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
7ffc75ae 254sub _move_value {
255 my $self = shift;
256 my ($old_key, $new_key) = @_;
257
258 my $val = $self->FETCH( $old_key );
259 if ( eval { $val->isa( 'DBM::Deep::Hash' ) } ) {
260 $self->STORE( $new_key, { %$val } );
261 }
262 elsif ( eval { $val->isa( 'DBM::Deep::Array' ) } ) {
263 $self->STORE( $new_key, [ @$val ] );
264 }
265 else {
266 $self->STORE( $new_key, $val );
267 }
268}
269
6fe26b29 270sub SHIFT {
eea0d863 271 my $self = shift->_get_self;
9281d66b 272
273 $self->lock( $self->LOCK_EX );
274
504185fb 275 my $length = $self->FETCHSIZE();
276
277 if ($length) {
278 my $content = $self->FETCH( 0 );
279
280 for (my $i = 0; $i < $length - 1; $i++) {
7ffc75ae 281 $self->_move_value( $i+1, $i );
504185fb 282 }
283 $self->DELETE( $length - 1 );
9281d66b 284
285 $self->unlock;
504185fb 286
287 return $content;
288 }
289 else {
9281d66b 290 $self->unlock;
504185fb 291 return;
292 }
6fe26b29 293}
294
295sub UNSHIFT {
2ac02042 296 my $self = shift->_get_self;
504185fb 297 my @new_elements = @_;
9281d66b 298
299 $self->lock( $self->LOCK_EX );
300
504185fb 301 my $length = $self->FETCHSIZE();
302 my $new_size = scalar @new_elements;
303
304 if ($length) {
305 for (my $i = $length - 1; $i >= 0; $i--) {
7ffc75ae 306 $self->_move_value( $i, $i+$new_size );
504185fb 307 }
308 }
309
310 for (my $i = 0; $i < $new_size; $i++) {
311 $self->STORE( $i, $new_elements[$i] );
312 }
8f6d6ed0 313
9281d66b 314 $self->unlock;
315
8f6d6ed0 316 return $length + $new_size;
6fe26b29 317}
318
319sub SPLICE {
2ac02042 320 my $self = shift->_get_self;
9281d66b 321
322 $self->lock( $self->LOCK_EX );
323
504185fb 324 my $length = $self->FETCHSIZE();
325
326 ##
327 # Calculate offset and length of splice
328 ##
329 my $offset = shift;
714618f0 330 $offset = 0 unless defined $offset;
504185fb 331 if ($offset < 0) { $offset += $length; }
332
333 my $splice_length;
334 if (scalar @_) { $splice_length = shift; }
335 else { $splice_length = $length - $offset; }
336 if ($splice_length < 0) { $splice_length += ($length - $offset); }
337
338 ##
339 # Setup array with new elements, and copy out old elements for return
340 ##
341 my @new_elements = @_;
342 my $new_size = scalar @new_elements;
343
df3c5701 344 my @old_elements = map {
345 $self->FETCH( $_ )
346 } $offset .. ($offset + $splice_length - 1);
504185fb 347
348 ##
349 # Adjust array length, and shift elements to accomodate new section.
350 ##
6fe26b29 351 if ( $new_size != $splice_length ) {
352 if ($new_size > $splice_length) {
353 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
7ffc75ae 354 $self->_move_value( $i, $i + ($new_size - $splice_length) );
6fe26b29 355 }
356 }
357 else {
358 for (my $i = $offset + $splice_length; $i < $length; $i++) {
7ffc75ae 359 $self->_move_value( $i, $i + ($new_size - $splice_length) );
6fe26b29 360 }
361 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
362 $self->DELETE( $length - 1 );
363 $length--;
364 }
365 }
504185fb 366 }
367
368 ##
369 # Insert new elements into array
370 ##
371 for (my $i = $offset; $i < $offset + $new_size; $i++) {
372 $self->STORE( $i, shift @new_elements );
373 }
374
9281d66b 375 $self->unlock;
376
504185fb 377 ##
378 # Return deleted section, or last element in scalar context.
379 ##
380 return wantarray ? @old_elements : $old_elements[-1];
6fe26b29 381}
382
9a63e1f2 383# We don't need to populate it, yet.
460b1067 384# It will be useful, though, when we split out HASH and ARRAY
685e40f1 385sub EXTEND {
504185fb 386 ##
387 # Perl will call EXTEND() when the array is likely to grow.
388 # We don't care, but include it because it gets called at times.
389 ##
685e40f1 390}
6fe26b29 391
f9c33187 392sub _copy_node {
898fd1fd 393 my $self = shift;
f9c33187 394 my ($db_temp) = @_;
395
396 my $length = $self->length();
397 for (my $index = 0; $index < $length; $index++) {
398 my $value = $self->get($index);
399 $self->_copy_value( \$db_temp->[$index], $value );
400 }
401
402 return 1;
403}
404
6fe26b29 405##
406# Public method aliases
407##
f9c33187 408sub length { (shift)->FETCHSIZE(@_) }
409sub pop { (shift)->POP(@_) }
410sub push { (shift)->PUSH(@_) }
411sub unshift { (shift)->UNSHIFT(@_) }
412sub splice { (shift)->SPLICE(@_) }
413
414# This must be last otherwise we have to qualify all other calls to shift
415# as calls to CORE::shift
f75b719e 416sub shift { (CORE::shift)->SHIFT(@_) }
6fe26b29 417
4181;
419__END__