Commit | Line | Data |
a0d0e21e |
1 | # DB_File.pm -- Perl 5 interface to Berkeley DB |
2 | # |
3 | # written by Paul Marquess (pmarquess@bfsec.bt.co.uk) |
9d9477b1 |
4 | # last modified 19th November 1998 |
5 | # version 1.61 |
36477c24 |
6 | # |
a9fd575d |
7 | # Copyright (c) 1995-8 Paul Marquess. All rights reserved. |
36477c24 |
8 | # This program is free software; you can redistribute it and/or |
9 | # modify it under the same terms as Perl itself. |
10 | |
8e07c86e |
11 | |
12 | package DB_File::HASHINFO ; |
785da04d |
13 | |
610ab055 |
14 | require 5.003 ; |
15 | |
785da04d |
16 | use strict; |
8e07c86e |
17 | use Carp; |
88108326 |
18 | require Tie::Hash; |
19 | @DB_File::HASHINFO::ISA = qw(Tie::Hash); |
8e07c86e |
20 | |
88108326 |
21 | sub new |
8e07c86e |
22 | { |
88108326 |
23 | my $pkg = shift ; |
24 | my %x ; |
25 | tie %x, $pkg ; |
26 | bless \%x, $pkg ; |
8e07c86e |
27 | } |
28 | |
610ab055 |
29 | |
88108326 |
30 | sub TIEHASH |
31 | { |
32 | my $pkg = shift ; |
33 | |
36477c24 |
34 | bless { VALID => { map {$_, 1} |
35 | qw( bsize ffactor nelem cachesize hash lorder) |
36 | }, |
37 | GOT => {} |
38 | }, $pkg ; |
88108326 |
39 | } |
8e07c86e |
40 | |
610ab055 |
41 | |
8e07c86e |
42 | sub FETCH |
43 | { |
88108326 |
44 | my $self = shift ; |
45 | my $key = shift ; |
8e07c86e |
46 | |
36477c24 |
47 | return $self->{GOT}{$key} if exists $self->{VALID}{$key} ; |
88108326 |
48 | |
49 | my $pkg = ref $self ; |
50 | croak "${pkg}::FETCH - Unknown element '$key'" ; |
8e07c86e |
51 | } |
52 | |
53 | |
54 | sub STORE |
55 | { |
88108326 |
56 | my $self = shift ; |
57 | my $key = shift ; |
58 | my $value = shift ; |
59 | |
36477c24 |
60 | if ( exists $self->{VALID}{$key} ) |
8e07c86e |
61 | { |
36477c24 |
62 | $self->{GOT}{$key} = $value ; |
8e07c86e |
63 | return ; |
64 | } |
65 | |
88108326 |
66 | my $pkg = ref $self ; |
67 | croak "${pkg}::STORE - Unknown element '$key'" ; |
8e07c86e |
68 | } |
69 | |
70 | sub DELETE |
71 | { |
88108326 |
72 | my $self = shift ; |
73 | my $key = shift ; |
74 | |
36477c24 |
75 | if ( exists $self->{VALID}{$key} ) |
8e07c86e |
76 | { |
36477c24 |
77 | delete $self->{GOT}{$key} ; |
8e07c86e |
78 | return ; |
79 | } |
80 | |
88108326 |
81 | my $pkg = ref $self ; |
82 | croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ; |
8e07c86e |
83 | } |
84 | |
88108326 |
85 | sub EXISTS |
8e07c86e |
86 | { |
88108326 |
87 | my $self = shift ; |
88 | my $key = shift ; |
8e07c86e |
89 | |
36477c24 |
90 | exists $self->{VALID}{$key} ; |
8e07c86e |
91 | } |
92 | |
88108326 |
93 | sub NotHere |
8e07c86e |
94 | { |
18d2dc8c |
95 | my $self = shift ; |
88108326 |
96 | my $method = shift ; |
8e07c86e |
97 | |
18d2dc8c |
98 | croak ref($self) . " does not define the method ${method}" ; |
8e07c86e |
99 | } |
100 | |
18d2dc8c |
101 | sub FIRSTKEY { my $self = shift ; $self->NotHere("FIRSTKEY") } |
102 | sub NEXTKEY { my $self = shift ; $self->NotHere("NEXTKEY") } |
103 | sub CLEAR { my $self = shift ; $self->NotHere("CLEAR") } |
8e07c86e |
104 | |
105 | package DB_File::RECNOINFO ; |
785da04d |
106 | |
88108326 |
107 | use strict ; |
108 | |
045291aa |
109 | @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ; |
8e07c86e |
110 | |
111 | sub TIEHASH |
112 | { |
88108326 |
113 | my $pkg = shift ; |
114 | |
36477c24 |
115 | bless { VALID => { map {$_, 1} |
116 | qw( bval cachesize psize flags lorder reclen bfname ) |
117 | }, |
118 | GOT => {}, |
119 | }, $pkg ; |
8e07c86e |
120 | } |
121 | |
88108326 |
122 | package DB_File::BTREEINFO ; |
8e07c86e |
123 | |
88108326 |
124 | use strict ; |
8e07c86e |
125 | |
88108326 |
126 | @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ; |
8e07c86e |
127 | |
88108326 |
128 | sub TIEHASH |
8e07c86e |
129 | { |
88108326 |
130 | my $pkg = shift ; |
131 | |
36477c24 |
132 | bless { VALID => { map {$_, 1} |
133 | qw( flags cachesize maxkeypage minkeypage psize |
134 | compare prefix lorder ) |
135 | }, |
136 | GOT => {}, |
137 | }, $pkg ; |
8e07c86e |
138 | } |
139 | |
140 | |
8e07c86e |
141 | package DB_File ; |
785da04d |
142 | |
143 | use strict; |
1f70e1ea |
144 | use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO $db_version) ; |
8e07c86e |
145 | use Carp; |
146 | |
785da04d |
147 | |
9d9477b1 |
148 | $VERSION = "1.61" ; |
8e07c86e |
149 | |
150 | #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE; |
88108326 |
151 | $DB_BTREE = new DB_File::BTREEINFO ; |
152 | $DB_HASH = new DB_File::HASHINFO ; |
153 | $DB_RECNO = new DB_File::RECNOINFO ; |
8e07c86e |
154 | |
785da04d |
155 | require Tie::Hash; |
8e07c86e |
156 | require Exporter; |
157 | use AutoLoader; |
158 | require DynaLoader; |
785da04d |
159 | @ISA = qw(Tie::Hash Exporter DynaLoader); |
8e07c86e |
160 | @EXPORT = qw( |
161 | $DB_BTREE $DB_HASH $DB_RECNO |
88108326 |
162 | |
8e07c86e |
163 | BTREEMAGIC |
164 | BTREEVERSION |
165 | DB_LOCK |
166 | DB_SHMEM |
167 | DB_TXN |
168 | HASHMAGIC |
169 | HASHVERSION |
170 | MAX_PAGE_NUMBER |
171 | MAX_PAGE_OFFSET |
172 | MAX_REC_NUMBER |
173 | RET_ERROR |
174 | RET_SPECIAL |
175 | RET_SUCCESS |
176 | R_CURSOR |
177 | R_DUP |
178 | R_FIRST |
179 | R_FIXEDLEN |
180 | R_IAFTER |
181 | R_IBEFORE |
182 | R_LAST |
183 | R_NEXT |
184 | R_NOKEY |
185 | R_NOOVERWRITE |
186 | R_PREV |
187 | R_RECNOSYNC |
188 | R_SETCURSOR |
189 | R_SNAPSHOT |
190 | __R_UNUSED |
88108326 |
191 | |
045291aa |
192 | ); |
8e07c86e |
193 | |
194 | sub AUTOLOAD { |
785da04d |
195 | my($constname); |
8e07c86e |
196 | ($constname = $AUTOLOAD) =~ s/.*:://; |
785da04d |
197 | my $val = constant($constname, @_ ? $_[0] : 0); |
8e07c86e |
198 | if ($! != 0) { |
199 | if ($! =~ /Invalid/) { |
200 | $AutoLoader::AUTOLOAD = $AUTOLOAD; |
201 | goto &AutoLoader::AUTOLOAD; |
202 | } |
203 | else { |
785da04d |
204 | my($pack,$file,$line) = caller; |
8e07c86e |
205 | croak "Your vendor has not defined DB macro $constname, used at $file line $line. |
206 | "; |
207 | } |
208 | } |
209 | eval "sub $AUTOLOAD { $val }"; |
210 | goto &$AUTOLOAD; |
211 | } |
212 | |
f6b705ef |
213 | |
a6ed719b |
214 | eval { |
1f70e1ea |
215 | # Make all Fcntl O_XXX constants available for importing |
216 | require Fcntl; |
217 | my @O = grep /^O_/, @Fcntl::EXPORT; |
218 | Fcntl->import(@O); # first we import what we want to export |
219 | push(@EXPORT, @O); |
a6ed719b |
220 | }; |
f6b705ef |
221 | |
1f70e1ea |
222 | ## import borrowed from IO::File |
223 | ## exports Fcntl constants if available. |
224 | #sub import { |
225 | # my $pkg = shift; |
226 | # my $callpkg = caller; |
227 | # Exporter::export $pkg, $callpkg, @_; |
228 | # eval { |
229 | # require Fcntl; |
230 | # Exporter::export 'Fcntl', $callpkg, '/^O_/'; |
231 | # }; |
232 | #} |
233 | |
785da04d |
234 | bootstrap DB_File $VERSION; |
8e07c86e |
235 | |
236 | # Preloaded methods go here. Autoload methods go after __END__, and are |
237 | # processed by the autosplit program. |
238 | |
05475680 |
239 | sub tie_hash_or_array |
610ab055 |
240 | { |
241 | my (@arg) = @_ ; |
05475680 |
242 | my $tieHASH = ( (caller(1))[3] =~ /TIEHASH/ ) ; |
610ab055 |
243 | |
244 | $arg[4] = tied %{ $arg[4] } |
245 | if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ; |
246 | |
1f70e1ea |
247 | # make recno in Berkeley DB version 2 work like recno in version 1. |
248 | if ($db_version > 1 and defined $arg[4] and $arg[4] =~ /RECNO/ and |
249 | $arg[1] and ! -e $arg[1]) { |
250 | open(FH, ">$arg[1]") or return undef ; |
251 | close FH ; |
252 | chmod $arg[3] ? $arg[3] : 0666 , $arg[1] ; |
253 | } |
254 | |
05475680 |
255 | DoTie_($tieHASH, @arg) ; |
610ab055 |
256 | } |
257 | |
05475680 |
258 | sub TIEHASH |
259 | { |
260 | tie_hash_or_array(@_) ; |
261 | } |
262 | |
263 | sub TIEARRAY |
264 | { |
265 | tie_hash_or_array(@_) ; |
266 | } |
88108326 |
267 | |
045291aa |
268 | sub CLEAR |
269 | { |
1f70e1ea |
270 | my $self = shift; |
271 | my $key = "" ; |
272 | my $value = "" ; |
273 | my $status = $self->seq($key, $value, R_FIRST()); |
274 | my @keys; |
275 | |
276 | while ($status == 0) { |
277 | push @keys, $key; |
278 | $status = $self->seq($key, $value, R_NEXT()); |
279 | } |
280 | foreach $key (reverse @keys) { |
281 | my $s = $self->del($key); |
282 | } |
283 | } |
284 | |
045291aa |
285 | sub EXTEND { } |
286 | |
287 | sub STORESIZE |
288 | { |
289 | my $self = shift; |
290 | my $length = shift ; |
291 | my $current_length = $self->length() ; |
292 | |
293 | if ($length < $current_length) { |
294 | my $key ; |
295 | for ($key = $current_length - 1 ; $key >= $length ; -- $key) |
296 | { $self->del($key) } |
297 | } |
a9fd575d |
298 | elsif ($length > $current_length) { |
299 | $self->put($length-1, "") ; |
300 | } |
045291aa |
301 | } |
302 | |
88108326 |
303 | sub get_dup |
304 | { |
305 | croak "Usage: \$db->get_dup(key [,flag])\n" |
306 | unless @_ == 2 or @_ == 3 ; |
307 | |
308 | my $db = shift ; |
309 | my $key = shift ; |
310 | my $flag = shift ; |
f6b705ef |
311 | my $value = 0 ; |
88108326 |
312 | my $origkey = $key ; |
313 | my $wantarray = wantarray ; |
f6b705ef |
314 | my %values = () ; |
88108326 |
315 | my @values = () ; |
316 | my $counter = 0 ; |
f6b705ef |
317 | my $status = 0 ; |
88108326 |
318 | |
f6b705ef |
319 | # iterate through the database until either EOF ($status == 0) |
320 | # or a different key is encountered ($key ne $origkey). |
321 | for ($status = $db->seq($key, $value, R_CURSOR()) ; |
322 | $status == 0 and $key eq $origkey ; |
323 | $status = $db->seq($key, $value, R_NEXT()) ) { |
88108326 |
324 | |
f6b705ef |
325 | # save the value or count number of matches |
326 | if ($wantarray) { |
327 | if ($flag) |
328 | { ++ $values{$value} } |
329 | else |
330 | { push (@values, $value) } |
331 | } |
332 | else |
333 | { ++ $counter } |
88108326 |
334 | |
88108326 |
335 | } |
336 | |
f6b705ef |
337 | return ($wantarray ? ($flag ? %values : @values) : $counter) ; |
88108326 |
338 | } |
339 | |
340 | |
8e07c86e |
341 | 1; |
342 | __END__ |
343 | |
3b35bae3 |
344 | =head1 NAME |
345 | |
1f70e1ea |
346 | DB_File - Perl5 access to Berkeley DB version 1.x |
3b35bae3 |
347 | |
348 | =head1 SYNOPSIS |
349 | |
350 | use DB_File ; |
88108326 |
351 | |
352 | [$X =] tie %hash, 'DB_File', [$filename, $flags, $mode, $DB_HASH] ; |
353 | [$X =] tie %hash, 'DB_File', $filename, $flags, $mode, $DB_BTREE ; |
354 | [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ; |
760ac839 |
355 | |
3b35bae3 |
356 | $status = $X->del($key [, $flags]) ; |
357 | $status = $X->put($key, $value [, $flags]) ; |
358 | $status = $X->get($key, $value [, $flags]) ; |
760ac839 |
359 | $status = $X->seq($key, $value, $flags) ; |
3b35bae3 |
360 | $status = $X->sync([$flags]) ; |
361 | $status = $X->fd ; |
760ac839 |
362 | |
f6b705ef |
363 | # BTREE only |
88108326 |
364 | $count = $X->get_dup($key) ; |
365 | @list = $X->get_dup($key) ; |
366 | %list = $X->get_dup($key, 1) ; |
367 | |
f6b705ef |
368 | # RECNO only |
369 | $a = $X->length; |
370 | $a = $X->pop ; |
371 | $X->push(list); |
372 | $a = $X->shift; |
373 | $X->unshift(list); |
374 | |
3b35bae3 |
375 | untie %hash ; |
376 | untie @array ; |
377 | |
378 | =head1 DESCRIPTION |
379 | |
8e07c86e |
380 | B<DB_File> is a module which allows Perl programs to make use of the |
1f70e1ea |
381 | facilities provided by Berkeley DB version 1.x (if you have a newer |
382 | version of DB, see L<Using DB_File with Berkeley DB version 2>). It is |
383 | assumed that you have a copy of the Berkeley DB manual pages at hand |
384 | when reading this documentation. The interface defined here mirrors the |
385 | Berkeley DB interface closely. |
68dc0745 |
386 | |
8e07c86e |
387 | Berkeley DB is a C library which provides a consistent interface to a |
388 | number of database formats. B<DB_File> provides an interface to all |
389 | three of the database types currently supported by Berkeley DB. |
3b35bae3 |
390 | |
391 | The file types are: |
392 | |
393 | =over 5 |
394 | |
88108326 |
395 | =item B<DB_HASH> |
3b35bae3 |
396 | |
88108326 |
397 | This database type allows arbitrary key/value pairs to be stored in data |
8e07c86e |
398 | files. This is equivalent to the functionality provided by other |
399 | hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though, |
400 | the files created using DB_HASH are not compatible with any of the |
401 | other packages mentioned. |
3b35bae3 |
402 | |
8e07c86e |
403 | A default hashing algorithm, which will be adequate for most |
404 | applications, is built into Berkeley DB. If you do need to use your own |
405 | hashing algorithm it is possible to write your own in Perl and have |
406 | B<DB_File> use it instead. |
3b35bae3 |
407 | |
88108326 |
408 | =item B<DB_BTREE> |
409 | |
410 | The btree format allows arbitrary key/value pairs to be stored in a |
8e07c86e |
411 | sorted, balanced binary tree. |
3b35bae3 |
412 | |
8e07c86e |
413 | As with the DB_HASH format, it is possible to provide a user defined |
414 | Perl routine to perform the comparison of keys. By default, though, the |
415 | keys are stored in lexical order. |
3b35bae3 |
416 | |
88108326 |
417 | =item B<DB_RECNO> |
3b35bae3 |
418 | |
8e07c86e |
419 | DB_RECNO allows both fixed-length and variable-length flat text files |
420 | to be manipulated using the same key/value pair interface as in DB_HASH |
421 | and DB_BTREE. In this case the key will consist of a record (line) |
422 | number. |
3b35bae3 |
423 | |
424 | =back |
425 | |
1f70e1ea |
426 | =head2 Using DB_File with Berkeley DB version 2 |
427 | |
428 | Although B<DB_File> is intended to be used with Berkeley DB version 1, |
429 | it can also be used with version 2. In this case the interface is |
430 | limited to the functionality provided by Berkeley DB 1.x. Anywhere the |
431 | version 2 interface differs, B<DB_File> arranges for it to work like |
432 | version 1. This feature allows B<DB_File> scripts that were built with |
433 | version 1 to be migrated to version 2 without any changes. |
434 | |
435 | If you want to make use of the new features available in Berkeley DB |
436 | 2.x, use the Perl module B<BerkeleyDB> instead. |
437 | |
438 | At the time of writing this document the B<BerkeleyDB> module is still |
439 | alpha quality (the version number is < 1.0), and so unsuitable for use |
440 | in any serious development work. Once its version number is >= 1.0, it |
441 | is considered stable enough for real work. |
442 | |
443 | B<Note:> The database file format has changed in Berkeley DB version 2. |
444 | If you cannot recreate your databases, you must dump any existing |
445 | databases with the C<db_dump185> utility that comes with Berkeley DB. |
446 | Once you have upgraded DB_File to use Berkeley DB version 2, your |
447 | databases can be recreated using C<db_load>. Refer to the Berkeley DB |
448 | documentation for further details. |
449 | |
450 | Please read L<COPYRIGHT> before using version 2.x of Berkeley DB with |
451 | DB_File. |
452 | |
68dc0745 |
453 | =head2 Interface to Berkeley DB |
3b35bae3 |
454 | |
455 | B<DB_File> allows access to Berkeley DB files using the tie() mechanism |
8e07c86e |
456 | in Perl 5 (for full details, see L<perlfunc/tie()>). This facility |
457 | allows B<DB_File> to access Berkeley DB files using either an |
458 | associative array (for DB_HASH & DB_BTREE file types) or an ordinary |
459 | array (for the DB_RECNO file type). |
3b35bae3 |
460 | |
88108326 |
461 | In addition to the tie() interface, it is also possible to access most |
462 | of the functions provided in the Berkeley DB API directly. |
f6b705ef |
463 | See L<THE API INTERFACE>. |
3b35bae3 |
464 | |
88108326 |
465 | =head2 Opening a Berkeley DB Database File |
3b35bae3 |
466 | |
8e07c86e |
467 | Berkeley DB uses the function dbopen() to open or create a database. |
f6b705ef |
468 | Here is the C prototype for dbopen(): |
3b35bae3 |
469 | |
470 | DB* |
471 | dbopen (const char * file, int flags, int mode, |
472 | DBTYPE type, const void * openinfo) |
473 | |
474 | The parameter C<type> is an enumeration which specifies which of the 3 |
475 | interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used. |
476 | Depending on which of these is actually chosen, the final parameter, |
477 | I<openinfo> points to a data structure which allows tailoring of the |
478 | specific interface method. |
479 | |
8e07c86e |
480 | This interface is handled slightly differently in B<DB_File>. Here is |
88108326 |
481 | an equivalent call using B<DB_File>: |
3b35bae3 |
482 | |
88108326 |
483 | tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ; |
3b35bae3 |
484 | |
8e07c86e |
485 | The C<filename>, C<flags> and C<mode> parameters are the direct |
486 | equivalent of their dbopen() counterparts. The final parameter $DB_HASH |
487 | performs the function of both the C<type> and C<openinfo> parameters in |
488 | dbopen(). |
3b35bae3 |
489 | |
88108326 |
490 | In the example above $DB_HASH is actually a pre-defined reference to a |
491 | hash object. B<DB_File> has three of these pre-defined references. |
492 | Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO. |
3b35bae3 |
493 | |
8e07c86e |
494 | The keys allowed in each of these pre-defined references is limited to |
495 | the names used in the equivalent C structure. So, for example, the |
496 | $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>, |
88108326 |
497 | C<ffactor>, C<hash>, C<lorder> and C<nelem>. |
498 | |
499 | To change one of these elements, just assign to it like this: |
500 | |
501 | $DB_HASH->{'cachesize'} = 10000 ; |
502 | |
503 | The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are |
504 | usually adequate for most applications. If you do need to create extra |
505 | instances of these objects, constructors are available for each file |
506 | type. |
507 | |
508 | Here are examples of the constructors and the valid options available |
509 | for DB_HASH, DB_BTREE and DB_RECNO respectively. |
510 | |
511 | $a = new DB_File::HASHINFO ; |
512 | $a->{'bsize'} ; |
513 | $a->{'cachesize'} ; |
514 | $a->{'ffactor'}; |
515 | $a->{'hash'} ; |
516 | $a->{'lorder'} ; |
517 | $a->{'nelem'} ; |
518 | |
519 | $b = new DB_File::BTREEINFO ; |
520 | $b->{'flags'} ; |
521 | $b->{'cachesize'} ; |
522 | $b->{'maxkeypage'} ; |
523 | $b->{'minkeypage'} ; |
524 | $b->{'psize'} ; |
525 | $b->{'compare'} ; |
526 | $b->{'prefix'} ; |
527 | $b->{'lorder'} ; |
528 | |
529 | $c = new DB_File::RECNOINFO ; |
530 | $c->{'bval'} ; |
531 | $c->{'cachesize'} ; |
532 | $c->{'psize'} ; |
533 | $c->{'flags'} ; |
534 | $c->{'lorder'} ; |
535 | $c->{'reclen'} ; |
536 | $c->{'bfname'} ; |
537 | |
538 | The values stored in the hashes above are mostly the direct equivalent |
539 | of their C counterpart. Like their C counterparts, all are set to a |
f6b705ef |
540 | default values - that means you don't have to set I<all> of the |
88108326 |
541 | values when you only want to change one. Here is an example: |
542 | |
543 | $a = new DB_File::HASHINFO ; |
544 | $a->{'cachesize'} = 12345 ; |
545 | tie %y, 'DB_File', "filename", $flags, 0777, $a ; |
546 | |
36477c24 |
547 | A few of the options need extra discussion here. When used, the C |
88108326 |
548 | equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers |
549 | to C functions. In B<DB_File> these keys are used to store references |
550 | to Perl subs. Below are templates for each of the subs: |
551 | |
552 | sub hash |
553 | { |
554 | my ($data) = @_ ; |
555 | ... |
556 | # return the hash value for $data |
557 | return $hash ; |
558 | } |
3b35bae3 |
559 | |
88108326 |
560 | sub compare |
561 | { |
562 | my ($key, $key2) = @_ ; |
563 | ... |
564 | # return 0 if $key1 eq $key2 |
565 | # -1 if $key1 lt $key2 |
566 | # 1 if $key1 gt $key2 |
567 | return (-1 , 0 or 1) ; |
568 | } |
3b35bae3 |
569 | |
88108326 |
570 | sub prefix |
571 | { |
572 | my ($key, $key2) = @_ ; |
573 | ... |
574 | # return number of bytes of $key2 which are |
575 | # necessary to determine that it is greater than $key1 |
576 | return $bytes ; |
577 | } |
3b35bae3 |
578 | |
f6b705ef |
579 | See L<Changing the BTREE sort order> for an example of using the |
580 | C<compare> template. |
88108326 |
581 | |
36477c24 |
582 | If you are using the DB_RECNO interface and you intend making use of |
9a2c4ce3 |
583 | C<bval>, you should check out L<The 'bval' Option>. |
36477c24 |
584 | |
88108326 |
585 | =head2 Default Parameters |
586 | |
587 | It is possible to omit some or all of the final 4 parameters in the |
588 | call to C<tie> and let them take default values. As DB_HASH is the most |
589 | common file format used, the call: |
590 | |
591 | tie %A, "DB_File", "filename" ; |
592 | |
593 | is equivalent to: |
594 | |
18d2dc8c |
595 | tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ; |
88108326 |
596 | |
597 | It is also possible to omit the filename parameter as well, so the |
598 | call: |
599 | |
600 | tie %A, "DB_File" ; |
601 | |
602 | is equivalent to: |
603 | |
18d2dc8c |
604 | tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ; |
88108326 |
605 | |
f6b705ef |
606 | See L<In Memory Databases> for a discussion on the use of C<undef> |
88108326 |
607 | in place of a filename. |
608 | |
f6b705ef |
609 | =head2 In Memory Databases |
610 | |
611 | Berkeley DB allows the creation of in-memory databases by using NULL |
612 | (that is, a C<(char *)0> in C) in place of the filename. B<DB_File> |
613 | uses C<undef> instead of NULL to provide this functionality. |
614 | |
615 | =head1 DB_HASH |
616 | |
617 | The DB_HASH file format is probably the most commonly used of the three |
618 | file formats that B<DB_File> supports. It is also very straightforward |
619 | to use. |
620 | |
68dc0745 |
621 | =head2 A Simple Example |
f6b705ef |
622 | |
623 | This example shows how to create a database, add key/value pairs to the |
624 | database, delete keys/value pairs and finally how to enumerate the |
625 | contents of the database. |
626 | |
610ab055 |
627 | use strict ; |
f6b705ef |
628 | use DB_File ; |
610ab055 |
629 | use vars qw( %h $k $v ) ; |
f6b705ef |
630 | |
631 | tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH |
632 | or die "Cannot open file 'fruit': $!\n"; |
633 | |
634 | # Add a few key/value pairs to the file |
635 | $h{"apple"} = "red" ; |
636 | $h{"orange"} = "orange" ; |
637 | $h{"banana"} = "yellow" ; |
638 | $h{"tomato"} = "red" ; |
639 | |
640 | # Check for existence of a key |
641 | print "Banana Exists\n\n" if $h{"banana"} ; |
642 | |
643 | # Delete a key/value pair. |
644 | delete $h{"apple"} ; |
645 | |
646 | # print the contents of the file |
647 | while (($k, $v) = each %h) |
648 | { print "$k -> $v\n" } |
649 | |
650 | untie %h ; |
651 | |
652 | here is the output: |
653 | |
654 | Banana Exists |
655 | |
656 | orange -> orange |
657 | tomato -> red |
658 | banana -> yellow |
659 | |
660 | Note that the like ordinary associative arrays, the order of the keys |
661 | retrieved is in an apparently random order. |
662 | |
663 | =head1 DB_BTREE |
664 | |
665 | The DB_BTREE format is useful when you want to store data in a given |
666 | order. By default the keys will be stored in lexical order, but as you |
667 | will see from the example shown in the next section, it is very easy to |
668 | define your own sorting function. |
669 | |
670 | =head2 Changing the BTREE sort order |
671 | |
672 | This script shows how to override the default sorting algorithm that |
673 | BTREE uses. Instead of using the normal lexical ordering, a case |
674 | insensitive compare function will be used. |
88108326 |
675 | |
610ab055 |
676 | use strict ; |
f6b705ef |
677 | use DB_File ; |
610ab055 |
678 | |
679 | my %h ; |
f6b705ef |
680 | |
681 | sub Compare |
682 | { |
683 | my ($key1, $key2) = @_ ; |
684 | "\L$key1" cmp "\L$key2" ; |
685 | } |
686 | |
687 | # specify the Perl sub that will do the comparison |
688 | $DB_BTREE->{'compare'} = \&Compare ; |
689 | |
690 | tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE |
691 | or die "Cannot open file 'tree': $!\n" ; |
692 | |
693 | # Add a key/value pair to the file |
694 | $h{'Wall'} = 'Larry' ; |
695 | $h{'Smith'} = 'John' ; |
696 | $h{'mouse'} = 'mickey' ; |
697 | $h{'duck'} = 'donald' ; |
698 | |
699 | # Delete |
700 | delete $h{"duck"} ; |
701 | |
702 | # Cycle through the keys printing them in order. |
703 | # Note it is not necessary to sort the keys as |
704 | # the btree will have kept them in order automatically. |
705 | foreach (keys %h) |
706 | { print "$_\n" } |
707 | |
708 | untie %h ; |
709 | |
710 | Here is the output from the code above. |
711 | |
712 | mouse |
713 | Smith |
714 | Wall |
715 | |
716 | There are a few point to bear in mind if you want to change the |
717 | ordering in a BTREE database: |
718 | |
719 | =over 5 |
720 | |
721 | =item 1. |
722 | |
723 | The new compare function must be specified when you create the database. |
724 | |
725 | =item 2. |
726 | |
727 | You cannot change the ordering once the database has been created. Thus |
728 | you must use the same compare function every time you access the |
88108326 |
729 | database. |
730 | |
f6b705ef |
731 | =back |
732 | |
68dc0745 |
733 | =head2 Handling Duplicate Keys |
f6b705ef |
734 | |
735 | The BTREE file type optionally allows a single key to be associated |
736 | with an arbitrary number of values. This option is enabled by setting |
737 | the flags element of C<$DB_BTREE> to R_DUP when creating the database. |
738 | |
88108326 |
739 | There are some difficulties in using the tied hash interface if you |
740 | want to manipulate a BTREE database with duplicate keys. Consider this |
741 | code: |
742 | |
610ab055 |
743 | use strict ; |
88108326 |
744 | use DB_File ; |
610ab055 |
745 | |
746 | use vars qw($filename %h ) ; |
747 | |
88108326 |
748 | $filename = "tree" ; |
749 | unlink $filename ; |
750 | |
751 | # Enable duplicate records |
752 | $DB_BTREE->{'flags'} = R_DUP ; |
753 | |
754 | tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
755 | or die "Cannot open $filename: $!\n"; |
756 | |
757 | # Add some key/value pairs to the file |
758 | $h{'Wall'} = 'Larry' ; |
759 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
760 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
88108326 |
761 | $h{'Smith'} = 'John' ; |
762 | $h{'mouse'} = 'mickey' ; |
763 | |
764 | # iterate through the associative array |
765 | # and print each key/value pair. |
766 | foreach (keys %h) |
767 | { print "$_ -> $h{$_}\n" } |
768 | |
f6b705ef |
769 | untie %h ; |
770 | |
88108326 |
771 | Here is the output: |
772 | |
773 | Smith -> John |
774 | Wall -> Larry |
775 | Wall -> Larry |
f6b705ef |
776 | Wall -> Larry |
88108326 |
777 | mouse -> mickey |
778 | |
f6b705ef |
779 | As you can see 3 records have been successfully created with key C<Wall> |
88108326 |
780 | - the only thing is, when they are retrieved from the database they |
f6b705ef |
781 | I<seem> to have the same value, namely C<Larry>. The problem is caused |
782 | by the way that the associative array interface works. Basically, when |
783 | the associative array interface is used to fetch the value associated |
784 | with a given key, it will only ever retrieve the first value. |
88108326 |
785 | |
786 | Although it may not be immediately obvious from the code above, the |
787 | associative array interface can be used to write values with duplicate |
788 | keys, but it cannot be used to read them back from the database. |
789 | |
790 | The way to get around this problem is to use the Berkeley DB API method |
791 | called C<seq>. This method allows sequential access to key/value |
f6b705ef |
792 | pairs. See L<THE API INTERFACE> for details of both the C<seq> method |
793 | and the API in general. |
88108326 |
794 | |
795 | Here is the script above rewritten using the C<seq> API method. |
796 | |
610ab055 |
797 | use strict ; |
88108326 |
798 | use DB_File ; |
88108326 |
799 | |
610ab055 |
800 | use vars qw($filename $x %h $status $key $value) ; |
801 | |
88108326 |
802 | $filename = "tree" ; |
803 | unlink $filename ; |
804 | |
805 | # Enable duplicate records |
806 | $DB_BTREE->{'flags'} = R_DUP ; |
807 | |
808 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
809 | or die "Cannot open $filename: $!\n"; |
810 | |
811 | # Add some key/value pairs to the file |
812 | $h{'Wall'} = 'Larry' ; |
813 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
814 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
88108326 |
815 | $h{'Smith'} = 'John' ; |
816 | $h{'mouse'} = 'mickey' ; |
817 | |
f6b705ef |
818 | # iterate through the btree using seq |
88108326 |
819 | # and print each key/value pair. |
610ab055 |
820 | $key = $value = 0 ; |
f6b705ef |
821 | for ($status = $x->seq($key, $value, R_FIRST) ; |
822 | $status == 0 ; |
823 | $status = $x->seq($key, $value, R_NEXT) ) |
88108326 |
824 | { print "$key -> $value\n" } |
825 | |
826 | undef $x ; |
827 | untie %h ; |
828 | |
829 | that prints: |
830 | |
831 | Smith -> John |
832 | Wall -> Brick |
f6b705ef |
833 | Wall -> Brick |
88108326 |
834 | Wall -> Larry |
835 | mouse -> mickey |
836 | |
f6b705ef |
837 | This time we have got all the key/value pairs, including the multiple |
88108326 |
838 | values associated with the key C<Wall>. |
839 | |
68dc0745 |
840 | =head2 The get_dup() Method |
f6b705ef |
841 | |
842 | B<DB_File> comes with a utility method, called C<get_dup>, to assist in |
88108326 |
843 | reading duplicate values from BTREE databases. The method can take the |
844 | following forms: |
845 | |
846 | $count = $x->get_dup($key) ; |
847 | @list = $x->get_dup($key) ; |
848 | %list = $x->get_dup($key, 1) ; |
849 | |
850 | In a scalar context the method returns the number of values associated |
851 | with the key, C<$key>. |
852 | |
853 | In list context, it returns all the values which match C<$key>. Note |
f6b705ef |
854 | that the values will be returned in an apparently random order. |
88108326 |
855 | |
7a2e2cd6 |
856 | In list context, if the second parameter is present and evaluates |
857 | TRUE, the method returns an associative array. The keys of the |
858 | associative array correspond to the values that matched in the BTREE |
859 | and the values of the array are a count of the number of times that |
860 | particular value occurred in the BTREE. |
88108326 |
861 | |
f6b705ef |
862 | So assuming the database created above, we can use C<get_dup> like |
88108326 |
863 | this: |
864 | |
610ab055 |
865 | my $cnt = $x->get_dup("Wall") ; |
88108326 |
866 | print "Wall occurred $cnt times\n" ; |
867 | |
610ab055 |
868 | my %hash = $x->get_dup("Wall", 1) ; |
88108326 |
869 | print "Larry is there\n" if $hash{'Larry'} ; |
f6b705ef |
870 | print "There are $hash{'Brick'} Brick Walls\n" ; |
88108326 |
871 | |
610ab055 |
872 | my @list = $x->get_dup("Wall") ; |
88108326 |
873 | print "Wall => [@list]\n" ; |
874 | |
f6b705ef |
875 | @list = $x->get_dup("Smith") ; |
88108326 |
876 | print "Smith => [@list]\n" ; |
877 | |
f6b705ef |
878 | @list = $x->get_dup("Dog") ; |
88108326 |
879 | print "Dog => [@list]\n" ; |
880 | |
881 | |
882 | and it will print: |
883 | |
f6b705ef |
884 | Wall occurred 3 times |
88108326 |
885 | Larry is there |
f6b705ef |
886 | There are 2 Brick Walls |
887 | Wall => [Brick Brick Larry] |
88108326 |
888 | Smith => [John] |
889 | Dog => [] |
3b35bae3 |
890 | |
f6b705ef |
891 | =head2 Matching Partial Keys |
892 | |
893 | The BTREE interface has a feature which allows partial keys to be |
894 | matched. This functionality is I<only> available when the C<seq> method |
895 | is used along with the R_CURSOR flag. |
896 | |
897 | $x->seq($key, $value, R_CURSOR) ; |
898 | |
899 | Here is the relevant quote from the dbopen man page where it defines |
900 | the use of the R_CURSOR flag with seq: |
901 | |
f6b705ef |
902 | Note, for the DB_BTREE access method, the returned key is not |
903 | necessarily an exact match for the specified key. The returned key |
904 | is the smallest key greater than or equal to the specified key, |
905 | permitting partial key matches and range searches. |
906 | |
f6b705ef |
907 | In the example script below, the C<match> sub uses this feature to find |
908 | and print the first matching key/value pair given a partial key. |
909 | |
610ab055 |
910 | use strict ; |
f6b705ef |
911 | use DB_File ; |
912 | use Fcntl ; |
610ab055 |
913 | |
914 | use vars qw($filename $x %h $st $key $value) ; |
f6b705ef |
915 | |
916 | sub match |
917 | { |
918 | my $key = shift ; |
610ab055 |
919 | my $value = 0; |
f6b705ef |
920 | my $orig_key = $key ; |
921 | $x->seq($key, $value, R_CURSOR) ; |
922 | print "$orig_key\t-> $key\t-> $value\n" ; |
923 | } |
924 | |
925 | $filename = "tree" ; |
926 | unlink $filename ; |
927 | |
928 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
929 | or die "Cannot open $filename: $!\n"; |
930 | |
931 | # Add some key/value pairs to the file |
932 | $h{'mouse'} = 'mickey' ; |
933 | $h{'Wall'} = 'Larry' ; |
934 | $h{'Walls'} = 'Brick' ; |
935 | $h{'Smith'} = 'John' ; |
936 | |
937 | |
610ab055 |
938 | $key = $value = 0 ; |
f6b705ef |
939 | print "IN ORDER\n" ; |
940 | for ($st = $x->seq($key, $value, R_FIRST) ; |
941 | $st == 0 ; |
942 | $st = $x->seq($key, $value, R_NEXT) ) |
943 | |
944 | { print "$key -> $value\n" } |
945 | |
946 | print "\nPARTIAL MATCH\n" ; |
947 | |
948 | match "Wa" ; |
949 | match "A" ; |
950 | match "a" ; |
951 | |
952 | undef $x ; |
953 | untie %h ; |
954 | |
955 | Here is the output: |
956 | |
957 | IN ORDER |
958 | Smith -> John |
959 | Wall -> Larry |
960 | Walls -> Brick |
961 | mouse -> mickey |
962 | |
963 | PARTIAL MATCH |
964 | Wa -> Wall -> Larry |
965 | A -> Smith -> John |
966 | a -> mouse -> mickey |
967 | |
968 | =head1 DB_RECNO |
969 | |
970 | DB_RECNO provides an interface to flat text files. Both variable and |
971 | fixed length records are supported. |
3b35bae3 |
972 | |
88108326 |
973 | In order to make RECNO more compatible with Perl the array offset for |
974 | all RECNO arrays begins at 0 rather than 1 as in Berkeley DB. |
3b35bae3 |
975 | |
88108326 |
976 | As with normal Perl arrays, a RECNO array can be accessed using |
977 | negative indexes. The index -1 refers to the last element of the array, |
978 | -2 the second last, and so on. Attempting to access an element before |
979 | the start of the array will raise a fatal run-time error. |
3b35bae3 |
980 | |
68dc0745 |
981 | =head2 The 'bval' Option |
36477c24 |
982 | |
983 | The operation of the bval option warrants some discussion. Here is the |
984 | definition of bval from the Berkeley DB 1.85 recno manual page: |
985 | |
986 | The delimiting byte to be used to mark the end of a |
987 | record for variable-length records, and the pad charac- |
988 | ter for fixed-length records. If no value is speci- |
989 | fied, newlines (``\n'') are used to mark the end of |
990 | variable-length records and fixed-length records are |
991 | padded with spaces. |
992 | |
993 | The second sentence is wrong. In actual fact bval will only default to |
994 | C<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULL |
995 | openinfo parameter is used at all, the value that happens to be in bval |
996 | will be used. That means you always have to specify bval when making |
997 | use of any of the options in the openinfo parameter. This documentation |
998 | error will be fixed in the next release of Berkeley DB. |
999 | |
1000 | That clarifies the situation with regards Berkeley DB itself. What |
1001 | about B<DB_File>? Well, the behavior defined in the quote above is |
1002 | quite useful, so B<DB_File> conforms it. |
1003 | |
1004 | That means that you can specify other options (e.g. cachesize) and |
1005 | still have bval default to C<"\n"> for variable length records, and |
1006 | space for fixed length records. |
1007 | |
f6b705ef |
1008 | =head2 A Simple Example |
3b35bae3 |
1009 | |
f6b705ef |
1010 | Here is a simple example that uses RECNO. |
1011 | |
610ab055 |
1012 | use strict ; |
f6b705ef |
1013 | use DB_File ; |
f6b705ef |
1014 | |
610ab055 |
1015 | my @h ; |
f6b705ef |
1016 | tie @h, "DB_File", "text", O_RDWR|O_CREAT, 0640, $DB_RECNO |
1017 | or die "Cannot open file 'text': $!\n" ; |
1018 | |
1019 | # Add a few key/value pairs to the file |
1020 | $h[0] = "orange" ; |
1021 | $h[1] = "blue" ; |
1022 | $h[2] = "yellow" ; |
1023 | |
1024 | # Check for existence of a key |
1025 | print "Element 1 Exists with value $h[1]\n" if $h[1] ; |
1026 | |
1027 | # use a negative index |
1028 | print "The last element is $h[-1]\n" ; |
1029 | print "The 2nd last element is $h[-2]\n" ; |
1030 | |
1031 | untie @h ; |
3b35bae3 |
1032 | |
f6b705ef |
1033 | Here is the output from the script: |
1034 | |
1035 | |
1036 | Element 1 Exists with value blue |
1037 | The last element is yellow |
1038 | The 2nd last element is blue |
1039 | |
1040 | =head2 Extra Methods |
1041 | |
045291aa |
1042 | If you are using a version of Perl earlier than 5.004_57, the tied |
1043 | array interface is quite limited. The example script above will work, |
1044 | but you won't be able to use C<push>, C<pop>, C<shift>, C<unshift> |
1045 | etc. with the tied array. |
1046 | |
1047 | To make the interface more useful for older versions of Perl, a number |
1048 | of methods are supplied with B<DB_File> to simulate the missing array |
1049 | operations. All these methods are accessed via the object returned from |
1050 | the tie call. |
f6b705ef |
1051 | |
1052 | Here are the methods: |
1053 | |
1054 | =over 5 |
3b35bae3 |
1055 | |
f6b705ef |
1056 | =item B<$X-E<gt>push(list) ;> |
1057 | |
1058 | Pushes the elements of C<list> to the end of the array. |
1059 | |
1060 | =item B<$value = $X-E<gt>pop ;> |
1061 | |
1062 | Removes and returns the last element of the array. |
1063 | |
1064 | =item B<$X-E<gt>shift> |
1065 | |
1066 | Removes and returns the first element of the array. |
1067 | |
1068 | =item B<$X-E<gt>unshift(list) ;> |
1069 | |
1070 | Pushes the elements of C<list> to the start of the array. |
1071 | |
1072 | =item B<$X-E<gt>length> |
1073 | |
1074 | Returns the number of elements in the array. |
1075 | |
1076 | =back |
1077 | |
1078 | =head2 Another Example |
1079 | |
1080 | Here is a more complete example that makes use of some of the methods |
1081 | described above. It also makes use of the API interface directly (see |
1082 | L<THE API INTERFACE>). |
1083 | |
1084 | use strict ; |
1085 | use vars qw(@h $H $file $i) ; |
1086 | use DB_File ; |
1087 | use Fcntl ; |
1088 | |
1089 | $file = "text" ; |
1090 | |
1091 | unlink $file ; |
1092 | |
1093 | $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO |
1094 | or die "Cannot open file $file: $!\n" ; |
1095 | |
1096 | # first create a text file to play with |
1097 | $h[0] = "zero" ; |
1098 | $h[1] = "one" ; |
1099 | $h[2] = "two" ; |
1100 | $h[3] = "three" ; |
1101 | $h[4] = "four" ; |
1102 | |
1103 | |
1104 | # Print the records in order. |
1105 | # |
1106 | # The length method is needed here because evaluating a tied |
1107 | # array in a scalar context does not return the number of |
1108 | # elements in the array. |
1109 | |
1110 | print "\nORIGINAL\n" ; |
1111 | foreach $i (0 .. $H->length - 1) { |
1112 | print "$i: $h[$i]\n" ; |
1113 | } |
1114 | |
1115 | # use the push & pop methods |
1116 | $a = $H->pop ; |
1117 | $H->push("last") ; |
1118 | print "\nThe last record was [$a]\n" ; |
1119 | |
1120 | # and the shift & unshift methods |
1121 | $a = $H->shift ; |
1122 | $H->unshift("first") ; |
1123 | print "The first record was [$a]\n" ; |
1124 | |
1125 | # Use the API to add a new record after record 2. |
1126 | $i = 2 ; |
1127 | $H->put($i, "Newbie", R_IAFTER) ; |
1128 | |
1129 | # and a new record before record 1. |
1130 | $i = 1 ; |
1131 | $H->put($i, "New One", R_IBEFORE) ; |
1132 | |
1133 | # delete record 3 |
1134 | $H->del(3) ; |
1135 | |
1136 | # now print the records in reverse order |
1137 | print "\nREVERSE\n" ; |
1138 | for ($i = $H->length - 1 ; $i >= 0 ; -- $i) |
1139 | { print "$i: $h[$i]\n" } |
1140 | |
1141 | # same again, but use the API functions instead |
1142 | print "\nREVERSE again\n" ; |
610ab055 |
1143 | my ($s, $k, $v) = (0, 0, 0) ; |
f6b705ef |
1144 | for ($s = $H->seq($k, $v, R_LAST) ; |
1145 | $s == 0 ; |
1146 | $s = $H->seq($k, $v, R_PREV)) |
1147 | { print "$k: $v\n" } |
1148 | |
1149 | undef $H ; |
1150 | untie @h ; |
1151 | |
1152 | and this is what it outputs: |
1153 | |
1154 | ORIGINAL |
1155 | 0: zero |
1156 | 1: one |
1157 | 2: two |
1158 | 3: three |
1159 | 4: four |
1160 | |
1161 | The last record was [four] |
1162 | The first record was [zero] |
1163 | |
1164 | REVERSE |
1165 | 5: last |
1166 | 4: three |
1167 | 3: Newbie |
1168 | 2: one |
1169 | 1: New One |
1170 | 0: first |
1171 | |
1172 | REVERSE again |
1173 | 5: last |
1174 | 4: three |
1175 | 3: Newbie |
1176 | 2: one |
1177 | 1: New One |
1178 | 0: first |
1179 | |
1180 | Notes: |
1181 | |
1182 | =over 5 |
1183 | |
1184 | =item 1. |
1185 | |
1186 | Rather than iterating through the array, C<@h> like this: |
1187 | |
1188 | foreach $i (@h) |
1189 | |
1190 | it is necessary to use either this: |
1191 | |
1192 | foreach $i (0 .. $H->length - 1) |
1193 | |
1194 | or this: |
1195 | |
1196 | for ($a = $H->get($k, $v, R_FIRST) ; |
1197 | $a == 0 ; |
1198 | $a = $H->get($k, $v, R_NEXT) ) |
1199 | |
1200 | =item 2. |
1201 | |
1202 | Notice that both times the C<put> method was used the record index was |
1203 | specified using a variable, C<$i>, rather than the literal value |
1204 | itself. This is because C<put> will return the record number of the |
1205 | inserted line via that parameter. |
1206 | |
1207 | =back |
1208 | |
1209 | =head1 THE API INTERFACE |
3b35bae3 |
1210 | |
1211 | As well as accessing Berkeley DB using a tied hash or array, it is also |
88108326 |
1212 | possible to make direct use of most of the API functions defined in the |
8e07c86e |
1213 | Berkeley DB documentation. |
3b35bae3 |
1214 | |
88108326 |
1215 | To do this you need to store a copy of the object returned from the tie. |
3b35bae3 |
1216 | |
88108326 |
1217 | $db = tie %hash, "DB_File", "filename" ; |
3b35bae3 |
1218 | |
8e07c86e |
1219 | Once you have done that, you can access the Berkeley DB API functions |
88108326 |
1220 | as B<DB_File> methods directly like this: |
3b35bae3 |
1221 | |
1222 | $db->put($key, $value, R_NOOVERWRITE) ; |
1223 | |
88108326 |
1224 | B<Important:> If you have saved a copy of the object returned from |
1225 | C<tie>, the underlying database file will I<not> be closed until both |
1226 | the tied variable is untied and all copies of the saved object are |
610ab055 |
1227 | destroyed. |
88108326 |
1228 | |
1229 | use DB_File ; |
1230 | $db = tie %hash, "DB_File", "filename" |
1231 | or die "Cannot tie filename: $!" ; |
1232 | ... |
1233 | undef $db ; |
1234 | untie %hash ; |
1235 | |
9a2c4ce3 |
1236 | See L<The untie() Gotcha> for more details. |
778183f3 |
1237 | |
88108326 |
1238 | All the functions defined in L<dbopen> are available except for |
1239 | close() and dbopen() itself. The B<DB_File> method interface to the |
1240 | supported functions have been implemented to mirror the way Berkeley DB |
1241 | works whenever possible. In particular note that: |
1242 | |
1243 | =over 5 |
1244 | |
1245 | =item * |
1246 | |
1247 | The methods return a status value. All return 0 on success. |
1248 | All return -1 to signify an error and set C<$!> to the exact |
1249 | error code. The return code 1 generally (but not always) means that the |
1250 | key specified did not exist in the database. |
1251 | |
1252 | Other return codes are defined. See below and in the Berkeley DB |
1253 | documentation for details. The Berkeley DB documentation should be used |
1254 | as the definitive source. |
1255 | |
1256 | =item * |
3b35bae3 |
1257 | |
88108326 |
1258 | Whenever a Berkeley DB function returns data via one of its parameters, |
1259 | the equivalent B<DB_File> method does exactly the same. |
3b35bae3 |
1260 | |
88108326 |
1261 | =item * |
1262 | |
1263 | If you are careful, it is possible to mix API calls with the tied |
1264 | hash/array interface in the same piece of code. Although only a few of |
1265 | the methods used to implement the tied interface currently make use of |
1266 | the cursor, you should always assume that the cursor has been changed |
1267 | any time the tied hash/array interface is used. As an example, this |
1268 | code will probably not do what you expect: |
1269 | |
1270 | $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE |
1271 | or die "Cannot tie $filename: $!" ; |
1272 | |
1273 | # Get the first key/value pair and set the cursor |
1274 | $X->seq($key, $value, R_FIRST) ; |
1275 | |
1276 | # this line will modify the cursor |
1277 | $count = scalar keys %x ; |
1278 | |
1279 | # Get the second key/value pair. |
1280 | # oops, it didn't, it got the last key/value pair! |
1281 | $X->seq($key, $value, R_NEXT) ; |
1282 | |
1283 | The code above can be rearranged to get around the problem, like this: |
1284 | |
1285 | $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE |
1286 | or die "Cannot tie $filename: $!" ; |
1287 | |
1288 | # this line will modify the cursor |
1289 | $count = scalar keys %x ; |
1290 | |
1291 | # Get the first key/value pair and set the cursor |
1292 | $X->seq($key, $value, R_FIRST) ; |
1293 | |
1294 | # Get the second key/value pair. |
1295 | # worked this time. |
1296 | $X->seq($key, $value, R_NEXT) ; |
1297 | |
1298 | =back |
1299 | |
1300 | All the constants defined in L<dbopen> for use in the flags parameters |
1301 | in the methods defined below are also available. Refer to the Berkeley |
1302 | DB documentation for the precise meaning of the flags values. |
1303 | |
1304 | Below is a list of the methods available. |
3b35bae3 |
1305 | |
1306 | =over 5 |
1307 | |
f6b705ef |
1308 | =item B<$status = $X-E<gt>get($key, $value [, $flags]) ;> |
88108326 |
1309 | |
1310 | Given a key (C<$key>) this method reads the value associated with it |
1311 | from the database. The value read from the database is returned in the |
1312 | C<$value> parameter. |
3b35bae3 |
1313 | |
88108326 |
1314 | If the key does not exist the method returns 1. |
3b35bae3 |
1315 | |
88108326 |
1316 | No flags are currently defined for this method. |
3b35bae3 |
1317 | |
f6b705ef |
1318 | =item B<$status = $X-E<gt>put($key, $value [, $flags]) ;> |
3b35bae3 |
1319 | |
88108326 |
1320 | Stores the key/value pair in the database. |
1321 | |
1322 | If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter |
8e07c86e |
1323 | will have the record number of the inserted key/value pair set. |
3b35bae3 |
1324 | |
88108326 |
1325 | Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and |
1326 | R_SETCURSOR. |
1327 | |
f6b705ef |
1328 | =item B<$status = $X-E<gt>del($key [, $flags]) ;> |
3b35bae3 |
1329 | |
88108326 |
1330 | Removes all key/value pairs with key C<$key> from the database. |
3b35bae3 |
1331 | |
88108326 |
1332 | A return code of 1 means that the requested key was not in the |
1333 | database. |
3b35bae3 |
1334 | |
88108326 |
1335 | R_CURSOR is the only valid flag at present. |
3b35bae3 |
1336 | |
f6b705ef |
1337 | =item B<$status = $X-E<gt>fd ;> |
3b35bae3 |
1338 | |
88108326 |
1339 | Returns the file descriptor for the underlying database. |
3b35bae3 |
1340 | |
f6b705ef |
1341 | See L<Locking Databases> for an example of how to make use of the |
88108326 |
1342 | C<fd> method to lock your database. |
3b35bae3 |
1343 | |
f6b705ef |
1344 | =item B<$status = $X-E<gt>seq($key, $value, $flags) ;> |
3b35bae3 |
1345 | |
88108326 |
1346 | This interface allows sequential retrieval from the database. See |
1347 | L<dbopen> for full details. |
1348 | |
1349 | Both the C<$key> and C<$value> parameters will be set to the key/value |
1350 | pair read from the database. |
1351 | |
1352 | The flags parameter is mandatory. The valid flag values are R_CURSOR, |
1353 | R_FIRST, R_LAST, R_NEXT and R_PREV. |
1354 | |
f6b705ef |
1355 | =item B<$status = $X-E<gt>sync([$flags]) ;> |
88108326 |
1356 | |
1357 | Flushes any cached buffers to disk. |
1358 | |
1359 | R_RECNOSYNC is the only valid flag at present. |
3b35bae3 |
1360 | |
1361 | =back |
1362 | |
f6b705ef |
1363 | =head1 HINTS AND TIPS |
3b35bae3 |
1364 | |
3b35bae3 |
1365 | |
cb1a09d0 |
1366 | =head2 Locking Databases |
3b35bae3 |
1367 | |
cb1a09d0 |
1368 | Concurrent access of a read-write database by several parties requires |
1369 | them all to use some kind of locking. Here's an example of Tom's that |
1370 | uses the I<fd> method to get the file descriptor, and then a careful |
1371 | open() to give something Perl will flock() for you. Run this repeatedly |
1372 | in the background to watch the locks granted in proper order. |
3b35bae3 |
1373 | |
cb1a09d0 |
1374 | use DB_File; |
1375 | |
1376 | use strict; |
1377 | |
1378 | sub LOCK_SH { 1 } |
1379 | sub LOCK_EX { 2 } |
1380 | sub LOCK_NB { 4 } |
1381 | sub LOCK_UN { 8 } |
1382 | |
1383 | my($oldval, $fd, $db, %db, $value, $key); |
1384 | |
1385 | $key = shift || 'default'; |
1386 | $value = shift || 'magic'; |
1387 | |
1388 | $value .= " $$"; |
1389 | |
1390 | $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644) |
1391 | || die "dbcreat /tmp/foo.db $!"; |
1392 | $fd = $db->fd; |
1393 | print "$$: db fd is $fd\n"; |
1394 | open(DB_FH, "+<&=$fd") || die "dup $!"; |
1395 | |
1396 | |
1397 | unless (flock (DB_FH, LOCK_SH | LOCK_NB)) { |
1398 | print "$$: CONTENTION; can't read during write update! |
1399 | Waiting for read lock ($!) ...."; |
1400 | unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" } |
1401 | } |
1402 | print "$$: Read lock granted\n"; |
1403 | |
1404 | $oldval = $db{$key}; |
1405 | print "$$: Old value was $oldval\n"; |
1406 | flock(DB_FH, LOCK_UN); |
1407 | |
1408 | unless (flock (DB_FH, LOCK_EX | LOCK_NB)) { |
1409 | print "$$: CONTENTION; must have exclusive lock! |
1410 | Waiting for write lock ($!) ...."; |
1411 | unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" } |
1412 | } |
1413 | |
1414 | print "$$: Write lock granted\n"; |
1415 | $db{$key} = $value; |
610ab055 |
1416 | $db->sync; # to flush |
cb1a09d0 |
1417 | sleep 10; |
1418 | |
1419 | flock(DB_FH, LOCK_UN); |
88108326 |
1420 | undef $db; |
cb1a09d0 |
1421 | untie %db; |
1422 | close(DB_FH); |
1423 | print "$$: Updated db to $key=$value\n"; |
1424 | |
68dc0745 |
1425 | =head2 Sharing Databases With C Applications |
f6b705ef |
1426 | |
1427 | There is no technical reason why a Berkeley DB database cannot be |
1428 | shared by both a Perl and a C application. |
1429 | |
1430 | The vast majority of problems that are reported in this area boil down |
1431 | to the fact that C strings are NULL terminated, whilst Perl strings are |
1432 | not. |
1433 | |
1434 | Here is a real example. Netscape 2.0 keeps a record of the locations you |
1435 | visit along with the time you last visited them in a DB_HASH database. |
1436 | This is usually stored in the file F<~/.netscape/history.db>. The key |
1437 | field in the database is the location string and the value field is the |
1438 | time the location was last visited stored as a 4 byte binary value. |
1439 | |
1440 | If you haven't already guessed, the location string is stored with a |
1441 | terminating NULL. This means you need to be careful when accessing the |
1442 | database. |
1443 | |
1444 | Here is a snippet of code that is loosely based on Tom Christiansen's |
1445 | I<ggh> script (available from your nearest CPAN archive in |
1446 | F<authors/id/TOMC/scripts/nshist.gz>). |
1447 | |
610ab055 |
1448 | use strict ; |
f6b705ef |
1449 | use DB_File ; |
1450 | use Fcntl ; |
f6b705ef |
1451 | |
610ab055 |
1452 | use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ; |
f6b705ef |
1453 | $dotdir = $ENV{HOME} || $ENV{LOGNAME}; |
1454 | |
1455 | $HISTORY = "$dotdir/.netscape/history.db"; |
1456 | |
1457 | tie %hist_db, 'DB_File', $HISTORY |
1458 | or die "Cannot open $HISTORY: $!\n" ;; |
1459 | |
1460 | # Dump the complete database |
1461 | while ( ($href, $binary_time) = each %hist_db ) { |
1462 | |
1463 | # remove the terminating NULL |
1464 | $href =~ s/\x00$// ; |
1465 | |
1466 | # convert the binary time into a user friendly string |
1467 | $date = localtime unpack("V", $binary_time); |
1468 | print "$date $href\n" ; |
1469 | } |
1470 | |
1471 | # check for the existence of a specific key |
1472 | # remember to add the NULL |
1473 | if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) { |
1474 | $date = localtime unpack("V", $binary_time) ; |
1475 | print "Last visited mox.perl.com on $date\n" ; |
1476 | } |
1477 | else { |
1478 | print "Never visited mox.perl.com\n" |
1479 | } |
1480 | |
1481 | untie %hist_db ; |
1482 | |
68dc0745 |
1483 | =head2 The untie() Gotcha |
778183f3 |
1484 | |
7a2e2cd6 |
1485 | If you make use of the Berkeley DB API, it is I<very> strongly |
68dc0745 |
1486 | recommended that you read L<perltie/The untie Gotcha>. |
778183f3 |
1487 | |
1488 | Even if you don't currently make use of the API interface, it is still |
1489 | worth reading it. |
1490 | |
1491 | Here is an example which illustrates the problem from a B<DB_File> |
1492 | perspective: |
1493 | |
1494 | use DB_File ; |
1495 | use Fcntl ; |
1496 | |
1497 | my %x ; |
1498 | my $X ; |
1499 | |
1500 | $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_TRUNC |
1501 | or die "Cannot tie first time: $!" ; |
1502 | |
1503 | $x{123} = 456 ; |
1504 | |
1505 | untie %x ; |
1506 | |
1507 | tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT |
1508 | or die "Cannot tie second time: $!" ; |
1509 | |
1510 | untie %x ; |
1511 | |
1512 | When run, the script will produce this error message: |
1513 | |
1514 | Cannot tie second time: Invalid argument at bad.file line 14. |
1515 | |
1516 | Although the error message above refers to the second tie() statement |
1517 | in the script, the source of the problem is really with the untie() |
1518 | statement that precedes it. |
1519 | |
1520 | Having read L<perltie> you will probably have already guessed that the |
1521 | error is caused by the extra copy of the tied object stored in C<$X>. |
1522 | If you haven't, then the problem boils down to the fact that the |
1523 | B<DB_File> destructor, DESTROY, will not be called until I<all> |
1524 | references to the tied object are destroyed. Both the tied variable, |
1525 | C<%x>, and C<$X> above hold a reference to the object. The call to |
1526 | untie() will destroy the first, but C<$X> still holds a valid |
1527 | reference, so the destructor will not get called and the database file |
1528 | F<tst.fil> will remain open. The fact that Berkeley DB then reports the |
1529 | attempt to open a database that is alreday open via the catch-all |
1530 | "Invalid argument" doesn't help. |
1531 | |
1532 | If you run the script with the C<-w> flag the error message becomes: |
1533 | |
1534 | untie attempted while 1 inner references still exist at bad.file line 12. |
1535 | Cannot tie second time: Invalid argument at bad.file line 14. |
1536 | |
1537 | which pinpoints the real problem. Finally the script can now be |
1538 | modified to fix the original problem by destroying the API object |
1539 | before the untie: |
1540 | |
1541 | ... |
1542 | $x{123} = 456 ; |
1543 | |
1544 | undef $X ; |
1545 | untie %x ; |
1546 | |
1547 | $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT |
1548 | ... |
1549 | |
f6b705ef |
1550 | |
1551 | =head1 COMMON QUESTIONS |
1552 | |
1553 | =head2 Why is there Perl source in my database? |
1554 | |
1555 | If you look at the contents of a database file created by DB_File, |
1556 | there can sometimes be part of a Perl script included in it. |
1557 | |
1558 | This happens because Berkeley DB uses dynamic memory to allocate |
1559 | buffers which will subsequently be written to the database file. Being |
1560 | dynamic, the memory could have been used for anything before DB |
1561 | malloced it. As Berkeley DB doesn't clear the memory once it has been |
1562 | allocated, the unused portions will contain random junk. In the case |
1563 | where a Perl script gets written to the database, the random junk will |
1564 | correspond to an area of dynamic memory that happened to be used during |
1565 | the compilation of the script. |
1566 | |
1567 | Unless you don't like the possibility of there being part of your Perl |
1568 | scripts embedded in a database file, this is nothing to worry about. |
1569 | |
1570 | =head2 How do I store complex data structures with DB_File? |
1571 | |
1572 | Although B<DB_File> cannot do this directly, there is a module which |
1573 | can layer transparently over B<DB_File> to accomplish this feat. |
1574 | |
1575 | Check out the MLDBM module, available on CPAN in the directory |
1576 | F<modules/by-module/MLDBM>. |
1577 | |
1578 | =head2 What does "Invalid Argument" mean? |
1579 | |
1580 | You will get this error message when one of the parameters in the |
1581 | C<tie> call is wrong. Unfortunately there are quite a few parameters to |
1582 | get wrong, so it can be difficult to figure out which one it is. |
1583 | |
1584 | Here are a couple of possibilities: |
1585 | |
1586 | =over 5 |
1587 | |
1588 | =item 1. |
1589 | |
610ab055 |
1590 | Attempting to reopen a database without closing it. |
f6b705ef |
1591 | |
1592 | =item 2. |
1593 | |
1594 | Using the O_WRONLY flag. |
1595 | |
1596 | =back |
1597 | |
1598 | =head2 What does "Bareword 'DB_File' not allowed" mean? |
1599 | |
1600 | You will encounter this particular error message when you have the |
1601 | C<strict 'subs'> pragma (or the full strict pragma) in your script. |
1602 | Consider this script: |
1603 | |
1604 | use strict ; |
1605 | use DB_File ; |
1606 | use vars qw(%x) ; |
1607 | tie %x, DB_File, "filename" ; |
1608 | |
1609 | Running it produces the error in question: |
1610 | |
1611 | Bareword "DB_File" not allowed while "strict subs" in use |
1612 | |
1613 | To get around the error, place the word C<DB_File> in either single or |
1614 | double quotes, like this: |
1615 | |
1616 | tie %x, "DB_File", "filename" ; |
1617 | |
1618 | Although it might seem like a real pain, it is really worth the effort |
1619 | of having a C<use strict> in all your scripts. |
1620 | |
cb1a09d0 |
1621 | =head1 HISTORY |
1622 | |
1f70e1ea |
1623 | Moved to the Changes file. |
610ab055 |
1624 | |
1f70e1ea |
1625 | =head1 BUGS |
05475680 |
1626 | |
1f70e1ea |
1627 | Some older versions of Berkeley DB had problems with fixed length |
1628 | records using the RECNO file format. This problem has been fixed since |
1629 | version 1.85 of Berkeley DB. |
e858de61 |
1630 | |
1f70e1ea |
1631 | I am sure there are bugs in the code. If you do find any, or can |
1632 | suggest any enhancements, I would welcome your comments. |
a6ed719b |
1633 | |
1f70e1ea |
1634 | =head1 AVAILABILITY |
a6ed719b |
1635 | |
1f70e1ea |
1636 | B<DB_File> comes with the standard Perl source distribution. Look in |
1637 | the directory F<ext/DB_File>. Given the amount of time between releases |
1638 | of Perl the version that ships with Perl is quite likely to be out of |
1639 | date, so the most recent version can always be found on CPAN (see |
1640 | L<perlmod/CPAN> for details), in the directory |
1641 | F<modules/by-module/DB_File>. |
a6ed719b |
1642 | |
1f70e1ea |
1643 | This version of B<DB_File> will work with either version 1.x or 2.x of |
1644 | Berkeley DB, but is limited to the functionality provided by version 1. |
a6ed719b |
1645 | |
1f70e1ea |
1646 | The official web site for Berkeley DB is |
1647 | F<http://www.sleepycat.com/db>. The ftp equivalent is |
1648 | F<ftp.sleepycat.com:/pub>. Both versions 1 and 2 of Berkeley DB are |
1649 | available there. |
93af7a87 |
1650 | |
1f70e1ea |
1651 | Alternatively, Berkeley DB version 1 is available at your nearest CPAN |
1652 | archive in F<src/misc/db.1.85.tar.gz>. |
e858de61 |
1653 | |
1f70e1ea |
1654 | If you are running IRIX, then get Berkeley DB version 1 from |
1655 | F<http://reality.sgi.com/ariel>. It has the patches necessary to |
1656 | compile properly on IRIX 5.3. |
610ab055 |
1657 | |
1f70e1ea |
1658 | =head1 COPYRIGHT |
3b35bae3 |
1659 | |
a9fd575d |
1660 | Copyright (c) 1995-8 Paul Marquess. All rights reserved. This program |
1661 | is free software; you can redistribute it and/or modify it under the |
1662 | same terms as Perl itself. |
3b35bae3 |
1663 | |
1f70e1ea |
1664 | Although B<DB_File> is covered by the Perl license, the library it |
1665 | makes use of, namely Berkeley DB, is not. Berkeley DB has its own |
1666 | copyright and its own license. Please take the time to read it. |
3b35bae3 |
1667 | |
a9fd575d |
1668 | Here are are few words taken from the Berkeley DB FAQ (at |
1669 | http://www.sleepycat.com) regarding the license: |
68dc0745 |
1670 | |
a9fd575d |
1671 | Do I have to license DB to use it in Perl scripts? |
3b35bae3 |
1672 | |
a9fd575d |
1673 | No. The Berkeley DB license requires that software that uses |
1674 | Berkeley DB be freely redistributable. In the case of Perl, that |
1675 | software is Perl, and not your scripts. Any Perl scripts that you |
1676 | write are your property, including scripts that make use of |
1677 | Berkeley DB. Neither the Perl license nor the Berkeley DB license |
1678 | place any restriction on what you may do with them. |
88108326 |
1679 | |
1f70e1ea |
1680 | If you are in any doubt about the license situation, contact either the |
1681 | Berkeley DB authors or the author of DB_File. See L<"AUTHOR"> for details. |
a0b8c8c1 |
1682 | |
1683 | |
3b35bae3 |
1684 | =head1 SEE ALSO |
1685 | |
1686 | L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)> |
1687 | |
3b35bae3 |
1688 | =head1 AUTHOR |
1689 | |
8e07c86e |
1690 | The DB_File interface was written by Paul Marquess |
88108326 |
1691 | E<lt>pmarquess@bfsec.bt.co.ukE<gt>. |
d3ef3b8a |
1692 | Questions about the DB system itself may be addressed to |
1693 | E<lt>db@sleepycat.com<gt>. |
3b35bae3 |
1694 | |
1695 | =cut |