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