blib/lib/Tie/Senna.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # $Id: Senna.pm 2 2005-11-15 15:43:07Z daisuke $
2 #
3 # Copyright (c) 2005 Daisuke Maki <dmaki@cpan.org>
4 # All rights reserved.
5  
6 package Tie::Senna;
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use base qw(Tie::Hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Senna;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 our $VERSION = '0.02';
11  
12 sub TIEHASH
13 {
14 my $class = shift;
15 my %args = @_;
16 my $index = $args{index};
17 my $storage = $args{storage};
18  
19 # If not storage given, use a plain hash (all in memory)
20 if (!$storage) {
21 warn "no storage given, using in-memory hash (all data will be lost when program exits)";
22 $storage = {};
23 }
24  
25 if (ref($storage) ne 'HASH') {
26 Carp::croak("storage must be a hash!");
27 }
28  
29 bless {
30 index => $index,
31 storage => $storage,
32 }, $class;
33 }
34  
35 sub index { shift->{index} }
36 sub storage { shift->{storage} }
37  
38 sub search
39 {
40 my $self = shift;
41 my $query = shift;
42  
43 my $c = $self->{index}->search($query);
44 return wantarray ? $c->as_list : $c;
45 }
46  
47 sub STORE
48 {
49 my $self = shift;
50 my $key = shift;
51 my $val = shift;
52  
53 if ($self->EXISTS($key)) {
54 $self->{index}->replace($key,
55 delete $self->{storage}->{$key},
56 $val);
57 } else {
58 $self->{index}->put($key, $val);
59 }
60 $self->{storage}->{$key} = $val;
61 }
62  
63 sub DELETE
64 {
65 my $self = shift;
66 my $key = shift;
67 $self->{index}->del($key, $self->{storage}->{$key});
68 delete $self->{storage}->{$key};
69 }
70  
71 sub EXISTS
72 {
73 my $self = shift;
74 my $key = shift;
75 return exists $self->{storage}->{$key};
76 }
77  
78 sub FIRSTKEY
79 {
80 my $self = shift;
81 my $storage = $self->{storage};
82 if (my $tied = tied(%$storage)) {
83 return $tied->FIRSTKEY(@_);
84 } else {
85 my $a = scalar keys %{$storage};
86 return each %{$_[0]};
87 }
88 }
89  
90 sub NEXTKEY
91 {
92 my $self = shift;
93 my $storage = $self->{storage};
94 if (my $tied = tied(%$storage)) {
95 return $tied->NEXTKEY(@_);
96 } else {
97 my $a = scalar keys %{$storage};
98 return each %{$_[0]};
99 }
100 }
101  
102 sub CLEAR
103 {
104 my $self = shift;
105  
106 my @args = (
107 $self->{index}->filename,
108 $self->{index}->key_size,
109 $self->{index}->flags,
110 $self->{index}->initial_n_segments,
111 $self->{index}->encoding,
112 );
113 $self->{index}->remove;
114 $self->{index}->create(@args);
115  
116 if (my $tied = tied(%{$self->{storage}})) {
117 $tied->CLEAR(@_);
118 } else {
119 %{$self->{storage}} = ();
120 }
121 }
122  
123 1;
124  
125 __END__
↑ Summary