File Coverage

blib/lib/KinoSearch1/Index/DelDocs.pm
Criterion Covered Total %
statement 51 53 96.2
branch 8 8 100.0
condition n/a
subroutine 15 17 88.2
pod 1 11 9.0
total 75 89 84.2


line stmt bran cond sub pod time code
1             package KinoSearch1::Index::DelDocs;
2 34     34   47804 use strict;
  34         75  
  34         1112  
3 34     34   184 use warnings;
  34         254  
  34         1075  
4 34     34   841 use KinoSearch1::Util::ToolSet;
  34         78  
  34         4609  
5 34     34   187 use base qw( KinoSearch1::Util::BitVector );
  34         71  
  34         19902  
6              
7 34     34   19357 use KinoSearch1::Util::IntMap;
  34         85  
  34         21696  
8              
9             # instance vars:
10             my %num_deletions;
11              
12             sub new {
13 96     96 1 1463 my $self = shift->SUPER::new;
14 96         598 $num_deletions{"$self"} = 0;
15 96         321 return $self;
16             }
17              
18             # Read a deletions file if one exists.
19             sub read_deldocs {
20 3     3 0 12 my ( $self, $invindex, $filename ) = @_;
21              
22             # load the file into memory if it's there
23 3 100       12 if ( $invindex->file_exists($filename) ) {
24 2         9 my $instream = $invindex->open_instream($filename);
25 2         5 my $byte_size;
26 2         27 ( $byte_size, $num_deletions{"$self"} ) = $instream->lu_read('ii');
27 2         29 $self->set_bits( $instream->lu_read("a$byte_size") );
28 2         10 $instream->close;
29             }
30             }
31              
32             # Blast out a hard copy of the deletions held in memory.
33             sub write_deldocs {
34 3     3 0 10 my ( $self, $invindex, $filename, $max_doc ) = @_;
35 3 100       15 if ( $invindex->file_exists($filename) ) {
36 1         7 $invindex->delete_file($filename);
37             }
38 3         16 my $outstream = $invindex->open_outstream($filename);
39              
40             # pad out deldocs->bits
41 3         29 $self->set_capacity($max_doc);
42              
43             # write header followed by deletions data
44 3         19 my $byte_size = ceil( $max_doc / 8 );
45 3         72 $outstream->lu_write(
46             "iia$byte_size", $byte_size,
47             $num_deletions{"$self"}, $self->get_bits,
48             );
49              
50 3         13 $outstream->close;
51             }
52              
53             # Mark a doc as deleted.
54             sub set {
55 5     5 0 665 my ( $self, $doc_num ) = @_;
56             # ... only if it isn't already deleted
57 5 100       39 if ( !$self->get($doc_num) ) {
58 4         24 $self->SUPER::set($doc_num);
59 4         14 $num_deletions{"$self"}++;
60             }
61             }
62              
63             # Delete all the docs represented by a TermDocs object.
64             sub delete_by_term_docs {
65 4     4 0 4 my ( $self, $term_docs ) = @_;
66 4         80 $num_deletions{"$self"} += _delete_by_term_docs( $self, $term_docs );
67             }
68              
69             # Undelete a doc.
70             sub clear {
71 3     3 0 1385 my ( $self, $doc_num ) = @_;
72             # ... only if it was deleted before
73 3 100       20 if ( $self->get($doc_num) ) {
74 1         17 $self->SUPER::clear($doc_num);
75 1         7 $num_deletions{"$self"}--;
76             }
77             }
78              
79 142     142 0 2090 sub get_num_deletions { $num_deletions{"$_[0]"} }
80              
81             # Map around deleted documents.
82             sub generate_doc_map {
83 18     18 0 551 my ( $self, $max, $offset ) = @_;
84 18         259 my $map = $self->_generate_doc_map( $max, $offset );
85 18         153 return KinoSearch1::Util::IntMap->new($map);
86             }
87              
88             # If these get implemented, we'll need to write a range_count(first, last)
89             # method for BitVector.
90 0     0 0 0 sub bulk_set { shift->todo_death }
91 0     0 0 0 sub bulk_clear { shift->todo_death }
92              
93 39     39 0 79 sub close { }
94              
95             sub DESTROY {
96 94     94   17928 my $self = shift;
97 94         353 delete $num_deletions{"$self"};
98 94         8604 $self->SUPER::DESTROY;
99             }
100              
101             1;
102              
103             __END__