| 1 | package forks::BerkeleyDB::shared::array; | ||
| 2 | |||
| 3 | $VERSION = 0.060; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use BerkeleyDB 0.27; | ||
| 7 | use forks::BerkeleyDB::ElemNotExists; | ||
| 8 | use vars qw(@ISA); | ||
| 9 | @ISA = qw(BerkeleyDB::Recno); | ||
| 10 | |||
| 11 | #--------------------------------------------------------------------------- | ||
| 12 | sub new { | ||
| 13 | my $type = shift; | ||
| 14 | my $class = ref($type) || $type; | ||
| 15 | my $self = $class->SUPER::new(@_); | ||
| 16 | return undef unless defined $self; | ||
| 17 | return bless($self, $class); | ||
| 18 | } | ||
| 19 | |||
| 20 | # standard Perl feature methods implemented: | ||
| 21 | # TIEARRAY | ||
| 22 | # FETCH, STORE | ||
| 23 | # FETCHSIZE, STORESIZE | ||
| 24 | # EXTEND | ||
| 25 | # EXISTS, DELETE | ||
| 26 | # CLEAR | ||
| 27 | # PUSH, POP | ||
| 28 | # SHIFT, UNSHIFT | ||
| 29 | # SPLICE | ||
| 30 | # UNTIE, DESTROY | ||
| 31 | |||
| 32 | #--------------------------------------------------------------------------- | ||
| 33 | *TIEARRAY = *TIEARRAY = \&new; | ||
| 34 | |||
| 35 | sub _exists_elem ($) { | ||
| 36 | my $value = shift; | ||
| 37 | return defined $value && UNIVERSAL::isa($value, 'forks::BerkeleyDB::ElemNotExists') ? 0 : 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _db_filter_array_elem_not_exists_to_undef ($) { | ||
| 41 | my $value = shift; | ||
| 42 | return defined $value && UNIVERSAL::isa($value, 'forks::BerkeleyDB::ElemNotExists') ? undef : $value; | ||
| 43 | } | ||
| 44 | |||
| 45 | #--------------------------------------------------------------------------- | ||
| 46 | sub FETCH { | ||
| 47 | my $value = undef; | ||
| 48 | $_[0]->db_get($_[1], $value); | ||
| 49 | return defined $value && UNIVERSAL::isa($value, 'forks::BerkeleyDB::ElemNotExists') ? undef : $value; #_db_filter_array_elem_not_exists_to_undef | ||
| 50 | } | ||
| 51 | |||
| 52 | sub STORE { | ||
| 53 | if (defined $_[2]) { | ||
| 54 | return undef unless $_[0]->db_put($_[1], $_[2]) == 0; | ||
| 55 | } else { | ||
| 56 | no warnings 'uninitialized'; | ||
| 57 | return undef unless $_[0]->db_put($_[1], $_[2]) == 0; | ||
| 58 | } | ||
| 59 | return $_[2]; | ||
| 60 | } | ||
| 61 | |||
| 62 | #--------------------------------------------------------------------------- | ||
| 63 | #sub FETCHSIZE {} #use BerkeleyDB.pm method | ||
| 64 | |||
| 65 | sub STORESIZE { | ||
| 66 | my $self = shift; | ||
| 67 | my $count = shift; | ||
| 68 | my $nkeys = $self->FETCHSIZE(); | ||
| 69 | #warn "STORESIZE: count=$count; nkeys=$nkeys"; | ||
| 70 | if ($nkeys < $count) { #add undef elements | ||
| 71 | my $value = forks::BerkeleyDB::ElemNotExists->instance(); | ||
| 72 | $self->db_put($_, $value, DB_APPEND) for ($nkeys..($count - 1)); | ||
| 73 | } | ||
| 74 | elsif ($nkeys > $count) { #trim elements | ||
| 75 | my $value = undef; | ||
| 76 | my $cursor = $self->db_cursor(DB_WRITECURSOR); | ||
| 77 | for (($count - 1)..($nkeys - 1)) { | ||
| 78 | return $self->FETCHSIZE() unless $cursor->c_get($_, $value, DB_LAST) == 0; #optimized: using DB_LAST prevents database renumbering | ||
| 79 | return $self->FETCHSIZE() unless $cursor->c_del() == 0; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | return $self->FETCHSIZE(); | ||
| 83 | } | ||
| 84 | |||
| 85 | #--------------------------------------------------------------------------- | ||
| 86 | sub EXTEND { | ||
| 87 | return $_[1]; #no need for pre-allocation | ||
| 88 | } | ||
| 89 | |||
| 90 | #--------------------------------------------------------------------------- | ||
| 91 | sub EXISTS { #test that this works after delete | ||
| 92 | my $self = shift; | ||
| 93 | my $key = shift; | ||
| 94 | my $value = undef; | ||
| 95 | return 0 unless $self->db_get($key, $value) == 0; | ||
| 96 | return _exists_elem($value) ? 1 : 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub DELETE { #doesn't appear to support deleting entire array (delete @a[0..$#a] == DB truncate)? | ||
| 100 | my $self = shift; | ||
| 101 | return undef unless @_; | ||
| 102 | my $key = shift; | ||
| 103 | my $value = undef; | ||
| 104 | #warn "DELETE: key=$key"; | ||
| 105 | my $cursor = $self->db_cursor(DB_WRITECURSOR); | ||
| 106 | return undef unless $cursor->c_get($key, $value, DB_SET) == 0; #set cursor position | ||
| 107 | if ($key == $self->FETCHSIZE() - 1) { #if this is last key, delete element | ||
| 108 | return undef unless $cursor->c_del() == 0; | ||
| 109 | } | ||
| 110 | else { #initialize element to "not exists" state | ||
| 111 | my $new_value = forks::BerkeleyDB::ElemNotExists->instance(); | ||
| 112 | #warn "DELETE: success!"; | ||
| 113 | return undef unless $cursor->c_put($key, $new_value, DB_CURRENT) == 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | ### delete any other "not exists" elements, starting from last element ### | ||
| 117 | my ($cur_key, $cur_value) = (0, ''); | ||
| 118 | while ($cursor->c_get($key, $value, DB_LAST) == 0) { | ||
| 119 | if (_exists_elem($value)) { last; } | ||
| 120 | else { | ||
| 121 | return undef unless $cursor->c_del() == 0; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | $cursor->c_close(); | ||
| 126 | #warn "DELETE: success!"; | ||
| 127 | return _db_filter_array_elem_not_exists_to_undef($value); | ||
| 128 | } | ||
| 129 | |||
| 130 | #--------------------------------------------------------------------------- | ||
| 131 | sub CLEAR { | ||
| 132 | my $self = shift; | ||
| 133 | my $count = 0; | ||
| 134 | $self->truncate($count); | ||
| 135 | return defined $count && $count > 0 ? 1 : 0; | ||
| 136 | } | ||
| 137 | |||
| 138 | #--------------------------------------------------------------------------- | ||
| 139 | sub PUSH { | ||
| 140 | my $self = shift; | ||
| 141 | my $key = 0; | ||
| 142 | no warnings 'uninitialized'; | ||
| 143 | foreach (@_) { | ||
| 144 | return $self->FETCHSIZE() unless $self->db_put($key, $_, DB_APPEND) == 0; | ||
| 145 | } | ||
| 146 | return $self->FETCHSIZE(); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub POP { | ||
| 150 | my $self = shift; | ||
| 151 | my $value = $self->SUPER::POP(@_); | ||
| 152 | return defined $value && UNIVERSAL::isa($value, 'forks::BerkeleyDB::ElemNotExists') ? undef : $value; #_db_filter_array_elem_not_exists_to_undef | ||
| 153 | } | ||
| 154 | |||
| 155 | #--------------------------------------------------------------------------- | ||
| 156 | sub SHIFT { | ||
| 157 | my $self = shift; | ||
| 158 | my $value = $self->SUPER::SHIFT(@_); | ||
| 159 | return defined $value && UNIVERSAL::isa($value, 'forks::BerkeleyDB::ElemNotExists') ? undef : $value; #_db_filter_array_elem_not_exists_to_undef | ||
| 160 | } | ||
| 161 | |||
| 162 | sub UNSHIFT { | ||
| 163 | my $self = shift; | ||
| 164 | return undef unless @_; | ||
| 165 | $self->SUPER::UNSHIFT(@_); | ||
| 166 | return $self->FETCHSIZE(); | ||
| 167 | } | ||
| 168 | |||
| 169 | #--------------------------------------------------------------------------- | ||
| 170 | sub SPLICE { | ||
| 171 | my $self = shift; | ||
| 172 | my $offset = shift || 0; | ||
| 173 | my $length = shift; | ||
| 174 | |||
| 175 | my $nkeys = $self->FETCHSIZE(); | ||
| 176 | my $p_offset = $offset < 0 ? $nkeys + $offset : $offset; | ||
| 177 | $p_offset = $nkeys - 1 if $p_offset > $nkeys - 1; | ||
| 178 | |||
| 179 | ### handle warnings ### | ||
| 180 | unless (defined $length) { | ||
| 181 | warnings::warnif('uninitialized', 'Use of uninitialized value in splice'); | ||
| 182 | $length = $nkeys - $offset; | ||
| 183 | } | ||
| 184 | warnings::warnif('misc', 'splice() offset past end of array') if $offset > $nkeys - 1; | ||
| 185 | die "Modification of non-creatable array value attempted, subscript $offset" | ||
| 186 | if $offset < 0 && abs($offset) > $nkeys; | ||
| 187 | |||
| 188 | ### remove elements ### | ||
| 189 | my @removed; | ||
| 190 | #warn "length=$length"; | ||
| 191 | if ($length > 0) { | ||
| 192 | my $cursor = $self->db_cursor(DB_WRITECURSOR); | ||
| 193 | my $max_idx = $p_offset + $length - 1 > $nkeys - 1 ? $nkeys - 1 : $p_offset + $length - 1; | ||
| 194 | for ($p_offset..$max_idx) { | ||
| 195 | my $key = $p_offset; | ||
| 196 | my $value = undef; | ||
| 197 | my $status = $cursor->c_get($key, $value, DB_SET) == 0; #set cursor position | ||
| 198 | next if $status == DB_NOTFOUND || $status == DB_KEYEMPTY; | ||
| 199 | push @removed, _db_filter_array_elem_not_exists_to_undef($value); | ||
| 200 | return @removed unless $cursor->c_del() == 0; | ||
| 201 | $nkeys--; | ||
| 202 | } | ||
| 203 | $cursor->c_close(); | ||
| 204 | } | ||
| 205 | |||
| 206 | ### insert elements ### | ||
| 207 | if (@_) { | ||
| 208 | my $num_vals = scalar @_; | ||
| 209 | #warn "num_vals to insert=$num_vals"; | ||
| 210 | ### extend database to new size ### | ||
| 211 | $nkeys = $self->STORESIZE($nkeys + $num_vals); | ||
| 212 | #warn "new size=",$nkeys; | ||
| 213 | |||
| 214 | ### insert elements starting at offset (and temporarily save old ones) ### | ||
| 215 | my @values_to_move; | ||
| 216 | my $cursor = $self->db_cursor(DB_WRITECURSOR); | ||
| 217 | my $max_idx = $p_offset + ($num_vals - 1) > $nkeys - 1 ? $nkeys - 1 : $p_offset + ($num_vals - 1); | ||
| 218 | #warn "insert: range=$p_offset..$max_idx"; | ||
| 219 | for my $key ($p_offset..$max_idx) { | ||
| 220 | my $value = undef; | ||
| 221 | my $new_value = shift; | ||
| 222 | my $status = $cursor->c_get($key, $value, DB_SET) == 0; #set cursor position | ||
| 223 | next if $status == DB_NOTFOUND || $status == DB_KEYEMPTY; | ||
| 224 | push @values_to_move, $value; | ||
| 225 | #warn "insert: key=$key"; | ||
| 226 | return @removed unless $cursor->c_put($key, $new_value, DB_CURRENT) == 0; | ||
| 227 | #warn "insert success! (status=$status)"; | ||
| 228 | } | ||
| 229 | |||
| 230 | ### move elements shifted by splice ### | ||
| 231 | #warn "move: values=(",join(',', @values_to_move),")"; | ||
| 232 | #warn "move: range=",($p_offset + $num_vals)."..".($nkeys - 1); | ||
| 233 | for my $key (($p_offset + $num_vals)..($nkeys - 1)) { | ||
| 234 | #warn "move: key=$key"; | ||
| 235 | my $value = undef; | ||
| 236 | my $status = $cursor->c_get($key, $value, DB_SET) == 0; #set cursor position | ||
| 237 | next if $status == DB_NOTFOUND || $status == DB_KEYEMPTY; | ||
| 238 | push @values_to_move, $value; | ||
| 239 | return @removed unless $cursor->c_put($key, shift @values_to_move, DB_CURRENT) == 0; | ||
| 240 | } | ||
| 241 | $cursor->c_close(); | ||
| 242 | } | ||
| 243 | |||
| 244 | return @removed; | ||
| 245 | } | ||
| 246 | |||
| 247 | #--------------------------------------------------------------------------- | ||
| 248 | sub UNTIE { | ||
| 249 | eval { $_[0]->db_sync(); }; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub DESTROY { | ||
| 253 | # eval { $_[0]->db_sync(); }; | ||
| 254 | $_[0]->SUPER::DESTROY(@_) if $_[0]; | ||
| 255 | } | ||
| 256 | |||
| 257 | #--------------------------------------------------------------------------- | ||
| 258 | 1; | ||
| 259 | |||
| 260 | __END__ |