| 1 | |||||||||||||||||||||||||||||
| 2 | package MLDBM::Sync::SDBM_File; | ||||||||||||||||||||||||||||
| 3 | 7 | $VERSION = .17; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use SDBM_File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use vars qw(@ISA $MaxSegments $MaxSegmentLength %KEYS $Zlib $VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | @ISA = qw(SDBM_File); | |||||||||||||||||||||||||||
| 10 | 7 | $MaxSegments = 8192; # to a 1M limit | |||||||||||||||||||||||||||
| 11 | # leave room for key index pad | ||||||||||||||||||||||||||||
| 12 | 7 | $MaxSegmentLength = 128; | |||||||||||||||||||||||||||
| 13 | 7 | ▶ | eval "use Compress::Zlib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | $Zlib = $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 23 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||
| 17 | 23 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 18 | 23 | my $segment_length = $MaxSegmentLength; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 23 | my $total_rv; | |||||||||||||||||||||||||||
| 21 | 23 | ▶ | for(my $index = 0; $index < $MaxSegments; $index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 29 | my $rv = $self->SUPER::FETCH(_index_key($key, $index)); | |||||||||||||||||||||||||||
| 23 | 29 | ▶ | if(defined $rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 28 | ▶ | $total_rv ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$total_rv ||= ""
MC/DC100%1/1
$total_rv ||= ""
$total_rv
Truth table100%2/2
A
A= $total_rv
| |||||||||||||||||||||||||||||
| 25 | 28 | $total_rv .= $rv; | |||||||||||||||||||||||||||
| 26 | 28 | ▶ | last if length($rv) < $segment_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||
| 28 | 1 | last; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 23 | ▶ | if(defined $total_rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 22 | $total_rv =~ s/^(..)//s; | |||||||||||||||||||||||||||
| 34 | 22 | my $type = $1; | |||||||||||||||||||||||||||
| 35 | 22 | ▶ | if($type eq 'G}') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 36 | 22 | $total_rv = uncompress($total_rv); | |||||||||||||||||||||||||||
| 37 | } elsif ($type eq 'N}') { | ||||||||||||||||||||||||||||
| 38 | # nothing | ||||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||
| 40 | # old SDBM_File ? | ||||||||||||||||||||||||||||
| 41 | 0 | $total_rv = $type . $total_rv; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 23 | $total_rv; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 13 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=9 SCAR=22.5
| |||||||||||||||||||||||||||||
| 49 | 13 | my($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 50 | 13 | my $segment_length = $MaxSegmentLength; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # DELETE KEYS FIRST | ||||||||||||||||||||||||||||
| 53 | 13 | ▶ | for(my $index = 0; $index < $MaxSegments; $index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 19 | my $index_key = _index_key($key, $index); | |||||||||||||||||||||||||||
| 55 | 19 | my $rv = $self->SUPER::FETCH($index_key); | |||||||||||||||||||||||||||
| 56 | 19 | ▶ | if(defined $rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 7 | $self->SUPER::DELETE($index_key); | |||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||
| 59 | 12 | last; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 7 | ▶ | last if length($rv) < $segment_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # G - Gzip compression | ||||||||||||||||||||||||||||
| 65 | # N - No compression | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | 13 | my $old_value = $value; | |||||||||||||||||||||||||||
| 68 | 13 | ▶ | $value = ($Zlib && (length($value) >= $segment_length/2)) ? "G}".compress($value) : "N}".$value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$MLDBM::Sync::SDBM_File::Zlib and length $value >= $segment_length / 2
MC/DC0%0/2
$MLDBM::Sync::SDBM_File::Zlib and length $value >= $segment_length / 2
$MLDBM::Sync::SDBM_File::Zlib
length $value >= $segment_length / 2
Truth table33%1/3
A and B
A= $MLDBM::Sync::SDBM_File::ZlibB= length $value >= $segment_length / 2
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 13 | my($total_rv, $last_index); | |||||||||||||||||||||||||||
| 71 | 13 | ▶ | for(my $index = 0; $index < $MaxSegments; $index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 38 | ▶ | if($index == $MaxSegments) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | die("can't store more than $MaxSegments segments of $MaxSegmentLength bytes per key in ".__PACKAGE__); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 38 | $value =~ s/^(.{0,$segment_length})//so; | |||||||||||||||||||||||||||
| 76 | 38 | my $segment = $1; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 38 | ▶ | last if length($segment) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # print "STORING "._index_key($key, $index)." $segment\n"; | ||||||||||||||||||||||||||||
| 80 | 25 | my $rv = $self->SUPER::STORE(_index_key($key, $index), $segment); | |||||||||||||||||||||||||||
| 81 | 25 | $total_rv .= $segment; | |||||||||||||||||||||||||||
| 82 | 25 | $last_index = $index; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # use Time::HiRes; | ||||||||||||||||||||||||||||
| 86 | # print "[".&Time::HiRes::time()."] STORED ".($last_index+1)." segments for length ". | ||||||||||||||||||||||||||||
| 87 | # length($total_rv)." bytes for value ".length($old_value)."\n"; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 13 | $old_value; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 12 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||
| 93 | 12 | my($self, $key) = @_; | |||||||||||||||||||||||||||
| 94 | 12 | my $segment_length = $MaxSegmentLength; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 12 | my $total_rv; | |||||||||||||||||||||||||||
| 97 | 12 | ▶ | for(my $index = 0; $index < $MaxSegments; $index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 18 | my $index_key = _index_key($key, $index); | |||||||||||||||||||||||||||
| 99 | 18 | ▶ | my $rv = $self->SUPER::FETCH($index_key) || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->SUPER::FETCH($index_key) || ''
MC/DC0%0/1
$self->SUPER::FETCH($index_key) || ''
$self->SUPER::FETCH($index_key)
Truth table50%1/2
A
A= $self->SUPER::FETCH($index_key)
| |||||||||||||||||||||||||||||
| 100 | 18 | $self->SUPER::DELETE($index_key); | |||||||||||||||||||||||||||
| 101 | 18 | ▶ | $total_rv ||= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$total_rv ||= ""
MC/DC100%1/1
$total_rv ||= ""
$total_rv
Truth table100%2/2
A
A= $total_rv
| |||||||||||||||||||||||||||||
| 102 | 18 | $total_rv .= $rv; | |||||||||||||||||||||||||||
| 103 | 18 | ▶ | last if length($rv) < $segment_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 12 | $total_rv =~ s/^(..)//s; | |||||||||||||||||||||||||||
| 107 | 12 | my $type = $1; | |||||||||||||||||||||||||||
| 108 | 12 | ▶ | if($type eq 'G}') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 12 | $total_rv = uncompress($total_rv); | |||||||||||||||||||||||||||
| 110 | } elsif ($type eq 'N}') { | ||||||||||||||||||||||||||||
| 111 | # normal | ||||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||
| 113 | # old SDBM_File | ||||||||||||||||||||||||||||
| 114 | 0 | $total_rv = $type.$total_rv; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 12 | $total_rv; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 5 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 121 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | my $key = $self->SUPER::FIRSTKEY(); | |||||||||||||||||||||||||||
| 124 | 5 | my @keys = (); | |||||||||||||||||||||||||||
| 125 | 5 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 3 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 30 | ▶ | if($key !~ /\*\*\d+$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 30 | ▶ | if(my $new_key = _decode_key($key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 30 | push(@keys, $new_key); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } while($key = $self->SUPER::NEXTKEY($key)); | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 5 | $KEYS{$self} = \@keys; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 5 | $self->NEXTKEY; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 35 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 35 | shift(@{$KEYS{$self}}); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 91 | ▶ | sub _index_key { | ||||||||||||||||||||||||||
|
Subroutine
_index_key: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 145 | 91 | my($key, $index) = @_; | |||||||||||||||||||||||||||
| 146 | 0 | $key =~ s/([\%\*])/uc sprintf("%%%02x",ord($1))/esg; | |||||||||||||||||||||||||||
| 147 | 91 | ▶ | $index ? $key.'**'.$index : $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 30 | ▶ | sub _decode_key { | ||||||||||||||||||||||||||
|
Subroutine
_decode_key: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 151 | 30 | my $key = shift; | |||||||||||||||||||||||||||
| 152 | 0 | $key =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge; | |||||||||||||||||||||||||||
| 153 | 30 | $key; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 7 | 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||