| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package KiokuDB::Cmd::Command::GC; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use Carp qw(croak); | ||
| 7 | |||
| 8 | use namespace::clean -except => 'meta'; | ||
| 9 | |||
| 10 | extends qw(KiokuDB::Cmd::Base); | ||
| 11 | |||
| 12 | with qw( | ||
| 13 | KiokuDB::Cmd::WithDSN::Write | ||
| 14 | KiokuDB::Cmd::SpecifiedEntries | ||
| 15 | ); | ||
| 16 | |||
| 17 | has '+verbose' => ( default => 1 ); | ||
| 18 | |||
| 19 | has print => ( | ||
| 20 | traits => [qw(Getopt)], | ||
| 21 | isa => "Bool", | ||
| 22 | is => "ro", | ||
| 23 | cmd_aliases => "p", | ||
| 24 | documentation => "print garbage entries to STDOUT at end", | ||
| 25 | ); | ||
| 26 | |||
| 27 | has mode => ( | ||
| 28 | traits => [qw(Getopt)], | ||
| 29 | is => "ro", | ||
| 30 | default => "naive", | ||
| 31 | cmd_aliases => "m", | ||
| 32 | documentation => "the garbage collection mode to use", | ||
| 33 | predicate => "has_mode", | ||
| 34 | lazy => 1, | ||
| 35 | ); | ||
| 36 | |||
| 37 | my %modes = ( | ||
| 38 | naive => "KiokuDB::GC::Naive", | ||
| 39 | ); | ||
| 40 | |||
| 41 | has class => ( | ||
| 42 | traits => [qw(Getopt)], | ||
| 43 | isa => "Str", | ||
| 44 | is => "ro", | ||
| 45 | lazy_build => 1, | ||
| 46 | documentation => "explicitly specify the collector class (overrides 'mode')", | ||
| 47 | ); | ||
| 48 | |||
| 49 | sub _build_class { | ||
| 50 | my $self = shift; | ||
| 51 | $modes{$self->mode} or croak "Unknown mode: " . $self->mode; | ||
| 52 | } | ||
| 53 | |||
| 54 | has collector => ( | ||
| 55 | traits => [qw(NoGetopt)], | ||
| 56 | is => "ro", | ||
| 57 | lazy_build => 1, | ||
| 58 | ); | ||
| 59 | |||
| 60 | sub _build_collector { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | if ( $self->backend->does("KiokuDB::Backend::Role::GC") ) { | ||
| 64 | $self->backend->new_garbage_collector( | ||
| 65 | command => $self, | ||
| 66 | ( $self->has_mode ? ( mode => $self->mode ) : () ), | ||
| 67 | ( $self->has_class ? ( class => $self->class ) : () ), | ||
| 68 | ); | ||
| 69 | } else { | ||
| 70 | my $class = $self->class; | ||
| 71 | |||
| 72 | Class::MOP::load_class($class); | ||
| 73 | |||
| 74 | return $class->new( | ||
| 75 | backend => $self->backend, | ||
| 76 | verbose => $self->verbose, | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | augment run => sub { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | my $g = $self->collector->garbage; | ||
| 85 | |||
| 86 | if ( $g->size ) { | ||
| 87 | $self->v(sprintf "found %d dead objects\n", $g->size); | ||
| 88 | |||
| 89 | if ( $self->print ) { | ||
| 90 | local $, = local $\ = "\n"; | ||
| 91 | print STDOUT $g->members; | ||
| 92 | } else { | ||
| 93 | $self->v("cleaning...\n"); | ||
| 94 | $self->backend->delete( $g->members ) unless $self->dry_run; | ||
| 95 | } | ||
| 96 | } else { | ||
| 97 | $self->v("no dead objects\n"); | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->try_txn_commit( $self->backend ); | ||
| 101 | }; | ||
| 102 | |||
| 103 | __PACKAGE__->meta->make_immutable; | ||
| 104 | |||
| 105 | __PACKAGE__ | ||
| 106 | |||
| 107 | __END__ |