File Coverage

blib/lib/WARC/Index/Builder.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package WARC::Index::Builder; # -*- CPerl -*-
2              
3 5     5   72708 use strict;
  5         18  
  5         134  
4 5     5   22 use warnings;
  5         7  
  5         268  
5              
6             our @ISA = qw();
7              
8             require WARC; *WARC::Index::Builder::VERSION = \$WARC::VERSION;
9              
10 5     5   26 use Carp;
  5         10  
  5         1847  
11              
12             =head1 NAME
13              
14             WARC::Index::Builder - abstract base class for building indexes
15              
16             =head1 SYNOPSIS
17              
18             use WARC::Index;
19              
20             $cdx_builder = build WARC::Index::File::CDX (...);
21             $sdbm_builder = build WARC::Index::File::SDBM (...);
22              
23             $cdx_entry = $cdx_builder->add($record);
24             $sdbm_builder->add($cdx_entry);
25              
26             =head1 DESCRIPTION
27              
28             C is an abstract base class for constructing indexes
29             on WARC files. The interface is documented here, but implemented in
30             specialized classes for each index type. Some common code has also been
31             moved to this class, and is also documented here.
32              
33             =head2 Methods
34              
35             =over
36              
37             =item $builder-Eadd( ... )
38              
39             Add items to the growing index. All index types accept both WARC records
40             and entries from other indexes, although only metaindex-capable formats use
41             the latter. Any number of items may be added with a single call.
42              
43             Returns nothing.
44              
45             The C base class provides an implementation of this
46             method that dispatches to the internal _add_* methods listed below.
47              
48             =cut
49              
50             sub add {
51 27     27 1 998 my $self = shift;
52              
53 27         61 foreach (@_) {
54 27 100       72 if (not ref)
55             # treat a loose scalar as a WARC volume file name
56 15         77 { $self->_add_volume(mount WARC::Volume ($_)) }
57             else {
58 12 100       186 if ($_->isa('WARC::Volume')) { $self->_add_volume($_) }
  3 100       15  
    100          
    100          
59 3         18 elsif($_->isa('WARC::Index')) { $self->_add_index($_) }
60 2         10 elsif($_->isa('WARC::Index::Entry')) { $self->_add_entry($_) }
61 2         7 elsif($_->isa('WARC::Record::FromVolume')){ $self->_add_record($_) }
62 2         53 else { croak "unrecognized object $_" }
63             }
64             }
65             }
66              
67             =item $builder-Eflush
68              
69             Write any buffered data to the underlying storage. After calling this
70             method, all records added using this builder should be visible. Some index
71             systems do this implicitly; this method is a no-op in those cases.
72              
73             Returns nothing.
74              
75             =cut
76              
77       1 1   sub flush {}
78              
79             =back
80              
81             =head2 Internal Methods
82              
83             =over
84              
85             =item $builder-E_add_record( $record )
86              
87             Add one L to the index.
88              
89             =cut
90              
91             =item $builder-E_add_entry( $entry )
92              
93             Add one L to the index.
94              
95             The C base class provides a default implementation
96             that adds the corresponding record instead.
97              
98             =cut
99              
100             sub _add_entry {
101 5     5   8 my $self = shift;
102 5         7 my $source = shift;
103              
104 5         12 $self->_add_record($source->record);
105             }
106              
107             =item $builder-E_add_index( $index )
108              
109             Add all entries from an enumerable index to the index.
110              
111             The C base class provides a default implementation.
112              
113             =cut
114              
115             sub _add_index {
116 3     3   6 my $self = shift;
117 3         7 my $source = shift;
118              
119 3         11 for (my $entry = $source->first_entry; $entry; $entry = $entry->next)
120 54         159 { $self->_add_entry($entry) }
121             }
122              
123             =item $builder-E_add_volume( $volume )
124              
125             Add all records from a L to the index.
126              
127             The C base class provides a default implementation.
128              
129             =cut
130              
131             sub _add_volume {
132 18     18   30 my $self = shift;
133 18         25 my $volume = shift;
134              
135 18         42 for (my $record = $volume->first_record; $record; $record = $record->next)
136 237         789 { $self->_add_record($record) }
137             }
138              
139             =back
140              
141             =cut
142              
143             1;
144             __END__