r6200@rob-kinyons-computer-2 (orig r9980): rkinyon | 2007-09-22 21:02:54 -0400
[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
1cff45d7 8our $VERSION = q(1.0003);
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
807f63a7 254# XXX This really needs to be something more direct within the file, not a
255# fetch and re-store. -RobK, 2007-09-20
256sub _move_value {
257 my $self = shift;
258 my ($old_key, $new_key) = @_;
259
1cff45d7 260 return $self->_engine->make_reference( $self, $old_key, $new_key );
807f63a7 261}
262
6fe26b29 263sub SHIFT {
eea0d863 264 my $self = shift->_get_self;
9281d66b 265
266 $self->lock( $self->LOCK_EX );
267
504185fb 268 my $length = $self->FETCHSIZE();
269
1cff45d7 270 if ( !$length ) {
9281d66b 271 $self->unlock;
504185fb 272 return;
273 }
1cff45d7 274
275 my $content = $self->FETCH( 0 );
276
277 for (my $i = 0; $i < $length - 1; $i++) {
278 $self->_move_value( $i+1, $i );
279 }
280 $self->DELETE( $length - 1 );
281
282 $self->unlock;
283
284 return $content;
6fe26b29 285}
286
287sub UNSHIFT {
2ac02042 288 my $self = shift->_get_self;
504185fb 289 my @new_elements = @_;
9281d66b 290
291 $self->lock( $self->LOCK_EX );
292
504185fb 293 my $length = $self->FETCHSIZE();
294 my $new_size = scalar @new_elements;
295
296 if ($length) {
297 for (my $i = $length - 1; $i >= 0; $i--) {
807f63a7 298 $self->_move_value( $i, $i+$new_size );
504185fb 299 }
1cff45d7 300
301 $self->STORESIZE( $length + $new_size );
504185fb 302 }
303
304 for (my $i = 0; $i < $new_size; $i++) {
305 $self->STORE( $i, $new_elements[$i] );
306 }
8f6d6ed0 307
9281d66b 308 $self->unlock;
309
8f6d6ed0 310 return $length + $new_size;
6fe26b29 311}
312
313sub SPLICE {
2ac02042 314 my $self = shift->_get_self;
9281d66b 315
316 $self->lock( $self->LOCK_EX );
317
504185fb 318 my $length = $self->FETCHSIZE();
319
320 ##
321 # Calculate offset and length of splice
322 ##
323 my $offset = shift;
714618f0 324 $offset = 0 unless defined $offset;
504185fb 325 if ($offset < 0) { $offset += $length; }
326
327 my $splice_length;
328 if (scalar @_) { $splice_length = shift; }
329 else { $splice_length = $length - $offset; }
330 if ($splice_length < 0) { $splice_length += ($length - $offset); }
331
332 ##
333 # Setup array with new elements, and copy out old elements for return
334 ##
335 my @new_elements = @_;
336 my $new_size = scalar @new_elements;
337
df3c5701 338 my @old_elements = map {
339 $self->FETCH( $_ )
340 } $offset .. ($offset + $splice_length - 1);
504185fb 341
342 ##
343 # Adjust array length, and shift elements to accomodate new section.
344 ##
6fe26b29 345 if ( $new_size != $splice_length ) {
346 if ($new_size > $splice_length) {
347 for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
807f63a7 348 $self->_move_value( $i, $i + ($new_size - $splice_length) );
6fe26b29 349 }
1cff45d7 350 $self->STORESIZE( $length + $new_size - $splice_length );
6fe26b29 351 }
352 else {
353 for (my $i = $offset + $splice_length; $i < $length; $i++) {
807f63a7 354 $self->_move_value( $i, $i + ($new_size - $splice_length) );
6fe26b29 355 }
356 for (my $i = 0; $i < $splice_length - $new_size; $i++) {
357 $self->DELETE( $length - 1 );
358 $length--;
359 }
360 }
504185fb 361 }
362
363 ##
364 # Insert new elements into array
365 ##
366 for (my $i = $offset; $i < $offset + $new_size; $i++) {
367 $self->STORE( $i, shift @new_elements );
368 }
369
9281d66b 370 $self->unlock;
371
504185fb 372 ##
373 # Return deleted section, or last element in scalar context.
374 ##
375 return wantarray ? @old_elements : $old_elements[-1];
6fe26b29 376}
377
2120a181 378# We don't need to populate it, yet.
460b1067 379# It will be useful, though, when we split out HASH and ARRAY
685e40f1 380sub EXTEND {
504185fb 381 ##
382 # Perl will call EXTEND() when the array is likely to grow.
383 # We don't care, but include it because it gets called at times.
384 ##
685e40f1 385}
6fe26b29 386
f9c33187 387sub _copy_node {
898fd1fd 388 my $self = shift;
f9c33187 389 my ($db_temp) = @_;
390
391 my $length = $self->length();
392 for (my $index = 0; $index < $length; $index++) {
393 my $value = $self->get($index);
394 $self->_copy_value( \$db_temp->[$index], $value );
395 }
396
397 return 1;
398}
399
6fe26b29 400##
401# Public method aliases
402##
f9c33187 403sub length { (shift)->FETCHSIZE(@_) }
404sub pop { (shift)->POP(@_) }
405sub push { (shift)->PUSH(@_) }
406sub unshift { (shift)->UNSHIFT(@_) }
407sub splice { (shift)->SPLICE(@_) }
408
409# This must be last otherwise we have to qualify all other calls to shift
410# as calls to CORE::shift
f75b719e 411sub shift { (CORE::shift)->SHIFT(@_) }
6fe26b29 412
4131;
414__END__