| 1 | |||
| 2 | package OOPS; | ||
| 3 | |||
| 4 | our $VERSION = 0.2005; | ||
| 5 | our $SCHEMA_VERSION = 1005; | ||
| 6 | |||
| 7 | require 5.008002; | ||
| 8 | require Exporter; | ||
| 9 | @EXPORT = qw(transaction getref walk_hash); | ||
| 10 | @ISA = qw(Exporter); | ||
| 11 | @EXPORT_OK = qw(transaction $transaction_maxtries $transfailrx dbiconnect dboconnect workaround27555 walk_hash); | ||
| 12 | |||
| 13 | use DBI; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | #use diagnostics; | ||
| 17 | use Carp qw(confess longmess verbose croak longmess); | ||
| 18 | use Scalar::Util qw(refaddr reftype blessed weaken); | ||
| 19 | use Hash::Util qw(lock_keys); | ||
| 20 | use B qw(svref_2object); | ||
| 21 | require OOPS::DBO; | ||
| 22 | |||
| 23 | # | ||
| 24 | # This is a self source filter. | ||
| 25 | # | ||
| 26 | BEGIN { | ||
| 27 | package OOPS::SelfFilter; | ||
| 28 | sub filter | ||
| 29 | { | ||
| 30 | my $more = filter_read(); | ||
| 31 | $_ = "#\n" if /debug/ || (/assertions/ && /oops/); | ||
| 32 | return $more; | ||
| 33 | } | ||
| 34 | use Filter::Util::Call ; | ||
| 35 | filter_add(bless [], __PACKAGE__) | ||
| 36 | unless $OOPS::SelfFilter::defeat; | ||
| 37 | } | ||
| 38 | |||
| 39 | our $bigcutoff = 255; | ||
| 40 | our $cksumlength = 28; | ||
| 41 | our $demandthreshold = 500; | ||
| 42 | our $oopses = 0; | ||
| 43 | my $nopkey = 'nopkey'; | ||
| 44 | our $warnings = 1; | ||
| 45 | our $transaction_tries = 0; | ||
| 46 | our $transaction_maxtries = 15; | ||
| 47 | our $transaction_failure_sleep = 0.5; | ||
| 48 | our $transaction_failure_maxsleep = 10; | ||
| 49 | our @transaction_rollback; | ||
| 50 | our $dbi_bug_workaround_count_debug = 0; | ||
| 51 | our $gc_overflow_id = 4; | ||
| 52 | |||
| 53 | # This gets updated by OOPS::DBO as backends are used | ||
| 54 | our $transfailrx = qr/^wont^match^anything^yet/; | ||
| 55 | |||
| 56 | our $id_alloc_size = 10; | ||
| 57 | |||
| 58 | my %typesymbol = ( | ||
| 59 | HASH => '%', | ||
| 60 | ARRAY => '@', | ||
| 61 | SCALAR => '$', | ||
| 62 | REF => '$', | ||
| 63 | GLOB => '*', | ||
| 64 | CODE => '&', | ||
| 65 | H => '%', | ||
| 66 | A => '@', | ||
| 67 | S => '$', | ||
| 68 | ); | ||
| 69 | |||
| 70 | my %perltype2otype = ( | ||
| 71 | HASH => 'H', | ||
| 72 | ARRAY => 'A', | ||
| 73 | SCALAR => 'S', | ||
| 74 | REF => 'S', | ||
| 75 | ); | ||
| 76 | |||
| 77 | our $debug_free_tied = 0; | ||
| 78 | our $debug_tiedvars = 0; # produces no output -- just verification | ||
| 79 | our $debug_oops_instances = 0; # track allocation / destructions of OOPS objects | ||
| 80 | our $debug_load_object = 0; # basic loading of objects | ||
| 81 | our $debug_load_values = 0; # loading of all keys & values | ||
| 82 | our $debug_load_context = 0; # stack trace for each load | ||
| 83 | our $debug_load_group = 0; # touches: load groups | ||
| 84 | our $debug_arraylen = 0; # touches: arraylen hash | ||
| 85 | our $debug_tie = 0; # when tie'ing | ||
| 86 | our $debug_untie = 0; | ||
| 87 | our $debug_writes = 0; | ||
| 88 | our $debug_write_object = 0; # write to the object table | ||
| 89 | our $debug_blessing = 0; # bless operations | ||
| 90 | our $debug_memory = 0; # touches: memory | ||
| 91 | our $debug_memory2 = 0; # the memory set/clear routines | ||
| 92 | our $debug_cache = 0; # touches: cache | ||
| 93 | our $debug_oldobject = 0; # touches: oldobject | ||
| 94 | our $debug_refcount = 0; # touches: refmore, refless or refcount | ||
| 95 | our $debug_gcgeneration = 0; # touches: gc generation | ||
| 96 | our $debug_touched = 0; # touches: touched | ||
| 97 | our $debug_commit = 0; # save objects | ||
| 98 | our $debug_demand_iterator = 0; | ||
| 99 | our $debug_forcesave = 0; | ||
| 100 | our $debug_isvirtual = 0; | ||
| 101 | our $debug_27555 = 0; # touches: 27555 fixup code | ||
| 102 | our $debug_27555_context = 0; # 27555 fixup code calling context | ||
| 103 | our $debug_save_attributes = 0; # near: queries to save pval | ||
| 104 | our $debug_save_attr_arraylen = 0; # arraylen for attribute save | ||
| 105 | our $debug_save_attr_context = 0; # stack trace for each attribute save | ||
| 106 | our $debug_refarray = 0; # array elements as references | ||
| 107 | our $debug_refalias = 0; # references to other values inside objects | ||
| 108 | our $debug_refobject = 0; # references to other objects | ||
| 109 | our $debug_reftarget = 0; # regarding reference target tracking | ||
| 110 | our $debug_write_object_context = 0; # traceback in write_object() | ||
| 111 | our $debug_write_ref = 0; | ||
| 112 | our $debug_write_array = 0; # has ARRAY changed? | ||
| 113 | our $debug_normalarray = 0; # tied callbacks: non-virtual hash | ||
| 114 | our $debug_normalhash = 0; | ||
| 115 | our $debug_write_hash = 0; | ||
| 116 | our $debug_virtual_delete = 0; | ||
| 117 | our $debug_virtual_save = 0; | ||
| 118 | our $debug_virtual_hash = 0; # tied callbacks: virtual hash | ||
| 119 | our $debug_virtual_ovals = 0; # original values of virtual has | ||
| 120 | our $debug_hashscalar = 0; # scalar(%tied_hash) | ||
| 121 | our $debug_object_id = 0; # details of id allocation | ||
| 122 | our $debug_getobid_context = 0; # stack trace for new objects | ||
| 123 | our $debug_dbidelay = 0; # add small delay before changing transaction mode | ||
| 124 | our $debug_dbi = 0; # DBI debug level: 0 or 1 or 2 | ||
| 125 | our @debug_traceintercept = qw(); # Debug::TraceIntercept | ||
| 126 | our $debug_upgrade = 0; # debug schema upgrades | ||
| 127 | our $debug_initialize = 0; # debug initial queries | ||
| 128 | our $debug_setup = 0; # debug database initialization | ||
| 129 | our $debug_bigstuff = 0; # debug overflow table operations | ||
| 130 | our $debug_dbd = 0; # debug mysql.pm, pg.pm, etc. | ||
| 131 | our $debug_queries = 0; # bitfield: 1=select all (vs default==regex match), 2=print query name, 4=print query, 8=print args, 16=varients, 32=full query log | ||
| 132 | our $debug_q_regex_target = 'query'; # query regex match on 'comment', 'query', or 'data' | ||
| 133 | our $debug_q_regex = qr/insert/i; # query regex matches comment or query | ||
| 134 | our $debug_tdelay = 400; # maximum artifical wait time (milliseconds) | ||
| 135 | |||
| 136 | select(STDOUT); $| = 1; # debug | ||
| 137 | |||
| 138 | # debug set for ref.t | ||
| 139 | $debug_27555 = $debug_write_ref = $debug_load_object = $debug_load_values = $debug_memory = $debug_commit = $debug_refalias = $debug_write_ref = 1 if 0; | ||
| 140 | |||
| 141 | my $global_destruction = 0; | ||
| 142 | |||
| 143 | our %tiedvars; | ||
| 144 | |||
| 145 | tie my %qtype, 'OOPS::debug', sub { return reftype($_[0]) }; | ||
| 146 | tie my %qref, 'OOPS::debug', sub { return ref($_[0]) }; | ||
| 147 | tie my %qaddr, 'OOPS::debug', sub { return refaddr($_[0]) }; | ||
| 148 | tie my %qnone, 'OOPS::debug', sub { $_[0] }; | ||
| 149 | tie my %qmakeref, 'OOPS::debug', sub { \$_[0] }; | ||
| 150 | tie my %qval, 'OOPS::debug', sub { return defined $_[0] ? (ref($_[0]) ? "$_[0] \@ $qaddr{$_[0]}" : "'$_[0]'") : 'undef' }; | ||
| 151 | tie my %qplusminus, 'OOPS::debug', sub { $_[0] >= 0 ? "+$_[0]" : $_[0] }; | ||
| 152 | tie my %caller, 'OOPS::debug', sub { my $lvls = $_[0]+1; my ($p,$f,$l) = caller($lvls); my $s = (caller($lvls+1))[3]; $s =~ s/OOPS:://; $l = $f eq __FILE__ ? $l : "$f:$l"; return "$s/$l" }; | ||
| 153 | tie my %qmemval, 'OOPS::debug', sub { my $v = shift; return "*$v" unless ref $v; return "*$v->[0]/$qval{$v->[1]}" }; | ||
| 154 | tie my %qsym, 'OOPS::debug', sub { return $typesymbol{reftype(shift)} }; | ||
| 155 | |||
| 156 | sub OOPS::debug::TIEHASH { my $p = shift; return bless shift, $p } | ||
| 157 | sub OOPS::debug::FETCH { my $f = shift; return &$f(shift) } | ||
| 158 | |||
| 159 | require Debug::TraceIntercept if @debug_traceintercept; | ||
| 160 | for my $dti_debug (@debug_traceintercept) { | ||
| 161 | Debug::TraceIntercept::trace($dti_debug); | ||
| 162 | } # debug | ||
| 163 | |||
| 164 | sub new | ||
| 165 | { | ||
| 166 | my ($pkg, %args) = @_; | ||
| 167 | |||
| 168 | |||
| 169 | my $oops = bless { | ||
| 170 | otype => {}, # object id -> H(ash)/A(rray)/S(scalar or ref) | ||
| 171 | loadgroup => {}, # object id -> object loadgroup # | ||
| 172 | loadgrouplock => {}, # object id -> object id | ||
| 173 | groupset => {}, # group id -> object id -> 1 | ||
| 174 | cache => {}, # object id -> actual object | ||
| 175 | memory => {}, # ref memory location -> object id | ||
| 176 | memory2key => {}, # ref mem location -> [ object id, object key ] | ||
| 177 | new_memory => {}, # ref memory location -> object id | ||
| 178 | new_memory2key => {}, # ref mem location -> [ object id, object key ] | ||
| 179 | memrefs => {}, # ref mem location -> ref | ||
| 180 | memcount => {}, # ref mem location -> count of active references | ||
| 181 | memsetdebug => {}, # where mem->object mappings were made | ||
| 182 | deleted => {}, # object id -> has been deleted | ||
| 183 | unwatched => {}, # object id -> must check at save | ||
| 184 | virtual => {}, # object id -> is it virtual? yes=V, no=' ' | ||
| 185 | arraylen => {}, # object id -> integer; array length | ||
| 186 | reftarg => {}, # object id -> boolean: '0' || 'T'; Indicates if there are references to elements of this object | ||
| 187 | aliasdest => {}, # object id -> hash of objectids that reference id | ||
| 188 | oldvalue => {}, # object id & pkey -> original pval | ||
| 189 | oldobject => {}, # object id & pkey -> original object id reference | ||
| 190 | oldbig => {}, # object id & pkey -> checksum | ||
| 191 | objtouched => {}, # objedt id -> bit - object may need saving | ||
| 192 | demandwritten => {}, # object id -> tie control object | ||
| 193 | demandwrite => {}, # object id -> write this one via tied. | ||
| 194 | gcgeneration => {}, # object id -> garbage collection generation | ||
| 195 | refdebug => {}, # object id -> original reference count | ||
| 196 | objverdebug => {}, # object id -> object version counter | ||
| 197 | refcount => {}, # object id -> reference count | ||
| 198 | refmore => {}, # object id -> change in reference count (during commit()) - also serves as a todo list for saving objects | ||
| 199 | refless => {}, # object id -> change in reference count (during commit()) - also serves as a todo list for saving objects | ||
| 200 | forcesave => {}, # object id -> bit - for object row to be re-written XXX redesign | ||
| 201 | vcache => [], # objects that implement a CLEAR_CACHE() method | ||
| 202 | do_forcesave => 0, # always update object row when attributes change, varies per backend | ||
| 203 | savedone => {}, # during commit() - object written? | ||
| 204 | refstowrite => [], # during commit() - list of reference objects to save | ||
| 205 | insave => 0, # are we currently save()ing things? | ||
| 206 | loaded => 0, # number of objects "in memory" | ||
| 207 | tountie => {}, # scalars wishing to be untied | ||
| 208 | class => {}, # my original class/blessing | ||
| 209 | queries => {}, # text of queries - original text | ||
| 210 | binary_q_list => {}, # list of binary parametes to queries - DBD::Pg only | ||
| 211 | debug_q => {}, # query comments, debugging pragmas | ||
| 212 | commitdone => 0, # have we already done a commit()? | ||
| 213 | refcopy => {}, # object id & pkey -> ref to orig pval | ||
| 214 | aliascount => {}, # object id & pkey -> count of \aliases | ||
| 215 | oldalias => {}, # object id -> [ object id, pkey ] | ||
| 216 | disassociated => {}, # object id & pkey && refs => other disconnected ref | ||
| 217 | virtualize => {}, # objects to (un)virtualize | ||
| 218 | gcspillcount => 0, # number of objects spilled to the gc re-do buffer | ||
| 219 | args => \%args, # creation arguments | ||
| 220 | readonly => $args{readonly}, # commit not allowed? | ||
| 221 | }, $pkg; | ||
| 222 | |||
| 223 | print "# CREATE $$'s OOPS $oops\n" if $debug_oops_instances; | ||
| 224 | |||
| 225 | my $dbo = $oops->{dbo} = OOPS->dboconnect(%args); | ||
| 226 | $dbo->rebless($oops); | ||
| 227 | |||
| 228 | #print "BLESSED $oops at ".__LINE__."\n" if $debug_blessing; | ||
| 229 | |||
| 230 | # | ||
| 231 | # object.otype: | ||
| 232 | # | ||
| 233 | # H HASH | ||
| 234 | # A ARRAY | ||
| 235 | # S SCALAR/REF | ||
| 236 | # | ||
| 237 | # object.virtual: | ||
| 238 | # | ||
| 239 | # 0 default | ||
| 240 | # V load virtual | ||
| 241 | # | ||
| 242 | # attribute.ptype: | ||
| 243 | # | ||
| 244 | # 0 normal | ||
| 245 | # R reference to an OBJECT | ||
| 246 | # B Big scalar | ||
| 247 | # | ||
| 248 | # | ||
| 249 | |||
| 250 | $dbo->learn_queries($dbo->initial_query_set); | ||
| 251 | $dbo->learn_queries(<<END); | ||
| 252 | saveobject: 3 | ||
| 253 | INSERT INTO TP_object (id, loadgroup, class, otype, virtual, reftarg, rfe, alen, refs, counter, gcgeneration) | ||
| 254 | VALUES (?, ?, ?, ?, ?, ?, '0', ?, ?, 1, ?) | ||
| 255 | updateobject: 2 | ||
| 256 | UPDATE TP_object | ||
| 257 | SET loadgroup = ?, class = ?, otype = ?, virtual = ?, reftarg = ?, alen = ?, refs = ?, gcgeneration = ?, counter = (counter + 1) % 65536 | ||
| 258 | WHERE id = ? | ||
| 259 | objectset: | ||
| 260 | SELECT o.* FROM TP_object AS o, TP_object AS og | ||
| 261 | WHERE og.id = ? AND og.loadgroup = o.loadgroup | ||
| 262 | objectinfo: | ||
| 263 | # this has to be * so that it will work on all versions of the schema | ||
| 264 | # SELECT loadgroup,class,otype,virtual,reftarg,alen,refs,counter,gcgeneration FROM TP_object | ||
| 265 | SELECT * FROM TP_object | ||
| 266 | WHERE id = ? | ||
| 267 | objectgroupload: | ||
| 268 | SELECT a.* FROM TP_attribute AS a, TP_object AS g | ||
| 269 | WHERE g.loadgroup = ? AND g.id = a.id | ||
| 270 | objectload: | ||
| 271 | SELECT pkey, pval, ptype FROM TP_attribute | ||
| 272 | WHERE id = ? | ||
| 273 | objectreflist: | ||
| 274 | SELECT pval FROM TP_attribute | ||
| 275 | WHERE id = ? AND ptype = 'R' | ||
| 276 | reftargobject: 1 | ||
| 277 | SELECT TP_object.id FROM TP_object, TP_attribute | ||
| 278 | WHERE TP_attribute.pkey = ? | ||
| 279 | AND TP_object.id = TP_attribute.id | ||
| 280 | AND TP_object.otype = 'S' | ||
| 281 | reftargkey: 1 2 | ||
| 282 | SELECT TP_object.id FROM TP_object, TP_attribute | ||
| 283 | WHERE TP_attribute.pkey = ? | ||
| 284 | AND TP_attribute.pval = ? | ||
| 285 | AND TP_object.id = TP_attribute.id | ||
| 286 | AND TP_object.otype = 'S' | ||
| 287 | saveattribute: 2 3 | ||
| 288 | INSERT INTO TP_attribute | ||
| 289 | VALUES (?, ?, ?, ?) | ||
| 290 | loadpkey: 2 | ||
| 291 | SELECT pval, ptype FROM TP_attribute | ||
| 292 | WHERE id = ? AND pkey = ? | ||
| 293 | deleteattribute: 2 | ||
| 294 | DELETE FROM TP_attribute | ||
| 295 | WHERE id = ? AND pkey = ? | ||
| 296 | savepkey: 2 4 6 7 # show | ||
| 297 | DELETE FROM TP_attribute | ||
| 298 | WHERE id = ? AND pkey = ?; | ||
| 299 | DELETE FROM TP_big | ||
| 300 | WHERE id = ? AND pkey = ?; | ||
| 301 | INSERT INTO TP_attribute | ||
| 302 | VALUES (?, ?, ?, ?); | ||
| 303 | updateattribute: 1 4 | ||
| 304 | UPDATE TP_attribute | ||
| 305 | SET pval = ?, ptype = ? | ||
| 306 | WHERE id = ? AND pkey = ? | ||
| 307 | deletebig: 2 # show | ||
| 308 | DELETE FROM TP_big | ||
| 309 | WHERE id = ? AND pkey = ? | ||
| 310 | predelete1: # show | ||
| 311 | DELETE FROM TP_big WHERE id = ? | ||
| 312 | predelete2: | ||
| 313 | DELETE FROM TP_attribute WHERE id = ? AND ptype != 'R' | ||
| 314 | postdeleteV: | ||
| 315 | DELETE FROM TP_attribute | ||
| 316 | WHERE id = ?; | ||
| 317 | postdelete1: | ||
| 318 | DELETE FROM TP_attribute WHERE id = ? | ||
| 319 | postdelete2: | ||
| 320 | DELETE FROM TP_object WHERE id = ? | ||
| 321 | deleterange: 2 | ||
| 322 | DELETE FROM TP_attribute | ||
| 323 | WHERE id = ? AND pkey >= ? | ||
| 324 | deleteoverrange: 2 # show | ||
| 325 | DELETE FROM TP_big | ||
| 326 | WHERE id = ? AND pkey >= ? | ||
| 327 | countkeys: | ||
| 328 | SELECT count(*) | ||
| 329 | FROM TP_attribute | ||
| 330 | WHERE id = ? | ||
| 331 | END | ||
| 332 | |||
| 333 | Time::HiRes::sleep(rand($debug_tdelay)/1000) if $debug_tdelay && $debug_dbidelay; | ||
| 334 | $oops->{do_forcesave} = $dbo->do_forcesave; | ||
| 335 | |||
| 336 | eval { $oops->{named_objects} = $oops->load_virtual_object(1) }; | ||
| 337 | if ($@) { | ||
| 338 | print "Could not load object #1\n" if $debug_setup; | ||
| 339 | my $e = $@; | ||
| 340 | require OOPS::Setup; | ||
| 341 | $oops->load_failure($e) || die $e; | ||
| 342 | return new($pkg, %args); | ||
| 343 | } | ||
| 344 | |||
| 345 | if ($oops->{arraylen}{1} != $SCHEMA_VERSION) { | ||
| 346 | my $schema_version = $oops->{arraylen}{1}; | ||
| 347 | die "schema version = '$schema_version'" unless $schema_version =~ /\A\d+\z/; | ||
| 348 | if ($oops->{args}{auto_upgrade} || $ENV{OOPS_UPGRADE}) { | ||
| 349 | $dbo->disconnect(); | ||
| 350 | require "OOPS/Upgrade/To$SCHEMA_VERSION.pm"; | ||
| 351 | no strict qw(refs); | ||
| 352 | &{"OOPS::Upgrade::To${SCHEMA_VERSION}::upgrade"}($schema_version, %{$oops->{args}}); | ||
| 353 | return new($pkg, %args); | ||
| 354 | } else { | ||
| 355 | $dbo->disconnect(); | ||
| 356 | require "OOPS/OOPS$schema_version.pm" | ||
| 357 | || die "could not find historical version $schema_version: $@"; | ||
| 358 | no strict qw(refs); | ||
| 359 | return &{"OOPS::OOPS${schema_version}::new"}("OOPS::OOPS$schema_version", %{$oops->{args}}); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | $oopses++; | ||
| 364 | print "CREATE OOPS $oops [$oopses]\n" if $debug_free_tied; | ||
| 365 | $tiedvars{$oops} = longmess if $debug_tiedvars; | ||
| 366 | lock_keys(%$oops); | ||
| 367 | assertions($oops); | ||
| 368 | |||
| 369 | return $oops if $args{no_front_end}; | ||
| 370 | return OOPS::FrontEnd->new($oops); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub dbms | ||
| 374 | { | ||
| 375 | return OOPS::DBO::dbms(@_); | ||
| 376 | } | ||
| 377 | |||
| 378 | sub dbiconnect | ||
| 379 | { | ||
| 380 | return OOPS::DBO::dbiconnect(@_); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub dboconnect | ||
| 384 | { | ||
| 385 | return OOPS::DBO::dboconnect(@_); | ||
| 386 | } | ||
| 387 | |||
| 388 | sub errstr | ||
| 389 | { | ||
| 390 | my $oops = shift; | ||
| 391 | return $oops->{dbo}->errstr; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub dbo | ||
| 395 | { | ||
| 396 | my $oops = shift; | ||
| 397 | return $oops->{dbo}; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub query | ||
| 401 | { | ||
| 402 | my $oops = shift; | ||
| 403 | return $oops->{dbo}->query(@_); | ||
| 404 | } | ||
| 405 | |||
| 406 | sub initial_setup | ||
| 407 | { | ||
| 408 | require OOPS::Setup; | ||
| 409 | goto &initial_setup_real; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub load_object | ||
| 413 | { | ||
| 414 | my ($oops, $objectid) = @_; | ||
| 415 | confess unless $oops->isa('OOPS'); # XX why? | ||
| 416 | $objectid = $oops->{named_objects}->{$objectid} | ||
| 417 | if $objectid == 0; | ||
| 418 | confess unless $objectid; | ||
| 419 | confess if ref $objectid; | ||
| 420 | |||
| 421 | print Carp::longmess("DEBUG: load_object($objectid) called") if $debug_load_context; | ||
| 422 | |||
| 423 | if (exists $oops->{cache}{$objectid}) { | ||
| 424 | print "*$objectid load_object is cached: $qval{$oops->{cache}{$objectid}}\n" if $debug_load_object || $debug_cache; | ||
| 425 | return $oops->{cache}{$objectid}; | ||
| 426 | } | ||
| 427 | |||
| 428 | print "load_object($objectid) from $caller{0}\n" if $debug_load_object && ! $debug_load_context; | ||
| 429 | |||
| 430 | my $objectsetQ = $oops->query('objectset', execute => $objectid); | ||
| 431 | |||
| 432 | my $atloadgroup; | ||
| 433 | |||
| 434 | my $cache = $oops->{cache}; | ||
| 435 | my $type = $oops->{otype}; | ||
| 436 | |||
| 437 | my $refcount = $oops->{refcount}; | ||
| 438 | my $oloadgroup = $oops->{loadgroup}; | ||
| 439 | my $oclass = $oops->{class}; | ||
| 440 | my $refcopy = $oops->{refcopy}; | ||
| 441 | my $memory = $oops->{memory}; | ||
| 442 | my $memory2key = $oops->{memory2key}; | ||
| 443 | |||
| 444 | #print "load_object $objectid. Already cached: ".join(' ',keys %$cache).".\n"; | ||
| 445 | |||
| 446 | # | ||
| 447 | # We may bail out of this early if it's a virtual object so we can't | ||
| 448 | # add anything to $oops->{cache} yet. | ||
| 449 | # | ||
| 450 | my %newptype; | ||
| 451 | my %new; | ||
| 452 | my ($object, $loadgroup, $class, $otype, $virtual, $reftarg, $arraylen, $references, $ocounter, $gcgen); | ||
| 453 | while (($object, $loadgroup, $class, $otype, $virtual, $reftarg, undef, $arraylen, $references, $ocounter, $gcgen) = $objectsetQ->fetchrow_array()) { | ||
| 454 | if (exists $cache->{$object}) { | ||
| 455 | print "skipping $otype $object $loadgroup $class -- already cached\n" if $debug_load_values || $debug_cache; | ||
| 456 | next; | ||
| 457 | } | ||
| 458 | if ($virtual eq 'V') { | ||
| 459 | if ($object == $objectid) { | ||
| 460 | die "internal error: virtual objects should not share load groups" if $atloadgroup; | ||
| 461 | $objectsetQ->finish(); | ||
| 462 | return $oops->load_virtual_object($objectid); | ||
| 463 | } else { | ||
| 464 | die "internal error: virtual objects should not be object loadgroup members"; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | die unless $loadgroup; | ||
| 468 | $atloadgroup = $loadgroup; | ||
| 469 | $oops->{groupset}{$atloadgroup}{$object} = 1; | ||
| 470 | $oops->{objverdebug}{$object} = $ocounter; | ||
| 471 | $oops->{refdebug}{$object} = $references; | ||
| 472 | $refcount->{$object} = $references; | ||
| 473 | print "load *$object loadgroup:$loadgroup class:$class otype:$otype refcount:$references virtual:$virtual reftarg:$reftarg arraylen:$arraylen\n" if $debug_load_values || $debug_arraylen || $debug_refcount; | ||
| 474 | if ($otype eq 'H') { | ||
| 475 | $new{$object} = {}; | ||
| 476 | $cache->{$object} = {}; | ||
| 477 | print "*$object load_object cache := fresh empty hash: $qval{$cache->{$object}}\n" if $debug_cache; | ||
| 478 | } elsif ($otype eq 'A') { | ||
| 479 | $new{$object} = $cache->{$object} = []; | ||
| 480 | $#{$cache->{$object}} = $arraylen-1; | ||
| 481 | $oops->{objtouched}{$object} = 'untied array'; | ||
| 482 | print "*$object load_object cache := fresh array: $qval{$cache->{$object}}\n" if $debug_cache; | ||
| 483 | print "in load_object, *$object is always touched 'cause it's an array\n" if $debug_touched; | ||
| 484 | } elsif ($otype eq 'S') { | ||
| 485 | my $x; | ||
| 486 | $cache->{$object} = \$x; | ||
| 487 | print "*$object load_object cache := fresh scalar: $qval{$cache->{$object}}\n" if $debug_cache; | ||
| 488 | } else { | ||
| 489 | confess; | ||
| 490 | } | ||
| 491 | $oops->{arraylen}{$object} = $arraylen; | ||
| 492 | $oops->{reftarg}{$object} = $reftarg; | ||
| 493 | $oops->{virtual}{$object} = $virtual; | ||
| 494 | |||
| 495 | print "*$object loaded gcgen = $gcgen\n" if $debug_gcgeneration; | ||
| 496 | $oops->{gcgeneration}{$object} = $gcgen; | ||
| 497 | |||
| 498 | $type->{$object} = $otype; | ||
| 499 | $newptype{$object} = {}; | ||
| 500 | $oloadgroup->{$object} = $loadgroup; | ||
| 501 | print "in load_object, *$object loadgroup = $loadgroup\n" if $debug_load_group; | ||
| 502 | $oclass->{$object} = $class; | ||
| 503 | $oops->{loaded}++; | ||
| 504 | } | ||
| 505 | |||
| 506 | |||
| 507 | confess "object *$objectid not found in database" unless $cache->{$objectid}; | ||
| 508 | |||
| 509 | my @references; | ||
| 510 | my ($id, $pkey, $pval, $ptype); | ||
| 511 | if ($atloadgroup) { | ||
| 512 | # | ||
| 513 | # we are loading the attributes for a whole loadgroup of objects | ||
| 514 | # | ||
| 515 | print "load loadgroup: $atloadgroup\n" if $debug_load_values; | ||
| 516 | my $objectgrouploadQ = $oops->query('objectgroupload', execute => $atloadgroup); | ||
| 517 | no warnings; | ||
| 518 | local($objectgrouploadQ->{HandleError}) = undef; | ||
| 519 | local($objectgrouploadQ->{RaiseError}) = 0; | ||
| 520 | use warnings; | ||
| 521 | for (;;) { | ||
| 522 | while(($id, $pkey, $pval, $ptype) = $objectgrouploadQ->fetchrow_array) { | ||
| 523 | next unless exists $newptype{$id}; # need something that is set on new objects only | ||
| 524 | # | ||
| 525 | # $t is the type of object we're loading | ||
| 526 | # $ptype is the type of attribute we're loading | ||
| 527 | # | ||
| 528 | my $t = $type->{$id}; | ||
| 529 | print "$typesymbol{$t}$id/$pkey = '$pval' (ptype $ptype)\n" if $debug_load_values && defined $pval; | ||
| 530 | print "$typesymbol{$t}$id/$pkey = undef (ptype $ptype)\n" if $debug_load_values && ! defined $pval; | ||
| 531 | my $ref; | ||
| 532 | if ($t eq 'H') { | ||
| 533 | $new{$id}{$pkey} = $pval; | ||
| 534 | } elsif ($t eq 'A') { | ||
| 535 | if ($ptype eq '0') { | ||
| 536 | $cache->{$id}[$pkey] = $pval; | ||
| 537 | $oops->{oldvalue}{$id}{$pkey} = $pval; | ||
| 538 | } elsif ($ptype eq 'R') { | ||
| 539 | # | ||
| 540 | # Even if this object is already in memory, tie it so that we | ||
| 541 | # can track if the linkage is used. | ||
| 542 | # | ||
| 543 | $cache->{$id}[$pkey] = undef; | ||
| 544 | print "TIE *${id}[$pkey] OOPS::ObjectInArray object=*$pval\n" if $debug_tie; | ||
| 545 | tie $cache->{$id}[$pkey], 'OOPS::ObjectInArray', $id, $pkey, $pval, $oops; | ||
| 546 | $oops->{oldobject}{$id}{$pkey} = $pval; | ||
| 547 | print "OLDOBJECT loadobject *$id/$pkey = *$pval (in array)\n" if $debug_oldobject | ||
| 548 | } elsif ($ptype eq 'B') { | ||
| 549 | $cache->{$id}[$pkey] = $pval; | ||
| 550 | print "TIE *${id}[$pkey] OOPS::BigInArray cksum=$pval\n" if $debug_tie; | ||
| 551 | tie $cache->{$id}[$pkey], 'OOPS::BigInArray', $id, $pkey, $pval, $oops; | ||
| 552 | $oops->{oldbig}{$id}{$pkey} = $pval; | ||
| 553 | } else { | ||
| 554 | confess "ptype = $ptype"; | ||
| 555 | } | ||
| 556 | $ref = \$cache->{$id}[$pkey]; | ||
| 557 | } elsif ($t eq 'S') { | ||
| 558 | next if $pkey eq $id; # see write_ref() | ||
| 559 | if ($pkey eq $nopkey) { | ||
| 560 | my $x; | ||
| 561 | $cache->{$id} = \$x; | ||
| 562 | print "*$object load_object cache := new fresh scalar: $qval{$cache->{$object}}\n" if $debug_cache; | ||
| 563 | if ($ptype eq 'R') { | ||
| 564 | print "\$*$id = *$pval -- RefObject\n" if $debug_refalias && defined($pval); | ||
| 565 | print "TIE \$*$id OOPS::RefObject object=*$pval\n" if $debug_tie; | ||
| 566 | tie ${$cache->{$id}}, 'OOPS::RefObject', $oops, $id, $pval; | ||
| 567 | } elsif ($ptype eq 'B') { | ||
| 568 | print "\$*$id = '$pval...' -- RefBig\n" if $debug_refalias && defined($pval); | ||
| 569 | print "TIE \$*$id OOPS::RefBig val='$pval'\n" if $debug_tie; | ||
| 570 | tie ${$cache->{$id}}, 'OOPS::RefBig', $oops, $id, $pval; | ||
| 571 | } elsif ($ptype eq '0') { | ||
| 572 | $oops->{objtouched}{$id} = 'untied reference'; | ||
| 573 | $oops->{oldvalue}{$id}{$nopkey} = $pval; | ||
| 574 | $x = $pval; | ||
| 575 | print "\$*$id = '$pval' -- no tie at all\n" if $debug_refalias && defined($pval); | ||
| 576 | print "\$*$id = undef -- no tie at all\n" if $debug_refalias && ! defined($pval); | ||
| 577 | } else { | ||
| 578 | confess; | ||
| 579 | } | ||
| 580 | # } elsif (exists $cache->{$pkey} | ||
| 581 | # && ! exists $new{$pkey} | ||
| 582 | # && defined $pval | ||
| 583 | # && reftype($cache->{$pkey}) eq 'HASH' | ||
| 584 | # && (my $tied = tied(%{$cache->{$pkey}}))) | ||
| 585 | # { | ||
| 586 | # # | ||
| 587 | # # A reference to a tied has that's already loaded. | ||
| 588 | # # we need to fast-path this because we might be being called | ||
| 589 | # # during a hash's SAVE_SELF() and the hash key may already | ||
| 590 | # # be deleted. | ||
| 591 | # # | ||
| 592 | # $cache->{$id} = $tied->GETREFORIG($pval); | ||
| 593 | # $oops->{oldalias}{$id} = [ $pkey, $pval ]; | ||
| 594 | # $oops->{aliasdest}{$pkey}{$id} = $pval; | ||
| 595 | # $oops->{unwatched}{$id} = 1; | ||
| 596 | # print "\$*$id load_object cache = untied reference to *$pkey/'$pval' ($qval{$cache->{$id}})\n" if $debug_refalias || $debug_cache; | ||
| 597 | } else { | ||
| 598 | print "\$*$id = '$pval' -- RefAlias to $pkey/'$pval'\n" if $debug_refalias && defined($pval); | ||
| 599 | print "TIE \$*$id OOPS::RefAlias target=*$pkey/'$pval' ($qval{$cache->{$id}})\n" if $debug_tie; | ||
| 600 | tie $cache->{$id}, 'OOPS::RefAlias', $oops, $id, $pkey, $pval; | ||
| 601 | $oops->{aliasdest}{$pkey}{$id} = $pval; | ||
| 602 | } | ||
| 603 | } else { | ||
| 604 | confess; | ||
| 605 | } | ||
| 606 | # | ||
| 607 | # | ||
| 608 | # | ||
| 609 | $newptype{$id}{$pkey} = $ptype | ||
| 610 | if $ptype; | ||
| 611 | if ($ref) { | ||
| 612 | $refcopy->{$id}{$pkey} = $ref; | ||
| 613 | my $m = refaddr($ref); | ||
| 614 | print "MEMORY2KEY $m := *$id/'$pkey' in load_object\n" if $debug_memory; | ||
| 615 | $oops->memory2key($ref, $id, $pkey); | ||
| 616 | } | ||
| 617 | } | ||
| 618 | if ($objectgrouploadQ->err) { | ||
| 619 | if ($objectgrouploadQ->errstr() =~ /fetch\(\) without execute\(\)/) { | ||
| 620 | warn "working around DBI bug"; # debug | ||
| 621 | $objectgrouploadQ->execute($atloadgroup) || confess $objectgrouploadQ->errstr; | ||
| 622 | $dbi_bug_workaround_count_debug++; | ||
| 623 | next; | ||
| 624 | } else { | ||
| 625 | confess "fetch_array error ".$objectgrouploadQ->errstr; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | last; | ||
| 629 | } | ||
| 630 | # $objectgrouploadQ->finish(); | ||
| 631 | } else { | ||
| 632 | confess "no loadgroup!"; | ||
| 633 | } | ||
| 634 | |||
| 635 | my @cblist; | ||
| 636 | my @reflist; | ||
| 637 | for my $id (keys %newptype) { | ||
| 638 | # unless ($typesymbol{$oclass->{$id}}) { | ||
| 639 | #print "WILL BLESS $id as $oclass->{$id}\n"; | ||
| 640 | # bless $cache->{$id}, $oclass->{$id}; | ||
| 641 | # print "*$id load_object BLESS $qval{$cache->{$id}} at ".__LINE__."\n" if $debug_blessing || $debug_cache; | ||
| 642 | # } | ||
| 643 | # print "$typesymbol{$type->{$id}}$id is $oclass->{$id}\n" if $debug_load_values; | ||
| 644 | confess if $oclass->{$id} eq 'OOPS'; | ||
| 645 | if ($type->{$id} eq 'H') { | ||
| 646 | print "\%$id loaded - $qval{$cache->{$id}}\n" if $debug_load_object; | ||
| 647 | # tied hashes cannot access the underlying variable during callbacks | ||
| 648 | print "TIE %$id OOPS::NormalHash\n" if $debug_tie; | ||
| 649 | my $tied = tie %{$cache->{$id}}, 'OOPS::NormalHash', $new{$id}, $newptype{$id}, $oops, $id; | ||
| 650 | $oops->memory($tied, $id); | ||
| 651 | print "MEMORY(TIED) ".refaddr($tied)." := *$id' - tied hash, in load_object\n" if $debug_memory; | ||
| 652 | $oops->memory($cache->{$id}, $id); | ||
| 653 | print "MEMORY $qaddr{$cache->{$id}} := *$id - hash, in load_object\n" if $debug_memory; | ||
| 654 | } elsif ($type->{$id} eq 'A') { | ||
| 655 | print "\@$id loaded - $qval{$cache->{$id}}\n" if $debug_load_object; | ||
| 656 | # tied arrays are buggy so we're not using them. | ||
| 657 | $oops->memory($cache->{$id}, $id); | ||
| 658 | print "MEMORY $qval{$cache->{$id}} := *$id - array, in load_object\n" if $debug_memory; | ||
| 659 | } elsif ($type->{$id} eq 'S') { | ||
| 660 | # | ||
| 661 | # Do these later so that when you FETCH a RefAlias you don't deadlock. Does | ||
| 662 | # this really solve the problem? What about references to references? | ||
| 663 | # | ||
| 664 | push(@reflist, $id); | ||
| 665 | } else { | ||
| 666 | confess; | ||
| 667 | } | ||
| 668 | print "in load_object, $typesymbol{$type->{$id}} *$id loaded, refcount (=$refcount->{$id})\n" if $debug_refcount; | ||
| 669 | # push(@cblist, $id) | ||
| 670 | # if ! $typesymbol{$oclass->{$id}} | ||
| 671 | # && $cache->{$id}->can('postload'); | ||
| 672 | } | ||
| 673 | for my $id (@reflist) { | ||
| 674 | my $a = refaddr($cache->{$id}); | ||
| 675 | if (exists $memory->{$a}) { | ||
| 676 | if ($memory->{$a} > $id) { | ||
| 677 | $oops->memory2key($cache->{$id}, $id, $nopkey); | ||
| 678 | $oops->memory($cache->{$id}, $id); | ||
| 679 | print "MEMORY $a := *$id - NEW LEAD REF, in load_object\n" if $debug_memory; | ||
| 680 | print "MEMORY2KEY $a := *$id - joining refs, in load_object\n" if $debug_memory; | ||
| 681 | } elsif (defined $memory2key->{$a}) { | ||
| 682 | print "MEMORY2KEY $a already exists... *$memory2key->{$a}\n" if $debug_memory; | ||
| 683 | } else { | ||
| 684 | $oops->memory2key($cache->{$id}, $memory->{$a}, $nopkey); | ||
| 685 | print "MEMORY2KEY $a := *$id - REFS NOW JOINED, in load_object\n" if $debug_memory; | ||
| 686 | } | ||
| 687 | } else { | ||
| 688 | $oops->memory($cache->{$id}, $id); | ||
| 689 | print "MEMORY $qval{$cache->{$id}} := *$id - ref, in load_object\n" if $debug_memory; | ||
| 690 | } | ||
| 691 | # $memory->{refaddr(\$cache->{$a})} = $id; | ||
| 692 | # print "MEMORY $qval{\$cache->{$a}} := *$id REF to CACHE\n" if $debug_memory; | ||
| 693 | } | ||
| 694 | for my $id (keys %newptype) { | ||
| 695 | unless ($typesymbol{$oclass->{$id}}) { | ||
| 696 | bless $cache->{$id}, $oclass->{$id}; | ||
| 697 | print "*$id load_object BLESS $qval{$cache->{$id}} at ".__LINE__."\n" if $debug_blessing || $debug_cache; | ||
| 698 | } | ||
| 699 | print "$typesymbol{$type->{$id}}$id is $oclass->{$id}\n" if $debug_load_values; | ||
| 700 | push(@cblist, $id) | ||
| 701 | if ! $typesymbol{$oclass->{$id}} | ||
| 702 | && $cache->{$id}->can('postload'); | ||
| 703 | } | ||
| 704 | while (@cblist) { | ||
| 705 | my $id = shift @cblist; | ||
| 706 | my $obj = $cache->{$id}; | ||
| 707 | $obj->postload($id); | ||
| 708 | } | ||
| 709 | print "*$objectid load_object finished: $qval{$cache->{$objectid}}\n" if $debug_load_values; | ||
| 710 | assertions($oops); | ||
| 711 | return $cache->{$objectid}; | ||
| 712 | } | ||
| 713 | |||
| 714 | sub load_virtual_object | ||
| 715 | { | ||
| 716 | my ($oops, $objectid) = @_; | ||
| 717 | |||
| 718 | $objectid = $oops->{named_objects}{$objectid} | ||
| 719 | if $objectid == 0; | ||
| 720 | confess unless $objectid; | ||
| 721 | |||
| 722 | my $objectinfoQ = $oops->query('objectinfo', execute => $objectid) || die $oops->errstr; | ||
| 723 | my (undef, $loadgroup, $class, $otype, $virtual, $reftarg, $reserved, $arraylen, $refs, undef, $gcgen) = $objectinfoQ->fetchrow_array(); | ||
| 724 | die "no object $objectid: ".$objectinfoQ->errstr unless $otype; | ||
| 725 | $objectinfoQ->finish(); | ||
| 726 | |||
| 727 | my %underlying; | ||
| 728 | my $obj = \%underlying; | ||
| 729 | bless $obj, $class unless $typesymbol{$class}; | ||
| 730 | print "*$objectid BLESSED $obj at ".__LINE__."\n" if $debug_blessing; | ||
| 731 | print "TIE %$objectid OOPS::DemandHash\n" if $debug_tie; | ||
| 732 | my $tied = tie %$obj, 'OOPS::DemandHash', $oops, $objectid; | ||
| 733 | $oops->{virtual}{$objectid} = 'V'; | ||
| 734 | $oops->{arraylen}{$objectid} = $arraylen; | ||
| 735 | $oops->{reftarg}{$objectid} = $reftarg; | ||
| 736 | print "new object *$objectid, arraylen = 0\n" if $debug_arraylen; | ||
| 737 | $oops->{otype}{$objectid} = 'H'; | ||
| 738 | $oops->{class}{$objectid} = $class; | ||
| 739 | $oops->{loadgroup}{$objectid} = $objectid; | ||
| 740 | $oops->{cache}{$objectid} = $obj; | ||
| 741 | $oops->{refcount}{$objectid} = $refs; | ||
| 742 | $oops->{gcgeneration}{$objectid} = $gcgen; | ||
| 743 | print "VH$objectid loaded gcgen = $gcgen\n" if $debug_gcgeneration; | ||
| 744 | $oops->memory($obj, $objectid); | ||
| 745 | $oops->memory($tied, $objectid); | ||
| 746 | print "MEMORY $qval{$obj} := *$objectid' - in load_virtual_object\n" if $debug_memory; | ||
| 747 | print "MEMORY(TIED) $qval{$tied} := *$objectid' - in load_virtual_object\n" if $debug_memory; | ||
| 748 | $oops->{groupset}{$objectid}{$objectid} = 1; | ||
| 749 | print "in load_virtual_object, V% *$objectid loaded, refcount=$refs\n" if $debug_refcount || $debug_load_object; | ||
| 750 | assertions($oops); | ||
| 751 | return $obj; | ||
| 752 | } | ||
| 753 | |||
| 754 | sub process_deferred_virtualize | ||
| 755 | { | ||
| 756 | my ($oops) = @_; | ||
| 757 | for my $obj (values %{$oops->{virtualize}}) { | ||
| 758 | next unless defined $obj; # it's a weak reference | ||
| 759 | my $id = $oops->get_object_id($obj); | ||
| 760 | $oops->virtual_object($id, 1); | ||
| 761 | } | ||
| 762 | } | ||
| 763 | |||
| 764 | sub virtual_object | ||
| 765 | { | ||
| 766 | my ($oops, $obj, $newval) = @_; | ||
| 767 | |||
| 768 | my $id; | ||
| 769 | if (ref($obj)) { | ||
| 770 | my $mem = refaddr($obj); | ||
| 771 | $id = $oops->{memory}{$mem}; | ||
| 772 | unless ($id) { | ||
| 773 | # defer virtualization until later | ||
| 774 | my $old = exists $oops->{virtualize}{$mem}; | ||
| 775 | if (@_ > 2) { | ||
| 776 | if ($newval) { | ||
| 777 | $oops->{virtualize}{$mem} = $obj; | ||
| 778 | weaken($oops->{virtualize}{$mem}); | ||
| 779 | } else { | ||
| 780 | delete $oops->{virtualize}{$mem}; | ||
| 781 | } | ||
| 782 | } | ||
| 783 | return $old; | ||
| 784 | } | ||
| 785 | } else { | ||
| 786 | $id = $obj; | ||
| 787 | } | ||
| 788 | |||
| 789 | croak unless $oops->{otype}{$id}; | ||
| 790 | |||
| 791 | my $old = $oops->{virtual}{$id} eq 'V'; | ||
| 792 | print "*$id - virtual_object($newval)\n" if $debug_load_group; | ||
| 793 | if (@_ > 2) { | ||
| 794 | if ($newval) { | ||
| 795 | unless ($oops->{virtual}{$id} eq 'V') { | ||
| 796 | $oops->{virtual}{$id} = 'V'; | ||
| 797 | |||
| 798 | # break apart old load group | ||
| 799 | my $olg = $oops->{loadgroup}{$id}; | ||
| 800 | print "in virtual_object($id), must break apart '$olg'\n" if $debug_load_group; | ||
| 801 | for my $o (keys %{$oops->{groupset}{$olg}}) { | ||
| 802 | print "in virtual_object($id) setting new group for *$o\n" if $debug_load_group; | ||
| 803 | print "in virtual_object, *$id forcesave\n" if $debug_forcesave; | ||
| 804 | $oops->{loadgroup}{$o} = $o; | ||
| 805 | $oops->{forcesave}{$o} = __LINE__; | ||
| 806 | } | ||
| 807 | } | ||
| 808 | } else { | ||
| 809 | $oops->{virtual}{$id} = '0'; | ||
| 810 | } | ||
| 811 | $oops->{forcesave}{$id} = | ||
| 812 | sprintf("%d/%d/%d", __LINE__, (caller(1))[2], (caller(2))[2]); my $x = # debug | ||
| 813 | __LINE__; | ||
| 814 | print "in virtual_object, forcesave *$id virtual=$newval\n" if $debug_forcesave; | ||
| 815 | print "%$id - virtual: $newval.\n" if $debug_isvirtual; | ||
| 816 | } | ||
| 817 | assertions($oops); | ||
| 818 | return $old; | ||
| 819 | } | ||
| 820 | |||
| 821 | sub lock { | ||
| 822 | my ($oops, $thing) = @_; | ||
| 823 | croak "lock() requires a reference" unless ref($thing); | ||
| 824 | my $mem = refaddr($thing); | ||
| 825 | if ((my $r = $oops->{memory2key}{$mem})) { | ||
| 826 | my ($id, $key) = @$r; | ||
| 827 | return $oops->{dbo}->lock_attribute($id, $key); | ||
| 828 | } | ||
| 829 | if ((my $id = $oops->{memory}{$mem})) { | ||
| 830 | |||
| 831 | return $oops->{dbo}->lock_object($id); | ||
| 832 | } | ||
| 833 | if ((my ($tiedaddr, $key) = tied_hash_reference($_[0]))) { | ||
| 834 | my $id = $oops->{memory}{$tiedaddr} || $oops->{new_memory}{$tiedaddr}; | ||
| 835 | return 0 unless $id; | ||
| 836 | return $oops->{dbo}->lock_attribute($id, $key); | ||
| 837 | } | ||
| 838 | return 0; | ||
| 839 | } | ||
| 840 | |||
| 841 | |||
| 842 | # clears only safe parts of the cache | ||
| 843 | sub clear_cache | ||
| 844 | { | ||
| 845 | my ($oops) = @_; | ||
| 846 | for my $obj (@{$oops->{vcache}}) { | ||
| 847 | $obj->CLEAR_CACHE(); | ||
| 848 | } | ||
| 849 | } | ||
| 850 | |||
| 851 | sub transaction | ||
| 852 | { | ||
| 853 | shift if ref $_[0] ne 'CODE'; | ||
| 854 | my ($code, @args) = @_; | ||
| 855 | local($transaction_tries) = 1; | ||
| 856 | my $auto_die; | ||
| 857 | for (;;) { | ||
| 858 | croak "next or redo inside eval" if $auto_die; # protect aginst 'next' et all inside eval | ||
| 859 | $auto_die = 1; | ||
| 860 | local(@transaction_rollback) = (); | ||
| 861 | if (wantarray) { | ||
| 862 | my @r; | ||
| 863 | eval { @r = (&$code(@args)); }; | ||
| 864 | return @r unless $@; | ||
| 865 | } else { | ||
| 866 | my $r; | ||
| 867 | eval { $r = &$code(@args); }; | ||
| 868 | return $r unless $@; | ||
| 869 | }; | ||
| 870 | my $error = $@; | ||
| 871 | for my $r (@transaction_rollback) { | ||
| 872 | &$r($error); | ||
| 873 | } | ||
| 874 | if ($error =~ /($transfailrx)/) { | ||
| 875 | croak "aborting transaction -- persistent deadlock: $1" | ||
| 876 | if $transaction_tries++ > $transaction_maxtries; | ||
| 877 | $auto_die = 0; | ||
| 878 | require Time::HiRes; | ||
| 879 | import Time::HiRes qw(sleep); | ||
| 880 | if ($transaction_failure_sleep) { | ||
| 881 | my $base = $transaction_failure_maxsleep ** (1 / $transaction_maxtries); | ||
| 882 | my $sleeptime = rand($transaction_failure_sleep * $base ** ($transaction_tries-1)); | ||
| 883 | printf STDERR "Sleeping %.2f seconds, restarting transaction ($transaction_tries)\n", $sleeptime; | ||
| 884 | sleep($sleeptime); | ||
| 885 | } else { | ||
| 886 | print STDERR "Restarting transaction ($transaction_tries)\n" if $warnings; | ||
| 887 | } | ||
| 888 | redo; | ||
| 889 | } | ||
| 890 | print STDERR "E='$error'\n"; # debug | ||
| 891 | croak $error; | ||
| 892 | } | ||
| 893 | croak "last inside eval"; # protect aginst 'next' et all inside eval | ||
| 894 | } | ||
| 895 | |||
| 896 | # | ||
| 897 | # make a reference to a tied hash key | ||
| 898 | # | ||
| 899 | sub getref(\%$) | ||
| 900 | { | ||
| 901 | my $hash = shift; | ||
| 902 | my $key = shift; | ||
| 903 | my $tied = tied %$hash; | ||
| 904 | confess unless reftype($hash) eq 'HASH'; | ||
| 905 | return \$hash->{$key} unless $tied && $tied->can('GETREF'); | ||
| 906 | print "getref getting references for '$key'\n" if $debug_27555; | ||
| 907 | return $tied->GETREF($key); | ||
| 908 | } | ||
| 909 | |||
| 910 | sub rollback | ||
| 911 | { | ||
| 912 | my $oops = shift; | ||
| 913 | $oops->{dbo}->rollback(); | ||
| 914 | $oops->DESTROY(); | ||
| 915 | } | ||
| 916 | |||
| 917 | sub commit | ||
| 918 | { | ||
| 919 | my $oops = shift; | ||
| 920 | die if $oops->{readonly}; | ||
| 921 | $oops->save; | ||
| 922 | my $x = int(rand($debug_tdelay)); if ($debug_tdelay && $debug_dbidelay) { for (my $i = 0; $i < $x; $i++) {} } | ||
| 923 | $oops->{dbo}->commit || die $oops->errstr; | ||
| 924 | print "COMMIT $oops done\n" if $debug_commit; | ||
| 925 | assertions($oops); | ||
| 926 | } | ||
| 927 | |||
| 928 | |||
| 929 | # | ||
| 930 | # There are two parts to saving state: saving the objects and | ||
| 931 | # saving the object attributes. | ||
| 932 | # | ||
| 933 | # Rewrite: save all attributes before saving the objects. | ||
| 934 | # | ||
| 935 | # Object records will be re-written if: | ||
| 936 | # $oops->{refmore}{$id} | ||
| 937 | # $oops->{refless}{$id} | ||
| 938 | # $oops->{forcesave}{$id} | ||
| 939 | # | ||
| 940 | # Contents will be re-written if: | ||
| 941 | # $oops->{unwatched}{$id} | ||
| 942 | # $oops->{objtouched}{$id} | ||
| 943 | # $oops->{demandwrite}{$id} | ||
| 944 | # | ||
| 945 | |||
| 946 | sub save | ||
| 947 | { | ||
| 948 | my ($oops) = @_; | ||
| 949 | |||
| 950 | confess "only one commit() allowed" if $oops->{commitdone}++; | ||
| 951 | |||
| 952 | print "COMMIT start \@ $caller{1}\n" if $debug_commit; | ||
| 953 | |||
| 954 | confess unless $oops->isa('OOPS'); | ||
| 955 | my $savedone = $oops->{savedone} = {}; | ||
| 956 | my $forcesave = $oops->{forcesave}; | ||
| 957 | |||
| 958 | my $cache = $oops->{cache}; | ||
| 959 | my $refcount = $oops->{refcount}; | ||
| 960 | my $oloadgroup = $oops->{loadgroup}; | ||
| 961 | my $type = $oops->{otype}; | ||
| 962 | my $oclass = $oops->{class}; | ||
| 963 | my $refmore = $oops->{refmore}; | ||
| 964 | my $refless = $oops->{refless}; | ||
| 965 | my $refstowrite = $oops->{refstowrite}; | ||
| 966 | my $loadgrouplock = $oops->{loadgrouplock}; | ||
| 967 | my $virtual = $oops->{virtual}; | ||
| 968 | my $arraylen = $oops->{arraylen}; | ||
| 969 | my $reftarg = $oops->{reftarg}; | ||
| 970 | my @tied; | ||
| 971 | |||
| 972 | local($oops->{insave}) = 1; | ||
| 973 | |||
| 974 | $oops->process_deferred_virtualize(); | ||
| 975 | |||
| 976 | # | ||
| 977 | # ARRAYs are always considered 'touched' and thus | ||
| 978 | # must be rechecked. | ||
| 979 | # | ||
| 980 | # HASHes that have been DESTROYed are considered 'touched' | ||
| 981 | # | ||
| 982 | # NEW object are always touched | ||
| 983 | # | ||
| 984 | for my $id (keys %{$oops->{objtouched}}) { | ||
| 985 | print "*$id->write_object (touched: $oops->{objtouched}{$id})\n" if $debug_commit; | ||
| 986 | $oops->write_object($id); | ||
| 987 | } | ||
| 988 | |||
| 989 | # | ||
| 990 | # tied HASHes (both sorts) that have been modified insert | ||
| 991 | # themselves into the demandwrite set. | ||
| 992 | # | ||
| 993 | # As a side-effect of saving tied hashes, there may be additional | ||
| 994 | # unwatched objects to save. | ||
| 995 | # | ||
| 996 | for my $id (keys %{$oops->{demandwrite}}) { | ||
| 997 | print "*$id->write_object (demandwrite)\n" if $debug_commit; | ||
| 998 | $oops->write_object($id); | ||
| 999 | my $tied; | ||
| 1000 | my $t = $type->{$id}; | ||
| 1001 | if ($t eq 'H') { | ||
| 1002 | $tied = tied %{$cache->{$id}}; | ||
| 1003 | } elsif ($t eq 'A') { | ||
| 1004 | $tied = tied @{$cache->{$id}}; | ||
| 1005 | } elsif ($t eq 'S') { | ||
| 1006 | $tied = tied ${$cache->{$id}}; | ||
| 1007 | } else { | ||
| 1008 | confess "type = $t."; | ||
| 1009 | } | ||
| 1010 | push(@tied, $tied); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | # | ||
| 1014 | # Some objects are not tied in any way and we just have to | ||
| 1015 | # check them to make sure they haven't changed. | ||
| 1016 | # | ||
| 1017 | for my $id (keys %{$oops->{unwatched}}) { | ||
| 1018 | print "*$id->write_object (unwatched)\n" if $debug_commit; | ||
| 1019 | $oops->write_object($id); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | my %classdone; | ||
| 1023 | my $firstid; | ||
| 1024 | my $updateobjectQ = $oops->query('updateobject'); | ||
| 1025 | my $objectinfoQ = $oops->query('objectinfo'); | ||
| 1026 | my %done; | ||
| 1027 | my $pass; | ||
| 1028 | |||
| 1029 | # | ||
| 1030 | # Look at objects and make adjustments while there | ||
| 1031 | # remains %refmore, %refless or @refstowrite. | ||
| 1032 | # | ||
| 1033 | for(;;) { | ||
| 1034 | # more refstowrite may be added while looking at refmore | ||
| 1035 | while (@$refstowrite) { | ||
| 1036 | $oops->write_ref(shift @$refstowrite); | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | # Do additions before subtractions so that we don't have | ||
| 1040 | # any false deletes. | ||
| 1041 | my $refchange; | ||
| 1042 | if (%{$oops->{refmore}}) { | ||
| 1043 | $refchange = $oops->{refmore}; | ||
| 1044 | $oops->{refmore} = {}; | ||
| 1045 | } elsif (%{$oops->{refless}}) { | ||
| 1046 | $refchange = $oops->{refless}; | ||
| 1047 | $oops->{refless} = {}; | ||
| 1048 | } else { | ||
| 1049 | last; | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | print "commit, pass $pass\n" if $debug_commit && $pass++; | ||
| 1053 | for my $id (keys %$refchange) { | ||
| 1054 | while (@$refstowrite) { | ||
| 1055 | $oops->write_ref(shift @$refstowrite); | ||
| 1056 | } | ||
| 1057 | if ($refchange->{$id}) { | ||
| 1058 | # | ||
| 1059 | # The reference count for $id needs to be changed. | ||
| 1060 | # | ||
| 1061 | if (exists $cache->{$id}) { | ||
| 1062 | # | ||
| 1063 | # Object is loaded. | ||
| 1064 | # | ||
| 1065 | printf "in commit, *%d refs: old %d + change %s (=%d)\n", $id, $refcount->{$id}, $qplusminus{$refchange->{$id}}, $refcount->{$id}+ $refchange->{$id} if $debug_refcount; | ||
| 1066 | my $newobject = ($refcount->{$id} == -1); | ||
| 1067 | $refcount->{$id} += $refchange->{$id}; | ||
| 1068 | if ($oops->{refless}{$id} and $refcount->{$id} + $oops->{refless}{$id} > 0) { | ||
| 1069 | $refcount->{$id} += $oops->{refless}{$id}; | ||
| 1070 | delete $oops->{refless}{$id}; | ||
| 1071 | } | ||
| 1072 | if ($refcount->{$id} > 0) { | ||
| 1073 | my $otype = $type->{$id} || confess; | ||
| 1074 | my $loadgroup; | ||
| 1075 | if (exists $loadgrouplock->{$id}) { | ||
| 1076 | my $locked_to = $loadgrouplock->{$id}; | ||
| 1077 | if (exists $refchange->{$locked_to}) { | ||
| 1078 | # | ||
| 1079 | # they'll be saved together | ||
| 1080 | # | ||
| 1081 | $firstid ||= $id; | ||
| 1082 | $loadgroup ||= $firstid; | ||
| 1083 | } else { | ||
| 1084 | $loadgroup = $oloadgroup->{$locked_to}; | ||
| 1085 | } | ||
| 1086 | } elsif ($virtual->{$id} eq 'V') { | ||
| 1087 | $loadgroup = $id; | ||
| 1088 | } else { | ||
| 1089 | $firstid ||= $id; | ||
| 1090 | $loadgroup ||= $firstid; | ||
| 1091 | } | ||
| 1092 | $oloadgroup->{$id} = $loadgroup; | ||
| 1093 | die if $oops->{deleted}{$id}; # assertion | ||
| 1094 | print "*$id updated1 (later). loadgroup=$loadgroup, class=$qref{$cache->{$id}} otype=$otype, virtual=$virtual->{$id} reftarg=$reftarg->{$id} refcount=$refcount->{$id} arraylen=$arraylen->{$id} gcgen=$oops->{gcgeneration}{$id}\n" if $debug_load_group || $debug_isvirtual || $debug_write_object || $debug_arraylen || $debug_refcount; | ||
| 1095 | $forcesave->{$id} = __LINE__; | ||
| 1096 | $oclass->{$id} = ref($cache->{$id}); | ||
| 1097 | $classdone{$id} = __LINE__; | ||
| 1098 | } elsif ($refcount->{$id} == 0) { | ||
| 1099 | print "*$id - no refereces, will delete\n" if $debug_write_object || $debug_refcount; | ||
| 1100 | #print "refcount *$id = 0, deleting\n"; | ||
| 1101 | $oops->delete_object($id); | ||
| 1102 | $done{$id} = __LINE__; | ||
| 1103 | } else { | ||
| 1104 | confess "refcount: $refcount->{$id}"; | ||
| 1105 | } | ||
| 1106 | } else { | ||
| 1107 | # | ||
| 1108 | # Object is not loaded, fetch the refcount. | ||
| 1109 | # | ||
| 1110 | # TODO/OPTIMIZATION: this may result in extra queries. Cache the refcounts and | ||
| 1111 | # delay the writes the same way as is done for cached objects. | ||
| 1112 | # | ||
| 1113 | $objectinfoQ->execute($id) || confess; | ||
| 1114 | my (undef, $loadgroup, $class, $otype, $ovirtual, $oreftarg, undef, $oarraylen, $refs, $counter, $gcgen) = $objectinfoQ->fetchrow_array; | ||
| 1115 | $objectinfoQ->finish(); | ||
| 1116 | confess unless $class; | ||
| 1117 | printf "in commit, uncached *%d refs: old %d +change %d = (=%d)\n", $id, $refs, $refchange->{$id}, $refs+ $refchange->{$id} if $debug_refcount || $debug_write_object; | ||
| 1118 | $refcount->{$id} = $refs + $refchange->{$id}; | ||
| 1119 | if ($oops->{refless}{$id} and $refcount->{$id} + $oops->{refless}{$id} > 0) { | ||
| 1120 | $refcount->{$id} += $oops->{refless}{$id}; | ||
| 1121 | delete $oops->{refless}{$id}; | ||
| 1122 | } | ||
| 1123 | confess if exists $cache->{$id}; | ||
| 1124 | if ($refcount->{$id} > 0) { | ||
| 1125 | die if $oops->{deleted}{$id}; # assertion | ||
| 1126 | $updateobjectQ->execute($loadgroup, $class, $otype, $ovirtual || '0', $oreftarg, $oarraylen, $refcount->{$id}, $gcgen, $id); | ||
| 1127 | print "*$id updated2. loadgroup=$loadgroup, type=$class, otype=$otype, refcount=$refcount->{$id} virtual=$ovirtual reftarg=$oreftarg arraylen=$oarraylen, gcgen=$gcgen\n" if $debug_load_group || $debug_write_object || $debug_arraylen || $debug_refcount || $debug_gcgeneration; | ||
| 1128 | $done{$id} = __LINE__; | ||
| 1129 | } elsif ($refcount->{$id} == 0) { | ||
| 1130 | $oops->delete_object($id); | ||
| 1131 | $done{$id} = __LINE__; | ||
| 1132 | } else { | ||
| 1133 | confess "refcount: $refcount->{$id}"; | ||
| 1134 | } | ||
| 1135 | } | ||
| 1136 | } else { | ||
| 1137 | if ($refcount->{$id} > 0) { | ||
| 1138 | printf "*$id no change in refcount, marking for forced saving\n" if $debug_refcount || $debug_write_object; | ||
| 1139 | $forcesave->{$id} = __LINE__; | ||
| 1140 | } elsif ($refcount->{$id} == 0) { | ||
| 1141 | printf "in commit, deleting unchanged unreferenced $oops->{otype}{$id}*$id (=0)\n" if $debug_refcount || $debug_write_object; | ||
| 1142 | $oops->delete_object($id); | ||
| 1143 | $done{$id} = __LINE__; | ||
| 1144 | } else { | ||
| 1145 | confess "negative refcount: *$id: $refcount->{$id}"; | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | # | ||
| 1152 | # We have to manually scan for objects that have | ||
| 1153 | # re-blessed themselves as there is no way to watch | ||
| 1154 | # for that. | ||
| 1155 | # | ||
| 1156 | for my $id (keys %$cache) { | ||
| 1157 | next unless defined $cache->{$id}; | ||
| 1158 | next if exists($oclass->{$id}) && ref($cache->{$id}) eq $oclass->{$id}; | ||
| 1159 | next if $classdone{$id}; | ||
| 1160 | printf "classchange %d: %s -> %s.\n", $id, $oclass->{$id}, ref($cache->{$id}) if $debug_commit; | ||
| 1161 | $oclass->{$id} = ref($cache->{$id}); | ||
| 1162 | $forcesave->{$id} = __LINE__ | ||
| 1163 | unless $forcesave->{$id}; | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | # | ||
| 1167 | # XX PARTIAL IMPLEMENTATION: | ||
| 1168 | # For database servers that use deadlock detection (rather | ||
| 1169 | # than opportunistic locking) we could force the object rows to | ||
| 1170 | # get updated whenever an attribute is added or deleted. This | ||
| 1171 | # allows a less strict tranaction locking default. | ||
| 1172 | # | ||
| 1173 | # | ||
| 1174 | my $die; | ||
| 1175 | for my $id (keys %$forcesave) { | ||
| 1176 | next if $done{$id}; | ||
| 1177 | my $otype = $type->{$id} || confess | ||
| 1178 | "at line $forcesave->{$id} (from $oops->{memsetdebug}{$id})... " . # debug | ||
| 1179 | "no type for object $id"; | ||
| 1180 | my $loadgroup = $oloadgroup->{$id} || $id; | ||
| 1181 | die if $oops->{deleted}{$id}; # assertion | ||
| 1182 | print "*$id updated3. loadgroup=$loadgroup, type=".ref($cache->{$id})." otype=$otype, refcount=$refcount->{$id} virtual=$virtual->{$id} reftarg=$reftarg arraylen=$arraylen->{$id} gcgen=$oops->{gcgeneration}{$id}\n" if $debug_load_group || $debug_write_object || $debug_arraylen || $debug_refcount || $debug_gcgeneration; | ||
| 1183 | $updateobjectQ->execute($loadgroup, ref($cache->{$id}), $otype, $virtual->{$id}, $reftarg->{$id}, $arraylen->{$id}, $refcount->{$id}, $oops->{gcgeneration}{$id}, $id) | ||
| 1184 | || confess $updateobjectQ->errstr; | ||
| 1185 | $oclass->{$id} = ref($cache->{$id}); | ||
| 1186 | } | ||
| 1187 | |||
| 1188 | for my $tied (@tied) { | ||
| 1189 | $tied->POST_SAVE; | ||
| 1190 | } | ||
| 1191 | $oops->{forcesave} = {}; | ||
| 1192 | } | ||
| 1193 | |||
| 1194 | # | ||
| 1195 | # This saves the contents of an object. The object | ||
| 1196 | # itself is updated in save(). | ||
| 1197 | # | ||
| 1198 | sub write_object | ||
| 1199 | { | ||
| 1200 | my ($oops, $id, $sponsoring_id) = @_; | ||
| 1201 | $id = $oops->get_object_id($id, $sponsoring_id) | ||
| 1202 | if ref $id; | ||
| 1203 | |||
| 1204 | return if $oops->{savedone}{$id}++; | ||
| 1205 | |||
| 1206 | print Carp::longmess("DEBUG: write_object(@_) called") if $debug_write_object_context; | ||
| 1207 | |||
| 1208 | my $obj = $oops->{cache}{$id}; | ||
| 1209 | my $type = $perltype2otype{reftype($obj)} || confess; | ||
| 1210 | |||
| 1211 | my $sym = $typesymbol{$type} || '???' if $debug_write_object; | ||
| 1212 | print "$sym*$id write_object $qval{$obj}\n" if $debug_write_object; | ||
| 1213 | |||
| 1214 | my $memory = $oops->{memory}; | ||
| 1215 | |||
| 1216 | # would this help? | ||
| 1217 | # if ($oops->{class}{$id} ne ref $obj) { | ||
| 1218 | # $oops->{refcount}{$id} += 0; # touchit | ||
| 1219 | # } | ||
| 1220 | |||
| 1221 | if ($type eq 'H') { | ||
| 1222 | my $tied = tied(%$obj); | ||
| 1223 | if ($tied && $tied =~ /^OOPS/) { | ||
| 1224 | print "%*$id write_object - using SAVE_SELF $qval{$tied}\n" if $debug_write_hash; | ||
| 1225 | $tied->SAVE_SELF(); | ||
| 1226 | } else { | ||
| 1227 | $oops->write_hash($obj, $id); | ||
| 1228 | } | ||
| 1229 | } elsif ($type eq 'A') { | ||
| 1230 | $oops->write_array($id); | ||
| 1231 | } elsif ($type eq 'S') { | ||
| 1232 | my $tied = tied($$obj); | ||
| 1233 | if ($tied && $tied =~ /^OOPS/) { | ||
| 1234 | print "\$*$id using SAVE_SELF $tied\n" if $debug_write_ref; | ||
| 1235 | $tied->SAVE_SELF() && push(@{$oops->{refstowrite}}, $id); | ||
| 1236 | } else { | ||
| 1237 | print "\$*$id will use write_ref later\n" if $debug_write_ref; | ||
| 1238 | if (ref $$obj) { | ||
| 1239 | my $m; | ||
| 1240 | if ($m = $memory->{refaddr($$obj)}) { | ||
| 1241 | print "lookup MEMORY($qval{$$obj}) = $m in write_object - ref\n" if $debug_memory; | ||
| 1242 | print "\$*$id is an existing object *$m\n" if $debug_write_ref; | ||
| 1243 | } else { | ||
| 1244 | print "lookup MEMORY($qval{$$obj}) = ? in write_object - ref\n" if $debug_memory; | ||
| 1245 | $m = $oops->get_object_id($$obj, $id); | ||
| 1246 | print "\$*$id is a new object *$m: $qval{$$obj}\n" if $debug_write_ref; | ||
| 1247 | } | ||
| 1248 | $oops->write_object($m, $id); | ||
| 1249 | } else { | ||
| 1250 | print "\$*$id is a ref to a scalar $qval{$$obj}\n" if $debug_write_ref; | ||
| 1251 | } | ||
| 1252 | push(@{$oops->{refstowrite}}, $id); | ||
| 1253 | } | ||
| 1254 | } else { | ||
| 1255 | confess; | ||
| 1256 | } | ||
| 1257 | print "$sym*$id done with write_object\n" if $debug_write_object; | ||
| 1258 | assertions($oops); | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | sub write_hash | ||
| 1262 | { | ||
| 1263 | my ($oops, $obj, $id, $ptypes, $added) = @_; | ||
| 1264 | |||
| 1265 | print Carp::longmess("DEBUG: write_hash(@_) called") if 0; # debug | ||
| 1266 | |||
| 1267 | my $oldvalue = $oops->{oldvalue}; | ||
| 1268 | my $oldobject = $oops->{oldobject}; | ||
| 1269 | my $oldbig = $oops->{oldbig}; | ||
| 1270 | my $memory = $oops->{memory}; | ||
| 1271 | my $memory2key = $oops->{memory2key}; | ||
| 1272 | my $new_memory = $oops->{new_memory}; | ||
| 1273 | my $new_memory2key = $oops->{new_memory2key}; | ||
| 1274 | my $tied = tied %{$oops->{cache}{$id}}; | ||
| 1275 | |||
| 1276 | confess unless ref $obj; | ||
| 1277 | |||
| 1278 | my (@k) = keys %$obj; | ||
| 1279 | |||
| 1280 | # | ||
| 1281 | # auto-virtualize. Good idea? | ||
| 1282 | # | ||
| 1283 | # if (@k > $demandthreshold && ! exists($oops->{loadvirtual}{$id})) { | ||
| 1284 | # $oops->virtual_object($id, 1) | ||
| 1285 | # } elsif (@k < $demandthreshold && exists($oops->{loadvirtual}{$id})) { | ||
| 1286 | # $oops->virtual_object($id, 0) | ||
| 1287 | # } | ||
| 1288 | |||
| 1289 | for my $pkey (@k) { | ||
| 1290 | unless ($tied) { | ||
| 1291 | my $m = refaddr(\$obj->{$pkey}); | ||
| 1292 | $oops->new_memory2key(\$obj->{$pkey}, $id, $pkey); | ||
| 1293 | print "NEWMEMORY2KEY ".$m." := \%*$id/'$pkey' - in write_hash\n" if $debug_memory; | ||
| 1294 | } | ||
| 1295 | { no warnings; print "\%$id/$qval{$pkey} pondering... ($qval{$obj->{$pkey}})\n" if $debug_write_hash; } | ||
| 1296 | print "ref to \%$id/$qval{$pkey} is $qval{\$obj->{$pkey}}\n" if $debug_write_hash && $debug_refalias; | ||
| 1297 | if ($ptypes && exists $ptypes->{$pkey}) { | ||
| 1298 | print "\%$id/$pkey ...still not loaded ($ptypes->{$pkey})\n" if $debug_write_hash; | ||
| 1299 | } elsif (exists $oldvalue->{$id} && exists $oldvalue->{$id}{$pkey}) { | ||
| 1300 | no warnings; | ||
| 1301 | if ($oldvalue->{$id}{$pkey} eq $obj->{$pkey} | ||
| 1302 | && defined($oldvalue->{$id}{$pkey}) == defined($obj->{$pkey}) | ||
| 1303 | && ref($oldvalue->{$id}{$pkey}) eq ref($obj->{$pkey})) | ||
| 1304 | { | ||
| 1305 | use warnings; | ||
| 1306 | print "\%$id/$pkey ...unchanged\n" if $debug_write_hash; | ||
| 1307 | print "lookup MEMORY($qval{$obj->{$pkey}}) in write_hash\n" if $debug_memory; | ||
| 1308 | $oops->write_object($memory->{refaddr($obj->{$pkey})}, $id) | ||
| 1309 | if ref $obj->{$pkey}; | ||
| 1310 | } else { | ||
| 1311 | use warnings; | ||
| 1312 | { no warnings; print "\%$id/$pkey ...changed. old value was $oldvalue->{$id}{$pkey}\n" if $debug_write_hash; } | ||
| 1313 | $oops->update_attribute($id, $pkey, $obj->{$pkey}, undef, $oldvalue->{$id}{$pkey}); | ||
| 1314 | } | ||
| 1315 | } elsif (exists $oldbig->{$id} && exists $oldbig->{$id}{$pkey}) { | ||
| 1316 | my $ock = ref($obj->{$pkey}) ? '' : bigcksum($obj->{$pkey}); | ||
| 1317 | if ($oldbig->{$id}{$pkey} eq $ock) { | ||
| 1318 | print "\%$id/$pkey ...unchanged (big)\n" if $debug_write_hash; | ||
| 1319 | # this attribute is unchanged | ||
| 1320 | } else { | ||
| 1321 | print "\%$id/$pkey ...changed. old big\n" if $debug_write_hash; | ||
| 1322 | $oops->update_attribute($id, $pkey, $obj->{$pkey}, $ock); | ||
| 1323 | } | ||
| 1324 | } elsif (exists $oldobject->{$id} && exists $oldobject->{$id}{$pkey}) { | ||
| 1325 | # this used to be an object | ||
| 1326 | print "\%$id/$pkey this used to be an object...\n" if $debug_write_hash; | ||
| 1327 | if (ref $obj->{$pkey} && $oldobject->{$id}{$pkey} == $oops->get_object_id($obj->{$pkey}, $id)) { | ||
| 1328 | # no change | ||
| 1329 | print "\%$id/$pkey same one\n" if $debug_write_hash; | ||
| 1330 | $oops->write_object($oldobject->{$id}{$pkey}, $id); | ||
| 1331 | } else { | ||
| 1332 | print "\%$id/$pkey changed to $qval{$obj->{$pkey}}\n" if $debug_write_hash; | ||
| 1333 | $oops->update_attribute($id, $pkey, $obj->{$pkey}); | ||
| 1334 | } | ||
| 1335 | } elsif ($added) { | ||
| 1336 | if (exists $added->{$pkey}) { | ||
| 1337 | # this is a new value | ||
| 1338 | { no warnings; print "\%$id/$pkey ...added: $qval{$obj->{$pkey}}\n" if $debug_write_hash; } | ||
| 1339 | $oops->insert_attribute($id, $pkey, undef, $obj->{$pkey}); | ||
| 1340 | } else { | ||
| 1341 | { no warnings; print "\%$id/$pkey ...still original value: $qval{$obj->{$pkey}}\n" if $debug_write_hash; } | ||
| 1342 | } | ||
| 1343 | } else { | ||
| 1344 | # this is a new value | ||
| 1345 | print "\%$id/$pkey ...new value A\n" if $debug_write_hash; | ||
| 1346 | $oops->insert_attribute($id, $pkey, undef, $obj->{$pkey}); | ||
| 1347 | } | ||
| 1348 | } | ||
| 1349 | if (exists $oldvalue->{$id}) { | ||
| 1350 | print "\%$id checking old values\n" if $debug_write_hash; | ||
| 1351 | for my $pkey (keys %{$oldvalue->{$id}}) { | ||
| 1352 | next if exists $obj->{$pkey}; | ||
| 1353 | # this pkey has gone away | ||
| 1354 | { no warnings; print "\%$id/$pkey delete extra old value \%$id/$pkey ($oldvalue->{$id}{$pkey})\n" if $debug_write_hash; }; | ||
| 1355 | $oops->delete_attribute($id, $pkey, $oldvalue->{$id}{$pkey}); | ||
| 1356 | } | ||
| 1357 | } | ||
| 1358 | if (exists $oldobject->{$id}) { | ||
| 1359 | print "\%$id checking old objects\n" if $debug_write_hash; | ||
| 1360 | for my $pkey (keys %{$oldobject->{$id}}) { | ||
| 1361 | next if exists $obj->{$pkey}; | ||
| 1362 | next if exists $oldvalue->{$id} && exists $oldvalue->{$id}{$pkey}; | ||
| 1363 | # this pkey has gone away | ||
| 1364 | print "\%$id/$pkey delete extra old object \%$id/$pkey ($oldvalue->{$id}{$pkey})\n" if $debug_write_hash; | ||
| 1365 | $oops->delete_attribute($id, $pkey); | ||
| 1366 | } | ||
| 1367 | } | ||
| 1368 | if (exists $oldbig->{$id}) { | ||
| 1369 | for my $pkey (keys %{$oldbig->{$id}}) { | ||
| 1370 | next if exists $obj->{$pkey}; | ||
| 1371 | next if exists $oldvalue->{$id} && exists $oldvalue->{$id}{$pkey}; | ||
| 1372 | print "\%$id/$pkey delete extra old big \%$id/$pkey\n" if $debug_write_hash; | ||
| 1373 | $oops->delete_attribute($id, $pkey); | ||
| 1374 | } | ||
| 1375 | } | ||
| 1376 | assertions($oops); | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | sub write_array | ||
| 1380 | { | ||
| 1381 | my ($oops, $id) = @_; | ||
| 1382 | |||
| 1383 | my $obj = $oops->{cache}{$id}; | ||
| 1384 | |||
| 1385 | my $sym = '@' if $debug_write_object || $debug_write_array; | ||
| 1386 | print "$sym$id write_object $obj\n" if $debug_write_object; | ||
| 1387 | |||
| 1388 | my $oldvalue = $oops->{oldvalue}; | ||
| 1389 | my $oldobject = $oops->{oldobject}; | ||
| 1390 | my $oldbig = $oops->{oldbig}; | ||
| 1391 | my $memory = $oops->{memory}; | ||
| 1392 | my $new_memory = $oops->{new_memory}; | ||
| 1393 | my $new_memory2key = $oops->{new_memory2key}; | ||
| 1394 | |||
| 1395 | my $tied; | ||
| 1396 | my $isnew = $oops->{refcount}{$id} == -1; | ||
| 1397 | |||
| 1398 | |||
| 1399 | #print "WRITE ARRAY ".__LINE__." with $#$obj\n"; | ||
| 1400 | #print join('',(map { exists $obj->[$_] ? '1' : '0' } 0..$#$obj),"\n"); | ||
| 1401 | |||
| 1402 | # | ||
| 1403 | # XXXTODO | ||
| 1404 | # There is a nasty problem: splice, shift, pop, unshift, and push can | ||
| 1405 | # move around array elements without causing big elements to be | ||
| 1406 | # loaded. When it comes time to save these moved elements, we have to | ||
| 1407 | # still have access to the old values. Doing the save in exactly the | ||
| 1408 | # right order could allow the values to be shifted in the database rather | ||
| 1409 | # than read and re-written. | ||
| 1410 | # | ||
| 1411 | # With refcopy we should be able to handle this now! | ||
| 1412 | # | ||
| 1413 | for (my $index = 0; $index <= $#$obj; $index++) { | ||
| 1414 | my $tied; | ||
| 1415 | next unless exists $obj->[$index]; | ||
| 1416 | next unless ($tied = tied $obj->[$index]) && $tied->isa('OOPS::BigInArray'); | ||
| 1417 | undef $tied; # keeping copies of tied objects prevents untieing. | ||
| 1418 | my $x = defined($obj->[$index]); # force loading (which will untie) | ||
| 1419 | } | ||
| 1420 | |||
| 1421 | my $end = $#$obj; | ||
| 1422 | $end = $oops->{arraylen}{$id} -1 | ||
| 1423 | if defined($oops->{arraylen}{$id}) && $oops->{arraylen}{$id} > $end; | ||
| 1424 | print "$sym$id checking 0..$end ($#$obj/$oops->{arraylen}{$id}) \n" if $debug_write_array; | ||
| 1425 | |||
| 1426 | for (my $index = 0; $index <= $end; $index++) { | ||
| 1427 | if (! exists $obj->[$index]) { | ||
| 1428 | if (exists $oldvalue->{$id} && exists $oldvalue->{$id}{$index}) { | ||
| 1429 | print "$sym$id/$index ...deleting extra old value ($oldvalue->{$id}{$index})\n" if $debug_write_array; | ||
| 1430 | $oops->delete_attribute($id, $index, $oldvalue->{$id}{$index}); | ||
| 1431 | } elsif (exists $oldobject->{$id} && exists $oldobject->{$id}{$index}) { | ||
| 1432 | print "$sym$id/$index ...deleting extra old object ($oldobject->{$id}{$index})\n" if $debug_write_array; | ||
| 1433 | $oops->delete_attribute($id, $index); | ||
| 1434 | } elsif (exists $oldbig->{$id} && exists $oldbig->{$id}{$index}) { | ||
| 1435 | print "$sym$id/$index ...deleting extra old big ($oldbig->{$id}{$index})\n" if $debug_write_array; | ||
| 1436 | $oops->delete_attribute($id, $index); | ||
| 1437 | } else { | ||
| 1438 | print "$sym$id/$index no value now, now value before\n" if $debug_write_array; | ||
| 1439 | } | ||
| 1440 | next; | ||
| 1441 | } | ||
| 1442 | print "$sym$id/$index pondering... ($obj->[$index])\n" if $debug_write_array; | ||
| 1443 | my $tied; | ||
| 1444 | if (($tied = tied $obj->[$index]) && $tied =~ /^OOPS::Demand/ && ! $tied->changed($index)) { | ||
| 1445 | print "\@$id/$index tied and unchanged\n" if $debug_write_array; | ||
| 1446 | } elsif (exists $oldvalue->{$id} && exists $oldvalue->{$id}{$index}) { | ||
| 1447 | no warnings; | ||
| 1448 | if ($oldvalue->{$id}{$index} eq $obj->[$index] | ||
| 1449 | && defined($oldvalue->{$id}{$index}) == defined($obj->[$index]) | ||
| 1450 | && ref($oldvalue->{$id}{$index}) eq ref($obj->[$index])) | ||
| 1451 | { | ||
| 1452 | use warnings; | ||
| 1453 | print "$sym$id/$index ...reference - no change\n" if $debug_write_array; | ||
| 1454 | print "lookup MEMORY($qval{$obj->[$index]}) in write_object - array\n" if $debug_memory && ref($obj->[$index]); | ||
| 1455 | $oops->write_object($memory->{refaddr($obj->[$index])}, $id) | ||
| 1456 | if ref $obj->[$index]; | ||
| 1457 | next; | ||
| 1458 | } else { | ||
| 1459 | use warnings; | ||
| 1460 | # this attribute's value has changed | ||
| 1461 | print "$sym$id/$index ...changed from '$oldvalue->{$id}{$index}'\n" if $debug_write_array; | ||
| 1462 | $oops->update_attribute($id, $index, $obj->[$index], undef, $oldvalue->{$id}{$index}); | ||
| 1463 | } | ||
| 1464 | } elsif (exists($oldobject->{$id}) && exists($oldobject->{$id}{$index})) { | ||
| 1465 | print "\@$id/$index this used to be an object: *$oldobject->{$id}{$index}\n" if $debug_write_array; | ||
| 1466 | if (ref $obj->[$index] && $oldobject->{$id}{$index} == $oops->get_object_id($obj->[$index], $id)) { | ||
| 1467 | print "\@$id/$index same one - no change\n" if $debug_write_array; | ||
| 1468 | $oops->write_object($oldobject->{$id}{$index}, $id); | ||
| 1469 | next; | ||
| 1470 | } else { | ||
| 1471 | print "\@$id/$index changed\n" if $debug_write_array; | ||
| 1472 | $oops->update_attribute($id, $index, $obj->[$index]); | ||
| 1473 | } | ||
| 1474 | } elsif ($bigcutoff && exists($oldbig->{$id}) && exists($oldbig->{$id}{$index})) { | ||
| 1475 | my $ock = (!ref($obj->[$index]) && defined($obj->[$index]) && length($obj->[$index]) > $bigcutoff) | ||
| 1476 | ? bigcksum($obj->[$index]) | ||
| 1477 | : undef; | ||
| 1478 | if ($ock && $oldbig->{$id}{$index} eq $ock) { | ||
| 1479 | print "$sym$id/$index ...big - no change\n" if $debug_write_array; | ||
| 1480 | next; | ||
| 1481 | } else { | ||
| 1482 | print "$sym$id/$index ...big changed\n" if $debug_write_array; | ||
| 1483 | $oops->update_attribute($id, $index, $obj->[$index], $ock); | ||
| 1484 | } | ||
| 1485 | } else { | ||
| 1486 | # this is a new value | ||
| 1487 | print "$sym$id/$index ...new value B\n" if $debug_write_array; | ||
| 1488 | $oops->insert_attribute($id, $index, undef, $obj->[$index]); | ||
| 1489 | } | ||
| 1490 | my $m = refaddr(\$obj->[$index]); | ||
| 1491 | $oops->new_memory2key(\$obj->[$index], $id, $index); | ||
| 1492 | print "NEWMEMORY2KEY ".$m." := \@*$id/$index - in write_object - array\n" if $debug_memory; | ||
| 1493 | } | ||
| 1494 | if (! defined($oops->{arraylen}{$id}) || $oops->{arraylen}{$id} != @$obj) { | ||
| 1495 | $oops->{arraylen}{$id} = @$obj; | ||
| 1496 | $oops->{forcesave}{$id} = __LINE__; | ||
| 1497 | print "in write_array, arraylen(\@*$id) = $oops->{arraylen}{$id}, forcesave\n" if $debug_arraylen || $debug_forcesave; | ||
| 1498 | } else { | ||
| 1499 | print "in write_array, leaving arraylen for \@*$id at $oops->{arraylen}{$id}\n" if $debug_arraylen; | ||
| 1500 | } | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | # | ||
| 1504 | # References are always indirect through another | ||
| 1505 | # object. If the reference isn't indirect, then | ||
| 1506 | # we pretend it is. | ||
| 1507 | # | ||
| 1508 | # ATTRIBUTE TABLE | ||
| 1509 | # id pkey pvalue ptype | ||
| 1510 | # targetid targetkey | ||
| 1511 | # | ||
| 1512 | # 383 400 swarmy 0 | ||
| 1513 | # | ||
| 1514 | # 384 384 'nopkey' 0 | ||
| 1515 | # 384 'nopkey' a-value 0 | ||
| 1516 | # | ||
| 1517 | # 385 384 'nopkey' 0 | ||
| 1518 | # | ||
| 1519 | # Reference 383 is indirect though object 400: \%400{swarmy} | ||
| 1520 | # | ||
| 1521 | # Reference 384 is a ref to scalar. It uses two rows. | ||
| 1522 | # | ||
| 1523 | # Reference 385 is a ref to scalar that is shared with another | ||
| 1524 | # reference. | ||
| 1525 | # | ||
| 1526 | # The purpose of this schema is to allow queries against references | ||
| 1527 | # without needing to know if the reference is to another object | ||
| 1528 | # or not. | ||
| 1529 | # | ||
| 1530 | # Unlike other variable types, write_ref might be called multiple | ||
| 1531 | # times for the same scalar during a commit() because that scalar | ||
| 1532 | # might have become disconnected. | ||
| 1533 | # | ||
| 1534 | |||
| 1535 | sub write_ref | ||
| 1536 | { | ||
| 1537 | my ($oops, $id) = @_; | ||
| 1538 | |||
| 1539 | if ($oops->{deleted}{$id}) { | ||
| 1540 | print "*$id WRITE_REF - already deleted - ignoring\n" if $debug_write_ref; | ||
| 1541 | return; | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | my $obj = $oops->{cache}{$id}; | ||
| 1545 | |||
| 1546 | my $oldvalue = $oops->{oldvalue}; | ||
| 1547 | my $oldobject = $oops->{oldobject}; | ||
| 1548 | my $oldbig = $oops->{oldbig}; | ||
| 1549 | my $memory = $oops->{memory}; | ||
| 1550 | my $memory2key = $oops->{memory2key}; | ||
| 1551 | my $new_memory = $oops->{new_memory}; | ||
| 1552 | my $new_memory2key = $oops->{new_memory2key}; | ||
| 1553 | my $oldalias = $oops->{oldalias}; | ||
| 1554 | |||
| 1555 | my $addr = refaddr($obj); | ||
| 1556 | my $sym; | ||
| 1557 | $sym = '$' if $debug_write_ref; | ||
| 1558 | |||
| 1559 | print "\$*id WRITE_REF new value: $qval{$obj}\n" if $debug_write_ref; | ||
| 1560 | |||
| 1561 | my $targetid; | ||
| 1562 | my $targetkey; | ||
| 1563 | my $targettiedmem; | ||
| 1564 | |||
| 1565 | if (ref($$obj) && $addr == refaddr($$obj)) { | ||
| 1566 | # reference to self... | ||
| 1567 | ($targetid, $targetkey) = ($nopkey, $obj); | ||
| 1568 | } elsif (exists $new_memory2key->{$addr} && $new_memory2key->{$addr}[0] != $id) { | ||
| 1569 | ($targetid, $targetkey) = @{$new_memory2key->{$addr}}; | ||
| 1570 | print "\$*$id WRITE_REF new_memory2key($qval{$obj}) says: *$targetid/$qval{$targetkey} \n" if $debug_write_ref || $debug_memory; | ||
| 1571 | } elsif (exists $memory2key->{$addr} && $memory2key->{$addr}[0] != $id) { | ||
| 1572 | ($targetid, $targetkey) = @{$memory2key->{$addr}}; | ||
| 1573 | no warnings; | ||
| 1574 | print "\$*$id WRITE_REF memory2key($qval{$obj}) says: *$targetid/$qval{$targetkey} \n" if $debug_write_ref || $debug_memory; | ||
| 1575 | } elsif (exists $new_memory->{$addr} && $memory->{$addr} != $id) { | ||
| 1576 | $targetid = $nopkey; | ||
| 1577 | $targetkey = $$obj; | ||
| 1578 | no warnings; | ||
| 1579 | print "\$*$id WRITE_REF new_memory($qval{$obj}) says: *$targetid/$qval{$targetkey} \n" if $debug_write_ref || $debug_memory; | ||
| 1580 | } elsif (exists $memory->{$addr} && $memory->{$addr} != $id) { | ||
| 1581 | $targetid = $nopkey; | ||
| 1582 | $targetkey = $$obj; | ||
| 1583 | print "\$*$id WRITE_REF memory($qval{$obj}) says: *$targetid/$qval{$targetkey} \n" if $debug_write_ref || $debug_memory; | ||
| 1584 | } elsif (($targettiedmem, $targetkey) = tied_hash_reference($obj)) { | ||
| 1585 | $targetid = $memory->{$targettiedmem} || $new_memory->{$targettiedmem}; | ||
| 1586 | print "\$*$id WRITE_REF tied hash reference: $targetid/$qval{$targetkey}\n" if $debug_write_ref; | ||
| 1587 | no warnings; | ||
| 1588 | if (! $targetid) { | ||
| 1589 | use warnings; | ||
| 1590 | print "\$*$id WRITE_REF was disassociated, now *$nopkey/$qval{$$obj}\n" if $debug_write_ref; | ||
| 1591 | $targetkey = $$obj; | ||
| 1592 | $targetid = $nopkey; | ||
| 1593 | $oops->new_memory2key($obj, $id, $nopkey); | ||
| 1594 | print "NEWMEMORY2KEY ".$addr." = \%*$id/$nopkey - in write_ref\n" if $debug_memory; | ||
| 1595 | } elsif ($$obj ne $oops->{cache}{$targetid}{$targetkey}) { | ||
| 1596 | # | ||
| 1597 | # Must have been delete'd in the meantime and become | ||
| 1598 | # disassoicated from the cache. Unfortunantly the refaddr() | ||
| 1599 | # for such references is unique so we must guess if they | ||
| 1600 | # should be rejoined. | ||
| 1601 | # | ||
| 1602 | # Ugh! | ||
| 1603 | # | ||
| 1604 | use warnings; | ||
| 1605 | if ($targetid && exists $oops->{disassociated}{$targetid}{$targetkey}{$$obj}) { | ||
| 1606 | print "\$*$id WRITE_REF was disassociated, joining to *$oops->{disassociated}{$targetid}{$targetkey}{$$obj}/$qval{$nopkey}\n" if $debug_write_ref; | ||
| 1607 | $targetid = $oops->{disassociated}{$targetid}{$targetkey}{$$obj}; | ||
| 1608 | $targetkey = $nopkey; | ||
| 1609 | } else { | ||
| 1610 | $oops->{disassociated}{$targetid}{$targetkey}{$$obj} = $id; | ||
| 1611 | print "\$*$id WRITE_REF was disassociated, now *$nopkey/$qval{$$obj}\n" if $debug_write_ref; | ||
| 1612 | $targetkey = $$obj; | ||
| 1613 | $targetid = $nopkey; | ||
| 1614 | $oops->new_memory2key($obj, $id, $nopkey); | ||
| 1615 | print "NEWMEMORY2KEY ".$addr." = \%*$id/$nopkey - in write_ref\n" if $debug_memory; | ||
| 1616 | } | ||
| 1617 | } | ||
| 1618 | } else { | ||
| 1619 | # must be a reference to an independent scalar or object | ||
| 1620 | $targetid = $nopkey; | ||
| 1621 | $targetkey = $$obj; | ||
| 1622 | print "\$*$id WRITE_REF independent, now *$targetid/$qval{$targetkey}\n" if $debug_write_ref; | ||
| 1623 | $oops->new_memory2key($obj, $id, $nopkey); | ||
| 1624 | print "NEWMEMORY2KEY ".$addr." = \%*$id/$nopkey - in write_ref\n" if $debug_memory; | ||
| 1625 | } | ||
| 1626 | |||
| 1627 | if (exists $oops->{deleted}{$targetid}) { | ||
| 1628 | print "\$*$id WRITE_REF now independent, now *$nopkey/$qval{$$obj} had been ref to $targetid/$qval{$targetkey} but *$targetid was deleted\n" if $debug_write_ref; | ||
| 1629 | $targetid = $nopkey; | ||
| 1630 | $targetkey = $$obj; | ||
| 1631 | $oops->new_memory2key($obj, $id, $nopkey); | ||
| 1632 | print "NEWMEMORY2KEY ".$addr." = \%*$id/$nopkey - in write_ref\n" if $debug_memory; | ||
| 1633 | } | ||
| 1634 | |||
| 1635 | my ($oldid, $oldpkey, $oldval); | ||
| 1636 | my $ock; | ||
| 1637 | |||
| 1638 | if (exists $oldalias->{$id}) { | ||
| 1639 | ($oldid, $oldpkey) = @{$oldalias->{$id}}; | ||
| 1640 | $oldval = $oldpkey; | ||
| 1641 | print "\$*$id WRITE_REF oldalias: *$oldid/$qval{$oldpkey} = $qval{$oldval}\n" if $debug_write_ref; | ||
| 1642 | } elsif (exists $oldvalue->{$id} && exists $oldvalue->{$id}{$nopkey}) { | ||
| 1643 | $oldid = $nopkey; | ||
| 1644 | $oldpkey = $oldvalue->{$id}{$nopkey}; | ||
| 1645 | $oldval = $oldpkey; | ||
| 1646 | print "\$*$id WRITE_REF oldvalue: *$oldid/$qval{$oldpkey} = $qval{$oldval}\n" if $debug_write_ref; | ||
| 1647 | } elsif (exists $oldbig->{$id} && exists $oldbig->{$id}{$nopkey}) { | ||
| 1648 | $oldid = $nopkey; | ||
| 1649 | $oldpkey = $oldbig->{$id}{$nopkey}; | ||
| 1650 | $ock = (!ref($$obj) && defined($$obj) && length($$obj) > $bigcutoff) | ||
| 1651 | ? bigcksum($$obj) | ||
| 1652 | : undef; | ||
| 1653 | $targetkey = $ock if $ock && $oldbig->{$id}{$nopkey} eq $ock; | ||
| 1654 | print "\$*$id WRITE_REF oldbig: *$oldid/$qval{$oldpkey} = $qval{$oldval}\n" if $debug_write_ref; | ||
| 1655 | } elsif (exists $oldobject->{$id} && exists $oldobject->{$id}{$nopkey}) { | ||
| 1656 | $oldid = $nopkey; | ||
| 1657 | $oldpkey = $oldobject->{$id}{$nopkey}; | ||
| 1658 | $oldval = $oops->{cache}{$oldpkey}; | ||
| 1659 | print "\$*$id WRITE_REF oldobject: *$oldid/$qval{$oldpkey} = $qval{$oldval}\n" if $debug_write_ref; | ||
| 1660 | } else { | ||
| 1661 | print "\$*$id WRITE_REF no old value\n" if $debug_write_ref; | ||
| 1662 | $oldid = undef; | ||
| 1663 | } | ||
| 1664 | |||
| 1665 | # confess if $targetid == $id && $targetkey ne $nopkey; XXXX turn this on -- it's off for debugging | ||
| 1666 | |||
| 1667 | confess unless defined $targetid; | ||
| 1668 | print "\$*$id WRITE_REF target:$targetid/$qval{$targetkey} old:$oldid/$qval{$oldpkey}\n" if $debug_write_ref && defined $oldid; | ||
| 1669 | print "\$*$id WRITE_REF target:$targetid/$qval{$targetkey} no old\n" if $debug_write_ref && ! defined $oldid; | ||
| 1670 | |||
| 1671 | if ($targetid ne $nopkey && ! $oops->{reftarg}{$targetid}) { | ||
| 1672 | # this is presently irreversable | ||
| 1673 | $oops->{reftarg}{$targetid} = 'T'; | ||
| 1674 | $oops->{forcesave}{$targetid} = __LINE__; | ||
| 1675 | print "force save of *$targetid as its referended by *$id\n" if $debug_forcesave; | ||
| 1676 | print "*$targetid is now reference target (from $id)\n" if $debug_reftarget; | ||
| 1677 | } | ||
| 1678 | |||
| 1679 | if (defined($oldid) && $targetid eq $oldid) { | ||
| 1680 | if (defined($targetkey) ? (defined($oldpkey) ? $targetkey eq $oldpkey : 0) : ! defined($oldpkey)) { | ||
| 1681 | # unchanged | ||
| 1682 | print "\$*$id WRITE_REF no change\n" if $debug_write_ref; | ||
| 1683 | } else { | ||
| 1684 | print "\$*$id WRITE_REF CHANGE to *$targetid/$qval{$targetkey} (oldval = $qval{$oldval})\n" if $debug_write_ref; | ||
| 1685 | if (ref($oldval)) { | ||
| 1686 | $oops->update_attribute($id, $targetid, $targetkey, $ock); | ||
| 1687 | } else { | ||
| 1688 | $oops->update_attribute($id, $targetid, $targetkey, $ock, $oldval); | ||
| 1689 | } | ||
| 1690 | delete $oops->{oldalias}{$id}; | ||
| 1691 | if ($targetid ne $nopkey) { | ||
| 1692 | $oops->{oldalias}{$id} = [ $targetid, $targetkey ]; | ||
| 1693 | $oops->{aliasdest}{$targetid}{$id} = $targetkey; | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | } | ||
| 1697 | } else { | ||
| 1698 | # | ||
| 1699 | # Normal references to other objects are: | ||
| 1700 | # | ||
| 1701 | # id=id key=other_obj_id value=other_obj_key | ||
| 1702 | # | ||
| 1703 | # References to unconnected scalars are: | ||
| 1704 | # | ||
| 1705 | # id=id key=nopkey value=the_scalar_value | ||
| 1706 | # | ||
| 1707 | # For references to unconnected scalars, we need an extra tuple | ||
| 1708 | # so that translated sql queries work right: | ||
| 1709 | # | ||
| 1710 | # id=id key=id value=nopkey | ||
| 1711 | # | ||
| 1712 | |||
| 1713 | print "\$*$id WRITE_REF DELETE $qval{$oldid}\n" if $debug_write_ref && defined $oldid; | ||
| 1714 | |||
| 1715 | $oops->delete_attribute($id, $oldid) | ||
| 1716 | if defined $oldid; | ||
| 1717 | |||
| 1718 | print "\$*$id WRITE_REF DELETE $qval{$id}\n" if $debug_write_ref && defined($oldid) && $oldid eq $id; | ||
| 1719 | |||
| 1720 | $oops->delete_attribute($id, $id) | ||
| 1721 | if defined($oldid) && $oldid eq $nopkey; | ||
| 1722 | |||
| 1723 | print "\$*$id WRITE_REF INSERT $qval{$targetid}/$qval{$targetkey}\n" if $debug_write_ref; | ||
| 1724 | |||
| 1725 | $oops->insert_attribute($id, $targetid, { ref => $targetid }, $targetkey); | ||
| 1726 | |||
| 1727 | print "\$*$id WRITE_REF INSERT $qval{$id}/$qval{$nopkey}\n" if $debug_write_ref && $targetid eq $nopkey; | ||
| 1728 | |||
| 1729 | $oops->insert_attribute($id, $id, undef, $nopkey) | ||
| 1730 | if $targetid eq $nopkey; | ||
| 1731 | |||
| 1732 | delete $oops->{oldalias}{$id}; | ||
| 1733 | if ($targetid ne $nopkey) { | ||
| 1734 | $oops->{oldalias}{$id} = [ $targetid, $targetkey ]; | ||
| 1735 | $oops->{aliasdest}{$targetid}{$id} = $targetkey; | ||
| 1736 | } | ||
| 1737 | } | ||
| 1738 | |||
| 1739 | # | ||
| 1740 | # With references to an ARRAY element, we must ensure that the reference is in | ||
| 1741 | # the same loadgroup as the ARRAY because the element may move within the ARRAY | ||
| 1742 | # and that requires re-saving the reference which will only happen if the reference | ||
| 1743 | # happens to be in memory. | ||
| 1744 | # | ||
| 1745 | # With references to a common scalar, we must do the same. | ||
| 1746 | # | ||
| 1747 | if ($targetid ne $nopkey && reftype($oops->{cache}{$targetid}) ne 'HASH') { | ||
| 1748 | if ($oops->{loadgroup}{$targetid} eq $oops->{loadgroup}{$id} && ! exists $oops->{refmore}{$targetid} && ! exists $oops->{refless}{$targetid}) { | ||
| 1749 | # great | ||
| 1750 | } else { | ||
| 1751 | $oops->{forcesave}{$id} = __LINE__; | ||
| 1752 | $oops->{loadgrouplock}{$id} = $targetid; | ||
| 1753 | print "force \$*$id group to be loged to *$targetid\n" if $debug_load_group || $debug_forcesave; | ||
| 1754 | } | ||
| 1755 | } | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | # | ||
| 1759 | # $oops->update_attribute($id, $pkey, $new_value, [ $new_checksum_value ], [ $old_value ]) | ||
| 1760 | # | ||
| 1761 | sub update_attribute | ||
| 1762 | { | ||
| 1763 | print Carp::longmess("DEBUG: update_attribute(@_) called") if 0; # debug | ||
| 1764 | my $oops = shift; | ||
| 1765 | my $id = shift; | ||
| 1766 | my $pkey = shift; | ||
| 1767 | my $oldover = exists $oops->{oldbig}{$id} && exists $oops->{oldbig}{$id}{$pkey}; | ||
| 1768 | my $oldobject = exists $oops->{oldobject}{$id} && exists $oops->{oldobject}{$id}{$pkey}; | ||
| 1769 | my $atval; | ||
| 1770 | my $newover; | ||
| 1771 | my $ptype = '0'; | ||
| 1772 | my $overcksum = $_[1]; | ||
| 1773 | my $oldvalue = $_[2]; | ||
| 1774 | my %change_refs; | ||
| 1775 | #my $newptype = $_[3]; | ||
| 1776 | if (defined($_[0]) && length($_[0]) > $bigcutoff) { | ||
| 1777 | $atval = $overcksum || bigcksum($_[0]); | ||
| 1778 | $newover = 1; | ||
| 1779 | $ptype = 'B'; | ||
| 1780 | } elsif (ref($_[0])) { | ||
| 1781 | $atval = $oops->get_object_id($_[0], $id); | ||
| 1782 | $change_refs{$atval} += 1; | ||
| 1783 | print "*$id/$pkey update_attribute1, add CURRENT ref to *$atval (+1)\n" if $debug_refcount; | ||
| 1784 | $ptype = 'R'; | ||
| 1785 | } else { | ||
| 1786 | $atval = $_[0]; | ||
| 1787 | } | ||
| 1788 | if (ref($_[2])) { | ||
| 1789 | # old value was a reference | ||
| 1790 | my $oldid = $oops->get_object_id($_[2], $id); | ||
| 1791 | $change_refs{$oldid} -= 1; | ||
| 1792 | print "OLDOBJECT *$id/$pkey update_attribute2, oldobject = undef (was $oops->{oldobject}{$id}{$pkey})\n" if $debug_oldobject; | ||
| 1793 | delete $oops->{oldobject}{$id}{$pkey}; | ||
| 1794 | print "*$id/$pkey update_attribute2, removed OLD ref to *$oldid (-1)\n" if $debug_refcount; | ||
| 1795 | } elsif ($oldobject) { | ||
| 1796 | my $oldid = $oops->{oldobject}{$id}{$pkey}; | ||
| 1797 | $change_refs{$oldid} -= 1; | ||
| 1798 | print "OLDOBJECT *$id/$pkey update_attribute3, oldobject = undef (was $oops->{oldobject}{$id}{$pkey})\n" if $debug_oldobject; | ||
| 1799 | delete $oops->{oldobject}{$id}{$pkey}; | ||
| 1800 | print "*$id/$pkey update_attribute3, removed OLD ref to *$oldid (-1)\n" if $debug_refcount; | ||
| 1801 | } | ||
| 1802 | if (ref($_[0])) { | ||
| 1803 | $oops->{oldobject}{$id}{$pkey} = $atval; | ||
| 1804 | print "OLDOBJECT *$id/$pkey update_attribute4 = *$atval\n" if $debug_oldobject; | ||
| 1805 | } | ||
| 1806 | |||
| 1807 | print "*$id/$pkey - now: $qval{$atval} ($ptype)\n" if $debug_save_attributes; | ||
| 1808 | |||
| 1809 | my $sym; | ||
| 1810 | $sym = $typesymbol{reftype($oops->{cache}{$id})} if $debug_writes; | ||
| 1811 | print "$sym$id/$pkey update_attribute $qval{$atval} (ptype $ptype)\n" if $debug_writes; | ||
| 1812 | |||
| 1813 | $atval = '0' if defined($atval) && $atval eq '0'; # make sure it's a string | ||
| 1814 | |||
| 1815 | my $updateattributeQ = $oops->query('updateattribute', execute => [ $atval, $ptype, $id, $pkey ]); | ||
| 1816 | if ($oldover && $newover) { | ||
| 1817 | $oops->update_big($id, $pkey, $_[0]); | ||
| 1818 | $oops->{oldbig}{$id}{$pkey} = $atval; | ||
| 1819 | delete $oops->{oldvalue}{$id}{$pkey} | ||
| 1820 | if exists $oops->{oldvalue}{$id} && exists $oops->{oldvalue}{$id}{$pkey}; | ||
| 1821 | } elsif ($oldover) { | ||
| 1822 | my $deletebigQ = $oops->query('deletebig', execute => [ $id, $pkey ]); | ||
| 1823 | $oops->{oldvalue}{$id}{$pkey} = $atval; | ||
| 1824 | } elsif ($newover) { | ||
| 1825 | $oops->save_big($id, $pkey, $_[0]); | ||
| 1826 | $oops->{oldbig}{$id}{$pkey} = $atval; | ||
| 1827 | delete $oops->{oldvalue}{$id}{$pkey} | ||
| 1828 | if exists $oops->{oldvalue}{$id} && exists $oops->{oldvalue}{$id}{$pkey}; | ||
| 1829 | } else { | ||
| 1830 | $oops->{oldvalue}{$id}{$pkey} = $atval; | ||
| 1831 | } | ||
| 1832 | $oops->{forcesave}{$id} = __LINE__ | ||
| 1833 | if $oops->{do_forcesave}; | ||
| 1834 | for my $i (keys %change_refs) { | ||
| 1835 | print "*$id/$pkey update_attribute refchange summary for *$i: $qplusminus{$change_refs{$i}}\n" if $debug_refcount; | ||
| 1836 | next unless $change_refs{$i}; | ||
| 1837 | $oops->refchange($id, $i, $change_refs{$i}); | ||
| 1838 | } | ||
| 1839 | assertions($oops); | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | sub prepare_insert_attribute | ||
| 1843 | { | ||
| 1844 | my $oops = shift; | ||
| 1845 | my $id = shift; | ||
| 1846 | my $pkey = shift; | ||
| 1847 | my $special_handling = shift; | ||
| 1848 | my $atval; | ||
| 1849 | my $ptype = '0'; | ||
| 1850 | if (ref($_[0])) { | ||
| 1851 | $atval = $oops->get_object_id($_[0], $id); | ||
| 1852 | print "*$id/$pkey is a reference to *$atval (preparing to save)\n" if $debug_save_attr_arraylen || $debug_write_ref; | ||
| 1853 | $ptype = 'R'; | ||
| 1854 | $oops->refchange($id,$atval,1); | ||
| 1855 | $oops->{oldobject}{$id}{$pkey} = $atval; | ||
| 1856 | print "OLDOBJECT *$id/$pkey prepare_insert_attribute = *$atval\n" if $debug_oldobject; | ||
| 1857 | print "in prepare_insert_attribute, ref to *$atval from *$id/$pkey is new (+1)\n" if $debug_refcount; | ||
| 1858 | } elsif (defined($_[0]) && length($_[0]) > $bigcutoff && ! ($special_handling && $special_handling->{ref} && $pkey ne $nopkey)) { | ||
| 1859 | $atval = $_[1] || bigcksum($_[0]); | ||
| 1860 | $ptype = 'B'; | ||
| 1861 | $oops->{oldbig}{$id}{$pkey} = $atval; | ||
| 1862 | print "*$id/$pkey is a big value\n" if $debug_save_attr_arraylen; | ||
| 1863 | $oops->save_big($id, $pkey, $_[0]); | ||
| 1864 | } else { | ||
| 1865 | $atval = $_[0]; | ||
| 1866 | $oops->{oldvalue}{$id}{$pkey} = $atval; | ||
| 1867 | print "*$id/$pkey is a normal value $qval{$atval}\n" if $debug_save_attr_arraylen; | ||
| 1868 | } | ||
| 1869 | $atval = '0' if defined($atval) && $atval eq '0'; # make sure it's a string | ||
| 1870 | assertions($oops); | ||
| 1871 | print "*$id/$pkey - new: $qval{$atval}\n" if $debug_save_attributes; | ||
| 1872 | return ($atval, $ptype); | ||
| 1873 | } | ||
| 1874 | |||
| 1875 | # | ||
| 1876 | # We don't copy all of @_ because it may have BLOBs... | ||
| 1877 | # | ||
| 1878 | sub insert_attribute | ||
| 1879 | { | ||
| 1880 | my ($oops, $id, $pkey, $special_handling) = @_; | ||
| 1881 | print Carp::longmess("DEBUG: insert_attribute(@_) called") if $debug_save_attr_context; | ||
| 1882 | my ($atval, $ptype) = $oops->prepare_insert_attribute($id, $pkey, $special_handling, $_[4], $_[5]); | ||
| 1883 | $atval = undef unless defined $atval; | ||
| 1884 | my $sym = $typesymbol{reftype($oops->{cache}{$id})} if $debug_writes; | ||
| 1885 | print "$sym$id/$pkey insert_attribute $qval{$atval} (ptype $ptype)\n" if $debug_writes; | ||
| 1886 | $atval = '' if defined($atval) && $atval eq ''; # why does this line help?!? | ||
| 1887 | my $saveattributeQ = $oops->query('saveattribute', execute => [ $id, $pkey, $atval, $ptype ]); | ||
| 1888 | $oops->{forcesave}{$id} = __LINE__ | ||
| 1889 | if $oops->{do_forcesave}; | ||
| 1890 | no warnings; | ||
| 1891 | print "*$id/$qval{$pkey} - '$atval'/$ptype inserted\n" if $debug_save_attributes; | ||
| 1892 | assertions($oops); | ||
| 1893 | } | ||
| 1894 | |||
| 1895 | sub delete_attribute | ||
| 1896 | { | ||
| 1897 | my $oops = shift; | ||
| 1898 | my $id = shift; | ||
| 1899 | my $pkey = shift; | ||
| 1900 | my $sym; | ||
| 1901 | $sym = $typesymbol{reftype($oops->{cache}{$id})} if $debug_writes; | ||
| 1902 | print "$sym$id/$pkey delete_attribute\n" if $debug_writes; | ||
| 1903 | my $oldvalue = shift; | ||
| 1904 | my $oldover = exists $oops->{oldbig}{$id} && exists $oops->{oldbig}{$id}{$pkey}; | ||
| 1905 | $pkey = '0' if $pkey eq '0'; # make sure it's a string | ||
| 1906 | my $deleteattributeQ = $oops->query('deleteattribute', execute => [ $id, $pkey ]); | ||
| 1907 | if (ref($oldvalue)) { | ||
| 1908 | my $oldid = $oops->get_object_id($oldvalue, $id); | ||
| 1909 | $oops->refchange($id,$oldid,-1); | ||
| 1910 | print "OLDOBJECT *$id/$pkey delete_attribute, = undef (was $oops->{oldobject}{$id}{$pkey})\n" if $debug_oldobject; | ||
| 1911 | delete $oops->{oldobject}{$id}{$pkey}; | ||
| 1912 | print "in delete_attribute, ref to *$oldid from *$id/$pkey is invalid (-1)\n" if $debug_refcount; | ||
| 1913 | } elsif (exists $oops->{oldobject}{$id} && exists $oops->{oldobject}{$id}{$pkey}) { | ||
| 1914 | $oops->refchange($id, $oops->{oldobject}{$id}{$pkey}, -1); | ||
| 1915 | print "in delete_attribute, ref to *$oops->{oldobject}{$id}{$pkey} from *$id/$pkey is dropped (-1)\n" if $debug_refcount; | ||
| 1916 | print "OLDOBJECT *$id/$pkey delete_attribute2, = undef (was $oops->{oldobject}{$id}{$pkey})\n" if $debug_oldobject; | ||
| 1917 | delete $oops->{oldobject}{$id}{$pkey}; | ||
| 1918 | } | ||
| 1919 | if ($oldover) { | ||
| 1920 | my $deletebigQ = $oops->query('deletebig', execute => [ $id, $pkey ]); | ||
| 1921 | delete $oops->{oldbig}{$id}{$pkey}; | ||
| 1922 | } | ||
| 1923 | print "*$id/$pkey - delete'\n" if $debug_save_attributes; | ||
| 1924 | delete $oops->{oldvalue}{$id}{$pkey} | ||
| 1925 | if exists $oops->{oldvalue}{$id} && exists $oops->{oldvalue}{$id}{$pkey}; | ||
| 1926 | print "*$id/$pkey delete_attribute3, oldobject *$id/$pkey = undef (was $oops->{oldobject}{$id}{$pkey})\n" if $debug_oldobject && exists $oops->{oldobject}{$id} && exists $oops->{oldobject}{$id}{$pkey}; | ||
| 1927 | delete $oops->{oldobject}{$id}{$pkey} | ||
| 1928 | if exists $oops->{oldobject}{$id} && exists $oops->{oldobject}{$id}{$pkey}; | ||
| 1929 | $oops->{forcesave}{$id} = __LINE__ | ||
| 1930 | if $oops->{do_forcesave}; | ||
| 1931 | assertions($oops); | ||
| 1932 | } | ||
| 1933 | |||
| 1934 | sub get_object_id | ||
| 1935 | { | ||
| 1936 | my ($oops, $obj, $sponsoring_id) = @_; | ||
| 1937 | confess unless ref $oops; | ||
| 1938 | confess unless blessed $oops; | ||
| 1939 | confess unless $oops->isa('OOPS'); | ||
| 1940 | my $bt = reftype($obj); | ||
| 1941 | my $mem = refaddr($obj); | ||
| 1942 | my $found = $oops->{memory}{$mem}; | ||
| 1943 | print "lookup MEMORY($qval{$obj}) = $mem, memory{$mem} = $qval{$found}\n" if $debug_memory; | ||
| 1944 | return $found if $found; | ||
| 1945 | |||
| 1946 | print Carp::longmess("DEBUG: get_object_id($obj) called ") if $debug_getobid_context; | ||
| 1947 | |||
| 1948 | my $gcgen = $sponsoring_id | ||
| 1949 | ? $oops->{gcgeneration}{$sponsoring_id} | ||
| 1950 | : 0; | ||
| 1951 | |||
| 1952 | my $id = $oops->{dbo}->allocate_id(); | ||
| 1953 | |||
| 1954 | # | ||
| 1955 | # We save the object so that we don't have to worry about INSERT vs UPDATE | ||
| 1956 | # in the rest of the code. Perhaps we could save the UPDATE by putting | ||
| 1957 | # in better values now. | ||
| 1958 | # | ||
| 1959 | my $saveobjectQ = $oops->query('saveobject'); | ||
| 1960 | $saveobjectQ->execute($id, $id, "will be".ref($obj), '?', '?', '?', 0, -9999, $gcgen) || confess $saveobjectQ->errstr; | ||
| 1961 | |||
| 1962 | $id = $oops->{dbo}->post_new_object($id); | ||
| 1963 | |||
| 1964 | $oops->memory($obj, $id); | ||
| 1965 | print "MEMORY $mem := $id in get_object_id\n" if $debug_memory; | ||
| 1966 | $oops->{cache}{$id} = $obj; | ||
| 1967 | print "*$id get_object_id cache := $qval{$obj}\n" if $debug_cache; | ||
| 1968 | $oops->{class}{$id} = ref $obj; | ||
| 1969 | $oops->{virtual}{$id} = '0'; | ||
| 1970 | $oops->{arraylen}{$id} = 0; | ||
| 1971 | $oops->{reftarg}{$id} = '0'; | ||
| 1972 | $oops->{loadgroup}{$id} = $id; | ||
| 1973 | $oops->{groupset}{$id}{$id} = 1; | ||
| 1974 | $oops->{gcgeneration}{$id} = $gcgen; | ||
| 1975 | printf "NEW *%d, gcgen = %d (from %s)\n", $id, $gcgen, ($sponsoring_id ? $sponsoring_id : "none") if $debug_gcgeneration; | ||
| 1976 | |||
| 1977 | # | ||
| 1978 | # Together these will force a save. We don't use forcesave because | ||
| 1979 | # forcesave is cleared at the beginning of save(). | ||
| 1980 | # | ||
| 1981 | $oops->{refcount}{$id} = -1; | ||
| 1982 | $oops->{refmore}{$id} = 1; | ||
| 1983 | |||
| 1984 | print "in get_object_id, *$id is new: count=-1, change=+1 (=0)\n" if $debug_refcount; | ||
| 1985 | print "$typesymbol{$bt}$id created as new object: $obj\n" if $debug_writes || $debug_write_object; | ||
| 1986 | $oops->{otype}{$id} = $perltype2otype{$bt} || confess "bt='$bt',obj=$obj"; | ||
| 1987 | my $x = $obj->isa('OOPS::Aware') | ||
| 1988 | unless $typesymbol{ref($obj)}; | ||
| 1989 | $obj->object_id_assigned($id) | ||
| 1990 | if $x; | ||
| 1991 | #print "get_ob_id -> write $id\n"; | ||
| 1992 | |||
| 1993 | if ($oops->{insave}) { | ||
| 1994 | $oops->write_object($id, $sponsoring_id); | ||
| 1995 | } else { | ||
| 1996 | $oops->{objtouched}{$id} = 'new object'; | ||
| 1997 | } | ||
| 1998 | |||
| 1999 | $oops->{loaded}++; | ||
| 2000 | assertions($oops); | ||
| 2001 | return $id; | ||
| 2002 | } | ||
| 2003 | |||
| 2004 | sub refchange | ||
| 2005 | { | ||
| 2006 | my ($oops, $from, $to, $change) = @_; | ||
| 2007 | confess unless $to; # debug | ||
| 2008 | confess unless defined $change; # debug | ||
| 2009 | if ($change >= 0) { | ||
| 2010 | $oops->{refmore}{$to} += $change; | ||
| 2011 | } else { | ||
| 2012 | $oops->{refless}{$to} += $change; | ||
| 2013 | } | ||
| 2014 | confess unless $from; | ||
| 2015 | return if $change <= 0; | ||
| 2016 | my $gc = $oops->{gcgeneration}; | ||
| 2017 | if (! $gc->{$to}) { | ||
| 2018 | # | ||
| 2019 | # New object, don't worry about it. | ||
| 2020 | # | ||
| 2021 | $gc->{$to} = $gc->{$from}; | ||
| 2022 | |||
| 2023 | my $fg = $gc->{$from} ? $gc->{$from} : "none"; # debug | ||
| 2024 | print "*$to new gcgen $fg from $from\n" if $debug_gcgeneration; | ||
| 2025 | } elsif ($gc->{$from} && $gc->{$from} > $gc->{$to}) { | ||
| 2026 | # | ||
| 2027 | # We're in the middle of a GC pass. We're adding | ||
| 2028 | # a link from something that has already been swept | ||
| 2029 | # to something that has not been swept. | ||
| 2030 | # | ||
| 2031 | # Add it to the GC special-handling table. | ||
| 2032 | # | ||
| 2033 | print "GC: special handling from $from($gc->{$from}) -> $to($gc->{$to})... adding $to to special table\n" if $debug_gcgeneration; | ||
| 2034 | |||
| 2035 | my $checkQ = $oops->query('loadpkey', execute => [ $gc_overflow_id, $to ]) || confess $oops->errstr; | ||
| 2036 | unless(my ($junk1, $junk2) = $checkQ->fetchrow_array()) { | ||
| 2037 | $checkQ->finish(); | ||
| 2038 | my $setQ = $oops->query('saveattribute'); | ||
| 2039 | $setQ->execute(4, $to, '', '0') || confess $setQ->errstr; | ||
| 2040 | $oops->{gcspillcount}++; | ||
| 2041 | } | ||
| 2042 | } | ||
| 2043 | } | ||
| 2044 | |||
| 2045 | sub delete_object | ||
| 2046 | { | ||
| 2047 | my ($oops, $id) = @_; | ||
| 2048 | print "*$id begin delete\n" if $debug_cache; | ||
| 2049 | $oops->predelete_object($id); | ||
| 2050 | $oops->query('postdelete1', execute => $id); | ||
| 2051 | $oops->query('postdelete2', execute => $id); | ||
| 2052 | $oops->{deleted}{$id} = 1; | ||
| 2053 | print "*$id has been deleted\n" if $debug_cache; | ||
| 2054 | assertions($oops); | ||
| 2055 | } | ||
| 2056 | |||
| 2057 | # | ||
| 2058 | # Object may or may not be loaded. | ||
| 2059 | # Object row is not cleared. | ||
| 2060 | # | ||
| 2061 | sub predelete_object | ||
| 2062 | { | ||
| 2063 | my ($oops, $id) = @_; | ||
| 2064 | print Carp::longmess("DEBUG: predelete_object(@_) called") if 0; # debug | ||
| 2065 | unless (defined $oops->{reftarg}{$id}) { | ||
| 2066 | my $objectinfoQ = $oops->query('objectinfo', execute => $id); | ||
| 2067 | my (undef, $loadgroup, $class, $otype, $virtual, $reftarg, undef, $arraylen, $refs, $cntr, $gcgen) = $objectinfoQ->fetchrow_array(); | ||
| 2068 | confess unless $otype; | ||
| 2069 | $objectinfoQ->finish(); | ||
| 2070 | if ($oops->{reftarg}{$id} = $reftarg) { | ||
| 2071 | $oops->load_object($id); | ||
| 2072 | } | ||
| 2073 | } | ||
| 2074 | if ($oops->{reftarg}{$id}) { | ||
| 2075 | # | ||
| 2076 | # This can cause calls to write_ref later in the commit | ||
| 2077 | # process than normal -- during the reference counting stage | ||
| 2078 | # | ||
| 2079 | if ($oops->{otype}{$id} eq 'H') { | ||
| 2080 | %{$oops->{cache}{$id}} = (); | ||
| 2081 | } elsif ($oops->{otype}{$id} eq 'A') { | ||
| 2082 | @{$oops->{cache}{$id}} = (); | ||
| 2083 | } elsif ($oops->{otype}{$id} eq 'S') { | ||
| 2084 | # nada | ||
| 2085 | } else { | ||
| 2086 | confess; | ||
| 2087 | } | ||
| 2088 | print "*$id searching for references to self\n" if $debug_refalias || $debug_reftarget; | ||
| 2089 | my $reftargobjectQ = $oops->query('reftargobject', execute => $id); | ||
| 2090 | my $refid; | ||
| 2091 | my %done; | ||
| 2092 | while (($refid) = $reftargobjectQ->fetchrow_array()) { | ||
| 2093 | print "\%$id loading reference *$refid\n" if ($debug_refalias || $debug_reftarget) && ! exists $oops->{cache}{$refid}; | ||
| 2094 | unless (exists $oops->{cache}{$refid}) { | ||
| 2095 | $oops->load_object($refid); | ||
| 2096 | my $x = $oops->{cache}{$refid}; # force it to untie | ||
| 2097 | } | ||
| 2098 | print "*$id writing *$refid again\n" if $debug_reftarget || $debug_refalias; | ||
| 2099 | push(@{$oops->{refstowrite}}, $refid); | ||
| 2100 | $done{$refid} = 1; | ||
| 2101 | } | ||
| 2102 | if ($oops->{aliasdest}{$id}) { | ||
| 2103 | for $refid (keys %{$oops->{aliasdest}{$id}}) { | ||
| 2104 | push(@{$oops->{refstowrite}}, $refid) | ||
| 2105 | unless $done{$refid}; | ||
| 2106 | } | ||
| 2107 | } | ||
| 2108 | } | ||
| 2109 | $oops->query('predelete1', execute => $id); | ||
| 2110 | $oops->query('predelete2', execute => $id); | ||
| 2111 | my $objectreflistQ = $oops->query('objectreflist', execute => $id); | ||
| 2112 | my $objid; | ||
| 2113 | while (($objid) = $objectreflistQ->fetchrow_array) { | ||
| 2114 | $oops->refchange($id, $objid, -1); | ||
| 2115 | print "in predelete_object, $oops->{otype}{$id}*$id being deleted, no longer references $oops->{otype}{$objid}*$objid (-1)\n" if $debug_refcount; | ||
| 2116 | } | ||
| 2117 | assertions($oops); | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | sub load_big | ||
| 2121 | { | ||
| 2122 | my ($oops, $id, $pkey) = @_; | ||
| 2123 | my $bigloadQ = $oops->query('bigload', execute => [ $id, $pkey ]); | ||
| 2124 | print STDERR "BIGLOAD $id, '$pkey'\n" if $debug_bigstuff; | ||
| 2125 | my ($val) = $bigloadQ->fetchrow_array(); | ||
| 2126 | $bigloadQ->finish(); | ||
| 2127 | confess "null big *$id/'$pkey'" if ! defined($val) || $val eq ''; | ||
| 2128 | assertions($oops); | ||
| 2129 | return $val; | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | sub save_big | ||
| 2133 | { | ||
| 2134 | my $oops = shift; | ||
| 2135 | my $id = shift; | ||
| 2136 | my $pkey = shift; | ||
| 2137 | print STDERR "BIGSAVE $id, '$pkey'\n" if $debug_bigstuff; | ||
| 2138 | my $savebigQ = $oops->query('savebig'); | ||
| 2139 | $savebigQ->execute($id, $pkey, $_[0]) || confess; | ||
| 2140 | } | ||
| 2141 | |||
| 2142 | sub update_big | ||
| 2143 | { | ||
| 2144 | my $oops = shift; | ||
| 2145 | my $id = shift; | ||
| 2146 | my $pkey = shift; | ||
| 2147 | print STDERR "BIGUPDATE $id, '$pkey'\n" if $debug_bigstuff; | ||
| 2148 | my $updatebigQ = $oops->query('updatebig'); | ||
| 2149 | $updatebigQ->execute($_[0], $id, $pkey) || confess $updatebigQ->errstr; | ||
| 2150 | } | ||
| 2151 | |||
| 2152 | sub workaround27555 | ||
| 2153 | { | ||
| 2154 | my $oops = shift; | ||
| 2155 | print Carp::longmess("DEBUG: workaround27555 called") if $debug_27555_context; | ||
| 2156 | my ($tiedaddr, $key) = tied_hash_reference($_[0]); | ||
| 2157 | print "workaround27555($qaddr{\$_[0]}) no tied addr\n" if $debug_27555 && ! $tiedaddr; | ||
| 2158 | return $_[0] unless $tiedaddr; | ||
| 2159 | my $id = $oops->{memory}{$tiedaddr} || $oops->{new_memory}{$tiedaddr}; | ||
| 2160 | print "workaround27555($qaddr{\$_[0]}) addr $tiedaddr does not translate to id (key=$key)\n" if $debug_27555 && ! $id; | ||
| 2161 | return $_[0] unless $id; | ||
| 2162 | my $tied = tied %{$oops->{cache}{$id}}; | ||
| 2163 | confess unless $tied; | ||
| 2164 | $_[0] = $tied->GETREF($key); | ||
| 2165 | print "workaround27555($qaddr{\$_[0]}) references %*$id/'$key - replaced with GETREF\n" if $debug_27555; | ||
| 2166 | return $_[0]; | ||
| 2167 | } | ||
| 2168 | |||
| 2169 | # | ||
| 2170 | # The point of this is simply to prevent the reuse of refaddr()s. | ||
| 2171 | # This has a bad side effect: if nothing else already prevented it, | ||
| 2172 | # this will prevent DESTROY from being called. | ||
| 2173 | # | ||
| 2174 | sub setmem | ||
| 2175 | { | ||
| 2176 | my $oops = shift; | ||
| 2177 | my $mem = shift; | ||
| 2178 | my $a = refaddr($_[0]); | ||
| 2179 | if ($_[1]) { | ||
| 2180 | print "set \U$mem\E $qval{$_[0]} := $qmemval{$_[1]} at $caller{2}\n" if $debug_memory2; | ||
| 2181 | $oops->{memcount}{$a}++ | ||
| 2182 | unless exists $oops->{$mem}{$a}; | ||
| 2183 | $oops->{$mem}{$a} = $_[1]; | ||
| 2184 | $oops->{memrefs}{$a} = \$_[0] | ||
| 2185 | unless $_[1] == 1 || $a == refaddr($oops); | ||
| 2186 | $oops->{memsetdebug}{$_[1]} = (caller(2))[2]; | ||
| 2187 | } else { | ||
| 2188 | print "set \U$mem\E $qval{$_[0]} := undef at $caller{2}\n" if $debug_memory2; | ||
| 2189 | $oops->{memcount}{$a}-- | ||
| 2190 | if exists $oops->{$mem}{$a}; | ||
| 2191 | delete $oops->{$mem}{$a}; | ||
| 2192 | unless ($oops->{memcount}{$a}) { | ||
| 2193 | delete $oops->{memrefs}{$a}; | ||
| 2194 | } | ||
| 2195 | } | ||
| 2196 | } | ||
| 2197 | |||
| 2198 | sub memory | ||
| 2199 | { | ||
| 2200 | my $oops = shift; | ||
| 2201 | $oops->setmem('memory', @_); | ||
| 2202 | } | ||
| 2203 | sub new_memory | ||
| 2204 | { | ||
| 2205 | my $oops = shift; | ||
| 2206 | $oops->setmem('new_memory', @_); | ||
| 2207 | } | ||
| 2208 | #my ($oops, $ref, $id, $pkey) = @_; | ||
| 2209 | sub memory2key | ||
| 2210 | { | ||
| 2211 | my $oops = shift; | ||
| 2212 | if ($_[1]) { | ||
| 2213 | $oops->setmem('memory2key', $_[0], [ $_[1], $_[2] ]); | ||
| 2214 | } else { | ||
| 2215 | $oops->setmem('memory2key', $_[0]); | ||
| 2216 | } | ||
| 2217 | } | ||
| 2218 | sub new_memory2key | ||
| 2219 | { | ||
| 2220 | my $oops = shift; | ||
| 2221 | if ($_[1]) { | ||
| 2222 | $oops->setmem('new_memory2key', $_[0], [ $_[1], $_[2] ]); | ||
| 2223 | } else { | ||
| 2224 | $oops->setmem('new_memory2key', $_[0]); | ||
| 2225 | } | ||
| 2226 | } | ||
| 2227 | |||
| 2228 | |||
| 2229 | sub END | ||
| 2230 | { | ||
| 2231 | $global_destruction = 1; | ||
| 2232 | } | ||
| 2233 | |||
| 2234 | sub DESTROY | ||
| 2235 | { | ||
| 2236 | local($main::SIG{'__DIE__'}) = \&die_from_destroy; | ||
| 2237 | print "OOPS::DESTROY called\n" if $debug_free_tied; | ||
| 2238 | my $oops = shift; | ||
| 2239 | print "# DESTROY $$'s OOPS $oops\n" if $debug_oops_instances && $oops->{dbo}->dbh; | ||
| 2240 | #print STDERR "self = $oops\n"; | ||
| 2241 | my $cache = $oops->{cache} || {}; | ||
| 2242 | for my $id (keys %$cache) { | ||
| 2243 | my $tied; | ||
| 2244 | next unless defined $cache->{$id}; | ||
| 2245 | next unless ref $cache->{$id}; | ||
| 2246 | my $t = reftype($cache->{$id}); | ||
| 2247 | if ($t eq 'HASH') { | ||
| 2248 | $tied = tied %{$cache->{$id}}; | ||
| 2249 | } elsif ($t eq 'ARRAY') { | ||
| 2250 | $tied = tied @{$cache->{$id}}; | ||
| 2251 | } elsif ($t eq 'SCALAR' || $t eq 'REF') { | ||
| 2252 | $tied = tied($cache->{$id}) || ref($cache->{$id}) ? tied ${$cache->{$id}} : undef; | ||
| 2253 | } else { | ||
| 2254 | confess "type($id) = '$t'"; | ||
| 2255 | } | ||
| 2256 | #print "istied($id) = $tied\n"; | ||
| 2257 | next unless $tied; | ||
| 2258 | next unless $tied =~ /^OOPS/; | ||
| 2259 | print "Calling *$id->destroy $qval{$tied}\n" if $debug_free_tied; | ||
| 2260 | $tied->destroy; | ||
| 2261 | } | ||
| 2262 | if ($oops->{dbo}) { | ||
| 2263 | $oops->{dbo}->disconnect(); | ||
| 2264 | } | ||
| 2265 | %$oops = (); | ||
| 2266 | $oopses--; | ||
| 2267 | assertions($oops); | ||
| 2268 | print "DESTROY OOPS $oops [$oopses]\n" if $debug_free_tied; | ||
| 2269 | delete $tiedvars{$oops} if $debug_tiedvars; | ||
| 2270 | } | ||
| 2271 | |||
| 2272 | sub assertions | ||
| 2273 | { | ||
| 2274 | my $oops = shift; | ||
| 2275 | if (0) { | ||
| 2276 | if (exists($oops->{cache}) && defined($oops->{cache})) { | ||
| 2277 | for my $id (keys %{$oops->{cache}}) { | ||
| 2278 | confess "no otype for *$id" unless exists($oops->{otype}{$id}) && defined($oops->{otype}{$id}); | ||
| 2279 | } | ||
| 2280 | } | ||
| 2281 | } | ||
| 2282 | # print "okay at ".(caller(0))[2]."\n"; | ||
| 2283 | } | ||
| 2284 | |||
| 2285 | # | ||
| 2286 | # functions | ||
| 2287 | # | ||
| 2288 | |||
| 2289 | # withing DESTROY methods, die doesn't do anything | ||
| 2290 | sub die_from_destroy | ||
| 2291 | { | ||
| 2292 | print Carp::cluck; | ||
| 2293 | kill -9, $$; | ||
| 2294 | } | ||
| 2295 | |||
| 2296 | sub bigcksum | ||
| 2297 | { | ||
| 2298 | use Digest::MD5 qw(md5_base64); | ||
| 2299 | confess if ref $_[0]; | ||
| 2300 | confess unless defined $_[0]; | ||
| 2301 | my $cksum = substr($_[0], 0, $bigcutoff-$cksumlength); | ||
| 2302 | $cksum .= "(MD5:"; | ||
| 2303 | $cksum .= md5_base64($_[0]); | ||
| 2304 | $cksum .= ")"; | ||
| 2305 | return $cksum; | ||
| 2306 | } | ||
| 2307 | |||
| 2308 | # | ||
| 2309 | # for references to tied hash keys, this will return | ||
| 2310 | # the refaddr of the tie object and the hash key | ||
| 2311 | # | ||
| 2312 | sub tied_hash_reference | ||
| 2313 | { | ||
| 2314 | my ($ref) = @_; | ||
| 2315 | local($@); | ||
| 2316 | local $SIG{'__DIE__'}; | ||
| 2317 | return eval { | ||
| 2318 | my $magic = svref_2object($ref)->MAGIC; | ||
| 2319 | $magic = $magic->MOREMAGIC | ||
| 2320 | while lc($magic->TYPE) ne 'p'; | ||
| 2321 | return (${$magic->OBJ->RV}, $magic->PTR->as_string); | ||
| 2322 | }; | ||
| 2323 | } | ||
| 2324 | |||
| 2325 | # | ||
| 2326 | # Get a slice w/o reading the entire thing. | ||
| 2327 | # | ||
| 2328 | sub walk_hash(\%@) | ||
| 2329 | { | ||
| 2330 | my $obj = shift; | ||
| 2331 | my ($stride, $key) = @_; | ||
| 2332 | die unless $stride >= 1; | ||
| 2333 | my $tied = tied(%$obj); | ||
| 2334 | if ($tied && $tied->can('WALK_HASH')) { | ||
| 2335 | return $tied->WALK_HASH(@_); | ||
| 2336 | } | ||
| 2337 | my @ret; | ||
| 2338 | for my $k (sort keys %$obj) { | ||
| 2339 | if (@_ > 1 && defined($key)) { | ||
| 2340 | next unless $k gt $key; | ||
| 2341 | } | ||
| 2342 | push(@ret, $k); | ||
| 2343 | last if @ret >= $stride; | ||
| 2344 | } | ||
| 2345 | return @ret; | ||
| 2346 | } | ||
| 2347 | |||
| 2348 | # - - - - - - - - - - - | ||
| 2349 | |||
| 2350 | { | ||
| 2351 | # | ||
| 2352 | # These can only untie themselves if they can find themselves in | ||
| 2353 | # the array! We won't search 'cause that would probably waste a | ||
| 2354 | # lot of time. | ||
| 2355 | # | ||
| 2356 | |||
| 2357 | package OOPS::InArray; | ||
| 2358 | |||
| 2359 | #sub UNTIE | ||
| 2360 | #{ | ||
| 2361 | # my $self = shift; | ||
| 2362 | # print "\@$self->{id}"."->$self->{pkey} UNTIED\n" if $debug_untie; | ||
| 2363 | #} | ||
| 2364 | |||
| 2365 | sub SAVE_SELF {1} | ||
| 2366 | sub POST_SAVE {} | ||
| 2367 | |||
| 2368 | sub destroy | ||
| 2369 | { | ||
| 2370 | my $self = shift; | ||
| 2371 | %$self = (); | ||
| 2372 | } | ||
| 2373 | |||
| 2374 | sub DESTROY | ||
| 2375 | { | ||
| 2376 | my $self = shift; | ||
| 2377 | print "DESTROY ".ref($self)." \%*$self->{id} $self\n" if $debug_free_tied || $debug_refarray; | ||
| 2378 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 2379 | } | ||
| 2380 | |||
| 2381 | sub STORE | ||
| 2382 | { | ||
| 2383 | my ($self, $pval) = @_; | ||
| 2384 | print "\@$self->{id}"."->$self->{pkey} STORE '$pval'\n" if $debug_normalarray || $debug_refarray; | ||
| 2385 | $self->{changed} = 1; | ||
| 2386 | $self->{pval} = $pval; | ||
| 2387 | no warnings; | ||
| 2388 | my $a = $self->{oops}{cache}{$self->{id}}; | ||
| 2389 | if ($#$a >= $self->{pkey} && tied($a->[$self->{pkey}]) eq $self) { | ||
| 2390 | untie $a->[$self->{pkey}]; | ||
| 2391 | } | ||
| 2392 | $self->{oops}->assertions; | ||
| 2393 | return $pval; | ||
| 2394 | } | ||
| 2395 | |||
| 2396 | sub changed | ||
| 2397 | { | ||
| 2398 | my ($self, $pkey) = @_; | ||
| 2399 | print "\@$self->{id}"."->$pkey was at $self->{pkey} and changed=$self->{changed}\n" if $debug_write_array; | ||
| 2400 | return 1 unless $pkey eq $self->{pkey}; # there's room to make this go faster | ||
| 2401 | $self->{oops}->assertions; | ||
| 2402 | return $self->{changed}; | ||
| 2403 | } | ||
| 2404 | } | ||
| 2405 | # - - - - - - - - - - - | ||
| 2406 | { | ||
| 2407 | package OOPS::ObjectInArray; | ||
| 2408 | |||
| 2409 | use Scalar::Util qw(weaken); | ||
| 2410 | use Carp qw(longmess); | ||
| 2411 | our (@ISA) = ('OOPS::InArray'); | ||
| 2412 | |||
| 2413 | sub TIESCALAR | ||
| 2414 | { | ||
| 2415 | my $pkg = shift; | ||
| 2416 | my ($id, $pkey, $objectid, $oops) = @_; | ||
| 2417 | my $self = { | ||
| 2418 | id => $id, | ||
| 2419 | pkey => $pkey, | ||
| 2420 | objectid => $objectid, | ||
| 2421 | oops => $oops | ||
| 2422 | }; | ||
| 2423 | weaken $self->{oops}; | ||
| 2424 | bless $self, $pkg; | ||
| 2425 | print "BLESSED $self at ".__LINE__."\n" if $debug_blessing; | ||
| 2426 | print "CREATE ObjectflowInArray \%$id $self\n" if $debug_free_tied || $debug_refarray; | ||
| 2427 | $tiedvars{$self} = "%$id ".longmess if $debug_tiedvars; | ||
| 2428 | $self->{oops}->assertions; | ||
| 2429 | return $self; | ||
| 2430 | } | ||
| 2431 | |||
| 2432 | sub FETCH | ||
| 2433 | { | ||
| 2434 | my ($self) = shift; | ||
| 2435 | return $self->{pval} | ||
| 2436 | if exists $self->{pval}; | ||
| 2437 | print "\@$self->{id}"."->$self->{pkey} FETCH *$self->{objectid}\n" if $debug_normalarray || $debug_refarray; | ||
| 2438 | my $oops = $self->{oops}; | ||
| 2439 | $self->{pval} = $oops->load_object($self->{objectid}); | ||
| 2440 | $oops->workaround27555($self->{pval}); | ||
| 2441 | no warnings; | ||
| 2442 | my $a = $self->{oops}{cache}{$self->{id}}; | ||
| 2443 | if ($#$a >= $self->{pkey} && tied($a->[$self->{pkey}]) eq $self) { | ||
| 2444 | untie $a->[$self->{pkey}]; | ||
| 2445 | $oops->workaround27555($a->[$self->{pkey}]); | ||
| 2446 | } | ||
| 2447 | $self->{oops}->assertions; | ||
| 2448 | print "\@$self->{id}"."->$self->{pkey} FETCH *$self->{objectid} returns $qval{$self->{pval}}\n" if $debug_normalarray || $debug_refarray; | ||
| 2449 | return $self->{pval}; | ||
| 2450 | } | ||
| 2451 | } | ||
| 2452 | # - - - - - - - - - - - | ||
| 2453 | { | ||
| 2454 | package OOPS::BigInArray; | ||
| 2455 | |||
| 2456 | use Scalar::Util qw(weaken); | ||
| 2457 | use Carp qw(longmess); | ||
| 2458 | our (@ISA) = ('OOPS::InArray'); | ||
| 2459 | |||
| 2460 | sub TIESCALAR | ||
| 2461 | { | ||
| 2462 | my $pkg = shift; | ||
| 2463 | my ($id, $pkey, $cksum, $oops) = @_; | ||
| 2464 | my $self = { | ||
| 2465 | id => $id, | ||
| 2466 | pkey => $pkey, | ||
| 2467 | cksum => $cksum, | ||
| 2468 | oops => $oops | ||
| 2469 | }; | ||
| 2470 | weaken $self->{oops}; | ||
| 2471 | print "CREATE BigInArray \%$id/$pkey $self\n" if $debug_free_tied || $debug_refarray; | ||
| 2472 | $tiedvars{$self} = "%$id ".longmess if $debug_tiedvars; | ||
| 2473 | $self->{oops}->assertions; | ||
| 2474 | return bless $self, $pkg; | ||
| 2475 | } | ||
| 2476 | |||
| 2477 | sub FETCH | ||
| 2478 | { | ||
| 2479 | my ($self) = shift; | ||
| 2480 | |||
| 2481 | return $self->{pval} | ||
| 2482 | if exists $self->{pval}; | ||
| 2483 | |||
| 2484 | $self->{pval} = $self->{oops}->load_big($self->{id}, $self->{pkey}); | ||
| 2485 | print "\@$self->{id}"."->$self->{pkey} FETCH '$self->{pval}'\n" if $debug_normalarray || $debug_refarray; | ||
| 2486 | no warnings; | ||
| 2487 | my $a = $self->{oops}{cache}{$self->{id}}; | ||
| 2488 | if ($#$a >= $self->{pkey} && tied($a->[$self->{pkey}]) eq $self) { | ||
| 2489 | untie $a->[$self->{pkey}]; | ||
| 2490 | } | ||
| 2491 | $self->{oops}->assertions; | ||
| 2492 | return $self->{pval}; | ||
| 2493 | } | ||
| 2494 | } | ||
| 2495 | # - - - - - - - - - - - | ||
| 2496 | { | ||
| 2497 | package OOPS::RefAlias; | ||
| 2498 | |||
| 2499 | # | ||
| 2500 | # RefAlias handles references to elements within other objects. | ||
| 2501 | # \%foo{bar} and such. | ||
| 2502 | # | ||
| 2503 | |||
| 2504 | use Scalar::Util qw(weaken refaddr reftype); | ||
| 2505 | use Carp qw(confess longmess); | ||
| 2506 | |||
| 2507 | # | ||
| 2508 | # If we point into an array then there is some chance that | ||
| 2509 | # our situation has changed. We must untie ourself and return 1 | ||
| 2510 | # so that the normal reference writing code can handle it. | ||
| 2511 | # | ||
| 2512 | |||
| 2513 | sub SAVE_SELF | ||
| 2514 | { | ||
| 2515 | my $self = shift; | ||
| 2516 | my ($oops, $id, $objid, $objkey) = @$self; | ||
| 2517 | return if $oops->{savedone}{$id}++; | ||
| 2518 | return unless exists $oops->{cache}{$objid}; | ||
| 2519 | return unless reftype($oops->{cache}{$objid}) eq 'ARRAY'; | ||
| 2520 | print "SAVE_SELF RefAlias \%*$id $self\n" if $debug_refalias; | ||
| 2521 | $self->FETCH; | ||
| 2522 | return 1; | ||
| 2523 | } | ||
| 2524 | |||
| 2525 | sub POST_SAVE {} | ||
| 2526 | |||
| 2527 | sub DESTROY | ||
| 2528 | { | ||
| 2529 | my $self = shift; | ||
| 2530 | my ($oops, $id, $objid, $objkey) = @$self; | ||
| 2531 | print "DESTROY RefAlias \%*$id $self\n" if $debug_free_tied || $debug_refarray; | ||
| 2532 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 2533 | } | ||
| 2534 | |||
| 2535 | sub TIESCALAR | ||
| 2536 | { | ||
| 2537 | my $pkg = shift; | ||
| 2538 | my ($oops, $id, $refobid, $refobkey) = @_; | ||
| 2539 | my $self = bless [ $oops, $id, $refobid, $refobkey ], $pkg; | ||
| 2540 | weaken $self->[0]; | ||
| 2541 | print "CREATE RefAlias \%$id $self\n" if $debug_free_tied || $debug_refarray; | ||
| 2542 | $tiedvars{$self} = "%$id ".longmess if $debug_tiedvars; | ||
| 2543 | $oops->assertions; | ||
| 2544 | return $self; | ||
| 2545 | } | ||
| 2546 | |||
| 2547 | sub FETCH | ||
| 2548 | { | ||
| 2549 | my $self = shift; | ||
| 2550 | my ($oops, $id, $objid, $objkey) = @$self; | ||
| 2551 | print "\$*$id raFETCH *$objid/'$objkey' (will UNITE)\n" if $debug_refalias; | ||
| 2552 | my $tied; | ||
| 2553 | my $cache = $oops->{cache}; | ||
| 2554 | my $ref; | ||
| 2555 | my $wa; | ||
| 2556 | if (! exists $cache->{$objid}) { | ||
| 2557 | print "\$*$id raFETCH loading object\n" if $debug_refalias; | ||
| 2558 | $oops->load_object($objid) || confess; | ||
| 2559 | } | ||
| 2560 | my $type = reftype($oops->{cache}{$objid}); | ||
| 2561 | if ($type eq 'HASH') { | ||
| 2562 | if (($tied = tied %{$cache->{$objid}})) { | ||
| 2563 | $ref = $tied->GETREFORIG($objkey); | ||
| 2564 | print "\$*$id raFETCH tied, using *$objid->GETREFORIG($qval{$objkey}): $qval{$ref}\n" if $debug_refalias; | ||
| 2565 | } else { | ||
| 2566 | confess "untied hash reference \$$id/$objid/$objkey"; | ||
| 2567 | # $ref = \$oops->{cache}{$objid}{$objkey}; | ||
| 2568 | } | ||
| 2569 | } elsif ($type eq 'ARRAY') { | ||
| 2570 | if ($tied = tied @{$cache->{$objid}}) { | ||
| 2571 | confess "We don't support tied arrays yet"; | ||
| 2572 | } else { | ||
| 2573 | $ref = $oops->{refcopy}{$objid}{$objkey} || confess; | ||
| 2574 | print "\$*$id raFETCH from array, using refcopy: $qval{$ref}\n" if $debug_refalias; | ||
| 2575 | } | ||
| 2576 | } else { | ||
| 2577 | confess "Refalias to '$type' isn't allowed"; | ||
| 2578 | } | ||
| 2579 | untie $cache->{$id}; | ||
| 2580 | $oops->{unwatched}{$id} = 1; | ||
| 2581 | $oops->{oldalias}{$id} = [ $objid, $objkey ]; | ||
| 2582 | $cache->{$id} = $ref; | ||
| 2583 | print "*$id raFETCH cache := $qval{$ref}\n" if $debug_cache; | ||
| 2584 | $oops->memory($ref, $id); | ||
| 2585 | print "MEMORY $qval{$ref} = $id in raFETCH\n" if $debug_memory; | ||
| 2586 | confess unless $ref; | ||
| 2587 | print "\$*$id raFETCH *$objid/'$objkey' returns $qval{$ref}\n" if $debug_refalias; | ||
| 2588 | return $ref; | ||
| 2589 | } | ||
| 2590 | |||
| 2591 | sub STORE | ||
| 2592 | { | ||
| 2593 | confess "why could this happen?"; | ||
| 2594 | # my $self = shift; | ||
| 2595 | # my $val = shift; | ||
| 2596 | # my ($oops, $id, $objid, $objkey) = @$self; | ||
| 2597 | # print "\$*$id raSTORE cache := $qval{$val} (was *$objid/$objkey)\n" if $debug_refalias || $debug_cache; | ||
| 2598 | # | ||
| 2599 | # my $cache = $oops->{cache}; | ||
| 2600 | # untie $cache->{$id}; | ||
| 2601 | # $cache->{$id} = $val; | ||
| 2602 | # | ||
| 2603 | # XXX why not unwatched or oldalias? | ||
| 2604 | |||
| 2605 | #my $r = $self->FETCH(); | ||
| 2606 | #$oops->{unwatched}{$id} = 1; | ||
| 2607 | #$oops->{oldalias}{$id} = [ $objid, $objkey ]; | ||
| 2608 | #my $cache = $oops->{cache}; | ||
| 2609 | #untie ${$cache->{$id}}; | ||
| 2610 | #${$cache->{$id}} = $val; | ||
| 2611 | } | ||
| 2612 | |||
| 2613 | } | ||
| 2614 | # - - - - - - - - - - - | ||
| 2615 | { | ||
| 2616 | package OOPS::Ref; | ||
| 2617 | |||
| 2618 | use Scalar::Util qw(weaken); | ||
| 2619 | use Carp qw(longmess confess); | ||
| 2620 | |||
| 2621 | sub SAVE_SELF {1} | ||
| 2622 | |||
| 2623 | sub POST_SAVE {} | ||
| 2624 | |||
| 2625 | sub DESTROY | ||
| 2626 | { | ||
| 2627 | my $self = shift; | ||
| 2628 | my ($oops, $id, $val) = @$self; | ||
| 2629 | print "DESTROY Ref \%*$id $self\n" if $debug_free_tied || $debug_refalias; | ||
| 2630 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 2631 | } | ||
| 2632 | |||
| 2633 | sub destroy {} | ||
| 2634 | |||
| 2635 | sub UNTIE | ||
| 2636 | { | ||
| 2637 | my $self = shift; | ||
| 2638 | my ($oops, $id, $val) = @$self; | ||
| 2639 | print "*$id UNTIE\n" if $debug_refalias || $debug_refobject; | ||
| 2640 | $oops->{unwatched}{$id} = 1; | ||
| 2641 | } | ||
| 2642 | |||
| 2643 | sub TIESCALAR | ||
| 2644 | { | ||
| 2645 | my $pkg = shift; | ||
| 2646 | my $oops = shift; | ||
| 2647 | my $self = bless [ $oops, @_ ], $pkg; | ||
| 2648 | print "CREATE $pkg *$qval{$_[0]}/$qval{$_[1]}\n" if $debug_free_tied || $debug_refalias; | ||
| 2649 | $tiedvars{$self} = "*$qval{$_[0]} ".longmess if $debug_tiedvars; | ||
| 2650 | weaken $self->[0]; | ||
| 2651 | confess unless defined $oops; | ||
| 2652 | $oops->assertions; | ||
| 2653 | return $self; | ||
| 2654 | } | ||
| 2655 | } | ||
| 2656 | # - - - - - - - - - - - | ||
| 2657 | { | ||
| 2658 | package OOPS::RefObject; | ||
| 2659 | |||
| 2660 | our (@ISA) = qw(OOPS::Ref); | ||
| 2661 | |||
| 2662 | sub FETCH | ||
| 2663 | { | ||
| 2664 | my $self = shift; | ||
| 2665 | my ($oops, $id, $val, $suicide) = @$self; | ||
| 2666 | if (@$self == 5) { | ||
| 2667 | print "\$*id roFETCH overriden value (shouldn't happen), returning $qval{$suicide}\n" if $debug_refobject; | ||
| 2668 | return $suicide; | ||
| 2669 | } | ||
| 2670 | untie ${$oops->{cache}{$id}}; | ||
| 2671 | $oops->{oldobject}{$id}{$nopkey} = $val; | ||
| 2672 | print "OLDOBJECT *$id/$nopkey refobject = *$val (roFETCH)\n" if $debug_oldobject; | ||
| 2673 | print "\$*$id roFETCH will return *$val and attempt UNTIE\n" if $debug_refobject; | ||
| 2674 | $oops->{unwatched}{$id} = 1; | ||
| 2675 | return $oops->load_object($val); | ||
| 2676 | } | ||
| 2677 | |||
| 2678 | sub STORE | ||
| 2679 | { | ||
| 2680 | my $self = shift; | ||
| 2681 | my ($oops, $id, $val, $suicide) = @$self; | ||
| 2682 | untie ${$oops->{cache}{$id}}; | ||
| 2683 | print "\$*$id roSTORE $qval{$_[0]} (replacing *$val), attempt UNTIE\n" if $debug_refobject; | ||
| 2684 | unless ($oops->{oldobject}{$id}{$nopkey}) { | ||
| 2685 | print "OLDOBJECT *$id/$nopkey refobject = *$val (roSTORE)\n" if $debug_oldobject; | ||
| 2686 | $oops->{oldobject}{$id}{$nopkey} = $val; | ||
| 2687 | $oops->{unwatched}{$id} = 1; | ||
| 2688 | } | ||
| 2689 | $self->[4] = $_[0]; | ||
| 2690 | ${$oops->{cache}{$id}} = shift; | ||
| 2691 | untie ${$oops->{cache}{$id}}; | ||
| 2692 | if (@$self == 5) { | ||
| 2693 | print "\$*id roSTORE overriden value (shouldn't happen), returning $qval{$suicide}\n" if $debug_refobject; | ||
| 2694 | return $suicide; | ||
| 2695 | } | ||
| 2696 | return $val; | ||
| 2697 | } | ||
| 2698 | } | ||
| 2699 | # - - - - - - - - - - - | ||
| 2700 | { | ||
| 2701 | package OOPS::RefBig; | ||
| 2702 | |||
| 2703 | our (@ISA) = qw(OOPS::Ref); | ||
| 2704 | |||
| 2705 | sub FETCH | ||
| 2706 | { | ||
| 2707 | my $self = shift; | ||
| 2708 | my ($oops, $id, $val) = @$self; | ||
| 2709 | untie ${$oops->{cache}{$id}}; | ||
| 2710 | $oops->{oldbig}{$id}{$nopkey} = $val; | ||
| 2711 | return $oops->load_big($id, $nopkey); | ||
| 2712 | } | ||
| 2713 | |||
| 2714 | sub STORE | ||
| 2715 | { | ||
| 2716 | my $self = shift; | ||
| 2717 | my ($oops, $id, $val) = @$self; | ||
| 2718 | untie ${$oops->{cache}{$id}}; | ||
| 2719 | $oops->{oldbig}{$id}{$nopkey} = $val; | ||
| 2720 | ${$oops->{cache}{$id}} = shift; | ||
| 2721 | return $val; | ||
| 2722 | } | ||
| 2723 | } | ||
| 2724 | # - - - - - - - - - - - | ||
| 2725 | { | ||
| 2726 | package OOPS::NormalHash; | ||
| 2727 | |||
| 2728 | use Scalar::Util qw(weaken reftype refaddr); | ||
| 2729 | use Carp qw(confess longmess); | ||
| 2730 | |||
| 2731 | sub SAVE_SELF | ||
| 2732 | { | ||
| 2733 | my $self = shift; | ||
| 2734 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2735 | print "\%$id hSAVE_SELF\n" if $debug_normalhash; | ||
| 2736 | $self->LOAD_SELF_REF() if $oops->{reftarg}{$id}; | ||
| 2737 | $oops->write_hash($values, $id, $ptypes, $added); | ||
| 2738 | delete $oops->{demandwrite}{$id}; | ||
| 2739 | $oops->assertions; | ||
| 2740 | return 0; | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | sub POST_SAVE | ||
| 2744 | { | ||
| 2745 | my $self = shift; | ||
| 2746 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2747 | delete $vars->{during_save}; | ||
| 2748 | } | ||
| 2749 | |||
| 2750 | sub destroy | ||
| 2751 | { | ||
| 2752 | my $self = shift; | ||
| 2753 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2754 | print "destroy NormalHash \%$id $self\n" if $debug_free_tied || $debug_normalhash; | ||
| 2755 | %$ptypes = (); | ||
| 2756 | %$added = (); | ||
| 2757 | %$vars = (); | ||
| 2758 | $oops->assertions if defined $oops; | ||
| 2759 | } | ||
| 2760 | |||
| 2761 | # | ||
| 2762 | # could this be an UNTIE sub intead? If so, would it get | ||
| 2763 | # called too soon? | ||
| 2764 | sub DESTROY | ||
| 2765 | { | ||
| 2766 | local($main::SIG{'__DIE__'}) = \&OOPS::die_from_destroy; | ||
| 2767 | my $self = shift; | ||
| 2768 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2769 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 2770 | return unless defined $oops; # it's a weak reference | ||
| 2771 | return unless defined $oops->{cache}; # during destruction... | ||
| 2772 | $self->preserve_ptypes; | ||
| 2773 | confess if %$ptypes; | ||
| 2774 | $oops->{oldvalue}{$id} = {} | ||
| 2775 | unless exists $oops->{oldvalue}{$id}; | ||
| 2776 | my $ov = $oops->{oldvalue}{$id}; | ||
| 2777 | my $oo = $oops->{oldobject}{$id}; | ||
| 2778 | my $of = $oops->{oldbig}{$id}; | ||
| 2779 | for my $pkey (keys %$values) { | ||
| 2780 | no warnings qw(uninitialized); | ||
| 2781 | next if exists $added->{$pkey}; | ||
| 2782 | next if exists $ov->{$pkey}; | ||
| 2783 | next if $oo && exists $oo->{$pkey}; | ||
| 2784 | next if $of && exists $of->{$pkey}; | ||
| 2785 | $ov->{$pkey} = $values->{$pkey}; | ||
| 2786 | } | ||
| 2787 | confess if tied %{$oops->{cache}{$id}}; | ||
| 2788 | untie(%{$oops->{cache}{$id}}); # Yes, this is required. | ||
| 2789 | %{$oops->{cache}{$id}} = %$values; | ||
| 2790 | $oops->{objtouched}{$id} = 'destroyed'; | ||
| 2791 | delete $oops->{demandwrite}{$id}; | ||
| 2792 | print "in NormalHash::DESTROY, *$id is touched -- \$oops is still valid\n" if $debug_touched; | ||
| 2793 | print "DESTROY NormalHash \%*$id $self\n" if $debug_free_tied || $debug_normalhash; | ||
| 2794 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 2795 | # print "\%$id keys = ".join(' ',keys %{$oops->{cache}{$id}})."\n"; | ||
| 2796 | # print "\%$id oldvalues = ".join(' ',keys %{$oops->{oldvalue}{$id}})."\n"; | ||
| 2797 | # print "\%$id oldobject = ".join(' ',keys %{$oops->{oldobject}{$id}})."\n"; | ||
| 2798 | $oops->assertions; | ||
| 2799 | } | ||
| 2800 | |||
| 2801 | # tie %{$cache->{$id}}, 'OOPS::NormalHash', $new{$id}, $newptype{$id}, $oops, $id; | ||
| 2802 | |||
| 2803 | sub TIEHASH | ||
| 2804 | { | ||
| 2805 | my $pkg = shift; | ||
| 2806 | my ($values, $ptypes, $oops, $id) = @_; | ||
| 2807 | my $self = bless [ $values, $ptypes, {}, $oops, $id, {} ], $pkg; | ||
| 2808 | weaken $self->[3]; | ||
| 2809 | print "CREATE NormalHash \%$id $self\n" if $debug_free_tied || $debug_normalhash; | ||
| 2810 | $tiedvars{$self} = "%$id ".longmess if $debug_tiedvars; | ||
| 2811 | $oops->assertions; | ||
| 2812 | return $self; | ||
| 2813 | } | ||
| 2814 | |||
| 2815 | sub FETCH | ||
| 2816 | { | ||
| 2817 | my $self = shift; | ||
| 2818 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2819 | return undef unless defined $oops; # weak ref | ||
| 2820 | my $pkey = shift; | ||
| 2821 | no warnings qw(uninitialized); | ||
| 2822 | print "\%$id/$pkey begin hFETCH\n" if $debug_normalhash; | ||
| 2823 | if (exists $ptypes->{$pkey}) { | ||
| 2824 | my $ot = $ptypes->{$pkey}; | ||
| 2825 | if ($ot eq 'R') { | ||
| 2826 | print "OLDOBJECT *$id/$pkey hFETCH = *$values->{$pkey}\n" if $debug_oldobject; | ||
| 2827 | $oops->{oldobject}{$id}{$pkey} = $values->{$pkey}; | ||
| 2828 | $values->{$pkey} = $oops->load_object($values->{$pkey}); | ||
| 2829 | $oops->workaround27555($values->{$pkey}); | ||
| 2830 | } elsif ($ot eq 'B') { | ||
| 2831 | $oops->{oldbig}{$id}{$pkey} = $values->{$pkey}; | ||
| 2832 | $values->{$pkey} = $oops->load_big($id, $pkey); | ||
| 2833 | } else { | ||
| 2834 | confess; | ||
| 2835 | } | ||
| 2836 | delete $ptypes->{$pkey}; | ||
| 2837 | } | ||
| 2838 | print "\%$id/$pkey hFETCH = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 2839 | confess if exists $ptypes->{$pkey} && tied $ptypes->{$pkey}; | ||
| 2840 | $oops->assertions; | ||
| 2841 | return $values->{$pkey}; | ||
| 2842 | } | ||
| 2843 | |||
| 2844 | sub STORE | ||
| 2845 | { | ||
| 2846 | my $self = shift; | ||
| 2847 | my ($pkey, $pval) = @_; | ||
| 2848 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2849 | return undef unless defined $oops; # weak ref | ||
| 2850 | $oops->workaround27555($pval) if ref $pval; | ||
| 2851 | no warnings qw(uninitialized); | ||
| 2852 | if (exists $ptypes->{$pkey}) { | ||
| 2853 | my $ot = $ptypes->{$pkey}; | ||
| 2854 | if ($ot eq 'R') { | ||
| 2855 | print "*$id/$pkey hSTORE *$id/$pkey = *$values->{$pkey}\n" if $debug_oldobject; | ||
| 2856 | $oops->{oldobject}{$id}{$pkey} = $values->{$pkey}; | ||
| 2857 | } elsif ($ot eq 'B') { | ||
| 2858 | $oops->{oldbig}{$id}{$pkey} = $values->{$pkey}; | ||
| 2859 | } else { | ||
| 2860 | confess; | ||
| 2861 | } | ||
| 2862 | print "%$id/$pkey hSTORE Oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 2863 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 2864 | delete $ptypes->{$pkey}; | ||
| 2865 | } else { | ||
| 2866 | if (exists $oops->{oldvalue}{$id}{$pkey}) { | ||
| 2867 | # nada | ||
| 2868 | } elsif (exists($values->{$pkey}) && ! exists($added->{$pkey})) { | ||
| 2869 | print "%$id/$pkey hSTORE oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 2870 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 2871 | } else { | ||
| 2872 | no warnings; | ||
| 2873 | $added->{$pkey} = 1; | ||
| 2874 | } | ||
| 2875 | } | ||
| 2876 | $oops->{demandwrite}{$id} = 1; | ||
| 2877 | print "\%$id/$pkey hSTORE = $qval{$pval} ($qval{$values->{$pkey}})\n" if $debug_normalhash; | ||
| 2878 | $values->{$pkey} = $pval; | ||
| 2879 | $oops->assertions; | ||
| 2880 | } | ||
| 2881 | |||
| 2882 | sub DELETE | ||
| 2883 | { | ||
| 2884 | my $self = shift; | ||
| 2885 | my $pkey = shift; | ||
| 2886 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2887 | { no warnings; print "\%$id/$pkey hDELETE ($values->{$pkey})\n" if $debug_normalhash; } | ||
| 2888 | |||
| 2889 | no warnings qw(uninitialized); | ||
| 2890 | if (exists $values->{$pkey}) { | ||
| 2891 | if (exists $vars->{keyrefs}{$pkey}) { | ||
| 2892 | my $ref = $vars->{keyrefs}{$pkey}; | ||
| 2893 | my $addr = refaddr($ref); | ||
| 2894 | |||
| 2895 | unless (exists $added->{$pkey} || exists $vars->{deleted}{$pkey} || exists $vars->{alldelete}) { | ||
| 2896 | print "%*$id/'$pkey' hDELETE preserve $addr ($ref) in original_refs\n" if $debug_memory || $debug_refalias; | ||
| 2897 | confess if $vars->{original_reference}{$pkey}; | ||
| 2898 | $vars->{original_reference}{$pkey} = $ref | ||
| 2899 | } | ||
| 2900 | |||
| 2901 | print "%*$id/'$pkey' hDELETE MEMORY2KEY($addr) := undef ($ref)\n" if $debug_memory || $debug_refalias; | ||
| 2902 | $oops->memory2key($ref); | ||
| 2903 | delete $vars->{keyrefs}{$pkey}; | ||
| 2904 | } | ||
| 2905 | |||
| 2906 | if (exists $added->{$pkey}) { | ||
| 2907 | # nada | ||
| 2908 | } else { | ||
| 2909 | if (exists $ptypes->{$pkey}) { | ||
| 2910 | my $ot = $ptypes->{$pkey}; | ||
| 2911 | if ($ot eq 'R') { | ||
| 2912 | print "OLDOBJECT *$id/$pkey hDELETE = *$values->{$pkey}\n" if $debug_oldobject; | ||
| 2913 | $oops->{oldobject}{$id}{$pkey} = $values->{$pkey}; | ||
| 2914 | } elsif ($ot eq 'B') { | ||
| 2915 | $oops->{oldbig}{$id}{$pkey} = $values->{$pkey}; | ||
| 2916 | } else { | ||
| 2917 | confess; | ||
| 2918 | } | ||
| 2919 | print "%$id/$pkey hDELETE Oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 2920 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 2921 | delete $ptypes->{$pkey}; | ||
| 2922 | } elsif (! exists($oops->{oldvalue}{$id}{$pkey}) && ! exists $added->{$pkey}) { | ||
| 2923 | print "%$id/$pkey hDELETE oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 2924 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 2925 | } | ||
| 2926 | unless (exists($vars->{deleted}{$pkey}) || exists($vars->{alldelete})) { | ||
| 2927 | # first time it's deleted | ||
| 2928 | $vars->{deleted}{$pkey} = $values->{$pkey}; | ||
| 2929 | } | ||
| 2930 | } | ||
| 2931 | delete $values->{$pkey}; | ||
| 2932 | } | ||
| 2933 | $oops->{demandwrite}{$id} = 1; | ||
| 2934 | $oops->assertions; | ||
| 2935 | } | ||
| 2936 | |||
| 2937 | sub CLEAR | ||
| 2938 | { | ||
| 2939 | my $self = shift; | ||
| 2940 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2941 | return unless defined $oops; # weak ref | ||
| 2942 | print "\%$id hCLEAR\n" if $debug_normalhash; | ||
| 2943 | |||
| 2944 | $self->preserve_ptypes; | ||
| 2945 | |||
| 2946 | confess if %$ptypes; | ||
| 2947 | if ($vars->{keyrefs}) { | ||
| 2948 | for my $pkey (keys %{$vars->{keyrefs}}) { | ||
| 2949 | no warnings qw(uninitialized); | ||
| 2950 | next unless $vars->{keyrefs}{$pkey}; | ||
| 2951 | my $ref = $vars->{keyrefs}{$pkey}; | ||
| 2952 | my $addr = refaddr($ref); | ||
| 2953 | print "%*$id/'$pkey' hCLEAR MEMORY2KEY($addr) := undef ($ref)\n" if $debug_memory || $debug_refalias; | ||
| 2954 | $oops->memory2key($ref); | ||
| 2955 | |||
| 2956 | unless (exists $added->{$pkey} || exists $vars->{deleted}{$pkey} || exists $vars->{alldelete}) { | ||
| 2957 | print "%*$id/'$pkey' hCLEAR preserve $addr ($ref) in original_refs\n" if $debug_memory || $debug_refalias; | ||
| 2958 | confess if $vars->{original_reference}{$pkey}; | ||
| 2959 | $vars->{original_reference}{$pkey} = $ref | ||
| 2960 | } | ||
| 2961 | } | ||
| 2962 | delete $vars->{keyrefs}; | ||
| 2963 | } | ||
| 2964 | if (exists $vars->{alldelete}) { | ||
| 2965 | %$values = (); | ||
| 2966 | } else { | ||
| 2967 | delete @{$values}{keys %$added}; | ||
| 2968 | $vars->{alldelete} = $self->[0]; | ||
| 2969 | $self->[0] = {}; | ||
| 2970 | } | ||
| 2971 | delete $vars->{deleted}; | ||
| 2972 | %$added = (); | ||
| 2973 | $oops->{demandwrite}{$id} = 1; | ||
| 2974 | $oops->assertions; | ||
| 2975 | } | ||
| 2976 | |||
| 2977 | sub GETREFORIG | ||
| 2978 | { | ||
| 2979 | my $self = shift; | ||
| 2980 | my $pkey = shift; | ||
| 2981 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 2982 | no warnings qw(uninitialized); | ||
| 2983 | if (exists($vars->{alldelete}) || exists($vars->{deleted}{$pkey})) { | ||
| 2984 | $self->LOAD_SELF_REF() unless $vars->{ref_to_self_loaded}; | ||
| 2985 | print "%*$id/$pkey hGETREFORIG returning cached original $qaddr{$vars->{original_reference}{$pkey}} ($vars->{original_reference}{$pkey})\n" if $debug_refalias && exists $vars->{original_reference}{$pkey}; | ||
| 2986 | return $vars->{original_reference}{$pkey} | ||
| 2987 | if exists $vars->{original_reference}{$pkey}; | ||
| 2988 | my $pval; | ||
| 2989 | $vars->{during_save}{oldvalue} = %{$oops->{oldvalue}{$id}}; | ||
| 2990 | if (exists $oops->{oldobject}{$id}{$pkey}) { | ||
| 2991 | $pval = $oops->load_object($oops->{oldobject}{$id}{$pkey}); | ||
| 2992 | $oops->workaround27555($pval); | ||
| 2993 | print "%*$id/$pkey hGETREFORIG from loadobject $oops->{oldobject}{$id}{$pkey}\n" if $debug_refalias; | ||
| 2994 | } elsif (exists $oops->{oldbig}{$id}{$pkey}) { | ||
| 2995 | $pval = $oops->load_big($id, $pkey); | ||
| 2996 | print "%*$id/$pkey hGETREFORIG from loadbig\n" if $debug_refalias; | ||
| 2997 | } elsif (exists $oops->{oldvalue}{$id}{$pkey}) { | ||
| 2998 | $pval = $oops->{oldvalue}{$id}{$pkey}; | ||
| 2999 | print "%*$id/$pkey hGETREFORIG from oldvalue\n" if $debug_refalias; | ||
| 3000 | } elsif (exists $vars->{alldelete} && exists $vars->{alldelete}{$pkey}) { | ||
| 3001 | print "%*$id/$pkey hGETREFORIG from CLEARed value\n" if $debug_refalias; | ||
| 3002 | } else { | ||
| 3003 | print "%*$id/$pkey hGETREFORIG no prior value\n" if $debug_refalias; | ||
| 3004 | $pval = undef; | ||
| 3005 | } | ||
| 3006 | my $ref = \$pval; | ||
| 3007 | print "%*$id/$pkey hGETREFORIG original $qval{$pval} $qaddr{$ref} ($ref)\n" if $debug_refalias; | ||
| 3008 | $vars->{original_reference}{$pkey} = $ref; | ||
| 3009 | return $ref; | ||
| 3010 | } | ||
| 3011 | print "%*$id/$pkey hGETREFORIG returning NEW reference\n" if $debug_refalias; | ||
| 3012 | return $self->GETREF($pkey); | ||
| 3013 | } | ||
| 3014 | |||
| 3015 | # | ||
| 3016 | # This should be a normal tie method but it isn't so we depend on it's | ||
| 3017 | # being called by workaround27555 | ||
| 3018 | # | ||
| 3019 | sub GETREF | ||
| 3020 | { | ||
| 3021 | my $self = shift; | ||
| 3022 | my $pkey = shift; | ||
| 3023 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3024 | $self->STORE($pkey, $self->FETCH($pkey)); | ||
| 3025 | no warnings qw(uninitialized); | ||
| 3026 | confess unless exists $values->{$pkey}; | ||
| 3027 | $self->LOAD_SELF_REF() unless $vars->{ref_to_self_loaded}; | ||
| 3028 | my $ref = \$values->{$pkey}; | ||
| 3029 | $vars->{keyrefs}{$pkey} = $ref; | ||
| 3030 | $oops->memory2key($ref, $id, $pkey); | ||
| 3031 | $oops->{demandwrite}{$id}++; | ||
| 3032 | print "%*$id/'$pkey' hGETREF MEMORY2KEY $qval{$ref} := *$id/$pkey (ref to: $qval{$values->{$pkey}})\n" if $debug_memory || $debug_refalias; | ||
| 3033 | return $ref; | ||
| 3034 | } | ||
| 3035 | |||
| 3036 | |||
| 3037 | sub LOAD_SELF_REF | ||
| 3038 | { | ||
| 3039 | my $self = shift; | ||
| 3040 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3041 | if ($vars->{ref_to_self_loaded}) { | ||
| 3042 | print "%*$id hLOAD_SELF_REF - already done\n" if $debug_refalias && $debug_normalhash; | ||
| 3043 | return | ||
| 3044 | } | ||
| 3045 | $vars->{ref_to_self_loaded} = 1; # early in function to prevent re-entrency | ||
| 3046 | print "\%$id searching for references to keys\n" if $debug_refalias || $debug_normalhash; | ||
| 3047 | my $reftargobjectQ = $oops->query('reftargobject', execute => $id); | ||
| 3048 | my $refid; | ||
| 3049 | # | ||
| 3050 | # When a hash key is deleted, any references to the hash value become | ||
| 3051 | # independent from the original hash. We have to have these referneces | ||
| 3052 | # in-memory so that we can sort this out. | ||
| 3053 | # | ||
| 3054 | while (($refid) = $reftargobjectQ->fetchrow_array()) { | ||
| 3055 | print "\%$id loading reference *$refid\n" if $debug_refalias || $debug_normalhash; | ||
| 3056 | unless (exists $oops->{cache}{$refid}) { | ||
| 3057 | $oops->load_object($refid); | ||
| 3058 | my $x = $oops->{cache}{$refid}; # force untie | ||
| 3059 | } | ||
| 3060 | } | ||
| 3061 | print "%*$id hLOAD_SELF_REF - complete\n" if $vars->{ref_to_self_loaded} && $debug_refalias && $debug_normalhash; | ||
| 3062 | } | ||
| 3063 | |||
| 3064 | sub preserve_ptypes | ||
| 3065 | { | ||
| 3066 | my $self = shift; | ||
| 3067 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3068 | return unless defined $oops; # weak ref | ||
| 3069 | for my $pkey (keys %$values) { | ||
| 3070 | no warnings qw(uninitialized); | ||
| 3071 | if (exists $ptypes->{$pkey}) { | ||
| 3072 | my $ot = $ptypes->{$pkey}; | ||
| 3073 | if ($ot eq 'R') { | ||
| 3074 | print "OLDOBJECT *$id/$pkey hPreserve_ptypes = *$values->{$pkey}\n" if $debug_oldobject; | ||
| 3075 | $oops->{oldobject}{$id}{$pkey} = $values->{$pkey}; | ||
| 3076 | } elsif ($ot eq 'B') { | ||
| 3077 | $oops->{oldbig}{$id}{$pkey} = $values->{$pkey}; | ||
| 3078 | } else { | ||
| 3079 | confess; | ||
| 3080 | } | ||
| 3081 | print "%$id/$pkey hCLEAR oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 3082 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 3083 | delete $ptypes->{$pkey}; | ||
| 3084 | } elsif (exists $added->{$pkey}) { | ||
| 3085 | # nada | ||
| 3086 | } elsif (! exists $oops->{oldvalue}{$id}{$pkey}) { | ||
| 3087 | print "%$id/$pkey hCLEAR oldvalue = $qval{$values->{$pkey}}\n" if $debug_normalhash; | ||
| 3088 | $oops->{oldvalue}{$id}{$pkey} = $values->{$pkey}; | ||
| 3089 | } | ||
| 3090 | } | ||
| 3091 | $oops->assertions; | ||
| 3092 | } | ||
| 3093 | |||
| 3094 | sub EXISTS | ||
| 3095 | { | ||
| 3096 | my $self = shift; | ||
| 3097 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3098 | my $pkey = shift; | ||
| 3099 | no warnings qw(uninitialized); | ||
| 3100 | print "\%$id/$pkey hEXISTS? = ".(exists($values->{$pkey}) ? "YES" : "NO")."\n" if $debug_normalhash; | ||
| 3101 | $oops->assertions; | ||
| 3102 | return exists $values->{$pkey}; | ||
| 3103 | } | ||
| 3104 | |||
| 3105 | sub FIRSTKEY | ||
| 3106 | { | ||
| 3107 | my $self = shift; | ||
| 3108 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3109 | confess if tied $ptypes; | ||
| 3110 | my $t = tied %$ptypes; | ||
| 3111 | $vars->{ineach} = 1; | ||
| 3112 | keys %$values; | ||
| 3113 | print "\%$id hFIRSTKEY\n" if $debug_normalhash; | ||
| 3114 | $oops->assertions if $oops; | ||
| 3115 | return $self->NEXTKEY(); | ||
| 3116 | } | ||
| 3117 | |||
| 3118 | sub NEXTKEY | ||
| 3119 | { | ||
| 3120 | my $self = shift; | ||
| 3121 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3122 | my ($pkey, $pval) = each(%$values); | ||
| 3123 | if (defined $pkey) { | ||
| 3124 | no warnings qw(uninitialized); | ||
| 3125 | confess if exists $ptypes->{$pkey} && tied $ptypes->{$pkey}; | ||
| 3126 | print "\%$id hNEXTKEY = ".$qval{$pkey}."\n" if $debug_normalhash; | ||
| 3127 | } else { | ||
| 3128 | delete $vars->{ineach}; | ||
| 3129 | print "\%$id hNEXTKEY = undef\n" if $debug_normalhash; | ||
| 3130 | } | ||
| 3131 | $oops->assertions if $oops; | ||
| 3132 | return $pkey; | ||
| 3133 | } | ||
| 3134 | |||
| 3135 | sub SCALAR | ||
| 3136 | { | ||
| 3137 | my $self = shift; | ||
| 3138 | my ($values, $ptypes, $added, $oops, $id, $vars) = @$self; | ||
| 3139 | return scalar(keys(%$values)); | ||
| 3140 | } | ||
| 3141 | |||
| 3142 | } | ||
| 3143 | # - - - - - - - - - - - | ||
| 3144 | { | ||
| 3145 | package OOPS::DemandHash; | ||
| 3146 | |||
| 3147 | use Carp qw(confess longmess croak); | ||
| 3148 | use Scalar::Util qw(weaken refaddr); | ||
| 3149 | |||
| 3150 | # | ||
| 3151 | # $oops - OOPS object to use | ||
| 3152 | # $id - persistant object id | ||
| 3153 | # $rcache - read cache | ||
| 3154 | # $wcache - write cache | ||
| 3155 | # $necache - non-exists cache | ||
| 3156 | # $dcache - deleted cache | ||
| 3157 | # $ovcache - old values cache | ||
| 3158 | # $vars - { | ||
| 3159 | # dbseach - each() database cursor | ||
| 3160 | # alldelete - everything has been deleted at least once | ||
| 3161 | # } | ||
| 3162 | # | ||
| 3163 | |||
| 3164 | sub SAVE_SELF | ||
| 3165 | { | ||
| 3166 | my $self = shift; | ||
| 3167 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3168 | |||
| 3169 | printf "%%%s SAVE_SELF dcache=%s, wcache=%s\n", $id, join('/',keys %$dcache), join('/',keys %$wcache) if $debug_virtual_delete || $debug_virtual_save; | ||
| 3170 | |||
| 3171 | return unless %$wcache || %$dcache || $vars->{alldelete}; | ||
| 3172 | if ($vars->{alldelete}) { | ||
| 3173 | print "%$id alldelete\n" if $debug_virtual_save; | ||
| 3174 | $oops->predelete_object($id); # doesn't clear object row. | ||
| 3175 | $oops->{forcesave}{$id} = __LINE__ | ||
| 3176 | if $oops->{do_forcesave}; | ||
| 3177 | $oops->query('postdeleteV', execute => $id); | ||
| 3178 | $self->LOAD_SELF_REF() if $oops->{reftarg}{$id}; | ||
| 3179 | } elsif (%$dcache || %$wcache) { | ||
| 3180 | my %done; | ||
| 3181 | for my $pkey (keys %$dcache, keys %$wcache) { | ||
| 3182 | no warnings qw(uninitialized); | ||
| 3183 | confess if $done{$pkey}++; | ||
| 3184 | my ($pval, $ptype); | ||
| 3185 | if (exists $ovcache->{$pkey}) { | ||
| 3186 | ($pval, $ptype) = @{$ovcache->{$pkey}}; | ||
| 3187 | print "%$id/'$pkey' - old value is cached ('$pval', $ptype)\n" if $debug_virtual_save; | ||
| 3188 | } elsif (exists $necache->{$pkey}) { | ||
| 3189 | print "%$id/'$pkey' - old value known to be absent\n" if $debug_virtual_save; | ||
| 3190 | next; | ||
| 3191 | } else { | ||
| 3192 | print "%$id/'$pkey' - checking old pval in virtual SAVE_SELF\n" if $debug_virtual_delete || $debug_virtual_save; | ||
| 3193 | my $loadpkeyQ = $oops->query('loadpkey', execute => [ $id, $pkey ]); | ||
| 3194 | if (($pval, $ptype) = $loadpkeyQ->fetchrow_array) { | ||
| 3195 | $loadpkeyQ->finish(); | ||
| 3196 | $ovcache->{$pkey} = [ $pval, $ptype ]; | ||
| 3197 | } else { | ||
| 3198 | # no old value | ||
| 3199 | } | ||
| 3200 | } | ||
| 3201 | if (! $ptype) { | ||
| 3202 | # nothing | ||
| 3203 | } elsif ($ptype eq 'R') { | ||
| 3204 | print "%$id/'$pkey' - old value was a reference (*$pval)\n" if $debug_virtual_delete || $debug_virtual_save || $debug_refcount; | ||
| 3205 | $oops->refchange($id, $pval, -1); | ||
| 3206 | print "in demandhash save-self, V%$id reference to $oops->{otype}{$pval}*$pval gone (-1)\n" if $debug_refcount; | ||
| 3207 | } elsif ($ptype eq 'B') { | ||
| 3208 | print "%$id/'$pkey' - old value was big\n" if $debug_virtual_delete || $debug_virtual_save; | ||
| 3209 | $oops->query('deletebig', execute => [ $id, $pkey ]); | ||
| 3210 | } else { | ||
| 3211 | confess; | ||
| 3212 | } | ||
| 3213 | $self->LOAD_SELF_REF($pkey) if exists $dcache->{$pkey} && $oops->{reftarg}{$id}; | ||
| 3214 | } | ||
| 3215 | } | ||
| 3216 | if (%$dcache && ! $vars->{alldelete}) { | ||
| 3217 | for my $pkey (keys %$dcache) { | ||
| 3218 | no warnings qw(uninitialized); | ||
| 3219 | print "%$id/'$pkey' - commit virtual delete (SAVE_SELF)\n" if $debug_virtual_delete || $debug_virtual_save; | ||
| 3220 | $oops->query('deleteattribute', execute => [ $id, $pkey ]); | ||
| 3221 | } | ||
| 3222 | } | ||
| 3223 | if (%$wcache) { | ||
| 3224 | my $saveattributeQ = $oops->query('saveattribute'); | ||
| 3225 | for my $pkey (keys %$wcache) { | ||
| 3226 | no warnings qw(uninitialized); | ||
| 3227 | # print "%$id/'$pkey' - no change in value\n" if $debug_virtual_save && exists $rcache->{$pkey} && $rcache->{$pkey} eq $wcache->{$pkey} && defined($rcache->{$pkey}) eq defined($wcache->{$pkey}) && ref($rcache->{$pkey}) eq ref($wcache->{$pkey}); | ||
| 3228 | # next if exists $rcache->{$pkey} | ||
| 3229 | # && $rcache->{$pkey} eq $wcache->{$pkey} | ||
| 3230 | # && defined($rcache->{$pkey}) eq defined($wcache->{$pkey}) | ||
| 3231 | # && ref($rcache->{$pkey}) eq ref($wcache->{$pkey}); | ||
| 3232 | if (exists($ovcache->{$pkey}) && ! $vars->{alldelete}) { | ||
| 3233 | my ($atval, $ptype) = $oops->prepare_insert_attribute($id, $pkey, undef, $wcache->{$pkey}, undef); | ||
| 3234 | #print "pkey=$pkey atval=$atval\n"; | ||
| 3235 | print "%$id/'$pkey' - replacement value ('$atval', $ptype [was @{$ovcache->{$pkey}}])\n" if $debug_virtual_save; | ||
| 3236 | $oops->query('updateattribute', execute => [ $atval, $ptype, $id, $pkey ]); | ||
| 3237 | } else { | ||
| 3238 | my ($atval, $ptype) = $oops->prepare_insert_attribute($id, $pkey, undef, $wcache->{$pkey}, undef); | ||
| 3239 | print "%$id/'$pkey' - new value ('$atval', $ptype)\n" if $debug_virtual_save; | ||
| 3240 | $oops->query('saveattribute', execute => [ $id, $pkey, $atval, $ptype ]); | ||
| 3241 | } | ||
| 3242 | $vars->{new_rcache}{$pkey} = $wcache->{$pkey}; | ||
| 3243 | } | ||
| 3244 | } | ||
| 3245 | $oops->assertions; | ||
| 3246 | return 0; | ||
| 3247 | } | ||
| 3248 | |||
| 3249 | sub POST_SAVE | ||
| 3250 | { | ||
| 3251 | my $self = shift; | ||
| 3252 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3253 | print "%*$id POST_SAVE\n" if $debug_virtual_save; | ||
| 3254 | if ($vars->{alldelete}) { | ||
| 3255 | delete $vars->{original_reference}; | ||
| 3256 | %$ovcache = (); | ||
| 3257 | } elsif (%$dcache) { | ||
| 3258 | delete @{$vars->{original_reference}}{keys %$dcache}; | ||
| 3259 | delete @{$ovcache}{keys %$dcache}; | ||
| 3260 | } | ||
| 3261 | $self->[2] = $vars->{new_rcache} || {}; | ||
| 3262 | %$dcache = (); | ||
| 3263 | %$wcache = (); | ||
| 3264 | delete $vars->{alldelete}; | ||
| 3265 | delete $oops->{demandwrite}{$id}; | ||
| 3266 | delete $vars->{has_been_deleted}; | ||
| 3267 | } | ||
| 3268 | |||
| 3269 | sub destroy | ||
| 3270 | { | ||
| 3271 | my $self = shift; | ||
| 3272 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3273 | %$rcache = (); | ||
| 3274 | %$wcache = (); | ||
| 3275 | %$necache = (); | ||
| 3276 | %$ovcache = (); | ||
| 3277 | %$dcache = (); | ||
| 3278 | %$vars = (); | ||
| 3279 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 3280 | $oops->assertions if $oops; # tied var | ||
| 3281 | } | ||
| 3282 | |||
| 3283 | sub DESTROY | ||
| 3284 | { | ||
| 3285 | local($main::SIG{'__DIE__'}) = \&OOPS::die_from_destroy; | ||
| 3286 | my $self = shift; | ||
| 3287 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3288 | print "DESTROY DemandHash \%$id $self\n" if $debug_free_tied; | ||
| 3289 | delete $tiedvars{$self} if $debug_tiedvars; | ||
| 3290 | $oops->assertions if defined $oops; # weak reference | ||
| 3291 | } | ||
| 3292 | |||
| 3293 | sub TIEHASH | ||
| 3294 | { | ||
| 3295 | my ($pkg, $oops, $id) = @_; | ||
| 3296 | my $self = bless [ $oops, $id, {}, {}, {}, {}, {}, {} ], $pkg; | ||
| 3297 | weaken $self->[0]; | ||
| 3298 | push(@{$oops->{vcache}}, $self); | ||
| 3299 | print "CREATE DemandHash \%$id $self\n" if $debug_free_tied; | ||
| 3300 | $tiedvars{$self} = "%$id ".longmess if $debug_tiedvars; | ||
| 3301 | $oops->assertions; | ||
| 3302 | return $self; | ||
| 3303 | } | ||
| 3304 | |||
| 3305 | sub FETCH | ||
| 3306 | { | ||
| 3307 | my ($self, $pkey) = @_; | ||
| 3308 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3309 | return undef unless defined $oops; # weak ref | ||
| 3310 | no warnings qw(uninitialized); | ||
| 3311 | if (exists $dcache->{$pkey}) { | ||
| 3312 | print "%*$id/'$pkey' vFETCH: undef - in dcache\n" if $debug_virtual_hash; | ||
| 3313 | return undef ; | ||
| 3314 | } | ||
| 3315 | if (exists $wcache->{$pkey}) { | ||
| 3316 | print "%*$id/'$pkey' vFETCH: $qval{$wcache->{$pkey}} - in wcache\n" if $debug_virtual_hash; | ||
| 3317 | return $wcache->{$pkey}; | ||
| 3318 | } | ||
| 3319 | if (exists $rcache->{$pkey}) { | ||
| 3320 | print "%*$id/'$pkey' vFETCH: $qval{$rcache->{$pkey}} - in rcache\n" if $debug_virtual_hash; | ||
| 3321 | return $rcache->{$pkey}; | ||
| 3322 | } | ||
| 3323 | my $val; | ||
| 3324 | if ($vars->{alldelete}) { | ||
| 3325 | print "%*$id/'$pkey' vFETCH: undef - alldelete\n" if $debug_virtual_hash; | ||
| 3326 | $val = undef; | ||
| 3327 | } else { | ||
| 3328 | $val = $self->ORIGINAL_VALUE($pkey); | ||
| 3329 | if (exists $wcache->{$pkey}) { | ||
| 3330 | # | ||
| 3331 | # Override bad value in wcache when reanimating $x{y} = \$x{y}; | ||
| 3332 | # | ||
| 3333 | print "%*$id/$pkey vFETCH storing original value in WCACHE: $qval{$val}\n" if $debug_virtual_hash || $debug_refalias; | ||
| 3334 | $wcache->{$pkey} = $val; | ||
| 3335 | } else { | ||
| 3336 | print "%*$id/$pkey vFETCH original value: $qval{$val}\n" if $debug_virtual_hash; | ||
| 3337 | $rcache->{$pkey} = $val; | ||
| 3338 | } | ||
| 3339 | } | ||
| 3340 | $oops->assertions; | ||
| 3341 | print Carp::longmess("DEBUG: vFETCH(@_) returning") if 0; # debug | ||
| 3342 | return $val; | ||
| 3343 | } | ||
| 3344 | |||
| 3345 | sub EXISTS | ||
| 3346 | { | ||
| 3347 | my ($self, $pkey) = @_; | ||
| 3348 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3349 | return undef unless defined $oops; # weak ref | ||
| 3350 | no warnings qw(uninitialized); | ||
| 3351 | print "%*$id/'$pkey' vEXISTS: 0 - dcache\n" if $debug_virtual_hash && exists $dcache->{$pkey}; | ||
| 3352 | return 0 if exists $dcache->{$pkey}; | ||
| 3353 | print "%*$id/'$pkey' vEXISTS: 1 - rcache\n" if $debug_virtual_hash && exists $rcache->{$pkey}; | ||
| 3354 | print "%*$id/'$pkey' vEXISTS: 1 - wcache\n" if $debug_virtual_hash && exists $wcache->{$pkey}; | ||
| 3355 | print "%*$id/'$pkey' vEXISTS: 1 - ovcache\n" if $debug_virtual_hash && exists $ovcache->{$pkey}; | ||
| 3356 | return 1 if exists $rcache->{$pkey} || exists $wcache->{$pkey} || exists $ovcache->{$pkey}; | ||
| 3357 | print "%*$id/'$pkey' vEXISTS: 0 - necache\n" if $debug_virtual_hash && exists $necache->{$pkey}; | ||
| 3358 | return 0 if exists $necache->{$pkey}; | ||
| 3359 | print "%*$id/'$pkey' vEXISTS: 0 - alldelete\n" if $debug_virtual_hash && $vars->{alldelete}; | ||
| 3360 | return 0 if $vars->{alldelete}; | ||
| 3361 | my ($pval, $ptype); | ||
| 3362 | my $loadpkeyQ = $oops->query('loadpkey', execute => [ $id, $pkey ]); | ||
| 3363 | if (($pval, $ptype) = $loadpkeyQ->fetchrow_array) { | ||
| 3364 | $loadpkeyQ->finish(); | ||
| 3365 | if ($ptype) { | ||
| 3366 | $ovcache->{$pkey} = [ $pval, $ptype ]; | ||
| 3367 | } else { | ||
| 3368 | $rcache->{$pkey} = $pval; | ||
| 3369 | } | ||
| 3370 | print "%*$id/'$pkey' vEXISTS: 0 - found in db\n" if $debug_virtual_hash; | ||
| 3371 | return 1; | ||
| 3372 | } else { | ||
| 3373 | $necache->{$pkey} = 1; | ||
| 3374 | print "%*$id/'$pkey' vEXISTS: 0 - not found in db\n" if $debug_virtual_hash; | ||
| 3375 | return 0; | ||
| 3376 | } | ||
| 3377 | $oops->assertions; | ||
| 3378 | } | ||
| 3379 | |||
| 3380 | sub ORIGINAL_PTYPE | ||
| 3381 | { | ||
| 3382 | my ($self, $pkey) = @_; | ||
| 3383 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3384 | no warnings qw(uninitialized); | ||
| 3385 | if (exists $ovcache->{$pkey}) { | ||
| 3386 | print "%$id/$pkey vORIGINAL_PTYPE ovcache: @{$ovcache->{$pkey}}\n" if $debug_virtual_ovals; | ||
| 3387 | return @{$ovcache->{$pkey}}; | ||
| 3388 | } else { | ||
| 3389 | my ($pval, $ptype); | ||
| 3390 | my $loadpkeyQ = $oops->query('loadpkey', execute => [ $id, $pkey ]); | ||
| 3391 | my $found = ($pval, $ptype) = $loadpkeyQ->fetchrow_array; | ||
| 3392 | $loadpkeyQ->finish(); | ||
| 3393 | print "%$id/$pkey vORIGINAL_PTYPE none found\n" if $debug_virtual_ovals && ! $found; | ||
| 3394 | return () unless $found; | ||
| 3395 | $ovcache->{$pkey} = [ $pval, $ptype ] | ||
| 3396 | if $ptype; | ||
| 3397 | print "%$id/$pkey vORIGINAL_PTYPE lookup: $qval{$pval}/$ptype\n" if $debug_virtual_ovals; | ||
| 3398 | return ($pval, $ptype); | ||
| 3399 | } | ||
| 3400 | } | ||
| 3401 | |||
| 3402 | sub ORIGINAL_VALUE | ||
| 3403 | { | ||
| 3404 | my ($self, $pkey) = @_; | ||
| 3405 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3406 | my ($pval, $ptype) = $self->ORIGINAL_PTYPE($pkey); | ||
| 3407 | |||
| 3408 | if (! defined $ptype) { | ||
| 3409 | confess if defined $pval; | ||
| 3410 | } elsif ($ptype eq 'B') { | ||
| 3411 | print "%*$id/'$pkey' is big\n" if $debug_virtual_hash; | ||
| 3412 | $pval = $oops->load_big($id, $pkey); | ||
| 3413 | } elsif ($ptype eq 'R') { | ||
| 3414 | my $ov = $pval if $debug_virtual_hash; | ||
| 3415 | $pval = $oops->load_object($pval); | ||
| 3416 | print "%*$id/'$pkey' is object: *$ov: $qval{$pval}\n" if $debug_virtual_hash; | ||
| 3417 | # | ||
| 3418 | # possible side effects of load_object() include another call | ||
| 3419 | # to FETCH. | ||
| 3420 | # | ||
| 3421 | } elsif ($ptype ne '0') { | ||
| 3422 | confess; | ||
| 3423 | } | ||
| 3424 | no warnings qw(uninitialized); | ||
| 3425 | print "%*$id/$pkey vORIGINAL_VALUE = $qval{$pval}\n" if $debug_virtual_ovals; | ||
| 3426 | |||
| 3427 | |||
| 3428 | return $pval; | ||
| 3429 | } | ||
| 3430 | |||
| 3431 | sub STORE | ||
| 3432 | { | ||
| 3433 | my ($self, $pkey, $pval) = @_; | ||
| 3434 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3435 | return undef unless defined $oops; # weak ref | ||
| 3436 | $oops->workaround27555($pval) if ref $pval; | ||
| 3437 | no warnings qw(uninitialized); | ||
| 3438 | $wcache->{$pkey} = $pval; | ||
| 3439 | $vars->{has_been_deleted}{$pkey} = 1 if $dcache->{$pkey}; | ||
| 3440 | delete $dcache->{$pkey}; | ||
| 3441 | delete $necache->{$pkey}; | ||
| 3442 | delete $rcache->{$pkey}; | ||
| 3443 | $oops->{demandwrite}{$id}++; | ||
| 3444 | $oops->assertions; | ||
| 3445 | print "%*$id/'$pkey' vSTORE into $qval{$qmakeref{$wcache->{$pkey}}}\n" if $debug_refalias; | ||
| 3446 | print "%*$id/'$pkey' vSTORE: $qval{$pval}\n" if $debug_virtual_hash; | ||
| 3447 | return $pval; | ||
| 3448 | } | ||
| 3449 | |||
| 3450 | sub DELETE | ||
| 3451 | { | ||
| 3452 | my ($self, $pkey) = @_; | ||
| 3453 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3454 | no warnings qw(uninitialized); | ||
| 3455 | my $x = exists $wcache->{$pkey} | ||
| 3456 | ? $wcache->{$pkey} | ||
| 3457 | : $rcache->{$pkey}; | ||
| 3458 | $dcache->{$pkey} = 1; # xxx set to $x? | ||
| 3459 | if ($oops->{reftarg}{$id} | ||
| 3460 | && ! $vars->{alldelete} | ||
| 3461 | && ! exists $vars->{original_reference}{$pkey} | ||
| 3462 | && (defined($x) || exists $wcache->{$pkey} || exists $rcache->{$pkey}) | ||
| 3463 | && ! exists $vars->{has_been_deleted}{$pkey}) | ||
| 3464 | { | ||
| 3465 | if ($vars->{keyrefs}{$pkey}) { | ||
| 3466 | print "%$id/'$pkey' vDELETE orignal_reference copy from keyrefs $qaddr{$vars->{keyrefs}{$pkey}} ($vars->{keyrefs}{$pkey})\n" if $debug_refalias; | ||
| 3467 | $vars->{original_reference}{$pkey} = $vars->{keyrefs}{$pkey}; | ||
| 3468 | } else { | ||
| 3469 | my $ref = \$x; | ||
| 3470 | print "%$id/'$pkey' vDELETE orignal_reference copy from keyrefs $qaddr{$ref} ($ref)\n" if $debug_refalias; | ||
| 3471 | $vars->{original_reference}{$pkey} = ref; | ||
| 3472 | } | ||
| 3473 | } | ||
| 3474 | if ($vars->{keyrefs}{$pkey}) { | ||
| 3475 | my $addr = refaddr($vars->{keyrefs}{$pkey}); | ||
| 3476 | print "%*$id/'$pkey' vDELETE MEMORY2KEY($addr) := undef\n" if $debug_memory || $debug_refalias; | ||
| 3477 | $oops->memory2key($vars->{keyrefs}{$pkey}); | ||
| 3478 | } | ||
| 3479 | delete $vars->{keyrefs}{$pkey}; | ||
| 3480 | delete $wcache->{$pkey}; | ||
| 3481 | delete $rcache->{$pkey}; | ||
| 3482 | $oops->{demandwrite}{$id}++; | ||
| 3483 | print "%$id/'$pkey' - vDELETE\n" if $debug_virtual_delete || $debug_virtual_hash; | ||
| 3484 | $oops->assertions; | ||
| 3485 | return $x; | ||
| 3486 | } | ||
| 3487 | |||
| 3488 | sub CLEAR | ||
| 3489 | { | ||
| 3490 | my ($self) = @_; | ||
| 3491 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3492 | return () unless defined $oops; # weak ref | ||
| 3493 | |||
| 3494 | if ($vars->{alldelete}) { | ||
| 3495 | %$wcache = (); | ||
| 3496 | } else { | ||
| 3497 | delete @$wcache{keys %{$vars->{has_been_deleted}}}; | ||
| 3498 | $vars->{pre_clear_wcache} = $wcache; | ||
| 3499 | if ($vars->{keyrefs}) { | ||
| 3500 | for my $pkey (keys %{$vars->{keyrefs}}) { | ||
| 3501 | no warnings qw(uninitialized); | ||
| 3502 | next if exists $vars->{original_reference}{$pkey}; | ||
| 3503 | next if exists $dcache->{$pkey} || exists $vars->{has_been_deleted}{$pkey}; | ||
| 3504 | next unless exists $rcache->{$pkey} || ((undef, undef) = $self->ORIGINAL_PTYPE($pkey)); | ||
| 3505 | print "%$id/'$pkey' vCLEAR orignal_reference copy from keyrefs $qaddr{$vars->{keyrefs}{$pkey}} ($vars->{keyrefs}{$pkey})\n" if $debug_refalias; | ||
| 3506 | $vars->{original_reference}{$pkey} = $vars->{keyrefs}{$pkey}; | ||
| 3507 | } | ||
| 3508 | } | ||
| 3509 | $self->[3] = {}; | ||
| 3510 | } | ||
| 3511 | if ($vars->{keyrefs}) { | ||
| 3512 | for my $pkey (keys %{$vars->{keyrefs}}) { | ||
| 3513 | no warnings qw(uninitialized); | ||
| 3514 | my $ref = $vars->{keyrefs}{$pkey}; | ||
| 3515 | my $addr = refaddr($ref); | ||
| 3516 | print "%*$id/'$pkey' vCLEAR MEMORY2KEY($addr) := undef\n" if $debug_memory || $debug_refalias; | ||
| 3517 | $oops->memory2key($ref); | ||
| 3518 | } | ||
| 3519 | delete $vars->{keyrefs}; | ||
| 3520 | } | ||
| 3521 | delete $vars->{keyrefs}; | ||
| 3522 | %$rcache = (); | ||
| 3523 | %$necache = (); | ||
| 3524 | %$ovcache = (); | ||
| 3525 | %$dcache = (); | ||
| 3526 | $vars->{alldelete} += 1; | ||
| 3527 | $oops->{demandwrite}{$id}++; | ||
| 3528 | $oops->assertions; | ||
| 3529 | print "%*$id vCLEAR\n" if $debug_virtual_hash; | ||
| 3530 | return (); | ||
| 3531 | } | ||
| 3532 | |||
| 3533 | sub GETREFORIG | ||
| 3534 | { | ||
| 3535 | my ($self, $pkey) = @_; | ||
| 3536 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3537 | no warnings qw(uninitialized); | ||
| 3538 | if (exists $dcache->{$pkey} || $vars->{alldelete} || exists $vars->{has_been_deleted}{$pkey}) { | ||
| 3539 | $self->LOAD_SELF_REF($pkey); | ||
| 3540 | if (exists $vars->{original_reference}{$pkey}) { | ||
| 3541 | print "%*$id/$pkey GETREFORIG cached-answer $qaddr{$vars->{original_reference}{$pkey}} ($vars->{original_reference}{$pkey})\n" if $debug_refalias; | ||
| 3542 | return $vars->{original_reference}{$pkey}; | ||
| 3543 | } | ||
| 3544 | # | ||
| 3545 | # If there has been a delete then we want a reference to the orignal value. | ||
| 3546 | # This reference must be shared between references. | ||
| 3547 | # | ||
| 3548 | my $pval; | ||
| 3549 | if (exists $vars->{pre_clear_wcache}{$pkey} && ((undef, undef) = $self->ORIGINAL_PTYPE($pkey))) { | ||
| 3550 | $pval = $vars->{pre_clear_wcache}{$pkey}; | ||
| 3551 | print "%*$id/$pkey GETREFORIG pre-clear-wcache $qval{$pval}\n" if $debug_refalias; | ||
| 3552 | } else { | ||
| 3553 | $pval = $self->ORIGINAL_VALUE($pkey); | ||
| 3554 | print "%*$id/$pkey GETREFORIG original-value $qval{$pval}\n" if $debug_refalias; | ||
| 3555 | } | ||
| 3556 | my $ref = \$pval; | ||
| 3557 | print "%*$id/$pkey GETREFORIG new-answer $qaddr{$ref} ($ref)\n" if $debug_refalias; | ||
| 3558 | return ($vars->{original_reference}{$pkey} = $ref); | ||
| 3559 | } | ||
| 3560 | print "%*$id/$pkey GETREFORIG returning GETREF\n" if $debug_refalias; | ||
| 3561 | return $self->GETREF($pkey); | ||
| 3562 | } | ||
| 3563 | |||
| 3564 | # | ||
| 3565 | # This should be a normal tie method but it isn't so we depend on it's | ||
| 3566 | # being called by workaround27555. | ||
| 3567 | # | ||
| 3568 | # The ordering of this is important: we pre-compute the return reference before | ||
| 3569 | # we figure out what the reference is to. Subsequent calls can return before | ||
| 3570 | # we are done. | ||
| 3571 | # | ||
| 3572 | sub GETREF | ||
| 3573 | { | ||
| 3574 | my $self = shift; | ||
| 3575 | my $pkey = shift; | ||
| 3576 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3577 | no warnings qw(uninitialized); | ||
| 3578 | $self->STORE($pkey, undef) unless $self->EXISTS($pkey); | ||
| 3579 | my $wcache_already = exists $wcache->{$pkey}; | ||
| 3580 | my $ref = \$wcache->{$pkey}; | ||
| 3581 | $vars->{keyrefs}{$pkey} = $ref; | ||
| 3582 | $oops->memory2key($ref, $id, $pkey); | ||
| 3583 | $oops->{demandwrite}{$id}++; | ||
| 3584 | if ($wcache_already) { | ||
| 3585 | print "%*$id/$pkey vGETREF prior wcache: $qval{$wcache->{$pkey}}\n" if $debug_refalias; | ||
| 3586 | } else { | ||
| 3587 | if (exists $dcache->{$pkey}) { | ||
| 3588 | print "%*$id/$pkey vGETREF no wcache - dcache\n" if $debug_refalias; | ||
| 3589 | # done | ||
| 3590 | } elsif (exists $rcache->{$pkey}) { | ||
| 3591 | $wcache->{$pkey} = $rcache->{$pkey}; | ||
| 3592 | delete $rcache->{$pkey}; | ||
| 3593 | print "%*$id/$pkey vGETREF no wcache - rcache: $qval{$wcache->{$pkey}}\n" if $debug_refalias; | ||
| 3594 | } elsif ($vars->{alldelete}) { | ||
| 3595 | print "%*$id/$pkey vGETREF no wcache - alldelete\n" if $debug_refalias; | ||
| 3596 | # done | ||
| 3597 | } else { | ||
| 3598 | $wcache->{$pkey} = $self->ORIGINAL_VALUE($pkey); | ||
| 3599 | print "%*$id/$pkey vGETREF no wcache - original value: $qval{$wcache->{$pkey}}\n" if $debug_refalias; | ||
| 3600 | } | ||
| 3601 | } | ||
| 3602 | print "%*$id/'$pkey' vGETREF MEMORY2KEY $qval{$ref} := *$id/$pkey\n" if $debug_memory || $debug_refalias; | ||
| 3603 | $self->LOAD_SELF_REF($pkey); | ||
| 3604 | return $ref; | ||
| 3605 | } | ||
| 3606 | |||
| 3607 | |||
| 3608 | sub LOAD_SELF_REF | ||
| 3609 | { | ||
| 3610 | my $self = shift; | ||
| 3611 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3612 | return if exists $vars->{ref_to_self_loaded}; | ||
| 3613 | my $searchQ; | ||
| 3614 | if (@_) { | ||
| 3615 | my $pkey = shift; | ||
| 3616 | no warnings qw(uninitialized); | ||
| 3617 | return if exists $vars->{ref_to_pkey_loaded}{$pkey}; | ||
| 3618 | $vars->{ref_to_pkey_loaded}{$pkey} = 1; | ||
| 3619 | print "\%$id searching for references to $qval{$pkey}\n" if $debug_refalias || $debug_virtual_delete || $debug_virtual_save; | ||
| 3620 | $searchQ = $oops->query('reftargkey', execute => [ $id, $pkey ]); | ||
| 3621 | } else { | ||
| 3622 | $vars->{ref_to_self_loaded} = 1; | ||
| 3623 | print "\%$id searching for references to keys\n" if $debug_refalias || $debug_virtual_delete || $debug_virtual_save; | ||
| 3624 | $searchQ = $oops->query('reftargobject', execute => $id); | ||
| 3625 | } | ||
| 3626 | my $refid; | ||
| 3627 | while (($refid) = $searchQ->fetchrow_array()) { | ||
| 3628 | print "\%$id loading self-reference *$refid\n" if $debug_refalias || $debug_virtual_delete || $debug_virtual_save; | ||
| 3629 | unless (exists $oops->{cache}{$refid}) { | ||
| 3630 | $oops->load_object($refid); | ||
| 3631 | my $x = $oops->{cache}{$refid}; # force untie | ||
| 3632 | } | ||
| 3633 | } | ||
| 3634 | $searchQ->finish; | ||
| 3635 | } | ||
| 3636 | |||
| 3637 | |||
| 3638 | sub FIRSTKEY | ||
| 3639 | { | ||
| 3640 | my ($self) = @_; | ||
| 3641 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3642 | |||
| 3643 | # | ||
| 3644 | # We don't share these because someone might want to do | ||
| 3645 | # each() on more than one virutal object at the same time. | ||
| 3646 | # Of course, we could check for that... | ||
| 3647 | # | ||
| 3648 | $vars->{dbeach}->finish if ref($vars->{dbeach}); | ||
| 3649 | if ($vars->{alldelete}) { | ||
| 3650 | $vars->{dbeach} = 1; | ||
| 3651 | print "%*$id vFIRSTKEY - wcache\n" if $debug_virtual_hash || $debug_demand_iterator; | ||
| 3652 | } else { | ||
| 3653 | $vars->{dbeach} = $oops->query('objectload', execute => $id); | ||
| 3654 | print "%*$id vFIRSTKEY - query\n" if $debug_virtual_hash || $debug_demand_iterator; | ||
| 3655 | } | ||
| 3656 | keys %$wcache; # reset iterator | ||
| 3657 | $oops->assertions; | ||
| 3658 | return $self->NEXTKEY(); | ||
| 3659 | } | ||
| 3660 | |||
| 3661 | sub NEXTKEY | ||
| 3662 | { | ||
| 3663 | my ($self, $pkey) = @_; | ||
| 3664 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3665 | my $dbe = $vars->{dbeach}; | ||
| 3666 | return () unless $dbe; | ||
| 3667 | my ($name, $pval, $ptype); | ||
| 3668 | if (ref($dbe) && (($pkey, $pval, $ptype) = $dbe->fetchrow_array())) { | ||
| 3669 | { no warnings; print "%*$id vNEXTKEY: query: '$pkey' ($pval/$ptype)\n" if $debug_virtual_hash || $debug_demand_iterator; } | ||
| 3670 | no warnings qw(uninitialized); | ||
| 3671 | if (exists $dcache->{$pkey}) { | ||
| 3672 | print "%$id - nextpkey deleted\n" if $debug_demand_iterator; | ||
| 3673 | goto &NEXTKEY; | ||
| 3674 | } | ||
| 3675 | unless ($oops->{args}{less_caching}) { | ||
| 3676 | if ($ptype) { | ||
| 3677 | $ovcache->{$pkey} = [ $pval, $ptype ]; | ||
| 3678 | } else { | ||
| 3679 | $rcache->{$pkey} = $pval; | ||
| 3680 | } | ||
| 3681 | } | ||
| 3682 | if (exists $wcache->{$pkey}) { | ||
| 3683 | print "%$id - nextpkey is in wcache\n" if $debug_demand_iterator; | ||
| 3684 | goto &NEXTKEY; | ||
| 3685 | } | ||
| 3686 | return $pkey; | ||
| 3687 | } elsif (defined ($pkey = each(%$wcache))) { | ||
| 3688 | $vars->{dbeach} = 1 | ||
| 3689 | if ref $vars->{dbeach}; | ||
| 3690 | print "%*$id vNEXTKEY: wcache: '$pkey'\n" if $debug_virtual_hash || $debug_demand_iterator; | ||
| 3691 | return $pkey; | ||
| 3692 | } else { | ||
| 3693 | print "%*$id vNEXTKEY: done: undef\n" if $debug_virtual_hash || $debug_demand_iterator; | ||
| 3694 | delete $vars->{dbeach}; | ||
| 3695 | return (); | ||
| 3696 | } | ||
| 3697 | } | ||
| 3698 | |||
| 3699 | # to retrieve just some of the keys of a hash | ||
| 3700 | sub WALK_HASH | ||
| 3701 | { | ||
| 3702 | my ($self, $stride, $key) = @_; | ||
| 3703 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3704 | |||
| 3705 | my $gs; | ||
| 3706 | if (@_ > 2 && defined($key)) { | ||
| 3707 | $gs = $oops->{dbo}->adhoc_query(<<END, execute => [ $id, $key ]); | ||
| 3708 | SELECT pkey, pval, ptype | ||
| 3709 | FROM TP_attribute | ||
| 3710 | WHERE id = ? | ||
| 3711 | AND pkey > ? | ||
| 3712 | ORDER BY pkey | ||
| 3713 | LIMIT $stride | ||
| 3714 | END | ||
| 3715 | } else { | ||
| 3716 | $gs = $oops->{dbo}->adhoc_query(<<END, execute => [ $id ]); | ||
| 3717 | SELECT pkey, pval, ptype | ||
| 3718 | FROM TP_attribute | ||
| 3719 | WHERE id = ? | ||
| 3720 | ORDER BY pkey | ||
| 3721 | LIMIT $stride | ||
| 3722 | END | ||
| 3723 | } | ||
| 3724 | my @ret; | ||
| 3725 | my ($pkey, $pval, $ptype); | ||
| 3726 | while (($pkey, $pval, $ptype) = $gs->fetchrow_array()) { | ||
| 3727 | { no warnings; print "%*$id vAUTO_SLICE: query: '$pkey' ($pval/$ptype)\n" if $debug_virtual_hash}; | ||
| 3728 | no warnings qw(uninitialized); | ||
| 3729 | if (exists $dcache->{$pkey}) { | ||
| 3730 | print "%$id - nextpkey deleted\n" if $debug_demand_iterator; | ||
| 3731 | next; | ||
| 3732 | } | ||
| 3733 | unless ($oops->{args}{less_caching}) { | ||
| 3734 | if ($ptype) { | ||
| 3735 | $ovcache->{$pkey} = [ $pval, $ptype ]; | ||
| 3736 | } else { | ||
| 3737 | $rcache->{$pkey} = $pval; | ||
| 3738 | } | ||
| 3739 | } | ||
| 3740 | if (exists $wcache->{$pkey}) { | ||
| 3741 | print "%$id - nextpkey is in wcache\n" if $debug_demand_iterator; | ||
| 3742 | next; | ||
| 3743 | } | ||
| 3744 | push(@ret, $pkey); | ||
| 3745 | } | ||
| 3746 | $gs->finish(); | ||
| 3747 | return @ret; | ||
| 3748 | } | ||
| 3749 | |||
| 3750 | sub SCALAR | ||
| 3751 | { | ||
| 3752 | my ($self) = @_; | ||
| 3753 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3754 | |||
| 3755 | if ($vars->{alldelete}) { | ||
| 3756 | printf "%%*%d' vSCALAR: previous alldelete, returning %d (%s)\n", $id, scalar(keys(%$wcache)), scalar(%$wcache) if $debug_virtual_hash; | ||
| 3757 | return scalar(keys(%$wcache)); | ||
| 3758 | } | ||
| 3759 | |||
| 3760 | unless ($vars->{originalKeyCount}) { | ||
| 3761 | my $originalCountQ = $oops->query('countkeys', execute => [ $id ]); | ||
| 3762 | ($vars->{originalKeyCount}) = $originalCountQ->fetchrow_array(); | ||
| 3763 | $originalCountQ->finish(); | ||
| 3764 | } | ||
| 3765 | |||
| 3766 | my $doriginal = 0; | ||
| 3767 | my $loadpkeyQ; | ||
| 3768 | my %done; | ||
| 3769 | for my $pkey (keys %$dcache, keys %$wcache) { | ||
| 3770 | next if $done{$pkey}++; | ||
| 3771 | if (exists $rcache->{$pkey}) { | ||
| 3772 | print "%*$id/'$pkey' vSCALAR: in rcache\n" if $debug_virtual_hash; | ||
| 3773 | $doriginal++; | ||
| 3774 | next; | ||
| 3775 | } | ||
| 3776 | if (exists $necache->{$pkey}) { | ||
| 3777 | print "%*$id/'$pkey' vSCALAR: in rcache\n" if $debug_virtual_hash; | ||
| 3778 | next; | ||
| 3779 | } | ||
| 3780 | $loadpkeyQ = $oops->query('loadpkey') | ||
| 3781 | unless $loadpkeyQ; | ||
| 3782 | $loadpkeyQ->execute($id, $pkey); | ||
| 3783 | if (my ($pval, $ptype) = $loadpkeyQ->fetchrow_array) { | ||
| 3784 | $loadpkeyQ->finish(); | ||
| 3785 | if ($ptype) { | ||
| 3786 | $ovcache->{$pkey} = [ $pval, $ptype ]; | ||
| 3787 | } else { | ||
| 3788 | $rcache->{$pkey} = $pval; | ||
| 3789 | } | ||
| 3790 | $doriginal++; | ||
| 3791 | print "%*$id/'$pkey' vSCALAR: found in db\n" if $debug_virtual_hash; | ||
| 3792 | } else { | ||
| 3793 | $necache->{$pkey} = 1; | ||
| 3794 | print "%*$id/'$pkey' vSCALAR: not found in db\n" if $debug_virtual_hash; | ||
| 3795 | } | ||
| 3796 | die if $loadpkeyQ->{Active}; # debug | ||
| 3797 | } | ||
| 3798 | |||
| 3799 | print "%*$id' vSCALAR: original key count = $vars->{originalKeyCount}\n" if $debug_virtual_hash; | ||
| 3800 | print "%*$id' vSCALAR: deleted/replaced count: $doriginal\n" if $debug_virtual_hash; | ||
| 3801 | printf "%%*%d' vSCALAR: write cache count: %d (%s)\n", $id, scalar(keys(%$wcache)), scalar(%$wcache) if $debug_virtual_hash; | ||
| 3802 | |||
| 3803 | my $r = $vars->{originalKeyCount} - $doriginal + keys %$wcache; | ||
| 3804 | print "%*$id' vSCALAR: result = $r\n" if $debug_virtual_hash; | ||
| 3805 | return $r; | ||
| 3806 | } | ||
| 3807 | |||
| 3808 | sub CLEAR_CACHE | ||
| 3809 | { | ||
| 3810 | my ($self) = @_; | ||
| 3811 | my ($oops, $id, $rcache, $wcache, $necache, $ovcache, $dcache, $vars) = @$self; | ||
| 3812 | $necache = (); | ||
| 3813 | $ovcache = (); | ||
| 3814 | if (%$dcache or $vars->{keyrefs}) { | ||
| 3815 | for my $i (keys %$rcache) { | ||
| 3816 | delete $rcache->{$i} | ||
| 3817 | unless exists $dcache->{$i} | ||
| 3818 | or exists $vars->{keyrefs}{$i} | ||
| 3819 | } | ||
| 3820 | } else { | ||
| 3821 | $rcache = (); | ||
| 3822 | } | ||
| 3823 | print "%*$id vCLEAR_CACHE\n" if $debug_virtual_hash; | ||
| 3824 | } | ||
| 3825 | } | ||
| 3826 | # - - - - - - - - - - - | ||
| 3827 | { | ||
| 3828 | package OOPS::Aware; | ||
| 3829 | |||
| 3830 | # methods to overload... | ||
| 3831 | |||
| 3832 | sub object_id_assigned { my ($obj, $id) = @_; } | ||
| 3833 | sub destroy { } | ||
| 3834 | } | ||
| 3835 | |||
| 3836 | # - - - - - - - - - - - | ||
| 3837 | |||
| 3838 | { | ||
| 3839 | package OOPS::FrontEnd; | ||
| 3840 | |||
| 3841 | use Carp qw(longmess); | ||
| 3842 | use Scalar::Util qw(refaddr); | ||
| 3843 | |||
| 3844 | sub new | ||
| 3845 | { | ||
| 3846 | my ($pkg, $oops) = @_; | ||
| 3847 | tie my %x, 'OOPS::NamedObjects', $oops; | ||
| 3848 | my $self = bless \%x, $pkg; | ||
| 3849 | |||
| 3850 | # | ||
| 3851 | # the following is a lie, but hopefully it's one that won't | ||
| 3852 | # be caught. | ||
| 3853 | # | ||
| 3854 | $oops->memory($self, 1); | ||
| 3855 | print "MEMORY OOPS::FE $qval{$self} := 1\n" if $debug_memory; | ||
| 3856 | |||
| 3857 | $tiedvars{$self} = __PACKAGE__.longmess if $debug_tiedvars; | ||
| 3858 | return $self; | ||
| 3859 | } | ||
| 3860 | sub destroy { my $self = shift; { (tied %$self)->destroy; } untie %$self } | ||
| 3861 | sub DESTROY { my $self = shift; delete $tiedvars{$self} if $debug_tiedvars } | ||
| 3862 | sub commit { my $self = shift; my $tied = tied %$self; $tied->[0]->commit(@_); } | ||
| 3863 | sub virtual_object { my $self = shift; my $tied = tied %$self; $tied->[0]->virtual_object(@_); } | ||
| 3864 | sub workaround27555 { my $self = shift; my $tied = tied %$self; $tied->[0]->workaround27555(@_); } | ||
| 3865 | sub load_object { my $self = shift; my $tied = tied %$self; $tied->[0]->load_object(@_); } | ||
| 3866 | sub dbh { my $self = shift; my $tied = tied %$self; $tied->[0]->{dbo}->dbh } | ||
| 3867 | sub clear_cache { my $self = shift; my $tied = tied %$self; $tied->[0]->clear_cache(@_); } | ||
| 3868 | sub lock { my $self = shift; my $tied = tied %$self; $tied->[0]->lock(@_); } | ||
| 3869 | sub oops { my $self = shift; my $tied = tied %$self; return $self } | ||
| 3870 | } | ||
| 3871 | |||
| 3872 | # - - - - - - - - - - - | ||
| 3873 | |||
| 3874 | { | ||
| 3875 | package OOPS::NamedObjects; | ||
| 3876 | |||
| 3877 | use Carp qw(longmess); | ||
| 3878 | use Scalar::Util qw(refaddr); | ||
| 3879 | |||
| 3880 | sub TIEHASH | ||
| 3881 | { | ||
| 3882 | my ($pkg, $oops) = @_; | ||
| 3883 | my $not = tied %{$oops->{named_objects}}; | ||
| 3884 | my $self = bless [ $oops, $not ], $pkg; | ||
| 3885 | |||
| 3886 | # | ||
| 3887 | # the following is a lie, but hopefully it's one that won't | ||
| 3888 | # be caught. | ||
| 3889 | # | ||
| 3890 | $oops->memory($self, 1); | ||
| 3891 | print "MEMORY OOPS::NO $qval{$self} := 1\n" if $debug_memory; | ||
| 3892 | |||
| 3893 | $tiedvars{$self} = __PACKAGE__.longmess if $debug_tiedvars; | ||
| 3894 | return $self; | ||
| 3895 | } | ||
| 3896 | sub destroy { my $self = shift; $self->[0]->DESTROY; } | ||
| 3897 | sub DESTROY { my $self = shift; delete $tiedvars{$self} if $debug_tiedvars } | ||
| 3898 | sub FETCH { my $self = shift; $self->[1]->FETCH(@_) } | ||
| 3899 | sub EXISTS { my $self = shift; $self->[1]->EXISTS(@_) } | ||
| 3900 | sub STORE { my $self = shift; $self->[1]->STORE(@_) } | ||
| 3901 | sub DELETE { my $self = shift; $self->[1]->DELETE(@_) } | ||
| 3902 | sub CLEAR { my $self = shift; $self->[1]->CLEAR(@_) } | ||
| 3903 | sub GETREF { my $self = shift; $self->[1]->GETREF(@_) } | ||
| 3904 | sub FIRSTKEY { my $self = shift; $self->[1]->FIRSTKEY(@_) } | ||
| 3905 | sub NEXTKEY { my $self = shift; $self->[1]->NEXTKEY(@_) } | ||
| 3906 | sub SCALAR { my $self = shift; $self->[1]->SCALAR(@_) } | ||
| 3907 | sub SAVE_SELF { my $self = shift; $self->[1]->SAVE_SELF(@_) } | ||
| 3908 | sub POST_SAVE { my $self = shift; $self->[1]->POST_SAVE(@_) } | ||
| 3909 | } | ||
| 3910 | |||
| 3911 | 1; | ||
| 3912 | |||
| 3913 | __END__ |