OOPS/OOPS1003.pm untested

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