| 1 | package DBM::Deep::Engine::DBI; | ||
| 2 | |||
| 3 | use 5.008_004; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings FATAL => 'all'; | ||
| 7 | no warnings 'recursion'; | ||
| 8 | |||
| 9 | use base 'DBM::Deep::Engine'; | ||
| 10 | |||
| 11 | use DBM::Deep::Sector::DBI (); | ||
| 12 | use DBM::Deep::Storage::DBI (); | ||
| 13 | |||
| 14 | sub sector_type { 'DBM::Deep::Sector::DBI' } | ||
| 15 | sub iterator_class { 'DBM::Deep::Iterator::DBI' } | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $class = shift; | ||
| 19 | my ($args) = @_; | ||
| 20 | |||
| 21 | $args->{storage} = DBM::Deep::Storage::DBI->new( $args ) | ||
| 22 | unless exists $args->{storage}; | ||
| 23 | |||
| 24 | my $self = bless { | ||
| 25 | storage => undef, | ||
| 26 | external_refs => undef, | ||
| 27 | }, $class; | ||
| 28 | |||
| 29 | # Grab the parameters we want to use | ||
| 30 | foreach my $param ( keys %$self ) { | ||
| 31 | next unless exists $args->{$param}; | ||
| 32 | $self->{$param} = $args->{$param}; | ||
| 33 | } | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub setup { | ||
| 39 | my $self = shift; | ||
| 40 | my ($obj) = @_; | ||
| 41 | |||
| 42 | # Default the id to 1. This means that we will be creating a row if there | ||
| 43 | # isn't one. The assumption is that the row_id=1 cannot never be deleted. I | ||
| 44 | # don't know if this is a good assumption. | ||
| 45 | $obj->{base_offset} ||= 1; | ||
| 46 | |||
| 47 | my ($rows) = $self->storage->read_from( | ||
| 48 | refs => $obj->_base_offset, | ||
| 49 | qw( ref_type ), | ||
| 50 | ); | ||
| 51 | |||
| 52 | # We don't have a row yet. | ||
| 53 | unless ( @$rows ) { | ||
| 54 | $self->storage->write_to( | ||
| 55 | refs => $obj->_base_offset, | ||
| 56 | ref_type => $obj->_type, | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | my $sector = DBM::Deep::Sector::DBI::Reference->new({ | ||
| 61 | engine => $self, | ||
| 62 | offset => $obj->_base_offset, | ||
| 63 | }); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub read_value { | ||
| 67 | my $self = shift; | ||
| 68 | my ($obj, $key) = @_; | ||
| 69 | |||
| 70 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 71 | or return; | ||
| 72 | |||
| 73 | # if ( $sector->staleness != $obj->_staleness ) { | ||
| 74 | # return; | ||
| 75 | # } | ||
| 76 | |||
| 77 | # my $key_md5 = $self->_apply_digest( $key ); | ||
| 78 | |||
| 79 | my $value_sector = $sector->get_data_for({ | ||
| 80 | key => $key, | ||
| 81 | # key_md5 => $key_md5, | ||
| 82 | allow_head => 1, | ||
| 83 | }); | ||
| 84 | |||
| 85 | unless ( $value_sector ) { | ||
| 86 | return undef | ||
| 87 | } | ||
| 88 | |||
| 89 | return $value_sector->data; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get_classname { | ||
| 93 | my $self = shift; | ||
| 94 | my ($obj) = @_; | ||
| 95 | |||
| 96 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 97 | or return; | ||
| 98 | |||
| 99 | return $sector->get_classname; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub make_reference { | ||
| 103 | my $self = shift; | ||
| 104 | my ($obj, $old_key, $new_key) = @_; | ||
| 105 | |||
| 106 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 107 | or return; | ||
| 108 | |||
| 109 | # if ( $sector->staleness != $obj->_staleness ) { | ||
| 110 | # return; | ||
| 111 | # } | ||
| 112 | |||
| 113 | my $value_sector = $sector->get_data_for({ | ||
| 114 | key => $old_key, | ||
| 115 | allow_head => 1, | ||
| 116 | }); | ||
| 117 | |||
| 118 | unless ( $value_sector ) { | ||
| 119 | $value_sector = DBM::Deep::Sector::DBI::Scalar->new({ | ||
| 120 | engine => $self, | ||
| 121 | data => undef, | ||
| 122 | }); | ||
| 123 | |||
| 124 | $sector->write_data({ | ||
| 125 | key => $old_key, | ||
| 126 | value => $value_sector, | ||
| 127 | }); | ||
| 128 | } | ||
| 129 | |||
| 130 | if ( $value_sector->isa( 'DBM::Deep::Sector::DBI::Reference' ) ) { | ||
| 131 | $sector->write_data({ | ||
| 132 | key => $new_key, | ||
| 133 | value => $value_sector, | ||
| 134 | }); | ||
| 135 | $value_sector->increment_refcount; | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | $sector->write_data({ | ||
| 139 | key => $new_key, | ||
| 140 | value => $value_sector->clone, | ||
| 141 | }); | ||
| 142 | } | ||
| 143 | |||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | # exists returns '', not undefined. | ||
| 148 | sub key_exists { | ||
| 149 | my $self = shift; | ||
| 150 | my ($obj, $key) = @_; | ||
| 151 | |||
| 152 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 153 | or return ''; | ||
| 154 | |||
| 155 | # if ( $sector->staleness != $obj->_staleness ) { | ||
| 156 | # return ''; | ||
| 157 | # } | ||
| 158 | |||
| 159 | my $data = $sector->get_data_for({ | ||
| 160 | # key_md5 => $self->_apply_digest( $key ), | ||
| 161 | key => $key, | ||
| 162 | allow_head => 1, | ||
| 163 | }); | ||
| 164 | |||
| 165 | # exists() returns 1 or '' for true/false. | ||
| 166 | return $data ? 1 : ''; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub delete_key { | ||
| 170 | my $self = shift; | ||
| 171 | my ($obj, $key) = @_; | ||
| 172 | |||
| 173 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 174 | or return ''; | ||
| 175 | |||
| 176 | # if ( $sector->staleness != $obj->_staleness ) { | ||
| 177 | # return ''; | ||
| 178 | # } | ||
| 179 | |||
| 180 | return $sector->delete_key({ | ||
| 181 | # key_md5 => $self->_apply_digest( $key ), | ||
| 182 | key => $key, | ||
| 183 | allow_head => 0, | ||
| 184 | }); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub write_value { | ||
| 188 | my $self = shift; | ||
| 189 | my ($obj, $key, $value) = @_; | ||
| 190 | |||
| 191 | my $r = Scalar::Util::reftype( $value ) || ''; | ||
| 192 | { | ||
| 193 | last if $r eq ''; | ||
| 194 | last if $r eq 'HASH'; | ||
| 195 | last if $r eq 'ARRAY'; | ||
| 196 | |||
| 197 | DBM::Deep->_throw_error( | ||
| 198 | "Storage of references of type '$r' is not supported." | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | # Load the reference entry | ||
| 203 | # Determine if the row was deleted under us | ||
| 204 | # | ||
| 205 | |||
| 206 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 207 | or die "Cannot load sector at '@{[$obj->_base_offset]}'\n";; | ||
| 208 | |||
| 209 | my ($type, $class); | ||
| 210 | if ( | ||
| 211 | $r eq 'ARRAY' || $r eq 'HASH' and ref $value ne 'DBM::Deep::Null' | ||
| 212 | ) { | ||
| 213 | my $tmpvar; | ||
| 214 | if ( $r eq 'ARRAY' ) { | ||
| 215 | $tmpvar = tied @$value; | ||
| 216 | } elsif ( $r eq 'HASH' ) { | ||
| 217 | $tmpvar = tied %$value; | ||
| 218 | } | ||
| 219 | |||
| 220 | if ( $tmpvar ) { | ||
| 221 | my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $tmpvar->isa( 'DBM::Deep' ); }; | ||
| 222 | |||
| 223 | unless ( $is_dbm_deep ) { | ||
| 224 | DBM::Deep->_throw_error( "Cannot store something that is tied." ); | ||
| 225 | } | ||
| 226 | |||
| 227 | unless ( $tmpvar->_engine->storage == $self->storage ) { | ||
| 228 | DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." ); | ||
| 229 | } | ||
| 230 | |||
| 231 | # Load $tmpvar's sector | ||
| 232 | |||
| 233 | # First, verify if we're storing the same thing to this spot. If we | ||
| 234 | # are, then this should be a no-op. -EJS, 2008-05-19 | ||
| 235 | |||
| 236 | # See whether or not we are storing ourselves to ourself. | ||
| 237 | # Write the sector as data in this reference (keyed by $key) | ||
| 238 | my $value_sector = $self->load_sector( $tmpvar->_base_offset, 'refs' ); | ||
| 239 | $sector->write_data({ | ||
| 240 | key => $key, | ||
| 241 | # key_md5 => $self->_apply_digest( $key ), | ||
| 242 | value => $value_sector, | ||
| 243 | }); | ||
| 244 | $value_sector->increment_refcount; | ||
| 245 | |||
| 246 | return 1; | ||
| 247 | } | ||
| 248 | |||
| 249 | $type = substr( $r, 0, 1 ); | ||
| 250 | $class = 'DBM::Deep::Sector::DBI::Reference'; | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | if ( tied($value) ) { | ||
| 254 | DBM::Deep->_throw_error( "Cannot store something that is tied." ); | ||
| 255 | } | ||
| 256 | |||
| 257 | if ( ref $value eq 'DBM::Deep::Null' ) { | ||
| 258 | DBM::Deep::_warnif( | ||
| 259 | 'uninitialized', 'Assignment of stale reference' | ||
| 260 | ); | ||
| 261 | $value = undef; | ||
| 262 | } | ||
| 263 | |||
| 264 | $class = 'DBM::Deep::Sector::DBI::Scalar'; | ||
| 265 | $type = 'S'; | ||
| 266 | } | ||
| 267 | |||
| 268 | # Create this after loading the reference sector in case something bad | ||
| 269 | # happens. This way, we won't allocate value sector(s) needlessly. | ||
| 270 | my $value_sector = $class->new({ | ||
| 271 | engine => $self, | ||
| 272 | data => $value, | ||
| 273 | type => $type, | ||
| 274 | }); | ||
| 275 | |||
| 276 | $sector->write_data({ | ||
| 277 | key => $key, | ||
| 278 | # key_md5 => $self->_apply_digest( $key ), | ||
| 279 | value => $value_sector, | ||
| 280 | }); | ||
| 281 | |||
| 282 | $self->_descend( $value, $value_sector ); | ||
| 283 | |||
| 284 | return 1; | ||
| 285 | } | ||
| 286 | |||
| 287 | #sub begin_work { | ||
| 288 | # my $self = shift; | ||
| 289 | # die "Transactions are not supported by this engine" | ||
| 290 | # unless $self->supports('transactions'); | ||
| 291 | # | ||
| 292 | # if ( $self->in_txn ) { | ||
| 293 | # DBM::Deep->_throw_error( "Cannot begin_work within an active transaction" ); | ||
| 294 | # } | ||
| 295 | # | ||
| 296 | # $self->storage->begin_work; | ||
| 297 | # | ||
| 298 | # $self->in_txn( 1 ); | ||
| 299 | # | ||
| 300 | # return 1; | ||
| 301 | #} | ||
| 302 | # | ||
| 303 | #sub rollback { | ||
| 304 | # my $self = shift; | ||
| 305 | # die "Transactions are not supported by this engine" | ||
| 306 | # unless $self->supports('transactions'); | ||
| 307 | # | ||
| 308 | # if ( !$self->in_txn ) { | ||
| 309 | # DBM::Deep->_throw_error( "Cannot rollback without an active transaction" ); | ||
| 310 | # } | ||
| 311 | # | ||
| 312 | # $self->storage->rollback; | ||
| 313 | # | ||
| 314 | # $self->in_txn( 0 ); | ||
| 315 | # | ||
| 316 | # return 1; | ||
| 317 | #} | ||
| 318 | # | ||
| 319 | #sub commit { | ||
| 320 | # my $self = shift; | ||
| 321 | # die "Transactions are not supported by this engine" | ||
| 322 | # unless $self->supports('transactions'); | ||
| 323 | # | ||
| 324 | # if ( !$self->in_txn ) { | ||
| 325 | # DBM::Deep->_throw_error( "Cannot commit without an active transaction" ); | ||
| 326 | # } | ||
| 327 | # | ||
| 328 | # $self->storage->commit; | ||
| 329 | # | ||
| 330 | # $self->in_txn( 0 ); | ||
| 331 | # | ||
| 332 | # return 1; | ||
| 333 | #} | ||
| 334 | # | ||
| 335 | #sub in_txn { | ||
| 336 | # my $self = shift; | ||
| 337 | # $self->{in_txn} = shift if @_; | ||
| 338 | # $self->{in_txn}; | ||
| 339 | #} | ||
| 340 | |||
| 341 | sub supports { | ||
| 342 | my $self = shift; | ||
| 343 | my ($feature) = @_; | ||
| 344 | |||
| 345 | return if $feature eq 'transactions'; | ||
| 346 | return 1 if $feature eq 'singletons'; | ||
| 347 | return; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub db_version { | ||
| 351 | return '1.0020' | ||
| 352 | } | ||
| 353 | |||
| 354 | sub clear { | ||
| 355 | my $self = shift; | ||
| 356 | my $obj = shift; | ||
| 357 | |||
| 358 | my $sector = $self->load_sector( $obj->_base_offset, 'refs' ) | ||
| 359 | or return; | ||
| 360 | |||
| 361 | $sector->clear; | ||
| 362 | |||
| 363 | return; | ||
| 364 | } | ||
| 365 | |||
| 366 | 1; | ||
| 367 | __END__ |