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