File Coverage

blib/lib/KiokuDB/GC/Naive/Mark.pm
Criterion Covered Total %
statement 36 36 100.0
branch 5 6 83.3
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuDB::GC::Naive::Mark;
4 1     1   5 use Moose;
  1         2  
  1         5  
5              
6 1     1   4589 use namespace::clean -except => 'meta';
  1         2  
  1         10  
7              
8             with 'KiokuDB::Role::Scan' => { result_class => "KiokuDB::GC::Naive::Mark::Results" };
9              
10             {
11             package KiokuDB::GC::Naive::Mark::Results;
12 1     1   252 use Moose;
  1         2  
  1         3  
13              
14 1     1   5514 use Set::Object;
  1         6074  
  1         327  
15              
16             has [qw(seen root)] => (
17             isa => "Set::Object",
18             is => "ro",
19             default => sub { Set::Object->new },
20             );
21              
22             __PACKAGE__->meta->make_immutable;
23             }
24              
25             has '+scan_all' => ( default => 0 );
26              
27             has chunk_size => (
28             isa => "Int",
29             is => "ro",
30             default => 100,
31             );
32              
33             sub process_block {
34 9     9 0 35 my ( $self, %args ) = @_;
35              
36 9         22 my ( $block, $res ) = @args{qw(block results)};
37              
38 9         15 my ( $seen, $root ) = map { $res->$_ } qw(seen root);
  18         498  
39              
40 9         234 my ( $backend, $chunk_size ) = ( $self->backend, $self->chunk_size );
41              
42 9         21 $root->insert(map { $_->id } @$block);
  7         179  
43 9         16 @$block = grep { not $seen->includes($_->id) } @$block;
  7         174  
44              
45 9         18 $seen->insert(map { $_->id } @$block);
  7         164  
46              
47 9         11 my @queue;
48              
49             # recursively walk the entries making note of all seen entries
50             loop: {
51 9         12 foreach my $entry ( @$block ) {
  35         101  
52 2013 50       3486 croak("ERROR: Missing entry. Run FSCK") unless $entry;
53              
54 2013         44732 my $id = $entry->id;
55              
56 2013         4018 my @candidates = grep { not $seen->includes($_) } $entry->referenced_ids;
  4006         8967  
57              
58             # even though we technically haven't seen them yet, insert into the
59             # set so that we scan less data
60 2013         4505 $seen->insert(@candidates);
61              
62 2013         3298 push @queue, @candidates;
63             }
64              
65 35 100       338 if ( @queue ) {
66 26 100       504 my @ids = ( @queue > $chunk_size ) ? ( splice @queue, -$chunk_size ) : splice @queue;
67              
68             # reuse the block array so that we throw away unnecessary data
69 26         235 @$block = $backend->get(@ids);
70              
71 26         544 redo loop;
72             }
73             }
74             }
75              
76             __PACKAGE__->meta->make_immutable;
77              
78             __PACKAGE__
79              
80             __END__