| 1 | |||
| 2 | package OOPS::GC; | ||
| 3 | |||
| 4 | use OOPS; | ||
| 5 | require OOPS::Setup; | ||
| 6 | use OOPS::TxHash; | ||
| 7 | require Exporter; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Carp qw(confess); | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | our @EXPORT = qw(gc); | ||
| 14 | |||
| 15 | # scale adjusts the limits | ||
| 16 | our $min_scale_factor = 0.5; | ||
| 17 | our $max_scale_factor = 20; | ||
| 18 | our $scale_up = 1.2; | ||
| 19 | our $scale_down = 0.8; | ||
| 20 | |||
| 21 | # limits | ||
| 22 | our $too_many_todo = 5000; | ||
| 23 | our $work_length = 200; | ||
| 24 | our $clear_batchsize = 50; | ||
| 25 | our $virtual_hash_slice = 20; | ||
| 26 | our $maximum_spill_size = 500; | ||
| 27 | |||
| 28 | # debugging | ||
| 29 | our $debug = 0; | ||
| 30 | our $debug_scale = 0; | ||
| 31 | our $debug_cleaned = 0; | ||
| 32 | |||
| 33 | # counters | ||
| 34 | our $overflow_count = 0; | ||
| 35 | our $readsaved_count = 0; | ||
| 36 | our $error; | ||
| 37 | |||
| 38 | # | ||
| 39 | # Mark objects at the last minute, just before processing | ||
| 40 | # them? Advantage: simpler, less chance of collision with | ||
| 41 | # other users of the database. Disadvantage: more database | ||
| 42 | # query results because objects on the todo list will be | ||
| 43 | # returned. | ||
| 44 | # | ||
| 45 | our $mark_last_minute = 1; | ||
| 46 | |||
| 47 | sub gc | ||
| 48 | { | ||
| 49 | my (%args) = @_; | ||
| 50 | |||
| 51 | $error = undef; | ||
| 52 | my $dbms; | ||
| 53 | my $prefix; | ||
| 54 | my $hasbig = 1; | ||
| 55 | transaction(sub { | ||
| 56 | my $dbh; | ||
| 57 | ($dbh, $dbms, $prefix) = OOPS->dbiconnect(%args, readonly => 1); | ||
| 58 | $dbh->disconnect(); | ||
| 59 | }); | ||
| 60 | require "OOPS/$dbms.pm"; | ||
| 61 | |||
| 62 | # | ||
| 63 | # We'll need to transform the queries... | ||
| 64 | # | ||
| 65 | my $TPsub = sub { | ||
| 66 | my ($query) = @_; | ||
| 67 | $query =~ s/TP_/$prefix/g; | ||
| 68 | return $query; | ||
| 69 | }; | ||
| 70 | |||
| 71 | my $gcgen; | ||
| 72 | transaction(sub { | ||
| 73 | my ($dbh, $dbms, $prefix, $dbo) = OOPS->dbiconnect(%args); | ||
| 74 | transaction_lock($dbo); | ||
| 75 | undef $dbo; | ||
| 76 | ($gcgen) = $dbh->selectrow_array(&$TPsub(<<END)); | ||
| 77 | SELECT pval | ||
| 78 | FROM TP_attribute | ||
| 79 | WHERE id = 2 AND pkey = 'GC GENERATION' | ||
| 80 | END | ||
| 81 | unless ($gcgen) { | ||
| 82 | print "### ERROR: ".$dbh->errstr()."\n"; | ||
| 83 | confess $dbh->errstr() | ||
| 84 | } | ||
| 85 | my $old = $gcgen++; | ||
| 86 | print "# GC: New generation number: $gcgen\n"; | ||
| 87 | $dbh->do(&$TPsub(<<END), undef, $gcgen, $old) or confess $dbh->errstr; | ||
| 88 | UPDATE TP_attribute | ||
| 89 | SET pval = ? | ||
| 90 | WHERE id = 2 AND pkey = 'GC GENERATION' AND pval = ? | ||
| 91 | END | ||
| 92 | $dbh->do(&$TPsub(<<END), undef, $gcgen, $OOPS::last_reserved_oid) or confess $dbh->errstr; | ||
| 93 | UPDATE TP_object | ||
| 94 | SET gcgeneration = ? | ||
| 95 | WHERE id <= ? | ||
| 96 | END | ||
| 97 | $dbh->do(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 98 | DELETE FROM TP_attribute | ||
| 99 | WHERE id = $OOPS::gc_overflow_id | ||
| 100 | END | ||
| 101 | $dbh->commit; | ||
| 102 | }); | ||
| 103 | |||
| 104 | exit if $debug == 99; | ||
| 105 | |||
| 106 | my $foo; | ||
| 107 | my %todo_real = ( | ||
| 108 | 1 => \$foo, # virtual hash | ||
| 109 | 2 => undef, # hash | ||
| 110 | 3 => undef, # hash | ||
| 111 | ); | ||
| 112 | |||
| 113 | # | ||
| 114 | # Stating at the root nodes, follow all references changing the | ||
| 115 | # GC generation as we go. | ||
| 116 | # | ||
| 117 | |||
| 118 | my $bailout = 0; | ||
| 119 | my $scale_factor = 1; | ||
| 120 | my $last_overflow_rows = 0; | ||
| 121 | |||
| 122 | while (%todo_real && ! $bailout) { | ||
| 123 | printf "# GC: Top of loop, todo = %d, scale = %.1f\n", scalar(keys %todo_real), $scale_factor if $debug || $debug_scale; | ||
| 124 | my $accomplished = 0; | ||
| 125 | my $overflow_rows = 0; | ||
| 126 | my $restarts = 0; | ||
| 127 | transaction(sub { | ||
| 128 | my $th = tie my %todo, 'OOPS::TxHash', \%todo_real or confess; | ||
| 129 | |||
| 130 | my ($dbh, $dbms, $prefix, $dbo) = OOPS->dbiconnect(%args); | ||
| 131 | transaction_lock($dbo) if ++$restarts > 3; | ||
| 132 | |||
| 133 | my ($curgcgen) = $dbh->selectrow_array(&$TPsub(<<END)); | ||
| 134 | SELECT pval | ||
| 135 | FROM TP_attribute | ||
| 136 | WHERE id = 2 AND pkey = 'GC GENERATION' | ||
| 137 | END | ||
| 138 | if ($curgcgen != $gcgen) { | ||
| 139 | $bailout = "Another GC is running, only one allowed at a time"; | ||
| 140 | print "# GC: curgen($curgcgen) != gcgen($gcgen)\n" if $debug; | ||
| 141 | %todo_real = (); | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | my $overflow_rowsQ = $dbo->adhoc_query(<<END); | ||
| 146 | SELECT count(*) | ||
| 147 | FROM TP_attribute | ||
| 148 | WHERE id = $OOPS::gc_overflow_id | ||
| 149 | END | ||
| 150 | $overflow_rowsQ->execute() || confess; | ||
| 151 | ($overflow_rows) = $overflow_rowsQ->fetchrow_array(); | ||
| 152 | $overflow_rowsQ->finish; | ||
| 153 | |||
| 154 | my $work = 0; | ||
| 155 | my $objects_done = 0; | ||
| 156 | my $nlimit = int($virtual_hash_slice * $scale_factor); | ||
| 157 | |||
| 158 | printf "# GC: Inner loop, work = %d, todo = %d\n", $work, scalar(%todo) if $debug; | ||
| 159 | while ($work < $work_length * $scale_factor && %todo && %todo <= $too_many_todo * $scale_factor) { | ||
| 160 | my ($id, $key); | ||
| 161 | 1 while not (($id, $key) = each(%todo)); # OOPS::TxHash better be right about SCALAR... | ||
| 162 | |||
| 163 | printf "# GC: working on %d/%s\n", $id, defined($key) ? $key : "<undef>" if $debug; | ||
| 164 | |||
| 165 | my $q; | ||
| 166 | my $oldkey = 0; | ||
| 167 | my $limit = defined($key) ? "LIMIT $nlimit" : ""; | ||
| 168 | if (defined $key && ! ref($key)) { | ||
| 169 | print "# GC: continuing through a virtual hash\n" if $debug; | ||
| 170 | $q = $dbo->adhoc_query(<<END, execute => [ $id, $key, $gcgen]) or confess $dbh->errstr; | ||
| 171 | SELECT o.id, o.otype, o.virtual, a.pkey | ||
| 172 | FROM TP_object AS o, | ||
| 173 | TP_attribute AS a | ||
| 174 | WHERE a.id = ? | ||
| 175 | AND a.pkey > ? | ||
| 176 | AND a.ptype = 'R' | ||
| 177 | AND o.id = DBO:CAST:PGBYTEA2INT(a.pval) | ||
| 178 | AND o.gcgeneration < ? | ||
| 179 | ORDER BY a.pkey | ||
| 180 | LIMIT $nlimit | ||
| 181 | END | ||
| 182 | $oldkey = 1; | ||
| 183 | } else { | ||
| 184 | print "# GC: starting on a fresh object\n" if $debug; | ||
| 185 | $objects_done++; | ||
| 186 | if ($mark_last_minute) { | ||
| 187 | print "# GC: marking $id as gen $gcgen\n" if $debug > 5; | ||
| 188 | $q = $dbo->adhoc_query(<<END, execute => [$gcgen, $id, $gcgen]) or confess $dbh->errstr; | ||
| 189 | UPDATE TP_object | ||
| 190 | SET gcgeneration = ? | ||
| 191 | WHERE id = ? | ||
| 192 | AND gcgeneration < ? | ||
| 193 | END | ||
| 194 | $q->finish; | ||
| 195 | } | ||
| 196 | $q = $dbo->adhoc_query(<<END, execute => [ $id, $gcgen ]) or confess $dbh->errstr; | ||
| 197 | SELECT o.id, o.otype, o.virtual, a.pkey | ||
| 198 | FROM TP_object AS o, | ||
| 199 | TP_attribute AS a | ||
| 200 | WHERE a.id = ? | ||
| 201 | AND a.ptype = 'R' | ||
| 202 | AND o.id = DBO:CAST:PGBYTEA2INT(a.pval) | ||
| 203 | AND o.gcgeneration < ? | ||
| 204 | ORDER BY a.pkey | ||
| 205 | $limit | ||
| 206 | END | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | my ($count, $newkey) = get_todo($q, \%todo); | ||
| 211 | $work += $count || 1; | ||
| 212 | |||
| 213 | print "# GC: found $count new things to worry about\n" if $debug; | ||
| 214 | |||
| 215 | # | ||
| 216 | # mark objects as done as they are added to the todo list | ||
| 217 | # | ||
| 218 | if ($mark_last_minute) { | ||
| 219 | if ($limit and $count == $nlimit) { | ||
| 220 | $todo{$id} = $newkey; | ||
| 221 | } else { | ||
| 222 | delete $todo{$id}; | ||
| 223 | } | ||
| 224 | } elsif ($limit and $count == $nlimit) { | ||
| 225 | if ($oldkey) { | ||
| 226 | print "# GC: partial set from a virtual hash that was partway through '$key' to '$newkey'\n" if $debug; | ||
| 227 | $q = $dbo->adhoc_query(<<END, execute => [$gcgen, $id, $key, $newkey, $gcgen]) or confess $dbh->errstr; | ||
| 228 | UPDATE TP_object AS o, | ||
| 229 | TP_attribute AS a | ||
| 230 | SET o.gcgeneration = ? | ||
| 231 | WHERE a.id = ? | ||
| 232 | AND a.pkey > ? | ||
| 233 | AND a.pkey <= ? | ||
| 234 | AND a.ptype = 'R' | ||
| 235 | AND o.id = DBO:CAST:PGBYTEA2INT(a.pval) | ||
| 236 | AND o.gcgeneration < ? | ||
| 237 | END | ||
| 238 | } else { | ||
| 239 | # partial set from a virtual hash that just started | ||
| 240 | print "# GC: partial set from a new virtual hash ... upto '$newkey'\n" if $debug; | ||
| 241 | $q = $dbo->adhoc_query(<<END, execute => [$gcgen, $id, $newkey, $gcgen]) or confess $dbh->errstr; | ||
| 242 | UPDATE TP_object AS o, | ||
| 243 | TP_attribute AS a | ||
| 244 | SET o.gcgeneration = ? | ||
| 245 | WHERE a.id = ? | ||
| 246 | AND a.pkey <= ? | ||
| 247 | AND a.ptype = 'R' | ||
| 248 | AND o.id = a.pval | ||
| 249 | AND o.gcgeneration < ? | ||
| 250 | END | ||
| 251 | } | ||
| 252 | $todo{$id} = $newkey; | ||
| 253 | } elsif ($limit && $oldkey) { | ||
| 254 | print "# GC: finish a partially done virtual hash ... from '$key'\n" if $debug; | ||
| 255 | $q = $dbo->adhoc_query(<<END, execute => [$gcgen, $id, $key, $gcgen]) or confess $dbh->errstr; | ||
| 256 | UPDATE TP_object AS o, | ||
| 257 | TP_attribute AS a | ||
| 258 | SET o.gcgeneration = ? | ||
| 259 | WHERE a.id = ? | ||
| 260 | AND a.pkey > ? | ||
| 261 | AND a.ptype = 'R' | ||
| 262 | AND o.id = DBO:CAST:PGBYTEA2INT(a.pval) | ||
| 263 | AND o.gcgeneration < ? | ||
| 264 | END | ||
| 265 | delete $todo{$id}; | ||
| 266 | } else { | ||
| 267 | print "# GC: finished an object\n" if $debug; | ||
| 268 | $q = $dbo->adhoc_query(<<END, execute => [$gcgen, $id, $gcgen]) or confess $dbh->errstr; | ||
| 269 | UPDATE TP_object AS o, | ||
| 270 | TP_attribute AS a | ||
| 271 | SET o.gcgeneration = ? | ||
| 272 | WHERE a.id = ? | ||
| 273 | AND a.ptype = 'R' | ||
| 274 | AND o.id = a.pval | ||
| 275 | AND o.gcgeneration < ? | ||
| 276 | END | ||
| 277 | delete $todo{$id}; | ||
| 278 | } | ||
| 279 | |||
| 280 | } | ||
| 281 | |||
| 282 | # | ||
| 283 | # At this point, we've either done enough work to finish the | ||
| 284 | # transaction or we've run out of things to do. First, if we've | ||
| 285 | # run out, see if other processes have left us some new stuff | ||
| 286 | # for our todo list. | ||
| 287 | # | ||
| 288 | |||
| 289 | while (! %todo) { | ||
| 290 | print "# GC: TODO is empty, look for more\n" if $debug; | ||
| 291 | my $q = $dbo->adhoc_query(<<END, execute => [$gcgen]) or confess $dbh->errstr; | ||
| 292 | SELECT o.id, o.otype, o.virtual, a.pkey | ||
| 293 | FROM TP_object AS o, | ||
| 294 | TP_attribute AS a | ||
| 295 | WHERE a.id = $OOPS::gc_overflow_id | ||
| 296 | AND o.id = DBO:CAST:PGBYTEA2INT(a.pkey) | ||
| 297 | AND o.gcgeneration < ? | ||
| 298 | ORDER BY a.pkey | ||
| 299 | LIMIT $nlimit | ||
| 300 | END | ||
| 301 | my ($count, $pkey) = get_todo($q, \%todo); | ||
| 302 | $q->finish; | ||
| 303 | my @args; | ||
| 304 | if ($count == $nlimit) { | ||
| 305 | $q = $dbo->adhoc_query(<<END) or confess $dbh->errstr; | ||
| 306 | DELETE FROM TP_attribute | ||
| 307 | WHERE id = $OOPS::gc_overflow_id | ||
| 308 | AND pkey <= ? | ||
| 309 | END | ||
| 310 | @args = ($pkey); | ||
| 311 | } else { | ||
| 312 | $q = $dbo->adhoc_query(<<END) or confess $dbh->errstr; | ||
| 313 | DELETE FROM TP_attribute | ||
| 314 | WHERE id = $OOPS::gc_overflow_id | ||
| 315 | END | ||
| 316 | @args = (); | ||
| 317 | } | ||
| 318 | my $r = $q->execute(@args); | ||
| 319 | confess unless $r; | ||
| 320 | $objects_done += $r - $count; | ||
| 321 | $q->finish; | ||
| 322 | $readsaved_count += $count; | ||
| 323 | print "# GC: TODO was empty, found $count more\n" if $debug; | ||
| 324 | last unless $count; | ||
| 325 | } | ||
| 326 | |||
| 327 | # | ||
| 328 | # Alternatively, if we're gotten here because we've done enough | ||
| 329 | # let's check to make sure our todo list isn't getting too big. If it | ||
| 330 | # is, let's save some of it to the database. | ||
| 331 | # | ||
| 332 | |||
| 333 | if (%todo > $too_many_todo * $scale_factor) { | ||
| 334 | print "# GC: TODO is overflowing, save some for later\n" if $debug; | ||
| 335 | my $q1 = $dbo->adhoc_query(<<END) or confess $dbh->errstr; | ||
| 336 | SELECT COUNT(*) | ||
| 337 | FROM TP_attribute | ||
| 338 | WHERE id = $OOPS::gc_overflow_id | ||
| 339 | AND pkey = ? | ||
| 340 | END | ||
| 341 | my $q2 = $dbo->adhoc_query(<<END) or confess $dbh->errstr; | ||
| 342 | INSERT INTO TP_attribute | ||
| 343 | VALUES ($OOPS::gc_overflow_id, ?, '', '0') | ||
| 344 | END | ||
| 345 | my $spilled = 0; | ||
| 346 | for my $id (keys %todo) { | ||
| 347 | next if defined $todo{$id}; | ||
| 348 | $q1->execute($id) or confess $dbh->errstr; | ||
| 349 | my ($rc) = $q1->fetchrow_array(); | ||
| 350 | next if $rc > 0; | ||
| 351 | $q2->execute($id) or confess $dbh->errstr; | ||
| 352 | delete $todo{$id}; | ||
| 353 | $overflow_count++; | ||
| 354 | last if %todo < $too_many_todo * $scale_factor / 2; | ||
| 355 | last if ++$spilled > $maximum_spill_size * $scale_factor; | ||
| 356 | } | ||
| 357 | $q1->finish(); | ||
| 358 | $q2->finish(); | ||
| 359 | } | ||
| 360 | # | ||
| 361 | # Okay, time to record what we've done and go 'round again. | ||
| 362 | # | ||
| 363 | |||
| 364 | print "# GC commit\n" if $debug; | ||
| 365 | $dbh->commit or confess $dbh->errstr; | ||
| 366 | $th->commit; | ||
| 367 | $accomplished = $objects_done; | ||
| 368 | }); | ||
| 369 | printf "# GC: accomplished: %d change in overflow rows: %d old scale: %.2f restarts: %d\n", $accomplished, $overflow_rows - $last_overflow_rows, $scale_factor, $restarts if $debug_scale; | ||
| 370 | if ($accomplished < $overflow_rows - $last_overflow_rows) { | ||
| 371 | # we didn't get as much done as new work came in. | ||
| 372 | if ($restarts < 5) { | ||
| 373 | # try to do more work in each transaction | ||
| 374 | $scale_factor *= $scale_up; | ||
| 375 | } else { | ||
| 376 | # maybe we're trying to do too much at once? | ||
| 377 | $scale_factor *= $scale_down; | ||
| 378 | } | ||
| 379 | } else { | ||
| 380 | # let's do less at once so we have a smaller impact | ||
| 381 | # on other transactions | ||
| 382 | $scale_factor *= ((1+$scale_down)/2); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | if ($bailout) { | ||
| 387 | print STDERR "# GC: $bailout\n"; | ||
| 388 | $error = $bailout; | ||
| 389 | return undef; | ||
| 390 | } | ||
| 391 | |||
| 392 | my $total = 0; | ||
| 393 | |||
| 394 | # | ||
| 395 | # Now, we want to run a scan across all objects but we don't | ||
| 396 | # want to lock the whole database as we do it. | ||
| 397 | # | ||
| 398 | |||
| 399 | print "# GC: Scanning for objects we didn't mark\n" if $debug || $debug_cleaned; | ||
| 400 | |||
| 401 | for(;;) { | ||
| 402 | my $idlast = -1; | ||
| 403 | my $last; | ||
| 404 | my $restarts = 0; | ||
| 405 | transaction(sub { | ||
| 406 | my ($dbh, $dbms, $prefix, $dbo) = OOPS->dbiconnect(%args); | ||
| 407 | transaction_lock($dbo) if ++$restarts > 3; | ||
| 408 | my $q = $dbh->prepare(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 409 | SELECT id | ||
| 410 | FROM TP_object | ||
| 411 | WHERE id > ? | ||
| 412 | AND gcgeneration < ? | ||
| 413 | LIMIT $clear_batchsize | ||
| 414 | END | ||
| 415 | $q->execute($idlast, $gcgen) or confess $dbh->errstr; | ||
| 416 | my @idset; | ||
| 417 | while (my ($id) = $q->fetchrow_array()) { | ||
| 418 | push(@idset, $id); | ||
| 419 | } | ||
| 420 | $q->finish(); | ||
| 421 | return unless @idset; | ||
| 422 | |||
| 423 | print "# GC: found @idset, will delete 'em\n" if $debug || $debug_cleaned; | ||
| 424 | |||
| 425 | my $decrement = $dbo->adhoc_query(<<END) or confess $dbh->errstr; | ||
| 426 | # DBO:name GC::decrement | ||
| 427 | :sqlite2: | ||
| 428 | UPDATE TP_object | ||
| 429 | SET refs = refs - 1 | ||
| 430 | WHERE id IN ( | ||
| 431 | SELECT pval | ||
| 432 | FROM TP_attribute | ||
| 433 | WHERE id = ? | ||
| 434 | AND ptype = 'R' | ||
| 435 | ) | ||
| 436 | :sqlite: | ||
| 437 | UPDATE TP_object | ||
| 438 | SET refs = refs - 1 | ||
| 439 | WHERE id IN ( | ||
| 440 | SELECT pval | ||
| 441 | FROM TP_attribute | ||
| 442 | WHERE id = ? | ||
| 443 | AND ptype = 'R' | ||
| 444 | ) | ||
| 445 | :mysql: | ||
| 446 | UPDATE TP_object AS o, TP_attribute AS a | ||
| 447 | SET o.refs = o.refs - 1 | ||
| 448 | WHERE a.id = ? | ||
| 449 | AND a.ptype = 'R' | ||
| 450 | AND o.id = a.pval | ||
| 451 | :pg: | ||
| 452 | UPDATE TP_object | ||
| 453 | SET refs = refs - 1 | ||
| 454 | FROM TP_attribute | ||
| 455 | WHERE TP_attribute.id = ? | ||
| 456 | AND TP_attribute.ptype = 'R' | ||
| 457 | AND TP_object.id = DBO:CAST:PGBYTEA2INT(TP_attribute.pval) | ||
| 458 | END | ||
| 459 | my $ro = $dbh->prepare(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 460 | DELETE FROM TP_object | ||
| 461 | WHERE id = ? | ||
| 462 | END | ||
| 463 | my $ra = $dbh->prepare(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 464 | DELETE FROM TP_attribute | ||
| 465 | WHERE id = ? | ||
| 466 | END | ||
| 467 | my $ref = $dbh->prepare(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 468 | UPDATE TP_attribute | ||
| 469 | SET pval = NULL, ptype = '0' | ||
| 470 | WHERE pval = ? | ||
| 471 | END | ||
| 472 | for my $id (@idset) { | ||
| 473 | $decrement->execute($id) or confess $dbh->errstr; | ||
| 474 | $ro->execute($id) or confess $dbh->errstr; | ||
| 475 | $ra->execute($id) or confess $dbh->errstr; | ||
| 476 | $ref->execute($id) or confess $dbh->errstr; | ||
| 477 | } | ||
| 478 | |||
| 479 | if ($hasbig) { | ||
| 480 | my $rb = $dbh->prepare(&$TPsub(<<END)) or confess $dbh->errstr; | ||
| 481 | DELETE FROM TP_big | ||
| 482 | WHERE id = ? | ||
| 483 | END | ||
| 484 | for my $id (@idset) { | ||
| 485 | $rb->execute($id) or confess $dbh->errstr; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | $dbh->commit or confess $dbh->errstr; | ||
| 489 | $total += @idset; | ||
| 490 | $last = $idset[$clear_batchsize-1]; | ||
| 491 | }); | ||
| 492 | last unless $last; | ||
| 493 | $idlast = $last; | ||
| 494 | }; | ||
| 495 | |||
| 496 | print "# GC $total objects removed\n" if $debug; | ||
| 497 | return $total; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub get_todo | ||
| 501 | { | ||
| 502 | my ($q, $todoref) = @_; | ||
| 503 | |||
| 504 | my ($oid, $otype, $ovirtual, $pkey); | ||
| 505 | my $count = 0; | ||
| 506 | while (($oid, $otype, $ovirtual, $pkey) = $q->fetchrow_array()) { | ||
| 507 | print "# GC: more todo $oid $otype $ovirtual $pkey\n" if $debug; | ||
| 508 | $count++; | ||
| 509 | next if exists $todoref->{$oid}; | ||
| 510 | if ($ovirtual) { | ||
| 511 | $todoref->{$oid} = \'VIRTUAL'; | ||
| 512 | } else { | ||
| 513 | $todoref->{$oid} = undef; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | $q->finish; | ||
| 517 | return $count unless wantarray(); | ||
| 518 | return ($count, $pkey); | ||
| 519 | } | ||
| 520 | |||
| 521 | # | ||
| 522 | # Hopefully this will avoid persistent deadlock problems | ||
| 523 | # with mysql. | ||
| 524 | # | ||
| 525 | sub transaction_lock | ||
| 526 | { | ||
| 527 | my ($dbo) = @_; | ||
| 528 | $dbo->adhoc_query(<<END, execute => [1]); | ||
| 529 | SELECT counter | ||
| 530 | FROM TP_object | ||
| 531 | WHERE id = ? | ||
| 532 | END | ||
| 533 | } | ||
| 534 | |||
| 535 | 1; | ||
| 536 | |||
| 537 | __END__ |