OOPS/OOPS1001.pm untested

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