| 1 | # $Id: Buffered.pm,v 1.3 2005/11/29 11:55:01 dk Exp $ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package DBIx::Roles::Buffered; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Saves do() and selectrow_array() in a buffer, calls these as a single query later. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # Useful with lots of UPDATES and INSERTS over connections with high latency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use vars qw(%defaults $VERSION); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | %defaults = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | Buffered => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | BufferLimit => 16384, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | sub initialize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | buffer => [], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | params => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | curr => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | lock => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | }, \%defaults, qw(flush); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | sub dbi_method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 13 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbi_method: called CC=12 SCAR=26.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | my ( $self, $storage, $method, @params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | return $self-> super( $method, @params) if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | $storage-> {lock} or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | not $self->{attr}->{Buffered} or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 13 | ▶ | ( $method ne 'do' and $method ne 'selectrow_array'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$storage->{"lock"} or not $self->{"attr"}{"Buffered"} or $method ne "do" and $method ne "selectrow_array"
$storage->{"lock"} or not $self->{"attr"}{"Buffered"}
$method ne "do" and $method ne "selectrow_array"
MC/DC50%2/4
$storage->{"lock"} or not $self->{"attr"}{"Buffered"} or $method ne "do" and $method ne "selectrow_array"
$storage->{"lock"}
not $self->{"attr"}{"Buffered"}
$method ne "do"
$method ne "selectrow_array"
Truth table60%3/5
A or not(B) or C and D
A= $storage->{"lock"}B= not $self->{"attr"}{"Buffered"}C= $method ne "do"D= $method ne "selectrow_array"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my ( $query, $attr_hash) = ( shift @params, shift @params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | die "Fatal: DBIx::Roles::Buffered does not implement \%attr passed to DBI methods\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$attr_hash and scalar keys %$attr_hash
MC/DC0%0/1
$attr_hash and scalar keys %$attr_hash
$attr_hash
Truth table50%1/2
A
A= $attr_hash
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | if $attr_hash and scalar keys %$attr_hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my $length = length($query); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $length += 2 + length $_ for @params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | flush( $self, $storage) if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | $self-> {attr}-> {BufferLimit} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | $length + $storage-> {curr} > $self-> {attr}-> {BufferLimit}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"attr"}{"BufferLimit"} and $length + $storage->{"curr"} > $self->{"attr"}{"BufferLimit"}
MC/DC0%0/2
$self->{"attr"}{"BufferLimit"} and $length + $storage->{"curr"} > $self->{"attr"}{"BufferLimit"}
$self->{"attr"}{"BufferLimit"}
$length + $storage->{"curr"} > $self->{"attr"}{"BufferLimit"}
Truth table33%1/3
A and B
A= $self->{"attr"}{"BufferLimit"}B= $length + $storage->{"curr"} > $self->{"attr"}{"BufferLimit"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my $expected = scalar( @_ = $query =~ m/\?/g ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | die "Query '$query' contains references to $expected parameters, got ", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | scalar(@params), " passed\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | if $expected != @params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | push @{$storage-> {buffer}}, $query; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | push @{$storage-> {params}}, @params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | $storage-> {curr} += $length; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | return ( $method eq 'do') ? "0E0" : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | sub flush | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 11 | my ( $self, $storage, $discard) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | return unless $storage-> {curr}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # clear the internal state to be re-entrant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my $q = join(';', @{$storage->{buffer}}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | my @p = @{$storage->{params}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | @{$storage->{buffer}} = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | @{$storage->{params}} = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | $storage-> {curr} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | local $storage->{lock} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $self-> do( $q, {}, @p) unless $discard; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | sub begin_work | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
begin_work: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | my ( $self, $storage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | flush( $self, $storage); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | return $self-> super; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | sub rollback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rollback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | my ( $self, $storage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | flush( $self, $storage, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | return $self-> super; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | sub commit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | my ( $self, $storage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | flush( $self, $storage); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return $self-> super; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | sub disconnect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | my ( $self, $storage) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | flush( $self, $storage); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | return $self-> super; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | sub STORE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 17 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 17 | my ( $self, $storage, $key, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 17 | ▶ | if ( $key eq 'Buffered' and not $val) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$key eq "Buffered" and not $val
MC/DC100%2/2
$key eq "Buffered" and not $val
$key eq "Buffered"
not $val
Truth table100%3/3
A and not(B)
A= $key eq "Buffered"B= not $val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | $self-> {attr}-> {Buffered} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | flush( $self, $storage); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } elsif ( $key eq 'BufferLimit') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | die "Fatal: 'BufferLimit' must be a positive integer" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | unless $val =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 17 | return $self-> super( $key, $val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||