File Coverage

blib/lib/Fsdb/IO/Replayable.pm
Criterion Covered Total %
statement 6 35 17.1
branch 0 12 0.0
condition n/a
subroutine 2 6 33.3
pod 4 4 100.0
total 12 57 21.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             #
4             # Fsdb::IO::Replayable.pm
5             # Copyright (C) 2007-2008 by John Heidemann
6             # $Id: f32b4b55b6822dc8038d31e8123af0a099f77752 $
7             #
8             # This program is distributed under terms of the GNU general
9             # public license, version 2. See the file COPYING
10             # in $dblibdir for details.
11             #
12              
13              
14             package Fsdb::IO::Replayable;
15              
16             =head1 NAME
17              
18             Fsdb::IO::Replayable - support for buffering fsdb rows
19              
20             =head1 SYNOPSIS
21              
22             Buffer and replaying fsdb rows
23              
24             =head1 FUNCTIONS
25              
26             =head2 new
27              
28             $replayable = new Fsdb::IO::Replayable(-writer_args => \@writer_args,
29             -reader_args => \@reader_args);
30             $writer = $replayable->writer;
31             $replayable->close; # warning: close replayable, NOT writer
32             $reader = $replayable->reader;
33              
34             Creates a buffer for Fsdb::IO objects that will run with bounded memory usage.
35             After you close it, you can replay it one or more times by opening readers.
36              
37             Arguments to the new method:
38              
39             =over 4
40              
41             =item -writer_args => @arref
42             Gives arguments to pass to Fsdb::IO::Writer to make a new stream.
43              
44             =item -reader_args => @arref
45             Gives arguments to pass to Fsdb::IO::Reader to make a new stream.
46              
47             =item -tmpdir => $dirname
48             Specifies wher tmpfiles go.
49              
50             =back
51              
52             =cut
53              
54             @ISA = ();
55             ($VERSION) = 1.0;
56              
57 1     1   22241 use Carp;
  1         4  
  1         102  
58 1     1   410 use Fsdb::Support::NamedTmpfile;
  1         4  
  1         593  
59              
60             sub new {
61 0     0 1   my $class = shift @_;
62 0           my $self = bless {
63             _tmpdir => undef,
64             _reader_args => [],
65             _writer_args => [],
66              
67             _filename => undef,
68             _writer => undef,
69              
70             _write_version => 0, # can write if write>0
71             _read_version => 0, # can read if read>0 && read==write
72             }, $class;
73              
74 0           my(@args) = @_;
75 0           while ($#args >= 0) {
76 0           my($key) = shift @args;
77 0           my($value) = shift @args;
78 0 0         if ($key eq '-writer_args') {
    0          
    0          
79 0           $self->{_writer_args} = $value;
80             } elsif ($key eq '-reader_args') {
81 0           $self->{_reader_args} = $value;
82             } elsif ($key eq '-tmpdir') {
83 0           $self->{_tmpdir} = $value;
84             } else {
85 0           croak "Fsdb::IO::Replayable: unknown argument $key\n";
86             };
87             };
88              
89 0           $self->{_filename} = Fsdb::Support::NamedTmpfile::alloc($self->{_tmpdir});
90              
91 0           return $self;
92             }
93              
94              
95             =head2 writer
96              
97             $writer = $replayable->writer;
98              
99             Return a fsdb writer object. If the file was written already,
100             resets it for rewriting.
101              
102             =cut
103              
104             sub writer {
105 0     0 1   my $self = shift @_;
106              
107 0           $self->{_write_version}++;
108 0           $self->{_writer} = new Fsdb::IO::Writer(-file => $self->{_filename}, @{$self->{_writer_args}});
  0            
109 0           return $self->{_writer};
110             }
111              
112             =head2 close
113              
114             $replayable->close;
115              
116             Close the replayable for writing.
117             Closes the writer object, if any.
118             Allows reading to start.
119              
120             =cut
121              
122             sub close {
123 0     0 1   my $self = shift @_;
124              
125             croak "Fsdb::IO::Replayable: close called without open writer.\n"
126 0 0         if (!defined($self->{_writer}));
127 0           $self->{_writer}->close;
128 0           $self->{_writer} = undef;
129 0           $self->{_read_version} = $self->{_write_version};
130             }
131              
132             =head2 reader
133              
134             $reader = $replayable->reader;
135              
136             Return a fsdb reader object to re-read the file.
137             Can be called once.
138              
139             The caller is expected to close and discard any readers.
140              
141             =cut
142              
143             sub reader {
144 0     0 1   my $self = shift @_;
145              
146             croak "Fsdb::IO::Replayable: reader called without ever having a writer.\n"
147 0 0         if ($self->{_read_version} == 0);
148             croak "Fsdb::IO::Replayable: reader called without closed writer.\n"
149 0 0         if ($self->{_read_version} != $self->{_write_version});
150              
151 0           my $reader = new Fsdb::IO::Reader(-file => $self->{_filename}, @{$self->{_reader_args}});
  0            
152 0           return $reader;
153             }
154              
155              
156             1;