| 1 | package BeePack; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Primitive MsgPack based key value storage | ||||||||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.200'; | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use bytes; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use CDB_File; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Data::MessagePack; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub true { Data::MessagePack::true() } | ||||||||||||||||||||||||||||||||
|
Subroutine
true: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub false { Data::MessagePack::false() } | ||||||||||||||||||||||||||||||||
|
Subroutine
false: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | # CDB_File has no in-place update, so the source of truth is an in-memory | ||||||||||||||||||||||||||||||||||
| 15 | # buffer of key => raw MsgPack bytes, seeded from the existing file on open | ||||||||||||||||||||||||||||||||||
| 16 | # and written back out atomically on save. | ||||||||||||||||||||||||||||||||||
| 17 | 3 | has _data => ( | |||||||||||||||||||||||||||||||||
| 18 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 19 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | sub _build__data { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build__data: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 23 | 6 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 24 | 6 | my %data; | |||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | if ( -f $self->filename ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | tie my %cdb, 'CDB_File', $self->filename | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 27 | or croak("Can't open BeePack ".$self->filename.": ".$!); | ||||||||||||||||||||||||||||||||||
| 28 | 4 | %data = %cdb; | |||||||||||||||||||||||||||||||||
| 29 | 4 | untie %cdb; | |||||||||||||||||||||||||||||||||
| 30 | } elsif ( $self->readonly ) { | ||||||||||||||||||||||||||||||||||
| 31 | 0 | croak("Can't open non-existing readonly database ".$self->filename); | |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | 6 | return \%data; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub keys { | ||||||||||||||||||||||||||||||||
|
Subroutine
keys: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 37 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 38 | 0 | return CORE::keys %{$self->_data}; | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 3 | has filename => ( | |||||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 43 | required => 1, | ||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 3 | has tempfile => ( | |||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 48 | predicate => 1, | ||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 3 | has nil_exists => ( | |||||||||||||||||||||||||||||||||
| 52 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub _build_nil_exists { 0 } | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_nil_exists: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 3 | has readonly => ( | |||||||||||||||||||||||||||||||||
| 58 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 6 | ▶ | sub _build_readonly { | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_readonly: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 62 | 6 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | return $self->has_tempfile ? 0 : 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 3 | has data_messagepack => ( | |||||||||||||||||||||||||||||||||
| 67 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 68 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 6 | ▶ | sub _build_data_messagepack { Data::MessagePack->new->canonical->utf8 } | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_data_messagepack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 74 | 6 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | croak("Read/Write opening requires tempfile") if !$self->readonly && !$self->has_tempfile; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->readonly or $self->has_tempfile
MC/DC0%0/2
$self->readonly or $self->has_tempfile
$self->readonly
$self->has_tempfile
Truth table66%2/3
A or B
A= $self->readonlyB= $self->has_tempfile
| |||||||||||||||||||||||||||||||||||
| 76 | 6 | $self->_data; | |||||||||||||||||||||||||||||||||
| 77 | 6 | $self->data_messagepack; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 6 | ▶ | sub open { | ||||||||||||||||||||||||||||||||
|
Subroutine
open: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 81 | 6 | my ( $class, $filename, $tempfile, %attr ) = @_; | |||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | return $class->new( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | filename => $filename, | ||||||||||||||||||||||||||||||||||
| 84 | defined $tempfile ? ( tempfile => $tempfile ) : (), | ||||||||||||||||||||||||||||||||||
| 85 | %attr, | ||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 22 | ▶ | sub set { | ||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 90 | 22 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||||||||
| 91 | 22 | $self->readonly_check; | |||||||||||||||||||||||||||||||||
| 92 | 21 | $self->_data->{$key} = $self->data_messagepack->pack($value); | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 22 | ▶ | sub readonly_check { | ||||||||||||||||||||||||||||||||
|
Subroutine
readonly_check: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 96 | 22 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 97 | 22 | ▶ | croak("Trying to set on readonly BeePack") if $self->readonly; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub set_type { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_type: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 101 | 0 | my ( $self, $key, $type, $value ) = @_; | |||||||||||||||||||||||||||||||||
| 102 | 0 | $self->readonly_check; | |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $t = defined $type ? substr($type,0,1) : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($t eq 'i') { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->set_integer($key,$value); | |||||||||||||||||||||||||||||||||
| 106 | } elsif ($t eq 'b') { | ||||||||||||||||||||||||||||||||||
| 107 | 0 | $self->set_bool($key,$value); | |||||||||||||||||||||||||||||||||
| 108 | } elsif ($t eq 's') { | ||||||||||||||||||||||||||||||||||
| 109 | 0 | $self->set_string($key,$value); | |||||||||||||||||||||||||||||||||
| 110 | } elsif ($t eq 'n') { | ||||||||||||||||||||||||||||||||||
| 111 | 0 | $self->set_nil($key,$value); | |||||||||||||||||||||||||||||||||
| 112 | } elsif ($t eq 'a') { | ||||||||||||||||||||||||||||||||||
| 113 | 0 | my @array = @{$value}; | |||||||||||||||||||||||||||||||||
| 114 | 0 | $self->set($key,\@array); | |||||||||||||||||||||||||||||||||
| 115 | } elsif ($t eq 'h') { | ||||||||||||||||||||||||||||||||||
| 116 | 0 | my %hash = %{$value}; | |||||||||||||||||||||||||||||||||
| 117 | 0 | $self->set($key,\%hash); | |||||||||||||||||||||||||||||||||
| 118 | } elsif ($t eq '') { | ||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->set($key,$value); | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 12 | ▶ | sub set_integer { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_integer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 124 | 12 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||||||||
| 125 | 12 | $self->set($key, 0 + $value); | |||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | sub set_bool { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_bool: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 129 | 2 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | $self->set($key, $value | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 131 | ? Data::MessagePack::true() | ||||||||||||||||||||||||||||||||||
| 132 | : Data::MessagePack::false() | ||||||||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub set_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 137 | 2 | my ( $self, $key, $value ) = @_; | |||||||||||||||||||||||||||||||||
| 138 | 2 | $self->set($key, "$value"); | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub set_nil { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_nil: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 142 | 1 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||||||||
| 143 | 1 | $self->set($key, undef); | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 23 | ▶ | sub exists { | ||||||||||||||||||||||||||||||||
|
Subroutine
exists: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 147 | 23 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||||||||
| 148 | 23 | ▶ | return 0 unless CORE::exists $self->_data->{$key}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 149 | 23 | ▶ | return 1 if $self->nil_exists; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 150 | 17 | my $value = $self->data_messagepack->unpack($self->_data->{$key}); | |||||||||||||||||||||||||||||||||
| 151 | 17 | ▶ | return defined $value ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 21 | ▶ | sub get { | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 155 | 21 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||||||||
| 156 | 21 | ▶ | return undef unless $self->exists($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 157 | 20 | return $self->data_messagepack->unpack($self->_data->{$key}); | |||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub get_raw { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_raw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 161 | 0 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||||||||
| 162 | 0 | return $self->_data->{$key}; | |||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | sub save { | ||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 166 | 3 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | croak("Trying to save readonly CDB ".$self->filename) if $self->readonly; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | my $cdb = CDB_File->new($self->filename,$self->tempfile) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | or croak("Can't create BeePack ".$self->filename.": ".$!); | ||||||||||||||||||||||||||||||||||
| 170 | 3 | for my $key ( sort CORE::keys %{$self->_data} ) { | |||||||||||||||||||||||||||||||||
| 171 | 22 | $cdb->insert($key,$self->_data->{$key}); | |||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | 3 | $cdb->finish; | |||||||||||||||||||||||||||||||||
| 174 | # in-memory buffer stays the source of truth, so the pack is usable for | ||||||||||||||||||||||||||||||||||
| 175 | # further reads and writes after save | ||||||||||||||||||||||||||||||||||
| 176 | 3 | return 1; | |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | 3 | 1; | |||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||||||||