| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package KiokuDB::Backend::Hash; | ||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use Data::Stream::Bulk::Util qw(bulk); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 21 | with ( | |||||||||||||||||||||||||||
| 13 | 'KiokuDB::Backend::Serialize::Delegate', | ||||||||||||||||||||||||||||
| 14 | 'KiokuDB::Backend', | ||||||||||||||||||||||||||||
| 15 | 'KiokuDB::Backend::Role::Query::Simple::Linear', | ||||||||||||||||||||||||||||
| 16 | 'KiokuDB::Backend::Role::TXN::Memory::Scan', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has storage => ( | ||||||||||||||||||||||||||||
| 20 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 21 | is => "rw", | ||||||||||||||||||||||||||||
| 22 | 99 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 21 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub clear_storage { | ||||||||||||||||||||||||||||
| 26 | 166 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clear_storage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 166 | %{ $self->storage } = (); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub get_from_storage { | ||||||||||||||||||||||||||||
| 31 | 2769 | ▶ | my ( $self, @uids ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_from_storage: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2769 | my $s = $self->storage; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2769 | ▶ | return if grep { not exists $s->{$_} } @uids; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2734 | my @objs = map { $self->deserialize($_) } @{ $s }{@uids}; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2734 | ▶ | if ( @objs == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 2305 | return $objs[0]; | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 429 | return @objs; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub commit_entries { | ||||||||||||||||||||||||||||
| 47 | 1745 | ▶ | my ( $self, @entries ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
commit_entries: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1745 | my $s = $self->storage; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1745 | foreach my $entry ( @entries ) { | |||||||||||||||||||||||||||
| 52 | 4070 | my $id = $entry->id; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4070 | ▶ | if ( $entry->deleted ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 56 | delete $s->{$id}; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 4014 | ▶ | if ( exists $s->{$id} and not $entry->has_prev ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $s->{$id} and not $entry->has_prev
MC/DC100%2/2
exists $s->{$id} and not $entry->has_prev
exists $s->{$id}
not $entry->has_prev
Truth table100%3/3
A and not(B)
A= exists $s->{$id}B= not $entry->has_prev
| |||||||||||||||||||||||||||||
| 58 | 25 | croak "Entry $id already exists in the database"; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 3989 | $s->{$id} = $self->serialize($entry); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub exists_in_storage { | ||||||||||||||||||||||||||||
| 66 | 431 | ▶ | my ( $self, @uids ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
exists_in_storage: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 431 | map { exists $self->storage->{$_} } @uids; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub all_storage_entries { | ||||||||||||||||||||||||||||
| 72 | 783 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
all_storage_entries: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 783 | return bulk(map { $self->deserialize($_) } values %{ $self->storage }); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 21 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 21 | __PACKAGE__ | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =pod | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | KiokuDB::Backend::Hash - In memory backend for testing purposes. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $dir = KiokuDB->new( | ||||||||||||||||||||||||||||
| 91 | backend => KiokuDB::Backend::Hash->new(), | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | This L<KiokuDB> backend provides in memory storage and retrieval of | ||||||||||||||||||||||||||||
| 97 | L<KiokuDB::Entry> objects using L<Storable>'s C<dclone> to make dumps of the | ||||||||||||||||||||||||||||
| 98 | backend clear. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||