WARC/Record.pm

filter: Statementstmt 100% 75 / 75 Branchbran 94.4% 17 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod 100% 19 / 19 totaltotal 99.2% 138 / 139 CC 11 SCAR 8.3
CC
11
Cov
99%
block
6.0
compareTo
4.0
new
3.0
CRAP 2.3
« WARC/Volume.pm ↑ Summary WARC/Record/Replay/HTTP/Request.pm »
1 package WARC::Record; # -*- CPerl -*-
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 our @ISA = qw();
10  
11 28 require WARC; *WARC::Record::VERSION = \$WARC::VERSION;
12 28 require WARC::Date;
13  
14 =head1 NAME
15  
16 WARC::Record - one record from a WARC file
17  
18 =head1 SYNOPSIS
19  
20 use WARC; # or ...
21 use WARC::Volume; # or ...
22 use WARC::Collection;
23  
24 # WARC::Record objects are returned from ->record_at and ->search methods
25  
26 # Construct a record, as when preparing a WARC file
27 $warcinfo = new WARC::Record (type => 'warcinfo');
28  
29 # Accessors
30  
31 $value = $record->field($name);
32  
33 $version = $record->protocol; # analogous to HTTP::Message::protocol
34 $volume = $record->volume;
35 $offset = $record->offset;
36 $record = $record->next;
37  
38 $fields = $record->fields;
39  
40 # Supply a data block for an in-memory record
41 $warcinfo->block(new WARC::Fields ( ... ));
42  
43 =cut
44  
45 28 ▶use overload '<=>' => 'compareTo', 'cmp' => 'compareTo';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 28 ▶use overload fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # This implementation uses a hash as the underlying object.
49 # Keys defined by this class:
50 #
51 # fields
52 # Embedded WARC::Fields object
53 #
54 # Keys defined by this class but overridden and unused in subclasses:
55 #
56 # block
57 # Data block for this record
58  
59 sub _dbg_dump {
60 24 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=1 SCAR=0.0
61  
62 24 my $out = "WARC record [dumping as base class]\n";
63 24 my @out = map {s/^/ /gm; $_} $self->fields->as_string;
64 24 $out .= join("\n", @out);
65  
66 24 return $out;
67 }
68  
69 =head1 DESCRIPTION
70  
71 C<WARC::Record> objects come in two flavors with a common interface.
72 Records read from WARC files are read-only and have meaningful return
73 values from the methods listed in "Methods on records from WARC files".
74 Records constructed in memory can be updated and those same methods all
75 return undef.
76  
77 =head2 Common Methods
78  
79 =over
80  
81 =item $record-E<gt>fields
82  
83 Get the internal C<WARC::Fields> object that contains WARC record headers.
84  
85 =cut
86  
87 3357 ▶sub fields { (shift)->{fields} }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
88  
89 =item $record-E<gt>field( $name )
90  
91 Get the value of the WARC header named $name from the internal
92 C<WARC::Fields> object.
93  
94 =cut
95  
96 sub field {
97 5272 ▶ my $self = shift;
Subroutine field: called CC=1 SCAR=0.0
Pod documented
98 5272 return $self->fields->field(shift);
99 }
100  
101 =item $record E<lt>=E<gt> $other_record
102  
103 =item $record-E<gt>compareTo( $other_record )
104  
105 Compare two C<WARC::Record> objects according to a simple total order:
106 ordering by starting offset for two records in the same file, and by
107 filename of the containing C<WARC::Volume> objects for records in different
108 files. Constructed C<WARC::Record> objects are assumed to come from a
109 volume named "" (the empty string) for this purpose, and are ordered in an
110 arbitrary but stable manner amongst themselves. Distinct constructed
111 C<WARC::Record> objects never compare as equal.
112  
113 Perl constructs a C<==> operator using this method, so WARC record objects
114 will compare as equal iff they refer to the same physical record.
115  
116 =cut
117  
118 sub compareTo {
119 14 ▶ my $a = shift;
Subroutine compareTo: called CC=4 SCAR=13.9
Pod documented
120 14 my $b = shift;
121 14 my $swap = shift;
122  
123 # sort in-memory-only records ahead of on-disk records
124 14 ▶ return $swap ? 1 : -1 if defined $b->volume;
Branch: 2 branches
BranchTrueFalseTotal
if defined $b->volume 7 7 14
$swap ? : 3 4 7
125  
126 # neither record is from a WARC volume
127 7 my $cmp = (Scalar::Util::refaddr $a) <=> (Scalar::Util::refaddr $b);
128  
129 7 ▶ return $swap ? 0-$cmp : 0+$cmp;
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 0 7 7
130 }
131  
132 =back
133  
134 =head3 Convenience getters
135  
136 =over
137  
138 =item $record-E<gt>type
139  
140 Alias for C<$record-E<gt>field('WARC-Type')>.
141  
142 =cut
143  
144 302 ▶sub type { (shift)->field('WARC-Type') }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
145  
146 =item $record-E<gt>id
147  
148 Alias for C<$record-E<gt>field('WARC-Record-ID')>.
149  
150 =cut
151  
152 659 ▶sub id { (shift)->field('WARC-Record-ID') }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
153  
154 =item $record-E<gt>content_length
155  
156 Alias for C<$record-E<gt>field('Content-Length')>.
157  
158 =cut
159  
160 4 ▶sub content_length { (shift)->field('Content-Length') }
Subroutine content_length: called CC=1 SCAR=0.0
Pod documented
161  
162 =item $record-E<gt>date
163  
164 Return the C<'WARC-Date'> field as a C<WARC::Date> object.
165  
166 =cut
167  
168 262 ▶sub date { WARC::Date->from_string((shift)->field('WARC-Date')) }
Subroutine date: called CC=1 SCAR=0.0
Pod documented
169  
170 =back
171  
172 =head2 Methods on records from WARC files
173  
174 These methods all return undef if called on a C<WARC::Record> object that
175 does not represent a record in a WARC file.
176  
177 =over
178  
179 =item $record-E<gt>protocol
180  
181 Return the format and version tag for this record. For WARC 1.0, this
182 method returns 'WARC/1.0'.
183  
184 =cut
185  
186 1 ▶sub protocol { return undef }
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
187  
188 =item $record-E<gt>volume
189  
190 Return the C<WARC::Volume> object representing the file in which this
191 record is located.
192  
193 =cut
194  
195 11 ▶sub volume { return undef }
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
196  
197 =item $record-E<gt>offset
198  
199 Return the file offset at which this record can be found.
200  
201 =cut
202  
203 1 ▶sub offset { return undef }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
204  
205 =item $record-E<gt>logical
206  
207 Return the logical record object for this record. Logical records
208 reassemble WARC continuation segments. Records recorded without using WARC
209 segmentation are their own logical records. Reassembled logical records
210 are also their own logical records.
211  
212 =cut
213  
214 1 ▶sub logical { return undef }
Subroutine logical: called CC=1 SCAR=0.0
Pod documented
215  
216 =item $record-E<gt>segments
217  
218 Return a list of segments for this record. A record recorded without using
219 WARC segmentation, including a segment of a larger logical record, is
220 considered its own only segment. A constructed record is considered to
221 have no segments at all.
222  
223 This method exists on all records to allow
224 C<$record-E<gt>logical-E<gt>segments> to work.
225  
226 =cut
227  
228 1 ▶sub segments { return () }
Subroutine segments: called CC=1 SCAR=0.0
Pod documented
229  
230 =item $record-E<gt>next
231  
232 Return the next C<WARC::Record> in the WARC file that contains this record.
233 Returns an undefined value if called on the last record in a file.
234  
235 =cut
236  
237 1 ▶sub next { return undef }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
238  
239 =item $record-E<gt>open_block
240  
241 Return a tied filehandle that reads the WARC record block.
242  
243 The WARC record block is the content of a WARC record, analogous to the
244 entity body in an C<HTTP::Message>.
245  
246 =cut
247  
248 5 ▶sub open_block { return undef }
Subroutine open_block: called CC=1 SCAR=0.0
Pod documented
249  
250 =item $record-E<gt>open_continued
251  
252 Return a tied filehandle that reads the logical WARC record block.
253  
254 For records that do not use WARC segmentation, this is effectively an alias
255 for C<$record-E<gt>open_block>. For records that span multiple segments,
256 this is an alias for C<$record-E<gt>logical-E<gt>open_block>.
257  
258 =cut
259  
260 1 ▶sub open_continued { return undef }
Subroutine open_continued: called CC=1 SCAR=0.0
Pod documented
261  
262 =item $record-E<gt>replay
263  
264 =item $record-E<gt>replay( as =E<gt> $type )
265  
266 Return a protocol-specific object representing the record contents.
267  
268 This method returns undef if the library does not recognize the protocol
269 message stored in the record and croaks if a requested conversion is not
270 supported.
271  
272 A record with Content-Type "application/http" with an appropriate "msgtype"
273 parameter produces an C<HTTP::Request> or C<HTTP::Response> object. The
274 returned object may be a subclass to support deferred loading of entity
275 bodies.
276  
277 A request to replay a record "as =E<gt> http" attempts to convert whatever
278 is stored in the record to an HTTP exchange, analogous to the "everything
279 is HTTP" interface that C<LWP> provides.
280  
281 =cut
282  
283 1 ▶sub replay { return undef }
Subroutine replay: called CC=1 SCAR=0.0
Pod documented
284  
285 =item $record-E<gt>open_payload
286  
287 Return a tied filehandle that reads the WARC record payload.
288  
289 The WARC record payload is defined as the decoded content of the protocol
290 response or other resource stored in the record. This method returns undef
291 if called on a WARC record that has no payload or that has content that we
292 do not recognize.
293  
294 =cut
295  
296 1 ▶sub open_payload { return undef }
Subroutine open_payload: called CC=1 SCAR=0.0
Pod documented
297  
298 =back
299  
300 =head2 Methods on fresh WARC records
301  
302 =over
303  
304 =item $record = new WARC::Record (I<key> =E<gt> I<value>, ...)
305  
306 Construct a fresh WARC record, suitable for use with C<WARC::Builder>.
307  
308 =cut
309  
310 sub new {
311 9 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
312 9 my %opt = @_;
313  
314 9 foreach my $name (qw/type/)
315 9 ▶ { croak "required field '$name' not specified" unless $opt{$name} }
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{$name} 1 8 9
316  
317 8 my $fields = new WARC::Fields ('WARC-Type' => $opt{type});
318  
319 8 bless { fields => $fields }, $class;
320 }
321  
322 =item $record-E<gt>block
323  
324 =item $record-E<gt>block( $new_value )
325  
326 Get or set the block contents of an in-memory record. This method returns
327 undef if called on a WARC record from a volume and croaks if setting the
328 contents is attempted on a record from a volume.
329  
330 =cut
331  
332 sub block {
333 10 ▶ my $self = shift;
Subroutine block: called CC=6 SCAR=17.9
Pod documented
334 10 my $block = $self->{block};
335  
336 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 4 10
337 6 my $new = shift;
338  
339 6 ▶ if (ref $new) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new) { } 4 2 6
340 4 ▶ if ($new->can('as_block'))
Branch: 3 branches
BranchTrueFalseTotal
if ($new->can('as_block')) { } 1 3 4
elsif ($new->isa('HTTP::Message')) { } 1 2 3
elsif ($new->can('as_string')) { } 1 1 2
341 1 { $self->{block} = $new->as_block }
342 elsif ($new->isa('HTTP::Message'))
343 1 { $self->{block} = $new->as_string(WARC::CRLF()) }
344 elsif ($new->can('as_string'))
345 1 { $self->{block} = $new->as_string }
346 else
347 1 { croak "unrecognized object submitted as WARC record block" }
348 2 } else { $self->{block} = $new }
349  
350 28 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
351 5 $self->{fields}->field('Content-Length', length $self->{block});
352 }
353  
354 9 return $block;
355 }
356  
357 =back
358  
359 =cut
360  
361 28 1;
362 __END__
« WARC/Volume.pm ↑ Summary WARC/Record/Replay/HTTP/Request.pm »