| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ########################################################################## # | ||||||||||||||||||||||||||||
| 4 | # Title: Batch-of-rows processor | ||||||||||||||||||||||||||||
| 5 | # Creation date: 2007-03-05 | ||||||||||||||||||||||||||||
| 6 | # Author: Michael Zedeler | ||||||||||||||||||||||||||||
| 7 | # Henrik Andreasen | ||||||||||||||||||||||||||||
| 8 | # Description: Process batches of rows in a data stream | ||||||||||||||||||||||||||||
| 9 | # Data Stream class | ||||||||||||||||||||||||||||
| 10 | # Data transformer | ||||||||||||||||||||||||||||
| 11 | # Buffers rows | ||||||||||||||||||||||||||||
| 12 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/Buffer.pm,v $ | ||||||||||||||||||||||||||||
| 13 | # Repository: kronhjorten | ||||||||||||||||||||||||||||
| 14 | # State: $State: Exp $ | ||||||||||||||||||||||||||||
| 15 | # Documentation: inline | ||||||||||||||||||||||||||||
| 16 | # Recepient: - | ||||||||||||||||||||||||||||
| 17 | # ########################################################################## # | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | package DS::Transformer::Buffer; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use base qw{ DS::Transformer::TypePassthrough }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Carp::Assert; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our ($VERSION) = $DS::VERSION; | |||||||||||||||||||||||||||
| 28 | 1 | our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # new | ||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||
| 33 | # Class constructor | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 1 | my( $class, $source, $target ) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $self = $class->SUPER::new( $source, $target ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | $self->{buffer} = []; # Holds copies (?) of the currently buffered rows (stream is N elements long, indexed 0 .. N-1) | |||||||||||||||||||||||||||
| 41 | 1 | $self->{first} = 0; # Range: [0..N] Inv: Always at first buffered element (one past "last" when buffer is empty) | |||||||||||||||||||||||||||
| 42 | 1 | $self->{last} = -1; # Range: [-1..N-1] Inv: Always at last element (one before "first" when buffer is empty) | |||||||||||||||||||||||||||
| 43 | 1 | $self->{current} = -1; # Range: [-1..N] Inv: Always at current element, initially -1, finally N (past end) | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | return $self; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # receive_row | ||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||
| 50 | # Processes a row in stream. | ||||||||||||||||||||||||||||
| 51 | # Returns undef when no more rows are available. | ||||||||||||||||||||||||||||
| 52 | # It is allowed to call fetch after the stream has ended, each call returning undef. | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | 15 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 15 | my ($self, $row) = @_; | |||||||||||||||||||||||||||
| 56 | 15 | $self->push( $row ); | |||||||||||||||||||||||||||
| 57 | 15 | return $self->shift; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 15 | ▶ | sub shift { | ||||||||||||||||||||||||||
|
Subroutine
shift: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 15 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 15 | my $last = $self->{last}; | |||||||||||||||||||||||||||
| 64 | 15 | my $current = $self->{current}; | |||||||||||||||||||||||||||
| 65 | 15 | my $result = undef; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Find and return the "next" element, if any | ||||||||||||||||||||||||||||
| 68 | 15 | ▶ | ++$current if ($current <= $last); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 15 | ▶ | $result = ${$self->{buffer}}[$current] if ($current <= $last); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 15 | $self->{current} = $current; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 15 | return $result; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 15 | ▶ | sub push { | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 15 | my( $self, $row ) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 15 | my $last = $self->{last}; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Put row in buffer if not EOF or EOF not already registered in buffer | ||||||||||||||||||||||||||||
| 82 | 15 | ▶ | if ( $row or ${$self->{buffer}}[$last] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$row or do {
$$self{"buffer"}
}->[$last]
MC/DC100%2/2
$row or do {
$$self{"buffer"}
}->[$last]
$row
do {
$$self{"buffer"}
}->[$last]
Truth table100%3/3
A or B
A= $rowB= do {
$$self{"buffer"}
}->[$last]
| |||||||||||||||||||||||||||||
| 83 | 11 | ++$last; | |||||||||||||||||||||||||||
| 84 | 11 | ▶ | $row = {%$row} if $row; # Make a copy if not EOF | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 11 | ${$self->{buffer}}[$last] = $row; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 15 | $self->{last} = $last; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 15 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # fetch | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | # Re-fetches rows that has been unfetched. | ||||||||||||||||||||||||||||
| 96 | # It is a fatal error to try fetching beyond last row in the buffer | ||||||||||||||||||||||||||||
| 97 | #TODO Implement some kind of end of stream indicator that will allow fetch to return undef indicating end of stream | ||||||||||||||||||||||||||||
| 98 | 10 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 10 | my $last = $self->{last}; | |||||||||||||||||||||||||||
| 102 | 10 | my $current = $self->{current}; | |||||||||||||||||||||||||||
| 103 | 10 | my $result = undef; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Make sure we're not beyond end of buffer | ||||||||||||||||||||||||||||
| 106 | 10 | ▶ | if( $current < $last ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | # Find and return the "next" element | ||||||||||||||||||||||||||||
| 108 | 6 | ++$current; | |||||||||||||||||||||||||||
| 109 | 6 | $result = ${$self->{buffer}}[$current]; | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 4 | croak("Can't fetch past buffer end."); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | $self->{last} = $last; | |||||||||||||||||||||||||||
| 115 | 6 | $self->{current} = $current; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | return $result; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # unreceive_row | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # Moves the "current" position one step backwards within the buffered rows. | ||||||||||||||||||||||||||||
| 124 | # It is a fatal error to try to move before the start of the currently buffered rows. | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | 13 | ▶ | sub unfetch { | ||||||||||||||||||||||||||
|
Subroutine
unfetch: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 13 | my $first = $self->{first}; | |||||||||||||||||||||||||||
| 130 | 13 | my $last = $self->{last}; | |||||||||||||||||||||||||||
| 131 | 13 | my $current = $self->{current}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Validate the request | ||||||||||||||||||||||||||||
| 134 | 13 | ▶ | if ($current < $first) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 135 | 3 | die "Cannot unfetch beyond buffer start (frame starts at row number $first and current record is $current)\n"; | |||||||||||||||||||||||||||
| 136 | } elsif( $current > $last ) { | ||||||||||||||||||||||||||||
| 137 | # If EOF reached, return to the element that contains EOF | ||||||||||||||||||||||||||||
| 138 | 1 | assert( $current == $last + 1, '$current must never be more than one past $last' ); | |||||||||||||||||||||||||||
| 139 | 1 | --$current; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Move back one step | ||||||||||||||||||||||||||||
| 143 | 10 | --$current; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 10 | $self->{current} = $current; | |||||||||||||||||||||||||||
| 146 | 10 | return 1; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # flush | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # Clears the buffered rows up to and including the given point. | ||||||||||||||||||||||||||||
| 153 | # If the "current" position is flush, it is moved forward such that it will | ||||||||||||||||||||||||||||
| 154 | # return the first available element at next fetch, if any. | ||||||||||||||||||||||||||||
| 155 | # It is a fatal error to flush non-existent rows. | ||||||||||||||||||||||||||||
| 156 | # | ||||||||||||||||||||||||||||
| 157 | 2 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 2 | my ($self, $point) = @_; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 2 | my $first = $self->{first}; | |||||||||||||||||||||||||||
| 161 | 2 | my $last = $self->{last}; | |||||||||||||||||||||||||||
| 162 | 2 | my $current = $self->{current}; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Use current position to flush in no point provided | ||||||||||||||||||||||||||||
| 165 | 2 | ▶ | $point ||= $current; | ||||||||||||||||||||||||||
|
Condition66%2/3
$point ||= $current
MC/DC50%1/2
$point ||= $current
$point
$current
Truth table66%2/3
A ||= B
A= $pointB= $current
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Validate the request | ||||||||||||||||||||||||||||
| 168 | 2 | ▶ | if ($point < $first || $point > $last) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$point < $first or $point > $last
MC/DC0%0/2
$point < $first or $point > $last
$point < $first
$point > $last
Truth table33%1/3
A or B
A= $point < $firstB= $point > $last
| |||||||||||||||||||||||||||||
| 169 | 0 | croak("Cannot flush non-existent elements. ($point is not within valid range: $first .. $last)"); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Delete buffer elements and adjust pointers | ||||||||||||||||||||||||||||
| 173 | 2 | ▶ | for (my $i = $first; $i <= $point; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 3 | delete ${$self->{buffer}}[$i]; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 2 | $self->{first} = $point + 1; | |||||||||||||||||||||||||||
| 177 | 2 | ▶ | $self->{current} = $point if ($self->{current} < $point); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | return 1; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||||||||||