| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Plucene::Index::SegmentInfos; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Plucene::Index::SegmentInfos - A collection of SegmentInfo objects | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | my $segmentinfos = Plucene::Index::SegmentInfos->new; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | $segmentinfos->read($dir); | 
| 12 |  |  |  |  |  |  | $segmentinfos->write($dir); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | $segmentinfos->add_element(Plucene::Index::SegmentInfo $segment_info); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my Plucene::Index::SegmentInfo @segment_info | 
| 17 |  |  |  |  |  |  | = $segmentinfos->segments; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | This is a collection of Plucene::Index::SegmentInfo objects | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 METHODS | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =cut | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 18 |  |  | 18 |  | 104 | use strict; | 
|  | 18 |  |  |  |  | 37 |  | 
|  | 18 |  |  |  |  | 621 |  | 
| 28 | 18 |  |  | 18 |  | 96 | use warnings; | 
|  | 18 |  |  |  |  | 35 |  | 
|  | 18 |  |  |  |  | 533 |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 18 |  |  | 18 |  | 105 | use Carp; | 
|  | 18 |  |  |  |  | 45 |  | 
|  | 18 |  |  |  |  | 1165 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 18 |  |  | 18 |  | 9776 | use Plucene::Index::SegmentInfo; | 
|  | 18 |  |  |  |  | 51 |  | 
|  | 18 |  |  |  |  | 134 |  | 
| 33 | 18 |  |  | 18 |  | 1154 | use Plucene::Store::InputStream; | 
|  | 18 |  |  |  |  | 67 |  | 
|  | 18 |  |  |  |  | 393 |  | 
| 34 | 18 |  |  | 18 |  | 96 | use Plucene::Store::OutputStream; | 
|  | 18 |  |  |  |  | 36 |  | 
|  | 18 |  |  |  |  | 361 |  | 
| 35 | 18 |  |  | 18 |  | 99 | use File::Slurp; | 
|  | 18 |  |  |  |  | 30 |  | 
|  | 18 |  |  |  |  | 8790 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head2 new | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | my $segmentinfos = Plucene::Index::SegmentInfos->new; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | This will create a new (empty) Plucene::Index::SegmentInfos object. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 200 |  |  | 200 | 1 | 1453 | sub new { bless { segments => [] }, shift } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head2 read | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | $segmentinfos->read($dir); | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | This will read the segments file from the passed directory. | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =cut | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub read { | 
| 56 | 183 |  |  | 183 | 1 | 438 | my ($self, $directory) = @_; | 
| 57 | 183 |  |  |  |  | 1434 | my ($count, @unpack) = unpack "NN/(w/aN)", read_file("$directory/segments"); | 
| 58 | 183 |  |  |  |  | 26531 | my @segs; | 
| 59 | 183 |  |  |  |  | 1333 | while (my ($name, $count) = splice @unpack, 0, 2) { | 
| 60 | 271 |  |  |  |  | 2767 | push @segs, | 
| 61 |  |  |  |  |  |  | bless { | 
| 62 |  |  |  |  |  |  | name      => $name, | 
| 63 |  |  |  |  |  |  | doc_count => $count, | 
| 64 |  |  |  |  |  |  | dir       => $directory, | 
| 65 |  |  |  |  |  |  | } => 'Plucene::Index::SegmentInfo'; | 
| 66 |  |  |  |  |  |  | } | 
| 67 | 183 |  |  |  |  | 642 | $self->{segments} = \@segs; | 
| 68 | 183 |  |  |  |  | 1065 | $self->{counter}  = $count; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head2 write | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | $segmentinfos->write($dir); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | This will write the segments info file out. | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =cut | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub write { | 
| 80 | 56 |  |  | 56 | 1 | 172 | my ($self, $directory) = @_; | 
| 81 | 56 |  |  |  |  | 188 | my $segfile  = "$directory/segments"; | 
| 82 | 56 |  |  |  |  | 163 | my $tempfile = "${segfile}.new"; | 
| 83 | 56 |  |  |  |  | 202 | my @segs     = $self->segments; | 
| 84 | 56 |  |  |  |  | 248 | my $template = "NN" . ("w/a*N" x @segs); | 
| 85 | 97 |  |  |  |  | 783 | my $packed   = pack $template, $self->{counter} || 0, scalar @segs, | 
| 86 | 56 |  | 100 |  |  | 637 | map { $_->name => $_->doc_count } @segs; | 
| 87 | 56 |  |  |  |  | 904 | write_file($tempfile => $packed); | 
| 88 | 56 |  |  |  |  | 49407 | rename($tempfile => $segfile); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =head2 add_element | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | $segmentinfos->add_element(Plucene::Index::SegmentInfo $segment_info); | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | This will add the passed Plucene::Index::SegmentInfo object.. | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =cut | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 257 |  |  | 257 | 1 | 3891 | sub add_element { push @{ $_[0]->{segments} }, $_[1] } | 
|  | 257 |  |  |  |  | 1601 |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head2 info | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | my Plucene::Index::SegmentInfo $info | 
| 104 |  |  |  |  |  |  | = $segmentinfos->info($segment_no); | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | This will return the Plucene::Index::SegmentInfo object at the passed | 
| 107 |  |  |  |  |  |  | segment number. | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =cut | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 1802 |  |  | 1802 | 1 | 7592 | sub info { $_[0]->{segments}->[ $_[1] ] } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 segments | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | my Plucene::Index::SegmentInfo @segment_info | 
| 116 |  |  |  |  |  |  | = $segmentinfos->segments; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | This returns all the Plucene::Index::SegmentInfo onjects in this segment. | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =cut | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 659 |  |  | 659 | 1 | 1222 | sub segments { @{ $_[0]->{segments} } } | 
|  | 659 |  |  |  |  | 4006 |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | 1; |