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