61cd138b8995da9f973aa5a24184c31b63084042
[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 (pmarquess@bfsec.bt.co.uk)
4 # last modified 28th June 1996
5 # version 1.02
6
7 package DB_File::HASHINFO ;
8
9 use strict;
10 use Carp;
11 require Tie::Hash;
12 @DB_File::HASHINFO::ISA = qw(Tie::Hash);
13
14 sub new
15 {
16     my $pkg = shift ;
17     my %x ;
18     tie %x, $pkg ;
19     bless \%x, $pkg ;
20 }
21
22 sub TIEHASH
23 {
24     my $pkg = shift ;
25
26     bless {   'bsize'     => undef,
27               'ffactor'   => undef,
28               'nelem'     => undef,
29               'cachesize' => undef,
30               'hash'      => undef,
31               'lorder'    => undef,
32         }, $pkg ;
33 }
34
35 sub FETCH 
36 {  
37     my $self  = shift ;
38     my $key   = shift ;
39
40     return $self->{$key} if exists $self->{$key}  ;
41
42     my $pkg = ref $self ;
43     croak "${pkg}::FETCH - Unknown element '$key'" ;
44 }
45
46
47 sub STORE 
48 {
49     my $self  = shift ;
50     my $key   = shift ;
51     my $value = shift ;
52
53     if ( exists $self->{$key} )
54     {
55         $self->{$key} = $value ;
56         return ;
57     }
58     
59     my $pkg = ref $self ;
60     croak "${pkg}::STORE - Unknown element '$key'" ;
61 }
62
63 sub DELETE 
64 {
65     my $self = shift ;
66     my $key  = shift ;
67
68     if ( exists $self->{$key} )
69     {
70         delete $self->{$key} ;
71         return ;
72     }
73     
74     my $pkg = ref $self ;
75     croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ;
76 }
77
78 sub EXISTS
79 {
80     my $self = shift ;
81     my $key  = shift ;
82
83     exists $self->{$key} ;
84 }
85
86 sub NotHere
87 {
88     my $pkg = shift ;
89     my $method = shift ;
90
91     croak "${pkg} does not define the method ${method}" ;
92 }
93
94 sub DESTROY  { undef %{$_[0]} }
95 sub FIRSTKEY { my $self = shift ; $self->NotHere(ref $self, "FIRSTKEY") }
96 sub NEXTKEY  { my $self = shift ; $self->NotHere(ref $self, "NEXTKEY") }
97 sub CLEAR    { my $self = shift ; $self->NotHere(ref $self, "CLEAR") }
98
99 package DB_File::RECNOINFO ;
100
101 use strict ;
102
103 @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ;
104
105 sub TIEHASH
106 {
107     my $pkg = shift ;
108
109     bless {   'bval'      => undef,
110               'cachesize' => undef,
111               'psize'     => undef,
112               'flags'     => undef,
113               'lorder'    => undef,
114               'reclen'    => undef,
115               'bfname'    => "",
116             }, $pkg ;
117 }
118
119 package DB_File::BTREEINFO ;
120
121 use strict ;
122
123 @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ;
124
125 sub TIEHASH
126 {
127     my $pkg = shift ;
128
129     bless {   'flags'      => undef,
130               'cachesize'  => undef,
131               'maxkeypage' => undef,
132               'minkeypage' => undef,
133               'psize'      => undef,
134               'compare'    => undef,
135               'prefix'     => undef,
136               'lorder'     => undef,
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) ;
145 use Carp;
146
147
148 $VERSION = "1.02" ;
149
150 #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
151 #$DB_BTREE = TIEHASH DB_File::BTREEINFO ;
152 #$DB_HASH  = TIEHASH DB_File::HASHINFO ;
153 #$DB_RECNO = TIEHASH DB_File::RECNOINFO ;
154
155 $DB_BTREE = new DB_File::BTREEINFO ;
156 $DB_HASH  = new DB_File::HASHINFO ;
157 $DB_RECNO = new DB_File::RECNOINFO ;
158
159 require Tie::Hash;
160 require Exporter;
161 use AutoLoader;
162 require DynaLoader;
163 @ISA = qw(Tie::Hash Exporter DynaLoader);
164 @EXPORT = qw(
165         $DB_BTREE $DB_HASH $DB_RECNO 
166
167         BTREEMAGIC
168         BTREEVERSION
169         DB_LOCK
170         DB_SHMEM
171         DB_TXN
172         HASHMAGIC
173         HASHVERSION
174         MAX_PAGE_NUMBER
175         MAX_PAGE_OFFSET
176         MAX_REC_NUMBER
177         RET_ERROR
178         RET_SPECIAL
179         RET_SUCCESS
180         R_CURSOR
181         R_DUP
182         R_FIRST
183         R_FIXEDLEN
184         R_IAFTER
185         R_IBEFORE
186         R_LAST
187         R_NEXT
188         R_NOKEY
189         R_NOOVERWRITE
190         R_PREV
191         R_RECNOSYNC
192         R_SETCURSOR
193         R_SNAPSHOT
194         __R_UNUSED
195
196 );
197
198 sub AUTOLOAD {
199     my($constname);
200     ($constname = $AUTOLOAD) =~ s/.*:://;
201     my $val = constant($constname, @_ ? $_[0] : 0);
202     if ($! != 0) {
203         if ($! =~ /Invalid/) {
204             $AutoLoader::AUTOLOAD = $AUTOLOAD;
205             goto &AutoLoader::AUTOLOAD;
206         }
207         else {
208             my($pack,$file,$line) = caller;
209             croak "Your vendor has not defined DB macro $constname, used at $file line $line.
210 ";
211         }
212     }
213     eval "sub $AUTOLOAD { $val }";
214     goto &$AUTOLOAD;
215 }
216
217 bootstrap DB_File $VERSION;
218
219 # Preloaded methods go here.  Autoload methods go after __END__, and are
220 # processed by the autosplit program.
221
222
223 sub get_dup
224 {
225     croak "Usage: \$db->get_dup(key [,flag])\n"
226         unless @_ == 2 or @_ == 3 ;
227  
228     my $db        = shift ;
229     my $key       = shift ;
230     my $flag      = shift ;
231     my $value ;
232     my $origkey   = $key ;
233     my $wantarray = wantarray ;
234     my @values    = () ;
235     my $counter   = 0 ;
236  
237     # get the first value associated with the key, $key
238     $db->seq($key, $value, R_CURSOR()) ;
239  
240     if ( $key eq $origkey) {
241  
242         while (1) {
243             # save the value or count matches
244             if ($wantarray)
245                 { push (@values, $value) ; push(@values, 1) if $flag }
246             else
247                 { ++ $counter }
248      
249             # iterate through the database until either EOF 
250             # or a different key is encountered.
251             last if $db->seq($key, $value, R_NEXT()) != 0 or $key ne $origkey ;
252         }
253     }
254  
255     $wantarray ? @values : $counter ;
256 }
257
258
259 1;
260 __END__
261
262 =cut
263
264 =head1 NAME
265
266 DB_File - Perl5 access to Berkeley DB
267
268 =head1 SYNOPSIS
269
270  use DB_File ;
271  use Fcntl ;
272  
273  [$X =] tie %hash,  'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
274  [$X =] tie %hash,  'DB_File', $filename, $flags, $mode, $DB_BTREE ;
275  [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
276
277  $status = $X->del($key [, $flags]) ;
278  $status = $X->put($key, $value [, $flags]) ;
279  $status = $X->get($key, $value [, $flags]) ;
280  $status = $X->seq($key, $value, $flags) ;
281  $status = $X->sync([$flags]) ;
282  $status = $X->fd ;
283
284  $count = $X->get_dup($key) ;
285  @list  = $X->get_dup($key) ;
286  %list  = $X->get_dup($key, 1) ;
287
288  untie %hash ;
289  untie @array ;
290
291 =head1 DESCRIPTION
292
293 B<DB_File> is a module which allows Perl programs to make use of the
294 facilities provided by Berkeley DB.  If you intend to use this
295 module you should really have a copy of the Berkeley DB manual page at
296 hand. The interface defined here mirrors the Berkeley DB interface
297 closely.
298
299 Berkeley DB is a C library which provides a consistent interface to a
300 number of database formats.  B<DB_File> provides an interface to all
301 three of the database types currently supported by Berkeley DB.
302
303 The file types are:
304
305 =over 5
306
307 =item B<DB_HASH>
308
309 This database type allows arbitrary key/value pairs to be stored in data
310 files. This is equivalent to the functionality provided by other
311 hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
312 the files created using DB_HASH are not compatible with any of the
313 other packages mentioned.
314
315 A default hashing algorithm, which will be adequate for most
316 applications, is built into Berkeley DB. If you do need to use your own
317 hashing algorithm it is possible to write your own in Perl and have
318 B<DB_File> use it instead.
319
320 =item B<DB_BTREE>
321
322 The btree format allows arbitrary key/value pairs to be stored in a
323 sorted, balanced binary tree.
324
325 As with the DB_HASH format, it is possible to provide a user defined
326 Perl routine to perform the comparison of keys. By default, though, the
327 keys are stored in lexical order.
328
329 =item B<DB_RECNO>
330
331 DB_RECNO allows both fixed-length and variable-length flat text files
332 to be manipulated using the same key/value pair interface as in DB_HASH
333 and DB_BTREE.  In this case the key will consist of a record (line)
334 number.
335
336 =back
337
338 =head2 How does DB_File interface to Berkeley DB?
339
340 B<DB_File> allows access to Berkeley DB files using the tie() mechanism
341 in Perl 5 (for full details, see L<perlfunc/tie()>). This facility
342 allows B<DB_File> to access Berkeley DB files using either an
343 associative array (for DB_HASH & DB_BTREE file types) or an ordinary
344 array (for the DB_RECNO file type).
345
346 In addition to the tie() interface, it is also possible to access most
347 of the functions provided in the Berkeley DB API directly.
348 See L<"Using the Berkeley DB API Directly">.
349
350 =head2 Opening a Berkeley DB Database File
351
352 Berkeley DB uses the function dbopen() to open or create a database.
353 Below is the C prototype for dbopen().
354
355       DB*
356       dbopen (const char * file, int flags, int mode, 
357               DBTYPE type, const void * openinfo)
358
359 The parameter C<type> is an enumeration which specifies which of the 3
360 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
361 Depending on which of these is actually chosen, the final parameter,
362 I<openinfo> points to a data structure which allows tailoring of the
363 specific interface method.
364
365 This interface is handled slightly differently in B<DB_File>. Here is
366 an equivalent call using B<DB_File>:
367
368         tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ;
369
370 The C<filename>, C<flags> and C<mode> parameters are the direct
371 equivalent of their dbopen() counterparts. The final parameter $DB_HASH
372 performs the function of both the C<type> and C<openinfo> parameters in
373 dbopen().
374
375 In the example above $DB_HASH is actually a pre-defined reference to a
376 hash object. B<DB_File> has three of these pre-defined references.
377 Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
378
379 The keys allowed in each of these pre-defined references is limited to
380 the names used in the equivalent C structure. So, for example, the
381 $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>,
382 C<ffactor>, C<hash>, C<lorder> and C<nelem>. 
383
384 To change one of these elements, just assign to it like this:
385
386         $DB_HASH->{'cachesize'} = 10000 ;
387
388 The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are
389 usually adequate for most applications.  If you do need to create extra
390 instances of these objects, constructors are available for each file
391 type.
392
393 Here are examples of the constructors and the valid options available
394 for DB_HASH, DB_BTREE and DB_RECNO respectively.
395
396      $a = new DB_File::HASHINFO ;
397      $a->{'bsize'} ;
398      $a->{'cachesize'} ;
399      $a->{'ffactor'};
400      $a->{'hash'} ;
401      $a->{'lorder'} ;
402      $a->{'nelem'} ;
403
404      $b = new DB_File::BTREEINFO ;
405      $b->{'flags'} ;
406      $b->{'cachesize'} ;
407      $b->{'maxkeypage'} ;
408      $b->{'minkeypage'} ;
409      $b->{'psize'} ;
410      $b->{'compare'} ;
411      $b->{'prefix'} ;
412      $b->{'lorder'} ;
413
414      $c = new DB_File::RECNOINFO ;
415      $c->{'bval'} ;
416      $c->{'cachesize'} ;
417      $c->{'psize'} ;
418      $c->{'flags'} ;
419      $c->{'lorder'} ;
420      $c->{'reclen'} ;
421      $c->{'bfname'} ;
422
423 The values stored in the hashes above are mostly the direct equivalent
424 of their C counterpart. Like their C counterparts, all are set to a
425 default set of values - that means you don't have to set I<all> of the
426 values when you only want to change one. Here is an example:
427
428      $a = new DB_File::HASHINFO ;
429      $a->{'cachesize'} =  12345 ;
430      tie %y, 'DB_File', "filename", $flags, 0777, $a ;
431
432 A few of the values need extra discussion here. When used, the C
433 equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers
434 to C functions. In B<DB_File> these keys are used to store references
435 to Perl subs. Below are templates for each of the subs:
436
437     sub hash
438     {
439         my ($data) = @_ ;
440         ...
441         # return the hash value for $data
442         return $hash ;
443     }
444
445     sub compare
446     {
447         my ($key, $key2) = @_ ;
448         ...
449         # return  0 if $key1 eq $key2
450         #        -1 if $key1 lt $key2
451         #         1 if $key1 gt $key2
452         return (-1 , 0 or 1) ;
453     }
454
455     sub prefix
456     {
457         my ($key, $key2) = @_ ;
458         ...
459         # return number of bytes of $key2 which are 
460         # necessary to determine that it is greater than $key1
461         return $bytes ;
462     }
463
464 See L<"Using BTREE"> for an example of using the C<compare>
465
466 =head2 Default Parameters
467
468 It is possible to omit some or all of the final 4 parameters in the
469 call to C<tie> and let them take default values. As DB_HASH is the most
470 common file format used, the call:
471
472     tie %A, "DB_File", "filename" ;
473
474 is equivalent to:
475
476     tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0640, $DB_HASH ;
477
478 It is also possible to omit the filename parameter as well, so the
479 call:
480
481     tie %A, "DB_File" ;
482
483 is equivalent to:
484
485     tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0640, $DB_HASH ;
486
487 See L<"In Memory Databases"> for a discussion on the use of C<undef>
488 in place of a filename.
489
490 =head2 Handling duplicate keys in BTREE databases
491
492 The BTREE file type in Berkeley DB optionally allows a single key to be
493 associated with an arbitrary number of values. This option is enabled by
494 setting the flags element of C<$DB_BTREE> to R_DUP when creating the
495 database.
496
497 There are some difficulties in using the tied hash interface if you
498 want to manipulate a BTREE database with duplicate keys. Consider this
499 code:
500
501     use DB_File ;
502     use Fcntl ;
503  
504     $filename = "tree" ;
505     unlink $filename ;
506  
507     # Enable duplicate records
508     $DB_BTREE->{'flags'} = R_DUP ;
509  
510     tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE 
511         or die "Cannot open $filename: $!\n";
512  
513     # Add some key/value pairs to the file
514     $h{'Wall'} = 'Larry' ;
515     $h{'Wall'} = 'Brick' ; # Note the duplicate key
516     $h{'Smith'} = 'John' ;
517     $h{'mouse'} = 'mickey' ;
518
519     # iterate through the associative array
520     # and print each key/value pair.
521     foreach (keys %h)
522       { print "$_  -> $h{$_}\n" }
523
524 Here is the output:
525
526     Smith   -> John
527     Wall    -> Larry
528     Wall    -> Larry
529     mouse   -> mickey
530
531 As you can see 2 records have been successfully created with key C<Wall>
532 - the only thing is, when they are retrieved from the database they
533 both I<seem> to have the same value, namely C<Larry>. The problem is
534 caused by the way that the associative array interface works.
535 Basically, when the associative array interface is used to fetch the
536 value associated with a given key, it will only ever retrieve the first
537 value.
538
539 Although it may not be immediately obvious from the code above, the
540 associative array interface can be used to write values with duplicate
541 keys, but it cannot be used to read them back from the database.
542
543 The way to get around this problem is to use the Berkeley DB API method
544 called C<seq>.  This method allows sequential access to key/value
545 pairs. See L<"Using the Berkeley DB API Directly"> for details of both
546 the C<seq> method and the API in general.
547
548 Here is the script above rewritten using the C<seq> API method.
549
550     use DB_File ;
551     use Fcntl ;
552  
553     $filename = "tree" ;
554     unlink $filename ;
555  
556     # Enable duplicate records
557     $DB_BTREE->{'flags'} = R_DUP ;
558  
559     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE 
560         or die "Cannot open $filename: $!\n";
561  
562     # Add some key/value pairs to the file
563     $h{'Wall'} = 'Larry' ;
564     $h{'Wall'} = 'Brick' ; # Note the duplicate key
565     $h{'Smith'} = 'John' ;
566     $h{'mouse'} = 'mickey' ;
567  
568     # Point to the first record in the btree 
569     $x->seq($key, $value, R_FIRST) ;
570
571     # now iterate through the rest of the btree
572     # and print each key/value pair.
573     print "$key     -> $value\n" ;
574     while ( $x->seq($key, $value, R_NEXT) == 0)
575       {  print "$key -> $value\n" }
576  
577     undef $x ;
578     untie %h ;
579
580 that prints:
581
582     Smith   -> John
583     Wall    -> Brick
584     Wall    -> Larry
585     mouse   -> mickey
586
587 This time we have got all the key/value pairs, including both the
588 values associated with the key C<Wall>.
589
590 C<DB_File> comes with a utility method, called C<get_dup>, to assist in
591 reading duplicate values from BTREE databases. The method can take the
592 following forms:
593
594     $count = $x->get_dup($key) ;
595     @list  = $x->get_dup($key) ;
596     %list  = $x->get_dup($key, 1) ;
597
598 In a scalar context the method returns the number of values associated
599 with the key, C<$key>.
600
601 In list context, it returns all the values which match C<$key>. Note
602 that the values returned will be in an apparently random order.
603
604 If the second parameter is present and evaluates TRUE, the method
605 returns an associative array whose keys correspond to the the values
606 from the BTREE and whose values are all C<1>.
607
608 So assuming the database created above, we can use C<get_dups> like
609 this:
610
611     $cnt  = $x->get_dups("Wall") ;
612     print "Wall occurred $cnt times\n" ;
613
614     %hash = $x->get_dups("Wall", 1) ;
615     print "Larry is there\n" if $hash{'Larry'} ;
616
617     @list = $x->get_dups("Wall") ;
618     print "Wall =>      [@list]\n" ;
619
620     @list = $x->get_dups("Smith") ;
621     print "Smith =>     [@list]\n" ;
622  
623     @list = $x->get_dups("Dog") ;
624     print "Dog =>       [@list]\n" ;
625
626
627 and it will print:
628
629     Wall occurred 2 times
630     Larry is there
631     Wall =>     [Brick Larry]
632     Smith =>    [John]
633     Dog =>      []
634
635 =head2 RECNO
636
637 In order to make RECNO more compatible with Perl the array offset for
638 all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
639
640 As with normal Perl arrays, a RECNO array can be accessed using
641 negative indexes. The index -1 refers to the last element of the array,
642 -2 the second last, and so on. Attempting to access an element before
643 the start of the array will raise a fatal run-time error.
644
645 =head2 In Memory Databases
646
647 Berkeley DB allows the creation of in-memory databases by using NULL
648 (that is, a C<(char *)0> in C) in place of the filename.  B<DB_File>
649 uses C<undef> instead of NULL to provide this functionality.
650
651
652 =head2 Using the Berkeley DB API Directly
653
654 As well as accessing Berkeley DB using a tied hash or array, it is also
655 possible to make direct use of most of the API functions defined in the
656 Berkeley DB documentation.
657
658 To do this you need to store a copy of the object returned from the tie.
659
660         $db = tie %hash, "DB_File", "filename" ;
661
662 Once you have done that, you can access the Berkeley DB API functions
663 as B<DB_File> methods directly like this:
664
665         $db->put($key, $value, R_NOOVERWRITE) ;
666
667 B<Important:> If you have saved a copy of the object returned from
668 C<tie>, the underlying database file will I<not> be closed until both
669 the tied variable is untied and all copies of the saved object are
670 destroyed.
671
672     use DB_File ;
673     $db = tie %hash, "DB_File", "filename" 
674         or die "Cannot tie filename: $!" ;
675     ...
676     undef $db ;
677     untie %hash ;
678
679 All the functions defined in L<dbopen> are available except for
680 close() and dbopen() itself. The B<DB_File> method interface to the
681 supported functions have been implemented to mirror the way Berkeley DB
682 works whenever possible. In particular note that:
683
684 =over 5
685
686 =item *
687
688 The methods return a status value. All return 0 on success.
689 All return -1 to signify an error and set C<$!> to the exact
690 error code. The return code 1 generally (but not always) means that the
691 key specified did not exist in the database.
692
693 Other return codes are defined. See below and in the Berkeley DB
694 documentation for details. The Berkeley DB documentation should be used
695 as the definitive source.
696
697 =item *
698
699 Whenever a Berkeley DB function returns data via one of its parameters,
700 the equivalent B<DB_File> method does exactly the same.
701
702 =item *
703
704 If you are careful, it is possible to mix API calls with the tied
705 hash/array interface in the same piece of code. Although only a few of
706 the methods used to implement the tied interface currently make use of
707 the cursor, you should always assume that the cursor has been changed
708 any time the tied hash/array interface is used. As an example, this
709 code will probably not do what you expect:
710
711     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
712         or die "Cannot tie $filename: $!" ;
713
714     # Get the first key/value pair and set  the cursor
715     $X->seq($key, $value, R_FIRST) ;
716
717     # this line will modify the cursor
718     $count = scalar keys %x ; 
719
720     # Get the second key/value pair.
721     # oops, it didn't, it got the last key/value pair!
722     $X->seq($key, $value, R_NEXT) ;
723
724 The code above can be rearranged to get around the problem, like this:
725
726     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
727         or die "Cannot tie $filename: $!" ;
728
729     # this line will modify the cursor
730     $count = scalar keys %x ; 
731
732     # Get the first key/value pair and set  the cursor
733     $X->seq($key, $value, R_FIRST) ;
734
735     # Get the second key/value pair.
736     # worked this time.
737     $X->seq($key, $value, R_NEXT) ;
738
739 =back
740
741 All the constants defined in L<dbopen> for use in the flags parameters
742 in the methods defined below are also available. Refer to the Berkeley
743 DB documentation for the precise meaning of the flags values.
744
745 Below is a list of the methods available.
746
747 =over 5
748
749 =item C<$status = $X-E<gt>get($key, $value [, $flags]) ;>
750
751 Given a key (C<$key>) this method reads the value associated with it
752 from the database. The value read from the database is returned in the
753 C<$value> parameter.
754
755 If the key does not exist the method returns 1.
756
757 No flags are currently defined for this method.
758
759 =item C<$status = $X-E<gt>put($key, $value [, $flags]) ;>
760
761 Stores the key/value pair in the database.
762
763 If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter
764 will have the record number of the inserted key/value pair set.
765
766 Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
767 R_SETCURSOR.
768
769 =item C<$status = $X-E<gt>del($key [, $flags]) ;>
770
771 Removes all key/value pairs with key C<$key> from the database.
772
773 A return code of 1 means that the requested key was not in the
774 database.
775
776 R_CURSOR is the only valid flag at present.
777
778 =item C<$status = $X-E<gt>fd ;>
779
780 Returns the file descriptor for the underlying database.
781
782 See L<"Locking Databases"> for an example of how to make use of the
783 C<fd> method to lock your database.
784
785 =item C<$status = $X-E<gt>seq($key, $value, $flags) ;>
786
787 This interface allows sequential retrieval from the database. See
788 L<dbopen> for full details.
789
790 Both the C<$key> and C<$value> parameters will be set to the key/value
791 pair read from the database.
792
793 The flags parameter is mandatory. The valid flag values are R_CURSOR,
794 R_FIRST, R_LAST, R_NEXT and R_PREV.
795
796 =item C<$status = $X-E<gt>sync([$flags]) ;>
797
798 Flushes any cached buffers to disk.
799
800 R_RECNOSYNC is the only valid flag at present.
801
802 =back
803
804 =head1 EXAMPLES
805
806 It is always a lot easier to understand something when you see a real
807 example. So here are a few.
808
809 =head2 Using HASH
810
811         use DB_File ;
812         use Fcntl ;
813
814         tie %h,  "DB_File", "hashed", O_RDWR|O_CREAT, 0640, $DB_HASH 
815             or die "Cannot open file 'hashed': $!\n";
816
817         # Add a key/value pair to the file
818         $h{"apple"} = "orange" ;
819
820         # Check for existence of a key
821         print "Exists\n" if $h{"banana"} ;
822
823         # Delete 
824         delete $h{"apple"} ;
825
826         untie %h ;
827
828 =head2 Using BTREE
829
830 Here is a sample of code which uses BTREE. Just to make life more
831 interesting the default comparison function will not be used. Instead
832 a Perl sub, C<Compare()>, will be used to do a case insensitive
833 comparison.
834
835         use DB_File ;
836         use Fcntl ;
837
838         sub Compare
839         {
840             my ($key1, $key2) = @_ ;
841
842             "\L$key1" cmp "\L$key2" ;
843         }
844
845         $DB_BTREE->{'compare'} = 'Compare' ;
846
847         tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE 
848             or die "Cannot open file 'tree': $!\n" ;
849
850         # Add a key/value pair to the file
851         $h{'Wall'} = 'Larry' ;
852         $h{'Smith'} = 'John' ;
853         $h{'mouse'} = 'mickey' ;
854         $h{'duck'}   = 'donald' ;
855
856         # Delete
857         delete $h{"duck"} ;
858
859         # Cycle through the keys printing them in order.
860         # Note it is not necessary to sort the keys as
861         # the btree will have kept them in order automatically.
862         foreach (keys %h)
863           { print "$_\n" }
864
865         untie %h ;
866
867 Here is the output from the code above.
868
869         mouse
870         Smith
871         Wall
872
873
874 =head2 Using RECNO
875
876 Here is a simple example that uses RECNO.
877
878         use DB_File ;
879         use Fcntl ;
880
881         $DB_RECNO->{'psize'} = 3000 ;
882
883         tie @h, "DB_File", "text", O_RDWR|O_CREAT, 0640, $DB_RECNO 
884             or die "Cannot open file 'text': $!\n" ;
885
886         # Add a key/value pair to the file
887         $h[0] = "orange" ;
888
889         # Check for existence of a key
890         print "Exists\n" if $h[1] ;
891
892         untie @h ;
893
894 =head2 Locking Databases
895
896 Concurrent access of a read-write database by several parties requires
897 them all to use some kind of locking.  Here's an example of Tom's that
898 uses the I<fd> method to get the file descriptor, and then a careful
899 open() to give something Perl will flock() for you.  Run this repeatedly
900 in the background to watch the locks granted in proper order.
901
902     use Fcntl;
903     use DB_File;
904
905     use strict;
906
907     sub LOCK_SH { 1 }
908     sub LOCK_EX { 2 }
909     sub LOCK_NB { 4 }
910     sub LOCK_UN { 8 }
911
912     my($oldval, $fd, $db, %db, $value, $key);
913
914     $key = shift || 'default';
915     $value = shift || 'magic';
916
917     $value .= " $$";
918
919     $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644) 
920             || die "dbcreat /tmp/foo.db $!";
921     $fd = $db->fd;
922     print "$$: db fd is $fd\n";
923     open(DB_FH, "+<&=$fd") || die "dup $!";
924
925
926     unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {
927         print "$$: CONTENTION; can't read during write update!
928                     Waiting for read lock ($!) ....";
929         unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" }
930     } 
931     print "$$: Read lock granted\n";
932
933     $oldval = $db{$key};
934     print "$$: Old value was $oldval\n";
935     flock(DB_FH, LOCK_UN);
936
937     unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
938         print "$$: CONTENTION; must have exclusive lock!
939                     Waiting for write lock ($!) ....";
940         unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" }
941     } 
942
943     print "$$: Write lock granted\n";
944     $db{$key} = $value;
945     $db->sync;
946     sleep 10;
947
948     flock(DB_FH, LOCK_UN);
949     undef $db;
950     untie %db;
951     close(DB_FH);
952     print "$$: Updated db to $key=$value\n";
953
954 =head1 HISTORY
955
956 =over
957
958 =item 0.1
959
960 First Release.
961
962 =item 0.2
963
964 When B<DB_File> is opening a database file it no longer terminates the
965 process if I<dbopen> returned an error. This allows file protection
966 errors to be caught at run time. Thanks to Judith Grass
967 E<lt>grass@cybercash.comE<gt> for spotting the bug.
968
969 =item 0.3
970
971 Added prototype support for multiple btree compare callbacks.
972
973 =item 1.0
974
975 B<DB_File> has been in use for over a year. To reflect that, the
976 version number has been incremented to 1.0.
977
978 Added complete support for multiple concurrent callbacks.
979
980 Using the I<push> method on an empty list didn't work properly. This
981 has been fixed.
982
983 =item 1.01
984
985 Fixed a core dump problem with SunOS.
986
987 The return value from TIEHASH wasn't set to NULL when dbopen returned
988 an error.
989
990 =item 1.02
991
992 Merged OS2 specific code into DB_File.xs
993
994 Removed some redundant code in DB_File.xs.
995
996 Documentation update.
997
998 Allow negative subscripts with RECNO interface.
999
1000 Changed the default flags from O_RDWR to O_CREAT|O_RDWR.
1001
1002 The example code which showed how to lock a database needed a call to
1003 C<sync> added. Without it the resultant database file was empty.
1004
1005 Added get_dups method.
1006
1007 =head1 WARNINGS
1008
1009 If you happen to find any other functions defined in the source for
1010 this module that have not been mentioned in this document -- beware.  I
1011 may drop them at a moments notice.
1012
1013 If you cannot find any, then either you didn't look very hard or the
1014 moment has passed and I have dropped them.
1015
1016 =head1 BUGS
1017
1018 Some older versions of Berkeley DB had problems with fixed length
1019 records using the RECNO file format. The newest version at the time of
1020 writing was 1.85 - this seems to have fixed the problems with RECNO.
1021
1022 I am sure there are bugs in the code. If you do find any, or can
1023 suggest any enhancements, I would welcome your comments.
1024
1025 =head1 AVAILABILITY
1026
1027 Berkeley DB is available at your nearest CPAN archive (see
1028 L<perlmod/"CPAN"> for a list) in F<src/misc/db.1.85.tar.gz>, or via the
1029 host F<ftp.cs.berkeley.edu> in F</ucb/4bsd/db.tar.gz>.  It is I<not> under
1030 the GPL.
1031
1032 If you are running IRIX, then get Berkeley DB from
1033 F<http://reality.sgi.com/ariel>. It has the patches necessary to
1034 compile properly on IRIX 5.3.
1035
1036 =head1 SEE ALSO
1037
1038 L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)> 
1039
1040 Berkeley DB is available from F<ftp.cs.berkeley.edu> in the directory
1041 F</ucb/4bsd>.
1042
1043 =head1 AUTHOR
1044
1045 The DB_File interface was written by Paul Marquess
1046 E<lt>pmarquess@bfsec.bt.co.ukE<gt>.
1047 Questions about the DB system itself may be addressed to Keith Bostic
1048 E<lt>bostic@cs.berkeley.eduE<gt>.
1049
1050 =cut