r6200@rob-kinyons-computer-2 (orig r9980): rkinyon | 2007-09-22 21:02:54 -0400
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Array.pm
1 package DBM::Deep::Array;
2
3 use 5.006_000;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = q(1.0003);
9
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.
13 our $NEGATIVE_INDICES = 1;
14
15 use base 'DBM::Deep';
16
17 use Scalar::Util ();
18
19 sub _get_self {
20     eval { local $SIG{'__DIE__'}; tied( @{$_[0]} ) } || $_[0]
21 }
22
23 sub _repr { shift;[ @_ ] }
24
25 sub _import {
26     my $self = shift;
27     my ($struct) = @_;
28
29     $self->push( @$struct );
30
31     return 1;
32 }
33
34 sub TIEARRAY {
35     my $class = shift;
36     my $args = $class->_get_args( @_ );
37
38     $args->{type} = $class->TYPE_ARRAY;
39
40     return $class->_init($args);
41 }
42
43 sub FETCH {
44     my $self = shift->_get_self;
45     my ($key) = @_;
46
47     $self->lock( $self->LOCK_SH );
48
49     if ( !defined $key ) {
50         DBM::Deep->_throw_error( "Cannot use an undefined array index." );
51     }
52     elsif ( $key =~ /^-?\d+$/ ) {
53         if ( $key < 0 ) {
54             $key += $self->FETCHSIZE;
55             unless ( $key >= 0 ) {
56                 $self->unlock;
57                 return;
58             }
59         }
60     }
61     elsif ( $key ne 'length' ) {
62         $self->unlock;
63         DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
64     }
65
66     my $rv = $self->SUPER::FETCH( $key );
67
68     $self->unlock;
69
70     return $rv;
71 }
72
73 sub STORE {
74     my $self = shift->_get_self;
75     my ($key, $value) = @_;
76
77     $self->lock( $self->LOCK_EX );
78
79     my $size;
80     my $idx_is_numeric;
81     if ( !defined $key ) {
82         DBM::Deep->_throw_error( "Cannot use an undefined array index." );
83     }
84     elsif ( $key =~ /^-?\d+$/ ) {
85         $idx_is_numeric = 1;
86         if ( $key < 0 ) {
87             $size = $self->FETCHSIZE;
88             if ( $key + $size < 0 ) {
89                 die( "Modification of non-creatable array value attempted, subscript $key" );
90             }
91             $key += $size
92         }
93     }
94     elsif ( $key ne 'length' ) {
95         $self->unlock;
96         DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
97     }
98
99     my $rv = $self->SUPER::STORE( $key, $value );
100
101     if ( $idx_is_numeric ) {
102         $size = $self->FETCHSIZE unless defined $size;
103         if ( $key >= $size ) {
104             $self->STORESIZE( $key + 1 );
105         }
106     }
107
108     $self->unlock;
109
110     return $rv;
111 }
112
113 sub EXISTS {
114     my $self = shift->_get_self;
115     my ($key) = @_;
116
117     $self->lock( $self->LOCK_SH );
118
119     if ( !defined $key ) {
120         DBM::Deep->_throw_error( "Cannot use an undefined array index." );
121     }
122     elsif ( $key =~ /^-?\d+$/ ) {
123         if ( $key < 0 ) {
124             $key += $self->FETCHSIZE;
125             unless ( $key >= 0 ) {
126                 $self->unlock;
127                 return;
128             }
129         }
130     }
131     elsif ( $key ne 'length' ) {
132         $self->unlock;
133         DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
134     }
135
136     my $rv = $self->SUPER::EXISTS( $key );
137
138     $self->unlock;
139
140     return $rv;
141 }
142
143 sub DELETE {
144     my $self = shift->_get_self;
145     my ($key) = @_;
146
147     $self->lock( $self->LOCK_EX );
148
149     my $size = $self->FETCHSIZE;
150     if ( !defined $key ) {
151         DBM::Deep->_throw_error( "Cannot use an undefined array index." );
152     }
153     elsif ( $key =~ /^-?\d+$/ ) {
154         if ( $key < 0 ) {
155             $key += $size;
156             unless ( $key >= 0 ) {
157                 $self->unlock;
158                 return;
159             }
160         }
161     }
162     elsif ( $key ne 'length' ) {
163         $self->unlock;
164         DBM::Deep->_throw_error( "Cannot use '$key' as an array index." );
165     }
166
167     my $rv = $self->SUPER::DELETE( $key );
168
169     if ($rv && $key == $size - 1) {
170         $self->STORESIZE( $key );
171     }
172
173     $self->unlock;
174
175     return $rv;
176 }
177
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.
181 sub FETCHSIZE {
182     my $self = shift->_get_self;
183
184     $self->lock( $self->LOCK_SH );
185
186     my $SAVE_FILTER = $self->_storage->{filter_fetch_value};
187     $self->_storage->{filter_fetch_value} = undef;
188
189     my $size = $self->FETCH('length') || 0;
190
191     $self->_storage->{filter_fetch_value} = $SAVE_FILTER;
192
193     $self->unlock;
194
195     return $size;
196 }
197
198 sub STORESIZE {
199     my $self = shift->_get_self;
200     my ($new_length) = @_;
201
202     $self->lock( $self->LOCK_EX );
203
204     my $SAVE_FILTER = $self->_storage->{filter_store_value};
205     $self->_storage->{filter_store_value} = undef;
206
207     my $result = $self->STORE('length', $new_length, 'length');
208
209     $self->_storage->{filter_store_value} = $SAVE_FILTER;
210
211     $self->unlock;
212
213     return $result;
214 }
215
216 sub POP {
217     my $self = shift->_get_self;
218
219     $self->lock( $self->LOCK_EX );
220
221     my $length = $self->FETCHSIZE();
222
223     if ($length) {
224         my $content = $self->FETCH( $length - 1 );
225         $self->DELETE( $length - 1 );
226
227         $self->unlock;
228
229         return $content;
230     }
231     else {
232         $self->unlock;
233         return;
234     }
235 }
236
237 sub PUSH {
238     my $self = shift->_get_self;
239
240     $self->lock( $self->LOCK_EX );
241
242     my $length = $self->FETCHSIZE();
243
244     while (my $content = shift @_) {
245         $self->STORE( $length, $content );
246         $length++;
247     }
248
249     $self->unlock;
250
251     return $length;
252 }
253
254 # XXX This really needs to be something more direct within the file, not a
255 # fetch and re-store. -RobK, 2007-09-20
256 sub _move_value {
257     my $self = shift;
258     my ($old_key, $new_key) = @_;
259
260     return $self->_engine->make_reference( $self, $old_key, $new_key );
261 }
262
263 sub SHIFT {
264     my $self = shift->_get_self;
265
266     $self->lock( $self->LOCK_EX );
267
268     my $length = $self->FETCHSIZE();
269
270     if ( !$length ) {
271         $self->unlock;
272         return;
273     }
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;
285 }
286
287 sub UNSHIFT {
288     my $self = shift->_get_self;
289     my @new_elements = @_;
290
291     $self->lock( $self->LOCK_EX );
292
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--) {
298             $self->_move_value( $i, $i+$new_size );
299         }
300
301         $self->STORESIZE( $length + $new_size );
302     }
303
304     for (my $i = 0; $i < $new_size; $i++) {
305         $self->STORE( $i, $new_elements[$i] );
306     }
307
308     $self->unlock;
309
310     return $length + $new_size;
311 }
312
313 sub SPLICE {
314     my $self = shift->_get_self;
315
316     $self->lock( $self->LOCK_EX );
317
318     my $length = $self->FETCHSIZE();
319
320     ##
321     # Calculate offset and length of splice
322     ##
323     my $offset = shift;
324     $offset = 0 unless defined $offset;
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
338     my @old_elements = map {
339         $self->FETCH( $_ )
340     } $offset .. ($offset + $splice_length - 1);
341
342     ##
343     # Adjust array length, and shift elements to accomodate new section.
344     ##
345     if ( $new_size != $splice_length ) {
346         if ($new_size > $splice_length) {
347             for (my $i = $length - 1; $i >= $offset + $splice_length; $i--) {
348                 $self->_move_value( $i, $i + ($new_size - $splice_length) );
349             }
350             $self->STORESIZE( $length + $new_size - $splice_length );
351         }
352         else {
353             for (my $i = $offset + $splice_length; $i < $length; $i++) {
354                 $self->_move_value( $i, $i + ($new_size - $splice_length) );
355             }
356             for (my $i = 0; $i < $splice_length - $new_size; $i++) {
357                 $self->DELETE( $length - 1 );
358                 $length--;
359             }
360         }
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
370     $self->unlock;
371
372     ##
373     # Return deleted section, or last element in scalar context.
374     ##
375     return wantarray ? @old_elements : $old_elements[-1];
376 }
377
378 # We don't need to populate it, yet.
379 # It will be useful, though, when we split out HASH and ARRAY
380 sub EXTEND {
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     ##
385 }
386
387 sub _copy_node {
388     my $self = shift;
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
400 ##
401 # Public method aliases
402 ##
403 sub length { (shift)->FETCHSIZE(@_) }
404 sub pop { (shift)->POP(@_) }
405 sub push { (shift)->PUSH(@_) }
406 sub unshift { (shift)->UNSHIFT(@_) }
407 sub 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
411 sub shift { (CORE::shift)->SHIFT(@_) }
412
413 1;
414 __END__