| 1 | package WARC::Record::Sponge; # -*- CPerl -*- | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | our @ISA = qw(WARC::Record); | |||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use File::Temp qw/:seekable/; # imports SEEK_* constants | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use MIME::Base32 qw/encode_rfc3548/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Symbol qw//; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 1 | ▶ | use WARC; *WARC::Record::Sponge::VERSION = \$WARC::VERSION; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 1 | require WARC::Record; | |||||||||||
| 18 | |||||||||||||
| 19 | 1 | ▶ | use overload '*{}' => \&_as_handle; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use overload fallback => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | |||||||||||||
| 22 | =head1 NAME | ||||||||||||
| 23 | |||||||||||||
| 24 | WARC::Record::Sponge - data sponge for WARC records | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||
| 27 | |||||||||||||
| 28 | use Digest::SHA; | ||||||||||||
| 29 | use WARC::Builder; | ||||||||||||
| 30 | use WARC::Record::Sponge; | ||||||||||||
| 31 | |||||||||||||
| 32 | $builder = new WARC::Builder ( ... ); | ||||||||||||
| 33 | $sponge = new WARC::Record::Sponge ( type => 'response' ); | ||||||||||||
| 34 | $sponge->begin_digest(block => sha1 => new Digest::SHA ('sha1')); | ||||||||||||
| 35 | |||||||||||||
| 36 | while (<$socket>) { | ||||||||||||
| 37 | print $sponge $_; | ||||||||||||
| 38 | # ... other processing ... | ||||||||||||
| 39 | $sponge->begin_digest(payload => sha1 => new Digest::SHA ('sha1')) | ||||||||||||
| 40 | if $end_of_headers_reached; | ||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | $builder->add($sponge); # add to growing WARC volume | ||||||||||||
| 44 | |||||||||||||
| 45 | =cut | ||||||||||||
| 46 | |||||||||||||
| 47 | # This implementation uses a filehandle tied to a hash. | ||||||||||||
| 48 | # Keys defined in the inner hash: | ||||||||||||
| 49 | # | ||||||||||||
| 50 | # digests | ||||||||||||
| 51 | # Hash mapping digest names to [<tag>, <digest object>] pairs | ||||||||||||
| 52 | # file | ||||||||||||
| 53 | # File::Temp object | ||||||||||||
| 54 | # handle | ||||||||||||
| 55 | # Tied file handle | ||||||||||||
| 56 | # length | ||||||||||||
| 57 | # Number of valid bytes in the temporary file | ||||||||||||
| 58 | # writing | ||||||||||||
| 59 | # Current mode; true if in "soak" phase | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||
| 62 | |||||||||||||
| 63 | C<WARC::Record::Sponge> objects provide a streaming interface for | ||||||||||||
| 64 | constructing WARC records as data is received using a temporary file to | ||||||||||||
| 65 | store the record content. This allows recording records that exceed | ||||||||||||
| 66 | available memory. | ||||||||||||
| 67 | |||||||||||||
| 68 | This class provides objects with a tied filehandle interface using a data | ||||||||||||
| 69 | sponge model. In the "soak" phase, data is written to the handle, along | ||||||||||||
| 70 | with markers indicating the computation of digests for that data. In the | ||||||||||||
| 71 | "squeeze" phase, data is read back from the handle and the digests are | ||||||||||||
| 72 | collected. The object can then be reset to return to the "soak" phase to | ||||||||||||
| 73 | collect new data and "squeezed" again. All digest markers are removed upon | ||||||||||||
| 74 | returning to the "soak" phase. The handle is seekable in the "squeeze" | ||||||||||||
| 75 | phase, but append-only in the "soak" phase. | ||||||||||||
| 76 | |||||||||||||
| 77 | A C<WARC::Record::Sponge> isa C<WARC::Record> and inherits the C<fields> | ||||||||||||
| 78 | method. Header fields may be set on a C<WARC::Record::Sponge>, but all | ||||||||||||
| 79 | fields other than "WARC-Type" are erased when the C<reset> method is used. | ||||||||||||
| 80 | |||||||||||||
| 81 | =head2 Methods | ||||||||||||
| 82 | |||||||||||||
| 83 | =over | ||||||||||||
| 84 | |||||||||||||
| 85 | =item $sponge = new WARC::Record::Sponge ( ... ) | ||||||||||||
| 86 | |||||||||||||
| 87 | =cut | ||||||||||||
| 88 | |||||||||||||
| 89 | 1 | our $TmpDir = File::Spec->tmpdir; | |||||||||||
| 90 | |||||||||||||
| 91 | sub new { | ||||||||||||
| 92 | 3 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 93 | 3 | my $ob = $class->SUPER::new(@_); | |||||||||||
| 94 | 3 | %$ob = (%$ob, digests => {}, length => undef, writing => 1); | |||||||||||
| 95 | |||||||||||||
| 96 | 3 | my $xhandle = Symbol::geniosym; | |||||||||||
| 97 | 3 | tie *$xhandle, $class.'::TiedHandle', $ob; | |||||||||||
| 98 | 3 | $ob->{handle} = $xhandle; | |||||||||||
| 99 | |||||||||||||
| 100 | 3 | $ob->{file} = File::Temp->new | |||||||||||
| 101 | (UNLINK => 1, DIR => $TmpDir, TEMPLATE => 'warc-block-'.('X' x 12)); | ||||||||||||
| 102 | 3 | binmode $ob->{file}, ':raw'; | |||||||||||
| 103 | |||||||||||||
| 104 | 3 | { our $_total_constructed; $_total_constructed++ } | |||||||||||
| 105 | |||||||||||||
| 106 | 3 | return $ob | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | 3 | ▶ | sub DESTROY { our $_total_destroyed; $_total_destroyed++ } | ||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||
| 110 | |||||||||||||
| 111 | sub block { | ||||||||||||
| 112 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
block: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 113 | |||||||||||||
| 114 | 3 | ▶ | unless (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | # slurp the current contents; may run out of memory but that is the | ||||||||||||
| 116 | # caller's problem if this method is called on a record data sponge | ||||||||||||
| 117 | 1 | my $pos = $self->{file}->sysseek(0, SEEK_CUR); | |||||||||||
| 118 | 1 | $self->{file}->sysseek(0, SEEK_SET); | |||||||||||
| 119 | 1 | my $buf = do {local $/ = undef; readline $self->{file}}; | |||||||||||
| 120 | 1 | $self->{file}->sysseek($pos, SEEK_SET); | |||||||||||
| 121 | 1 | return $buf | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | # otherwise, replace the file contents with the provided data | ||||||||||||
| 125 | croak "attempt to replace block during squeeze phase" | ||||||||||||
| 126 | 2 | ▶ | unless $self->{writing}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | # setting digest markers is not possible if a block is supplied | ||||||||||||
| 128 | 1 | $self->{digests} = {}; | |||||||||||
| 129 | 1 | $self->SUPER::block(@_); | |||||||||||
| 130 | 1 | $self->{file}->sysseek(0, SEEK_SET); | |||||||||||
| 131 | 1 | $self->{file}->syswrite($self->{block}); | |||||||||||
| 132 | 1 | delete $self->{block}; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | =item $sponge-E<gt>begin_digest ( $key , $tag , $digest ) | ||||||||||||
| 136 | |||||||||||||
| 137 | Insert a digest marker using a digest object that must support the C<add>, | ||||||||||||
| 138 | C<clone>, and C<digest> methods from the C<Digest> API. All data written | ||||||||||||
| 139 | to the record is included in all digests active when the data is written. | ||||||||||||
| 140 | |||||||||||||
| 141 | =cut | ||||||||||||
| 142 | |||||||||||||
| 143 | sub begin_digest { | ||||||||||||
| 144 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
begin_digest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 145 | |||||||||||||
| 146 | 2 | $self->{digests}{$_[0]} = [$_[1], $_[2]]; | |||||||||||
| 147 | return | ||||||||||||
| 148 | 2 | } | |||||||||||
| 149 | |||||||||||||
| 150 | =item $value = $sponge-E<gt>get_digest ( $key ) | ||||||||||||
| 151 | |||||||||||||
| 152 | Return a digest value for the data from the digest marker inserted with the | ||||||||||||
| 153 | given key to the current end of the data. The result is a Base32 value | ||||||||||||
| 154 | labelled with the tag given when the digest was started. | ||||||||||||
| 155 | |||||||||||||
| 156 | =cut | ||||||||||||
| 157 | |||||||||||||
| 158 | sub get_digest { | ||||||||||||
| 159 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_digest: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 160 | 4 | my $cell = $self->{digests}{$_[0]}; | |||||||||||
| 161 | |||||||||||||
| 162 | 4 | ▶ | return undef unless $cell; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 3 | return $cell->[0].':'.encode_rfc3548($cell->[1]->clone->digest) | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | =item $sponge-E<gt>readback | ||||||||||||
| 167 | |||||||||||||
| 168 | End the "soak" phase and switch to the "squeeze" phase. | ||||||||||||
| 169 | |||||||||||||
| 170 | =cut | ||||||||||||
| 171 | |||||||||||||
| 172 | sub readback { | ||||||||||||
| 173 | 5 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
readback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 174 | |||||||||||||
| 175 | 5 | $self->{length} = $self->{file}->sysseek(0, SEEK_CUR); | |||||||||||
| 176 | 5 | $self->{fields}->field('Content-Length', $self->{length}); | |||||||||||
| 177 | 5 | $self->{writing} = 0; | |||||||||||
| 178 | 5 | $self->{file}->sysseek(0, SEEK_SET); | |||||||||||
| 179 | |||||||||||||
| 180 | return | ||||||||||||
| 181 | 5 | } | |||||||||||
| 182 | |||||||||||||
| 183 | =item $sponge-E<gt>content_length | ||||||||||||
| 184 | |||||||||||||
| 185 | Return the length of the data stored in the sponge if called in the | ||||||||||||
| 186 | "squeeze" phase. Returns undefined if called during the "soak" phase. | ||||||||||||
| 187 | |||||||||||||
| 188 | =cut | ||||||||||||
| 189 | |||||||||||||
| 190 | # The inherited method will work, but this is slightly more efficient. | ||||||||||||
| 191 | 2 | ▶ | sub content_length { (shift)->{length} } | ||||||||||
|
Subroutine
content_length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 192 | |||||||||||||
| 193 | =item $sponge-E<gt>reset | ||||||||||||
| 194 | |||||||||||||
| 195 | End the "squeeze" phase and return to a new "soak" phase. | ||||||||||||
| 196 | |||||||||||||
| 197 | =cut | ||||||||||||
| 198 | |||||||||||||
| 199 | sub reset { | ||||||||||||
| 200 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
reset: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 201 | |||||||||||||
| 202 | 3 | $self->{length} = undef; | |||||||||||
| 203 | 3 | $self->{digests} = {}; | |||||||||||
| 204 | 3 | $self->{writing} = 1; | |||||||||||
| 205 | 3 | $self->{file}->sysseek(0, SEEK_SET); | |||||||||||
| 206 | |||||||||||||
| 207 | 3 | foreach my $key (keys %{$self->{fields}}) | |||||||||||
| 208 | 6 | ▶ | { delete $self->{fields}{$key} unless $key eq 'WARC-Type' } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | |||||||||||||
| 210 | return | ||||||||||||
| 211 | 3 | } | |||||||||||
| 212 | |||||||||||||
| 213 | =back | ||||||||||||
| 214 | |||||||||||||
| 215 | =cut | ||||||||||||
| 216 | |||||||||||||
| 217 | 42 | ▶ | sub _as_handle { (shift)->{handle} } | ||||||||||
|
Subroutine
_as_handle: called CC=1 SCAR=0.0
| |||||||||||||
| 218 | |||||||||||||
| 219 | sub _add_syswrite_buffer { | ||||||||||||
| 220 | 11 | ▶ | my $self = shift; my $buf = substr $_[0], $_[2] || 0, $_[1]; | ||||||||||
|
Subroutine
_add_syswrite_buffer: called CC=3 SCAR=11.0
Condition100%2/2
$_[2] || 0
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||
| 221 | 11 | foreach (values %{$self->{digests}}) { $_->[1]->add($buf) } | |||||||||||
| 222 | } | ||||||||||||
| 223 | |||||||||||||
| 224 | { | ||||||||||||
| 225 | package WARC::Record::Sponge::TiedHandle; | ||||||||||||
| 226 | |||||||||||||
| 227 | 1 | ▶ | use Errno qw/EBADF/; # POSIX defines this, so we should have it. | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 228 | 1 | ▶ | use Fcntl qw/SEEK_CUR/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 229 | 1 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 230 | |||||||||||||
| 231 | # The underlying object is a weak reference to the parent record sponge. | ||||||||||||
| 232 | |||||||||||||
| 233 | sub TIEHANDLE { | ||||||||||||
| 234 | 3 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
TIEHANDLE: called CC=1 SCAR=0.0
| |||||||||||||
| 235 | 3 | my $parent = shift; | |||||||||||
| 236 | |||||||||||||
| 237 | 3 | my $ob = bless \ $parent, $class; | |||||||||||
| 238 | 3 | weaken $$ob; | |||||||||||
| 239 | 3 | return $ob | |||||||||||
| 240 | } | ||||||||||||
| 241 | |||||||||||||
| 242 | # The Perl debugger uses the fileno operator when printing a handle. | ||||||||||||
| 243 | 1 | ▶ | sub FILENO { my $self = ${(shift)}; fileno $self->{file} } | ||||||||||
|
Subroutine
FILENO: called CC=1 SCAR=0.0
| |||||||||||||
| 244 | |||||||||||||
| 245 | 1 | ▶ | sub BINMODE { my $self = ${(shift)}; binmode $self->{file}, shift } | ||||||||||
|
Subroutine
BINMODE: called CC=1 SCAR=0.0
| |||||||||||||
| 246 | 2 | ▶ | sub CLOSE { my $self = ${(shift)}; | ||||||||||
|
Subroutine
CLOSE: called CC=2 SCAR=6.9
| |||||||||||||
| 247 | 2 | ▶ | if ($self->{writing}) { $self->readback } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 248 | 1 | else { $self->reset } | |||||||||||
| 249 | 2 | return 1 } | |||||||||||
| 250 | |||||||||||||
| 251 | 12 | ▶ | sub WRITE { my $self = ${(shift)}; | ||||||||||
|
Subroutine
WRITE: called CC=2 SCAR=6.9
| |||||||||||||
| 252 | 12 | ▶ | unless ($self->{writing}) { $! = EBADF; return } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 253 | 11 | $self->_add_syswrite_buffer(@_); | |||||||||||
| 254 | 11 | $self->{file}->syswrite(@_) } | |||||||||||
| 255 | 7 | ▶ | sub PRINT { my $self = ${(shift)}; | ||||||||||
|
Subroutine
PRINT: called CC=3 SCAR=11.0
| |||||||||||||
| 256 | 7 | ▶ | my $buf = join(defined($,) ? $, : '', @_); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 257 | 7 | ▶ | $buf .= $\ if defined($\); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 7 | syswrite $self, $buf } | |||||||||||
| 259 | 1 | ▶ | sub PRINTF { my $self = ${(shift)}; my $fmt = shift; | ||||||||||
|
Subroutine
PRINTF: called CC=1 SCAR=0.0
| |||||||||||||
| 260 | 1 | local $\ = ''; print $self sprintf $fmt, @_ } | |||||||||||
| 261 | |||||||||||||
| 262 | 3 | ▶ | sub SEEK { my $self = ${(shift)}; | ||||||||||
|
Subroutine
SEEK: called CC=2 SCAR=6.9
| |||||||||||||
| 263 | 3 | ▶ | return undef if $self->{writing}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 264 | 2 | $self->{file}->sysseek(@_) } | |||||||||||
| 265 | 2 | ▶ | sub TELL { my $self = ${(shift)}; | ||||||||||
|
Subroutine
TELL: called CC=1 SCAR=0.0
| |||||||||||||
| 266 | 2 | $self->{file}->sysseek(0, SEEK_CUR) } | |||||||||||
| 267 | |||||||||||||
| 268 | 3 | ▶ | sub EOF { my $self = ${(shift)}; return 1 if $self->{writing}; | ||||||||||
|
Subroutine
EOF: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||
| 269 | 2 | return (tell $self >= $self->{length}) } | |||||||||||
| 270 | |||||||||||||
| 271 | # This sub must rely on the aliasing effect of @_. | ||||||||||||
| 272 | sub READ { | ||||||||||||
| 273 | 10 | ▶ | my $self = ${(shift)}; | ||||||||||
|
Subroutine
READ: called CC=8 SCAR=20.8
| |||||||||||||
| 274 | # args now: 0: buffer 1: length 2: offset into buffer or undef | ||||||||||||
| 275 | 10 | my $length = $_[1]; | |||||||||||
| 276 | 10 | ▶ | my $offset = $_[2] || 0; | ||||||||||
|
Condition100%2/2
$_[2] || 0
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||
| 277 | |||||||||||||
| 278 | 10 | ▶ | if ($self->{writing}) { $! = EBADF; return undef } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 279 | |||||||||||||
| 280 | my $excess = (($length + $self->{file}->sysseek(0, SEEK_CUR)) | ||||||||||||
| 281 | 9 | - $self->{length}); | |||||||||||
| 282 | 9 | ▶ | $length -= $excess if $excess > 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 283 | 9 | ▶ | return 0 unless $length; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 284 | |||||||||||||
| 285 | 8 | my $buf; my $count = sysread $self->{file}, $buf, $length; | |||||||||||
| 286 | 8 | ▶ | return undef unless defined $count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 287 | |||||||||||||
| 288 | 8 | ▶ | $_[0] = '' unless defined $_[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 289 | 8 | ▶ | $_[0] .= "\0" x ($offset - length($_[0])) if $offset > length $_[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 290 | 8 | substr $_[0], $offset, (length($_[0]) - $offset), $buf; | |||||||||||
| 291 | 8 | return $count; | |||||||||||
| 292 | } | ||||||||||||
| 293 | } | ||||||||||||
| 294 | |||||||||||||
| 295 | 1 | 1; | |||||||||||
| 296 | __END__ | ||||||||||||