blib/lib/Tie/PureDB.pm

filter: Statementstmt 96.6% 86 / 89 Branchbran 71.4% 10 / 14 Conditioncond 46.6% 7 / 15 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 26 / 26 Podpod n/a totaltotal 86.0% 130 / 151 CC 13 SCAR 10.5
CC
13
Cov
87%
new
7.3
new
6.1
STORE
2.3
CRAP 2.9
↑ Summary PureDB.xs »
1 =head1 NAME
2  
3 Tie::PureDB - Perl extension for pure-db
4  
5 =head1 SYNOPSIS
6  
7 use Tie::PureDB;
8 my $file = 'foo.db';
9 {
10 tie my %db, 'Tie::PureDB::Write', "$file.index", "$file.data", $file
11 or die "Couldn't create database ($!)";
12  
13 $db{createtime} = scalar gmtime;
14 $db{$_} = rand($_) for 1..100;
15  
16 untie %db;
17  
18 tie %db, 'Tie::PureDB::Read', $file
19 or die "Couldn't read database ($!)";
20  
21 print "This database was created on $db{createtime}\n";
22 print " 1 => $db{1}\n 6 => $db{6}\n\n";
23  
24 untie %db;
25 }
26 {
27 my $db = Tie::PureDB::Write->new( "$file.index", "$file.data", $file )
28 or die "Couldn't create database ($!)";
29  
30 $db->puredbw_add( createtime => scalar gmtime );
31 $db->add( $_ => rand($_)) for 1..100;
32  
33 undef $db;
34  
35 $db = Tie::PureDB::Read->($file)
36 or die "Couldn't read database ($!)";
37  
38 print "This database was created on ",
39 $db->read( $db->puredb_find('createtime') ), \n";
40  
41 print " 1 => ", $db->FETCH(1) || "EEEK!($!)", "\n";
42 print " 1 => ", $db->FETCH(9) || "EEEK!($!)", "\n";
43  
44 undef $db;
45 }
46  
47 =head1 DESCRIPTION
48  
49 This is the perl interface to PureDB.
50 If you wanna know what PureDB is, visit the
51 PureDB home page at http://www.pureftpd.org/puredb/ .
52  
53 Now go read the examples ;)
54  
55 =cut
56  
57  
58 package Tie::PureDB;
59  
60 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 8 ▶use DynaLoader();
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 8 ▶use vars qw[ @ISA $VERSION ];
Subroutine BEGIN: called CC=1 SCAR=0.0
65 8 @ISA = qw( DynaLoader );
66  
67 8 $VERSION = '0.04';
68  
69 8 bootstrap Tie::PureDB $VERSION;
70  
71 package Tie::PureDB::Write;
72 8 ▶use Carp qw[ carp croak ];
Subroutine BEGIN: called CC=1 SCAR=0.0
73 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 =head1 Tie::PureDB::Write
76  
77 This is the interface to libpuredb_write.
78  
79 If you use the tie interface, you can only use it to store values
80 (C<$db{foo}=1;> aka C<(tied %db)-E<gt>STORE(foo =E<gt> 1);> ).
81 It is highly reccomended that you use the tie interface.
82  
83 If you use the function interface, you'll wanna use the following functions.
84  
85 =head2 puredbw_open
86  
87 Also known as C<open>, or C<new>.
88 It takes 3 arguments: file_index, file_data, file_final.
89  
90 On success, returns a Tie::PureDB::Write object.
91 On failure, returns nothing while setting $!.
92  
93 =cut
94  
95 8 ▶{ no strict 'refs'; *open = *puredbw_open = *new; }
Subroutine BEGIN: called CC=1 SCAR=0.0
96 24 ▶sub new {
Subroutine new: called CC=6 SCAR=19.9
97 24 my $package = shift;
98  
99 24 ▶ croak "Usage error : 3 arguments expected (file_index, file_data, file_final)"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 and length $_[0] and length $_[1] and length $_[2] 10 14 24
Condition55%5/9
@_ == 3 and length $_[0] and length $_[1] and length $_[2]
!ll&&!rl&&r
10014
@_ == 3 and length $_[0] and length $_[1]
!ll&&!rl&&r
01014
@_ == 3 and length $_[0]
!ll&&!rl&&r
0024
MC/DC25%1/4
@_ == 3 and length $_[0] and length $_[1] and length $_[2]
@_ == 3 length $_[0] length $_[1] length $_[2]
Truth table40%2/5
A and B and C and D
A= @_ == 3B= length $_[0]C= length $_[1]D= length $_[2]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
100 unless
101 @_ == 3
102 and length $_[0]
103 and length $_[1]
104 and length $_[2];
105  
106 14 my $it = xs_new(@_);
107 14 ▶ return bless \$it, $package if defined $it;
Branch: 1 branch
BranchTrueFalseTotal
if defined $it 14 0 14
108 0 return();
109 }
110  
111 =head2 add
112  
113 Also known as C<open>, or C<puredbw_add>.
114 It takes 2 arguments: key,value.
115  
116 On success, returns a true value.
117 On failure, returns nothing while setting $!.
118  
119 =cut
120  
121 8 ▶{ no strict 'refs'; *add = *puredbw_add; }
Subroutine BEGIN: called CC=1 SCAR=0.0
122 18 ▶sub puredbw_add {
Subroutine puredbw_add: called CC=1 SCAR=0.0
123 18 my( $self, $key, $value ) = @_;
124 18 return xs_puredbw_add($$self, $key, $value);
125 }
126  
127 =head2 CAVEATS(undefined functions)
128  
129 Don't try to use the following functions, they are not defined
130 (for example: C<keys %db>. See L<perltie|perltie> for more info details.).
131  
132 # these would require an extension to libpuredb_write, which I ain't ready for
133 sub FIRSTKEY(){}
134 sub NEXTKEY(){}
135  
136 # these are NO-NOs (libpuredb_write don't know this)
137 sub FETCH(){}
138 sub EXISTS(){}
139 sub DELETE(){}
140 sub CLEAR(){}
141  
142 =cut
143  
144 7 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=8.1
145 7 my( $self, $key, $value ) = @_;
146 7 ▶ return $value if xs_puredbw_add($$self, $key, $value);
Branch: 1 branch
BranchTrueFalseTotal
if &xs_puredbw_add($$self, $key, $value) 7 0 7
147 0 return();
148 }
149  
150  
151 11 ▶sub TIEHASH { goto &new; }
Subroutine TIEHASH: called CC=1 SCAR=0.0
152  
153 6 ▶sub UNTIE {
Subroutine UNTIE: called CC=2 SCAR=6.9
154 6 my ($obj,$count) = @_;
155 6 ▶ carp "untie attempted while $count inner references still exist" if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 1 5 6
156 6 return();
157 }
158  
159 14 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
160 14 my $self = shift;
161 14 xs_free($$self);
162 }
163  
164  
165 package Tie::PureDB::Read;
166 8 ▶use Carp qw[ carp croak ];
Subroutine BEGIN: called CC=1 SCAR=0.0
167 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 =head1 Tie::PureDB::Read
170  
171 This is the interface to libpuredb_read.
172  
173 If you use the tie interface, you can only use it to read values
174 (C<print $db{foo};> aka C<print (tied %db)-E<gt>FETCH('foo');> ).
175 It is highly reccomended that you use the tie interface.
176  
177 If you use the function interface, you'll wanna use the following functions.
178  
179 =head2 puredb_open
180  
181 Also known as C<new>, or C<open>.
182 It takes 1 arguments: file_final.
183  
184 On success, returns a Tie::PureDB::Read object.
185 On failure, returns nothing while setting $!.
186  
187 =cut
188  
189 8 ▶{ no strict 'refs'; *open = *puredb_open = *new; }
Subroutine BEGIN: called CC=1 SCAR=0.0
190 14 ▶sub new {
Subroutine new: called CC=5 SCAR=18.1
191 14 my $package = shift;
192  
193 14 ▶ croak "Usage error : 1 argument of length greater than 0 expected"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and defined $_[0] and length $_[0] 0 14 14
Condition33%2/6
@_ and defined $_[0] and length $_[0]
!ll&&!rl&&r
0014
@_ and defined $_[0]
!ll&&!rl&&r
0014
MC/DC0%0/3
@_ and defined $_[0] and length $_[0]
@_ defined $_[0] length $_[0]
Truth table25%1/4
A and B and C
A= @_B= defined $_[0]C= length $_[0]
ABCresult
0XX0
10X0
1100
1111
194 unless @_
195 and defined $_[0]
196 and length $_[0];
197  
198 14 my $it = xs_new(@_);
199 14 ▶ return bless \$it, $package if defined $it;
Branch: 1 branch
BranchTrueFalseTotal
if defined $it 4 10 14
200 }
201  
202  
203 =head2 getsize
204  
205 Also known as C<puredb_getsize>.
206 Takes 0 arguments.
207 Returns the size of the database in bytes
208 (same number as C<-s $file>).
209  
210 =cut
211  
212 8 ▶{ no strict 'refs'; *getsize = *puredb_getsize; }
Subroutine BEGIN: called CC=1 SCAR=0.0
213 4 ▶sub puredb_getsize {
Subroutine puredb_getsize: called CC=1 SCAR=0.0
214 4 my $self = shift;
215 4 return xs_puredb_getsize($$self);
216 }
217  
218  
219 =head2 find
220  
221 Also known as C<puredb_find>.
222 Takes 1 argument (the key to find),
223 On success, returns offset,length.
224 On failure, returns nothing while setting $!.
225  
226 =cut
227  
228 8 ▶{ no strict 'refs'; *find = *EXISTS = *puredb_find; }
Subroutine BEGIN: called CC=1 SCAR=0.0
229 22 ▶sub puredb_find {
Subroutine puredb_find: called CC=1 SCAR=0.0
230 22 my $self = shift;
231  
232 22 return xs_puredb_find($$self,@_);
233 }
234  
235 =head2 read
236  
237 Also known as C<puredb_read>.
238 Takes 2 arguments (offset,length).
239 On success, returns the value.
240 On failure, returns nothing while setting $!.
241  
242 B<**WARNING> --
243 It is highly discouraged that you use C<read> with invalid offsets.
244 Always use those returned by C<find>,
245 or simply use C<FETCH> or the tie interface.
246  
247 =cut
248  
249 8 ▶{ no strict 'refs'; *read = *puredb_read; }
Subroutine BEGIN: called CC=1 SCAR=0.0
250 16 ▶sub puredb_read {
Subroutine puredb_read: called CC=1 SCAR=0.0
251 16 my $self = shift;
252 16 return xs_puredb_read($$self,@_);
253 }
254  
255  
256 =head2 FETCH
257  
258 A I<utiliy method>.
259 use C<$db-E<gt>FETCH('foo')> instead of C<$db-E<gt>read( $db-E<gt>find('foo') );>
260 Returns undef on failure.
261  
262 =cut
263  
264 35 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.4
265 35 my($self,$key) = @_;
266 35 my @ret = xs_puredb_find($$self, $key);
267 35 ▶ if(@ret){
Branch: 1 branch
BranchTrueFalseTotal
if (@ret) { } 35 0 35
268 35 return xs_puredb_read($$self,@ret);
269 } else {
270 0 return undef;
271 }
272 }
273  
274 6 ▶sub TIEHASH { goto &new; }
Subroutine TIEHASH: called CC=1 SCAR=0.0
275  
276 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
277 4 my $self = shift;
278 4 xs_free($$self);# if defined $self;
279 }
280  
281  
282 =head2 CAVEATS (undefined functions)
283  
284 Don't try to use the following functions, they are not defined
285 (for example: C<keys %db>. See L<perltie|perltie> for more info details.).
286  
287 # these would require an extension to libpuredb_read, which I ain't ready for
288 sub FIRSTKEY(){}
289 sub NEXTKEY(){}
290  
291 # these are NO-NOs (libpuredb_read don't know this)
292 sub STORE(){}
293 sub DELETE(){}
294 sub CLEAR(){}
295  
296 =cut
297  
298  
299 8 1;
300  
301  
302 =head1 THREAD SAFETY
303  
304 AFAIK, this module and the underlying c library do not use
305 globally shared data, and as such, they are "thread-safe".
306  
307  
308 =head1 CAVEATS (The C<untie()> Gotcha)
309  
310 If you aren't aware of the I<Gotcha>,
311 read about it before even attempting to use this module ;)
312  
313 L<The untie Gotcha|perltie/The untie Gotcha> in L<perltie|perltie>.
314  
315  
316 =head1 Memoize
317  
318 You could use Memoize with this module.
319 All you have to do is add the following lines to your program:
320  
321 use Tie::PureDB;
322 BEGIN{
323 package Tie::PureDB::Read;
324 use Memoize();
325 Memoize::memoize('puredb_read','puredb_find','FETCH');
326 no strict 'refs';
327 *read = *puredb_read;
328 *find = *puredb_find;
329 *EXISTS = *puredb_find;
330 package main;
331 }
332 ## ... rest of your code follows
333  
334  
335 =head1 AUTHOR
336  
337 D. H. E<lt>PodMaster@cpan.orgE<gt>
338 who is very thankful to I<tye and B<the perlmonks>>,
339 as well as Tim Jenness and Simon Cozens
340 (authors of Extending and Embedding Perl -- http://www.manning.com/jenness/ ).
341  
342 =head1 SEE ALSO
343  
344 L<perl>,
345 L<perltie>,
346 L<perldata>,
347 L<AnyDBM_File>,
348 L<DB_File>,
349 L<BerkeleyDB>.
350  
351 =cut
352  
353 ## damn link targets podchecker
↑ Summary PureDB.xs »