File Coverage

blib/lib/KiokuDB/Backend/CHI.pm
Criterion Covered Total %
statement 2 4 50.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 4 6 66.6


line stmt bran cond sub pod time code
1             package KiokuDB::Backend::CHI;
2             BEGIN {
3 1     1   670 $KiokuDB::Backend::CHI::VERSION = '0.01';
4             }
5              
6             # ABSTRACT: CHI backend for KiokuDB
7              
8 1     1   414 use Moose;
  0            
  0            
9              
10             use CHI;
11             use Data::Stream::Bulk::Array;
12              
13             with qw/
14             KiokuDB::Backend
15             KiokuDB::Backend::Serialize::Delegate
16             KiokuDB::Backend::Role::Clear
17             KiokuDB::Backend::Role::Scan
18             /;
19              
20             has cache => (
21             is => 'ro',
22             isa => 'Object',
23             lazy => 1,
24             required => 1,
25             builder => '_build_cache',
26             );
27              
28             has check_duplicates => (
29             is => 'ro',
30             isa => 'Bool',
31             default => 1,
32             );
33              
34             sub _build_cache {
35             my ($self) = @_;
36              
37             return CHI->new (driver => 'Memory',datastore => {});
38             }
39              
40             sub insert {
41             my ($self,@entries) = @_;
42              
43             if ($self->check_duplicates) {
44             my @create = map { $_->id } grep { ! $_->has_prev } @entries;
45              
46             confess "Cannot insert duplicate key" if scalar grep { $_ } $self->exists (@create);
47             }
48              
49             $self->cache->set_multi ({ map { $_->id => $self->serialize ($_) } @entries });
50              
51             return;
52             }
53              
54             sub delete {
55             my ($self,@ids_or_entries) = @_;
56              
57             my @ids = map { ref ($_) ? $_->id : $_ } @ids_or_entries;
58              
59             $self->cache->remove_multi (\@ids);
60              
61             return;
62             }
63              
64             sub get {
65             my ($self,@ids) = @_;
66              
67             return map { defined $_ ? $self->deserialize ($_) : undef } @{ $self->cache->get_multi_arrayref (\@ids) };
68             }
69              
70             sub exists {
71             my ($self,@ids) = @_;
72              
73             return $self->cache->is_valid (@ids) if scalar @ids == 1;
74              
75             return map { ! ! $_ } $self->get (@ids);
76             }
77              
78             sub all_entries {
79             my ($self) = @_;
80              
81             my @entries = map { $self->deserialize ($_) } grep { defined $_ } values %{ $self->cache->dump_as_hash };
82              
83             return Data::Stream::Bulk::Array->new (array => \@entries);
84             }
85              
86             sub clear {
87             my ($self) = @_;
88              
89             $self->cache->clear;
90              
91             return;
92             }
93              
94             1;
95              
96              
97              
98             __END__
99             =pod
100              
101             =head1 NAME
102              
103             KiokuDB::Backend::CHI - CHI backend for KiokuDB
104              
105             =head1 VERSION
106              
107             version 0.01
108              
109             =head1 SYNOPSIS
110              
111             my $dir = KiokuDB->new(
112             backend => KiokuDB::Backend::CHI->new(
113             cache => CHI->new(driver => 'Memory',datastore => {}),
114             ),
115             );
116              
117             =head1 DESCRIPTION
118              
119             A more detailed description will come later, but the module should
120             hopefully be self-explanatory enough for the purposes of testing.
121              
122             =head1 BUGS
123              
124             Most software has bugs. This module probably isn't an exception.
125             If you find a bug please either email me, or add the bug to cpan-RT.
126              
127             =head1 AUTHOR
128              
129             Anders Nor Berle <berle@cpan.org>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2010 by Anders Nor Berle.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut
139