home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / BerkeleyDB.pod < prev    next >
Encoding:
Text File  |  2003-07-06  |  49.5 KB  |  1,855 lines

  1. =head1 NAME
  2.  
  3. BerkeleyDB - Perl extension for Berkeley DB version 2, 3 or 4
  4.  
  5. =head1 SYNOPSIS
  6.  
  7.   use BerkeleyDB;
  8.  
  9.   $env = new BerkeleyDB::Env [OPTIONS] ;
  10.  
  11.   $db  = tie %hash, 'BerkeleyDB::Hash', [OPTIONS] ;
  12.   $db  = new BerkeleyDB::Hash [OPTIONS] ;
  13.  
  14.   $db  = tie %hash, 'BerkeleyDB::Btree', [OPTIONS] ;
  15.   $db  = new BerkeleyDB::Btree [OPTIONS] ;
  16.  
  17.   $db  = tie @array, 'BerkeleyDB::Recno', [OPTIONS] ;
  18.   $db  = new BerkeleyDB::Recno [OPTIONS] ;
  19.  
  20.   $db  = tie @array, 'BerkeleyDB::Queue', [OPTIONS] ;
  21.   $db  = new BerkeleyDB::Queue [OPTIONS] ;
  22.  
  23.   $db  = new BerkeleyDB::Unknown [OPTIONS] ;
  24.  
  25.   $status = BerkeleyDB::db_remove [OPTIONS]
  26.   $status = BerkeleyDB::db_rename [OPTIONS]
  27.   $status = BerkeleyDB::db_verify [OPTIONS]
  28.  
  29.   $hash{$key} = $value ;
  30.   $value = $hash{$key} ;
  31.   each %hash ;
  32.   keys %hash ;
  33.   values %hash ;
  34.  
  35.   $status = $db->db_get()
  36.   $status = $db->db_put() ;
  37.   $status = $db->db_del() ;
  38.   $status = $db->db_sync() ;
  39.   $status = $db->db_close() ;
  40.   $status = $db->db_pget()
  41.   $hash_ref = $db->db_stat() ;
  42.   $status = $db->db_key_range();
  43.   $type = $db->type() ;
  44.   $status = $db->status() ;
  45.   $boolean = $db->byteswapped() ;
  46.   $status = $db->truncate($count) ;
  47.  
  48.   $bool = BerkeleyDB::cds_available();
  49.   $lock = $db->cds_lock();
  50.   $bool = $db->cds_enabled();
  51.   $bool = $db->locked();
  52.   
  53.   ($flag, $old_offset, $old_length) = $db->partial_set($offset, $length) ;
  54.   ($flag, $old_offset, $old_length) = $db->partial_clear() ;
  55.  
  56.   $cursor = $db->db_cursor([$flags]) ;
  57.   $newcursor = $cursor->c_dup([$flags]);
  58.   $status = $cursor->c_get() ;
  59.   $status = $cursor->c_put() ;
  60.   $status = $cursor->c_del() ;
  61.   $status = $cursor->c_count() ;
  62.   $status = $cursor->c_pget() ;
  63.   $status = $cursor->status() ;
  64.   $status = $cursor->c_close() ;
  65.  
  66.   $cursor = $db->db_join() ;
  67.   $status = $cursor->c_get() ;
  68.   $status = $cursor->c_close() ;
  69.  
  70.   $status = $env->txn_checkpoint()
  71.   $hash_ref = $env->txn_stat()
  72.   $status = $env->setmutexlocks()
  73.   $status = $env->set_flags()
  74.  
  75.   $txn = $env->txn_begin() ;
  76.   $db->Txn($txn);
  77.   $txn->Txn($db1, $db2,...);
  78.   $status = $txn->txn_prepare()
  79.   $status = $txn->txn_commit()
  80.   $status = $txn->txn_abort()
  81.   $status = $txn->txn_id()
  82.   $status = $txn->txn_discard()
  83.  
  84.   $status = $env->set_lg_dir();
  85.   $status = $env->set_lg_bsize();
  86.   $status = $env->set_lg_max();
  87.  
  88.   $status = $env->set_data_dir() ;
  89.   $status = $env->set_tmp_dir() ;
  90.   $status = $env->set_verbose() ;
  91.   $db_env_ptr = $env->DB_ENV() ;
  92.  
  93.   $BerkeleyDB::Error
  94.   $BerkeleyDB::db_version
  95.  
  96.   # DBM Filters
  97.   $old_filter = $db->filter_store_key  ( sub { ... } ) ;
  98.   $old_filter = $db->filter_store_value( sub { ... } ) ;
  99.   $old_filter = $db->filter_fetch_key  ( sub { ... } ) ;
  100.   $old_filter = $db->filter_fetch_value( sub { ... } ) ;
  101.  
  102.   # deprecated, but supported
  103.   $txn_mgr = $env->TxnMgr();
  104.   $status = $txn_mgr->txn_checkpoint()
  105.   $hash_ref = $txn_mgr->txn_stat()
  106.   $txn = $txn_mgr->txn_begin() ;
  107.  
  108. =head1 DESCRIPTION
  109.  
  110. B<NOTE: This document is still under construction. Expect it to be
  111. incomplete in places.>
  112.  
  113. This Perl module provides an interface to most of the functionality
  114. available in Berkeley DB versions 2, 3 and 4. In general it is safe to assume
  115. that the interface provided here to be identical to the Berkeley DB
  116. interface. The main changes have been to make the Berkeley DB API work
  117. in a Perl way. Note that if you are using Berkeley DB 2.x, the new
  118. features available in Berkeley DB 3.x or DB 4.x are not available via
  119. this module.
  120.  
  121. The reader is expected to be familiar with the Berkeley DB
  122. documentation. Where the interface provided here is identical to the
  123. Berkeley DB library and the... TODO
  124.  
  125. The B<db_appinit>, B<db_cursor>, B<db_open> and B<db_txn> man pages are
  126. particularly relevant.
  127.  
  128. The interface to Berkeley DB is implemented with a number of Perl
  129. classes.
  130.  
  131. =head1 ENV CLASS
  132.  
  133. The B<BerkeleyDB::Env> class provides an interface to the Berkeley DB
  134. function B<db_appinit> in Berkeley DB 2.x or B<db_env_create> and
  135. B<DBENV-E<gt>open> in Berkeley DB 3.x/4.x. Its purpose is to initialise a
  136. number of sub-systems that can then be used in a consistent way in all
  137. the databases you make use of in the environment.
  138.  
  139. If you don't intend using transactions, locking or logging, then you
  140. shouldn't need to make use of B<BerkeleyDB::Env>.
  141.  
  142. Note that an environment consists of a number of files that Berkeley DB
  143. manages behind the scenes for you. When you first use an environment, it
  144. needs to be explicitly created. This is done by including C<DB_CREATE>
  145. with the C<Flags> parameter, described below.
  146.  
  147. =head2 Synopsis
  148.  
  149.     $env = new BerkeleyDB::Env
  150.              [ -Home         => $path, ]
  151.              [ -Server       => $name, ]
  152.              [ -CacheSize    => $number, ]
  153.              [ -Config       => { name => value, name => value }, ]
  154.              [ -ErrFile      => filename, ]
  155.              [ -ErrPrefix    => "string", ]
  156.              [ -Flags        => number, ]
  157.              [ -SetFlags     => bitmask, ]
  158.              [ -LockDetect   => number, ]
  159.              [ -Verbose      => boolean, ]
  160.              [ -Encrypt      => { Password => "string",
  161.                               Flags    => number }, ]
  162.  
  163. =over 5
  164.  
  165. All the parameters to the BerkeleyDB::Env constructor are optional.
  166.  
  167. =item -Home
  168.  
  169. If present, this parameter should point to an existing directory. Any
  170. files that I<aren't> specified with an absolute path in the sub-systems
  171. that are initialised by the BerkeleyDB::Env class will be assumed to
  172. live in the B<Home> directory.
  173.  
  174. For example, in the code fragment below the database "fred.db" will be
  175. opened in the directory "/home/databases" because it was specified as a
  176. relative path, but "joe.db" will be opened in "/other" because it was
  177. part of an absolute path.
  178.  
  179.     $env = new BerkeleyDB::Env
  180.              -Home         => "/home/databases"
  181.     ...
  182.  
  183.     $db1 = new BerkeleyDB::Hash
  184.          -Filename = "fred.db",
  185.          -Env => $env
  186.     ...
  187.  
  188.     $db2 = new BerkeleyDB::Hash
  189.          -Filename = "/other/joe.db",
  190.          -Env => $env
  191.     ...
  192.  
  193. =item -Server
  194.  
  195. If present, this parameter should be the hostname of a server that is running
  196. the Berkeley DB RPC server. All databases will be accessed via the RPC server.
  197.  
  198. =item -Encrypt
  199.  
  200. If present, this parameter will enable encryption of  all data before
  201. it is written to the database. This parameters must be given a hash
  202. reference. The format is shown below.
  203.  
  204.     -Encrypt => { -Password => "abc", Flags => DB_ENCRYPT_AES }
  205.  
  206. Valid values for the Flags are 0 or C<DB_ENCRYPT_AES>.
  207.  
  208. This option requires Berkeley DB 4.1 or better.
  209.  
  210. =item -Cachesize
  211.  
  212. If present, this parameter sets the size of the environments shared memory
  213. buffer pool.
  214.  
  215. =item -Config
  216.  
  217. This is a variation on the C<-Home> parameter, but it allows finer
  218. control of where specific types of files will be stored.
  219.  
  220. The parameter expects a reference to a hash. Valid keys are:
  221. B<DB_DATA_DIR>, B<DB_LOG_DIR> and B<DB_TMP_DIR>
  222.  
  223. The code below shows an example of how it can be used.
  224.  
  225.     $env = new BerkeleyDB::Env
  226.              -Config => { DB_DATA_DIR => "/home/databases",
  227.                           DB_LOG_DIR  => "/home/logs",
  228.                           DB_TMP_DIR  => "/home/tmp"
  229.                         }
  230.     ...
  231.  
  232. =item -ErrFile
  233.  
  234. Expects a filenme. Any errors generated internally by Berkeley DB will
  235. be logged to this file.
  236.  
  237. =item -ErrPrefix
  238.  
  239. Allows a prefix to be added to the error messages before they are sent
  240. to B<-ErrFile>.
  241.  
  242. =item -Flags
  243.  
  244. The B<Flags> parameter specifies both which sub-systems to initialise,
  245. as well as a number of environment-wide options.
  246. See the Berkeley DB documentation for more details of these options.
  247.  
  248. Any of the following can be specified by OR'ing them:
  249.  
  250. B<DB_CREATE>
  251.  
  252. If any of the files specified do not already exist, create them.
  253.  
  254. B<DB_INIT_CDB>
  255.  
  256. Initialise the Concurrent Access Methods  
  257.  
  258. B<DB_INIT_LOCK>
  259.  
  260. Initialise the Locking sub-system.
  261.  
  262. B<DB_INIT_LOG>
  263.  
  264. Initialise the Logging sub-system.
  265.  
  266. B<DB_INIT_MPOOL>
  267.  
  268. Initialise the ...
  269.  
  270. B<DB_INIT_TXN>
  271.  
  272. Initialise the ...
  273.  
  274. B<DB_MPOOL_PRIVATE>
  275.  
  276. Initialise the ...
  277.  
  278. B<DB_INIT_MPOOL> is also specified.
  279.  
  280. Initialise the ...
  281.  
  282. B<DB_NOMMAP>
  283.  
  284. Initialise the ...
  285.  
  286. B<DB_RECOVER>
  287.  
  288.  
  289.  
  290. B<DB_RECOVER_FATAL>
  291.  
  292. B<DB_THREAD>
  293.  
  294. B<DB_TXN_NOSYNC>
  295.  
  296. B<DB_USE_ENVIRON>
  297.  
  298. B<DB_USE_ENVIRON_ROOT>
  299.  
  300. =item -SetFlags
  301.  
  302. Calls ENV->set_flags with the supplied bitmask. Use this when you need to make
  303. use of DB_ENV->set_flags before DB_ENV->open is called.
  304.  
  305. Only valid when Berkeley DB 3.x or better is used.
  306.  
  307. =item -LockDetect
  308.  
  309. Specifies what to do when a lock conflict occurs. The value should be one of
  310.  
  311. B<DB_LOCK_DEFAULT> 
  312.  
  313. B<DB_LOCK_OLDEST>
  314.  
  315. B<DB_LOCK_RANDOM>
  316.  
  317. B<DB_LOCK_YOUNGEST>
  318.  
  319. =item -Verbose
  320.  
  321. Add extra debugging information to the messages sent to B<-ErrFile>.
  322.  
  323. =back
  324.  
  325. =head2 Methods
  326.  
  327. The environment class has the following methods:
  328.  
  329. =over 5
  330.  
  331. =item $env->errPrefix("string") ;
  332.  
  333. This method is identical to the B<-ErrPrefix> flag. It allows the
  334. error prefix string to be changed dynamically.
  335.  
  336. =item $env->set_flags(bitmask, 1|0);
  337.  
  338. =item $txn = $env->TxnMgr()
  339.  
  340. Constructor for creating a B<TxnMgr> object.
  341. See L<"TRANSACTIONS"> for more details of using transactions.
  342.  
  343. This method is deprecated. Access the transaction methods using the B<txn_>
  344. methods below from the environment object directly.
  345.  
  346. =item $env->txn_begin()
  347.  
  348. TODO
  349.  
  350. =item $env->txn_stat()
  351.  
  352. TODO
  353.  
  354. =item $env->txn_checkpoint()
  355.  
  356. TODO
  357.  
  358. =item $env->status()
  359.  
  360. Returns the status of the last BerkeleyDB::Env method.
  361.  
  362.  
  363. =item $env->DB_ENV()
  364.  
  365. Returns a pointer to the underlying DB_ENV data structure that Berkeley
  366. DB uses.
  367.  
  368. =item $env->status()
  369.  
  370. Returns the status of the last BerkeleyDB::Env method.
  371.  
  372. =back
  373.  
  374. =head2 Examples
  375.  
  376. TODO.
  377.  
  378. =head1 Global Classes
  379.  
  380.   $status = BerkeleyDB::db_remove [OPTIONS]
  381.   $status = BerkeleyDB::db_rename [OPTIONS]
  382.   $status = BerkeleyDB::db_verify [OPTIONS]
  383.   
  384. =head1 THE DATABASE CLASSES
  385.  
  386. B<BerkeleyDB> supports the following database formats:
  387.  
  388. =over 5
  389.  
  390. =item B<BerkeleyDB::Hash>
  391.  
  392. This database type allows arbitrary key/value pairs to be stored in data
  393. files. This is equivalent to the functionality provided by other
  394. hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
  395. the files created using B<BerkeleyDB::Hash> are not compatible with any
  396. of the other packages mentioned.
  397.  
  398. A default hashing algorithm, which will be adequate for most applications,
  399. is built into BerkeleyDB. If you do need to use your own hashing algorithm
  400. it is possible to write your own in Perl and have B<BerkeleyDB> use
  401. it instead.
  402.  
  403. =item B<BerkeleyDB::Btree>
  404.  
  405. The Btree format allows arbitrary key/value pairs to be stored in a
  406. B+tree.
  407.  
  408. As with the B<BerkeleyDB::Hash> format, it is possible to provide a
  409. user defined Perl routine to perform the comparison of keys. By default,
  410. though, the keys are stored in lexical order.
  411.  
  412. =item B<BerkeleyDB::Recno>
  413.  
  414. TODO.
  415.  
  416.  
  417. =item B<BerkeleyDB::Queue>
  418.  
  419. TODO.
  420.  
  421. =item B<BerkeleyDB::Unknown>
  422.  
  423. This isn't a database format at all. It is used when you want to open an
  424. existing Berkeley DB database without having to know what type is it. 
  425.  
  426. =back
  427.  
  428.  
  429. Each of the database formats described above is accessed via a
  430. corresponding B<BerkeleyDB> class. These will be described in turn in
  431. the next sections.
  432.  
  433. =head1 BerkeleyDB::Hash
  434.  
  435. Equivalent to calling B<db_open> with type B<DB_HASH> in Berkeley DB 2.x and
  436. calling B<db_create> followed by B<DB-E<gt>open> with type B<DB_HASH> in
  437. Berkeley DB 3.x or greater. 
  438.  
  439. Two forms of constructor are supported:
  440.  
  441.     $db = new BerkeleyDB::Hash
  442.                 [ -Filename      => "filename", ]
  443.                 [ -Subname       => "sub-database name", ]
  444.                 [ -Flags         => flags,]
  445.                 [ -Property      => flags,]
  446.                 [ -Mode          => number,]
  447.                 [ -Cachesize     => number,]
  448.                 [ -Lorder        => number,]
  449.                 [ -Pagesize      => number,]
  450.                 [ -Env           => $env,]
  451.                 [ -Txn           => $txn,]
  452.                 [ -Encrypt       => { Password => "string",
  453.                                   Flags    => number }, ],
  454.                 # BerkeleyDB::Hash specific
  455.                 [ -Ffactor       => number,]
  456.                 [ -Nelem         => number,]
  457.                 [ -Hash          => code reference,]
  458.                 [ -DupCompare    => code reference,]
  459.  
  460. and this
  461.  
  462.     [$db =] tie %hash, 'BerkeleyDB::Hash', 
  463.                 [ -Filename      => "filename", ]
  464.                 [ -Subname       => "sub-database name", ]
  465.                 [ -Flags         => flags,]
  466.                 [ -Property      => flags,]
  467.                 [ -Mode          => number,]
  468.                 [ -Cachesize     => number,]
  469.                 [ -Lorder        => number,]
  470.                 [ -Pagesize      => number,]
  471.                 [ -Env           => $env,]
  472.                 [ -Txn           => $txn,]
  473.                 [ -Encrypt       => { Password => "string",
  474.                                   Flags    => number }, ],
  475.                 # BerkeleyDB::Hash specific
  476.                 [ -Ffactor       => number,]
  477.                 [ -Nelem         => number,]
  478.                 [ -Hash          => code reference,]
  479.                 [ -DupCompare    => code reference,]
  480.  
  481.  
  482. When the "tie" interface is used, reading from and writing to the database
  483. is achieved via the tied hash. In this case the database operates like
  484. a Perl associative array that happens to be stored on disk.
  485.  
  486. In addition to the high-level tied hash interface, it is possible to
  487. make use of the underlying methods provided by Berkeley DB
  488.  
  489. =head2 Options
  490.  
  491. In addition to the standard set of options (see L<COMMON OPTIONS>)
  492. B<BerkeleyDB::Hash> supports these options:
  493.  
  494. =over 5
  495.  
  496. =item -Property
  497.  
  498. Used to specify extra flags when opening a database. The following
  499. flags may be specified by bitwise OR'ing together one or more of the
  500. following values:
  501.  
  502. B<DB_DUP>
  503.  
  504. When creating a new database, this flag enables the storing of duplicate
  505. keys in the database. If B<DB_DUPSORT> is not specified as well, the
  506. duplicates are stored in the order they are created in the database.
  507.  
  508. B<DB_DUPSORT>
  509.  
  510. Enables the sorting of duplicate keys in the database. Ignored if
  511. B<DB_DUP> isn't also specified.
  512.  
  513. =item -Ffactor
  514.  
  515. =item -Nelem
  516.  
  517. See the Berkeley DB documentation for details of these options.
  518.  
  519. =item -Hash
  520.  
  521. Allows you to provide a user defined hash function. If not specified, 
  522. a default hash function is used. Here is a template for a user-defined
  523. hash function
  524.  
  525.     sub hash
  526.     {
  527.         my ($data) = shift ;
  528.         ...
  529.         # return the hash value for $data
  530.     return $hash ;
  531.     }
  532.  
  533.     tie %h, "BerkeleyDB::Hash", 
  534.         -Filename => $filename, 
  535.         -Hash     => \&hash,
  536.     ...
  537.  
  538. See L<""> for an example.
  539.  
  540. =item -DupCompare
  541.  
  542. Used in conjunction with the B<DB_DUPOSRT> flag. 
  543.  
  544.     sub compare
  545.     {
  546.     my ($key, $key2) = @_ ;
  547.         ...
  548.         # return  0 if $key1 eq $key2
  549.         #        -1 if $key1 lt $key2
  550.         #         1 if $key1 gt $key2
  551.         return (-1 , 0 or 1) ;
  552.     }
  553.  
  554.     tie %h, "BerkeleyDB::Hash", 
  555.         -Filename   => $filename, 
  556.     -Property   => DB_DUP|DB_DUPSORT,
  557.         -DupCompare => \&compare,
  558.     ...
  559.  
  560. =back
  561.  
  562.  
  563. =head2 Methods
  564.  
  565. B<BerkeleyDB::Hash> only supports the standard database methods.
  566. See L<COMMON DATABASE METHODS>.
  567.  
  568. =head2 A Simple Tied Hash Example
  569.  
  570.     use strict ;
  571.     use BerkeleyDB ;
  572.     use vars qw( %h $k $v ) ;
  573.  
  574.     my $filename = "fruit" ;
  575.     unlink $filename ;
  576.     tie %h, "BerkeleyDB::Hash",
  577.                 -Filename => $filename,
  578.         -Flags    => DB_CREATE
  579.         or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
  580.  
  581.     # Add a few key/value pairs to the file
  582.     $h{"apple"} = "red" ;
  583.     $h{"orange"} = "orange" ;
  584.     $h{"banana"} = "yellow" ;
  585.     $h{"tomato"} = "red" ;
  586.  
  587.     # Check for existence of a key
  588.     print "Banana Exists\n\n" if $h{"banana"} ;
  589.  
  590.     # Delete a key/value pair.
  591.     delete $h{"apple"} ;
  592.  
  593.     # print the contents of the file
  594.     while (($k, $v) = each %h)
  595.       { print "$k -> $v\n" }
  596.  
  597.     untie %h ;
  598.  
  599. here is the output:
  600.  
  601.     Banana Exists
  602.     
  603.     orange -> orange
  604.     tomato -> red
  605.     banana -> yellow
  606.  
  607. Note that the like ordinary associative arrays, the order of the keys
  608. retrieved from a Hash database are in an apparently random order.
  609.  
  610. =head2 Another Simple Hash Example
  611.  
  612. Do the same as the previous example but not using tie.
  613.  
  614.     use strict ;
  615.     use BerkeleyDB ;
  616.  
  617.     my $filename = "fruit" ;
  618.     unlink $filename ;
  619.     my $db = new BerkeleyDB::Hash
  620.                 -Filename => $filename,
  621.         -Flags    => DB_CREATE
  622.         or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
  623.  
  624.     # Add a few key/value pairs to the file
  625.     $db->db_put("apple", "red") ;
  626.     $db->db_put("orange", "orange") ;
  627.     $db->db_put("banana", "yellow") ;
  628.     $db->db_put("tomato", "red") ;
  629.  
  630.     # Check for existence of a key
  631.     print "Banana Exists\n\n" if $db->db_get("banana", $v) == 0;
  632.  
  633.     # Delete a key/value pair.
  634.     $db->db_del("apple") ;
  635.  
  636.     # print the contents of the file
  637.     my ($k, $v) = ("", "") ;
  638.     my $cursor = $db->db_cursor() ;
  639.     while ($cursor->c_get($k, $v, DB_NEXT) == 0)
  640.       { print "$k -> $v\n" }
  641.  
  642.     undef $cursor ;
  643.     undef $db ;
  644.  
  645. =head2 Duplicate keys
  646.  
  647. The code below is a variation on the examples above. This time the hash has
  648. been inverted. The key this time is colour and the value is the fruit name.
  649. The B<DB_DUP> flag has been specified to allow duplicates.
  650.  
  651.     use strict ;
  652.     use BerkeleyDB ;
  653.  
  654.     my $filename = "fruit" ;
  655.     unlink $filename ;
  656.     my $db = new BerkeleyDB::Hash
  657.                 -Filename => $filename,
  658.         -Flags    => DB_CREATE,
  659.         -Property  => DB_DUP
  660.         or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
  661.  
  662.     # Add a few key/value pairs to the file
  663.     $db->db_put("red", "apple") ;
  664.     $db->db_put("orange", "orange") ;
  665.     $db->db_put("green", "banana") ;
  666.     $db->db_put("yellow", "banana") ;
  667.     $db->db_put("red", "tomato") ;
  668.     $db->db_put("green", "apple") ;
  669.  
  670.     # print the contents of the file
  671.     my ($k, $v) = ("", "") ;
  672.     my $cursor = $db->db_cursor() ;
  673.     while ($cursor->c_get($k, $v, DB_NEXT) == 0)
  674.       { print "$k -> $v\n" }
  675.  
  676.     undef $cursor ;
  677.     undef $db ;
  678.  
  679. here is the output:
  680.  
  681.     orange -> orange
  682.     yellow -> banana
  683.     red -> apple
  684.     red -> tomato
  685.     green -> banana
  686.     green -> apple
  687.  
  688. =head2 Sorting Duplicate Keys
  689.  
  690. In the previous example, when there were duplicate keys, the values are
  691. sorted in the order they are stored in. The code below is
  692. identical to the previous example except the B<DB_DUPSORT> flag is
  693. specified.
  694.  
  695.     use strict ;
  696.     use BerkeleyDB ;
  697.  
  698.     my $filename = "fruit" ;
  699.     unlink $filename ;
  700.     my $db = new BerkeleyDB::Hash
  701.                 -Filename => $filename,
  702.         -Flags    => DB_CREATE,
  703.         -Property  => DB_DUP | DB_DUPSORT
  704.         or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
  705.  
  706.     # Add a few key/value pairs to the file
  707.     $db->db_put("red", "apple") ;
  708.     $db->db_put("orange", "orange") ;
  709.     $db->db_put("green", "banana") ;
  710.     $db->db_put("yellow", "banana") ;
  711.     $db->db_put("red", "tomato") ;
  712.     $db->db_put("green", "apple") ;
  713.  
  714.     # print the contents of the file
  715.     my ($k, $v) = ("", "") ;
  716.     my $cursor = $db->db_cursor() ;
  717.     while ($cursor->c_get($k, $v, DB_NEXT) == 0)
  718.       { print "$k -> $v\n" }
  719.  
  720.     undef $cursor ;
  721.     undef $db ;
  722.  
  723. Notice that in the output below the duplicate values are sorted.
  724.  
  725.     orange -> orange
  726.     yellow -> banana
  727.     red -> apple
  728.     red -> tomato
  729.     green -> apple
  730.     green -> banana
  731.  
  732. =head2 Custom Sorting Duplicate Keys
  733.  
  734. Another variation 
  735.  
  736. TODO
  737.  
  738. =head2 Changing the hash
  739.  
  740. TODO
  741.  
  742. =head2 Using db_stat
  743.  
  744. TODO
  745.  
  746. =head1 BerkeleyDB::Btree
  747.  
  748. Equivalent to calling B<db_open> with type B<DB_BTREE> in Berkeley DB 2.x and
  749. calling B<db_create> followed by B<DB-E<gt>open> with type B<DB_BTREE> in
  750. Berkeley DB 3.x or greater. 
  751.  
  752. Two forms of constructor are supported:
  753.  
  754.  
  755.     $db = new BerkeleyDB::Btree
  756.                 [ -Filename      => "filename", ]
  757.                 [ -Subname       => "sub-database name", ]
  758.                 [ -Flags         => flags,]
  759.                 [ -Property      => flags,]
  760.                 [ -Mode          => number,]
  761.                 [ -Cachesize     => number,]
  762.                 [ -Lorder        => number,]
  763.                 [ -Pagesize      => number,]
  764.                 [ -Env           => $env,]
  765.                 [ -Txn           => $txn,]
  766.                 [ -Encrypt       => { Password => "string",
  767.                                   Flags    => number }, ],
  768.                 # BerkeleyDB::Btree specific
  769.                 [ -Minkey        => number,]
  770.                 [ -Compare       => code reference,]
  771.                 [ -DupCompare    => code reference,]
  772.                 [ -Prefix        => code reference,]
  773.  
  774. and this
  775.  
  776.     [$db =] tie %hash, 'BerkeleyDB::Btree', 
  777.                 [ -Filename      => "filename", ]
  778.                 [ -Subname       => "sub-database name", ]
  779.                 [ -Flags         => flags,]
  780.                 [ -Property      => flags,]
  781.                 [ -Mode          => number,]
  782.                 [ -Cachesize     => number,]
  783.                 [ -Lorder        => number,]
  784.                 [ -Pagesize      => number,]
  785.                 [ -Env           => $env,]
  786.                 [ -Txn           => $txn,]
  787.                 [ -Encrypt       => { Password => "string",
  788.                                   Flags    => number }, ],
  789.                 # BerkeleyDB::Btree specific
  790.                 [ -Minkey        => number,]
  791.                 [ -Compare       => code reference,]
  792.                 [ -DupCompare    => code reference,]
  793.                 [ -Prefix        => code reference,]
  794.  
  795. =head2 Options
  796.  
  797. In addition to the standard set of options (see L<COMMON OPTIONS>)
  798. B<BerkeleyDB::Btree> supports these options:
  799.  
  800. =over 5
  801.  
  802. =item -Property
  803.  
  804. Used to specify extra flags when opening a database. The following
  805. flags may be specified by bitwise OR'ing together one or more of the
  806. following values:
  807.  
  808. B<DB_DUP>
  809.  
  810. When creating a new database, this flag enables the storing of duplicate
  811. keys in the database. If B<DB_DUPSORT> is not specified as well, the
  812. duplicates are stored in the order they are created in the database.
  813.  
  814. B<DB_DUPSORT>
  815.  
  816. Enables the sorting of duplicate keys in the database. Ignored if
  817. B<DB_DUP> isn't also specified.
  818.  
  819. =item Minkey
  820.  
  821. TODO
  822.  
  823. =item Compare
  824.  
  825. Allow you to override the default sort order used in the database. See
  826. L<"Changing the sort order"> for an example.
  827.  
  828.     sub compare
  829.     {
  830.     my ($key, $key2) = @_ ;
  831.         ...
  832.         # return  0 if $key1 eq $key2
  833.         #        -1 if $key1 lt $key2
  834.         #         1 if $key1 gt $key2
  835.         return (-1 , 0 or 1) ;
  836.     }
  837.  
  838.     tie %h, "BerkeleyDB::Hash", 
  839.         -Filename   => $filename, 
  840.         -Compare    => \&compare,
  841.     ...
  842.  
  843. =item Prefix
  844.  
  845.     sub prefix
  846.     {
  847.     my ($key, $key2) = @_ ;
  848.         ...
  849.         # return number of bytes of $key2 which are 
  850.         # necessary to determine that it is greater than $key1
  851.         return $bytes ;
  852.     }
  853.  
  854.     tie %h, "BerkeleyDB::Hash", 
  855.         -Filename   => $filename, 
  856.         -Prefix     => \&prefix,
  857.     ...
  858. =item DupCompare
  859.  
  860.     sub compare
  861.     {
  862.     my ($key, $key2) = @_ ;
  863.         ...
  864.         # return  0 if $key1 eq $key2
  865.         #        -1 if $key1 lt $key2
  866.         #         1 if $key1 gt $key2
  867.         return (-1 , 0 or 1) ;
  868.     }
  869.  
  870.     tie %h, "BerkeleyDB::Hash", 
  871.         -Filename   => $filename, 
  872.         -DupCompare => \&compare,
  873.     ...
  874.  
  875. =back
  876.  
  877. =head2 Methods
  878.  
  879. B<BerkeleyDB::Btree> supports the following database methods.
  880. See also L<COMMON DATABASE METHODS>.
  881.  
  882. All the methods below return 0 to indicate success.
  883.  
  884. =over 5
  885.  
  886. =item $status = $db->db_key_range($key, $less, $equal, $greater [, $flags])
  887.  
  888. Given a key, C<$key>, this method returns the proportion of keys less than 
  889. C<$key> in C<$less>, the proportion equal to C<$key> in C<$equal> and the
  890. proportion greater than C<$key> in C<$greater>.
  891.  
  892. The proportion is returned as a double in the range 0.0 to 1.0.
  893.  
  894. =back
  895.  
  896. =head2 A Simple Btree Example
  897.  
  898. The code below is a simple example of using a btree database.
  899.  
  900.     use strict ;
  901.     use BerkeleyDB ;
  902.  
  903.     my $filename = "tree" ;
  904.     unlink $filename ;
  905.     my %h ;
  906.     tie %h, 'BerkeleyDB::Btree',
  907.             -Filename   => $filename,
  908.             -Flags      => DB_CREATE
  909.       or die "Cannot open $filename: $!\n" ;
  910.  
  911.     # Add a key/value pair to the file
  912.     $h{'Wall'} = 'Larry' ;
  913.     $h{'Smith'} = 'John' ;
  914.     $h{'mouse'} = 'mickey' ;
  915.     $h{'duck'}  = 'donald' ;
  916.  
  917.     # Delete
  918.     delete $h{"duck"} ;
  919.  
  920.     # Cycle through the keys printing them in order.
  921.     # Note it is not necessary to sort the keys as
  922.     # the btree will have kept them in order automatically.
  923.     foreach (keys %h)
  924.       { print "$_\n" }
  925.  
  926.     untie %h ;
  927.  
  928. Here is the output from the code above. The keys have been sorted using
  929. Berkeley DB's default sorting algorithm.
  930.  
  931.     Smith
  932.     Wall
  933.     mouse
  934.  
  935.  
  936. =head2 Changing the sort order
  937.  
  938. It is possible to supply your own sorting algorithm if the one that Berkeley
  939. DB used isn't suitable. The code below is identical to the previous example
  940. except for the case insensitive compare function.
  941.  
  942.     use strict ;
  943.     use BerkeleyDB ;
  944.  
  945.     my $filename = "tree" ;
  946.     unlink $filename ;
  947.     my %h ;
  948.     tie %h, 'BerkeleyDB::Btree',
  949.             -Filename   => $filename,
  950.             -Flags      => DB_CREATE,
  951.         -Compare    => sub { lc $_[0] cmp lc $_[1] }
  952.       or die "Cannot open $filename: $!\n" ;
  953.  
  954.     # Add a key/value pair to the file
  955.     $h{'Wall'} = 'Larry' ;
  956.     $h{'Smith'} = 'John' ;
  957.     $h{'mouse'} = 'mickey' ;
  958.     $h{'duck'}  = 'donald' ;
  959.  
  960.     # Delete
  961.     delete $h{"duck"} ;
  962.  
  963.     # Cycle through the keys printing them in order.
  964.     # Note it is not necessary to sort the keys as
  965.     # the btree will have kept them in order automatically.
  966.     foreach (keys %h)
  967.       { print "$_\n" }
  968.  
  969.     untie %h ;
  970.  
  971. Here is the output from the code above.
  972.  
  973.     mouse
  974.     Smith
  975.     Wall
  976.  
  977. There are a few point to bear in mind if you want to change the
  978. ordering in a BTREE database:
  979.  
  980. =over 5
  981.  
  982. =item 1.
  983.  
  984. The new compare function must be specified when you create the database.
  985.  
  986. =item 2.
  987.  
  988. You cannot change the ordering once the database has been created. Thus
  989. you must use the same compare function every time you access the
  990. database.
  991.  
  992. =back 
  993.  
  994. =head2 Using db_stat
  995.  
  996. TODO
  997.  
  998. =head1 BerkeleyDB::Recno
  999.  
  1000. Equivalent to calling B<db_open> with type B<DB_RECNO> in Berkeley DB 2.x and
  1001. calling B<db_create> followed by B<DB-E<gt>open> with type B<DB_RECNO> in
  1002. Berkeley DB 3.x or greater. 
  1003.  
  1004. Two forms of constructor are supported:
  1005.  
  1006.     $db = new BerkeleyDB::Recno
  1007.                 [ -Filename      => "filename", ]
  1008.                 [ -Subname       => "sub-database name", ]
  1009.                 [ -Flags         => flags,]
  1010.                 [ -Property      => flags,]
  1011.                 [ -Mode          => number,]
  1012.                 [ -Cachesize     => number,]
  1013.                 [ -Lorder        => number,]
  1014.                 [ -Pagesize      => number,]
  1015.                 [ -Env           => $env,]
  1016.                 [ -Txn           => $txn,]
  1017.                 [ -Encrypt       => { Password => "string",
  1018.                                   Flags    => number }, ],
  1019.                 # BerkeleyDB::Recno specific
  1020.                 [ -Delim           => byte,]
  1021.                 [ -Len             => number,]
  1022.                 [ -Pad             => byte,]
  1023.                 [ -Source          => filename,]
  1024.  
  1025. and this
  1026.  
  1027.     [$db =] tie @arry, 'BerkeleyDB::Recno', 
  1028.                 [ -Filename      => "filename", ]
  1029.                 [ -Subname       => "sub-database name", ]
  1030.                 [ -Flags         => flags,]
  1031.                 [ -Property      => flags,]
  1032.                 [ -Mode          => number,]
  1033.                 [ -Cachesize     => number,]
  1034.                 [ -Lorder        => number,]
  1035.                 [ -Pagesize      => number,]
  1036.                 [ -Env           => $env,]
  1037.                 [ -Txn           => $txn,]
  1038.                 [ -Encrypt       => { Password => "string",
  1039.                                   Flags    => number }, ],
  1040.                 # BerkeleyDB::Recno specific
  1041.                 [ -Delim           => byte,]
  1042.                 [ -Len             => number,]
  1043.                 [ -Pad             => byte,]
  1044.                 [ -Source          => filename,]
  1045.  
  1046. =head2 A Recno Example
  1047.  
  1048. Here is a simple example that uses RECNO (if you are using a version 
  1049. of Perl earlier than 5.004_57 this example won't work -- see 
  1050. L<Extra RECNO Methods> for a workaround).
  1051.  
  1052.     use strict ;
  1053.     use BerkeleyDB ;
  1054.  
  1055.     my $filename = "text" ;
  1056.     unlink $filename ;
  1057.  
  1058.     my @h ;
  1059.     tie @h, 'BerkeleyDB::Recno',
  1060.             -Filename   => $filename,
  1061.             -Flags      => DB_CREATE,
  1062.         -Property   => DB_RENUMBER
  1063.       or die "Cannot open $filename: $!\n" ;
  1064.  
  1065.     # Add a few key/value pairs to the file
  1066.     $h[0] = "orange" ;
  1067.     $h[1] = "blue" ;
  1068.     $h[2] = "yellow" ;
  1069.  
  1070.     push @h, "green", "black" ;
  1071.  
  1072.     my $elements = scalar @h ;
  1073.     print "The array contains $elements entries\n" ;
  1074.  
  1075.     my $last = pop @h ;
  1076.     print "popped $last\n" ;
  1077.  
  1078.     unshift @h, "white" ;
  1079.     my $first = shift @h ;
  1080.     print "shifted $first\n" ;
  1081.  
  1082.     # Check for existence of a key
  1083.     print "Element 1 Exists with value $h[1]\n" if $h[1] ;
  1084.  
  1085.     untie @h ;
  1086.  
  1087. Here is the output from the script:
  1088.  
  1089.     The array contains 5 entries
  1090.     popped black
  1091.     shifted white
  1092.     Element 1 Exists with value blue
  1093.     The last element is green
  1094.     The 2nd last element is yellow
  1095.  
  1096. =head1 BerkeleyDB::Queue
  1097.  
  1098. Equivalent to calling B<db_create> followed by B<DB-E<gt>open> with
  1099. type B<DB_QUEUE> in Berkeley DB 3.x or greater. This database format
  1100. isn't available if you use Berkeley DB 2.x.
  1101.  
  1102. Two forms of constructor are supported:
  1103.  
  1104.     $db = new BerkeleyDB::Queue
  1105.                 [ -Filename      => "filename", ]
  1106.                 [ -Subname       => "sub-database name", ]
  1107.                 [ -Flags         => flags,]
  1108.                 [ -Property      => flags,]
  1109.                 [ -Mode          => number,]
  1110.                 [ -Cachesize     => number,]
  1111.                 [ -Lorder        => number,]
  1112.                 [ -Pagesize      => number,]
  1113.                 [ -Env           => $env,]
  1114.                 [ -Txn           => $txn,]
  1115.                 [ -Encrypt       => { Password => "string",
  1116.                                   Flags    => number }, ],
  1117.                 # BerkeleyDB::Queue specific
  1118.                 [ -Len             => number,]
  1119.                 [ -Pad             => byte,]
  1120.                 [ -ExtentSize    => number, ]
  1121.  
  1122. and this
  1123.  
  1124.     [$db =] tie @arry, 'BerkeleyDB::Queue', 
  1125.                 [ -Filename      => "filename", ]
  1126.                 [ -Subname       => "sub-database name", ]
  1127.                 [ -Flags         => flags,]
  1128.                 [ -Property      => flags,]
  1129.                 [ -Mode          => number,]
  1130.                 [ -Cachesize     => number,]
  1131.                 [ -Lorder        => number,]
  1132.                 [ -Pagesize      => number,]
  1133.                 [ -Env           => $env,]
  1134.                 [ -Txn           => $txn,]
  1135.                 [ -Encrypt       => { Password => "string",
  1136.                                   Flags    => number }, ],
  1137.                 # BerkeleyDB::Queue specific
  1138.                 [ -Len             => number,]
  1139.                 [ -Pad             => byte,]
  1140.  
  1141.  
  1142. =head1 BerkeleyDB::Unknown
  1143.  
  1144. This class is used to open an existing database. 
  1145.  
  1146. Equivalent to calling B<db_open> with type B<DB_UNKNOWN> in Berkeley DB 2.x and
  1147. calling B<db_create> followed by B<DB-E<gt>open> with type B<DB_UNKNOWN> in
  1148. Berkeley DB 3.x or greater. 
  1149.  
  1150. The constructor looks like this:
  1151.  
  1152.     $db = new BerkeleyDB::Unknown
  1153.                 [ -Filename      => "filename", ]
  1154.                 [ -Subname       => "sub-database name", ]
  1155.                 [ -Flags         => flags,]
  1156.                 [ -Property      => flags,]
  1157.                 [ -Mode          => number,]
  1158.                 [ -Cachesize     => number,]
  1159.                 [ -Lorder        => number,]
  1160.                 [ -Pagesize      => number,]
  1161.                 [ -Env           => $env,]
  1162.                 [ -Txn           => $txn,]
  1163.                 [ -Encrypt       => { Password => "string",
  1164.                                   Flags    => number }, ],
  1165.  
  1166.  
  1167. =head2 An example 
  1168.  
  1169. =head1 COMMON OPTIONS
  1170.  
  1171. All database access class constructors support the common set of
  1172. options defined below. All are optional.
  1173.  
  1174. =over 5
  1175.  
  1176. =item -Filename
  1177.  
  1178. The database filename. If no filename is specified, a temporary file will
  1179. be created and removed once the program terminates.
  1180.  
  1181. =item -Subname
  1182.  
  1183. Specifies the name of the sub-database to open.
  1184. This option is only valid if you are using Berkeley DB 3.x or greater.
  1185.  
  1186. =item -Flags
  1187.  
  1188. Specify how the database will be opened/created. The valid flags are:
  1189.  
  1190. B<DB_CREATE>
  1191.  
  1192. Create any underlying files, as necessary. If the files do not already
  1193. exist and the B<DB_CREATE> flag is not specified, the call will fail.
  1194.  
  1195. B<DB_NOMMAP>
  1196.  
  1197. Not supported by BerkeleyDB.
  1198.  
  1199. B<DB_RDONLY>
  1200.  
  1201. Opens the database in read-only mode.
  1202.  
  1203. B<DB_THREAD>
  1204.  
  1205. Not supported by BerkeleyDB.
  1206.  
  1207. B<DB_TRUNCATE>
  1208.  
  1209. If the database file already exists, remove all the data before
  1210. opening it.
  1211.  
  1212. =item -Mode
  1213.  
  1214. Determines the file protection when the database is created. Defaults
  1215. to 0666.
  1216.  
  1217. =item -Cachesize
  1218.  
  1219. =item -Lorder
  1220.  
  1221. =item -Pagesize
  1222.  
  1223. =item -Env
  1224.  
  1225. When working under a Berkeley DB environment, this parameter
  1226.  
  1227. Defaults to no environment.
  1228.  
  1229. =item -Encrypt
  1230.  
  1231. If present, this parameter will enable encryption of  all data before
  1232. it is written to the database. This parameters must be given a hash
  1233. reference. The format is shown below.
  1234.  
  1235.     -Encrypt => { -Password => "abc", Flags => DB_ENCRYPT_AES }
  1236.  
  1237. Valid values for the Flags are 0 or C<DB_ENCRYPT_AES>.
  1238.  
  1239. This option requires Berkeley DB 4.1 or better.
  1240.  
  1241. =item -Txn
  1242.  
  1243. TODO.
  1244.  
  1245. =back
  1246.  
  1247. =head1 COMMON DATABASE METHODS
  1248.  
  1249. All the database interfaces support the common set of methods defined
  1250. below.
  1251.  
  1252. All the methods below return 0 to indicate success.
  1253.  
  1254. =head2 $status = $db->db_get($key, $value [, $flags])
  1255.  
  1256. Given a key (C<$key>) this method reads the value associated with it
  1257. from the database. If it exists, the value read from the database is
  1258. returned in the C<$value> parameter.
  1259.  
  1260. The B<$flags> parameter is optional. If present, it must be set to B<one>
  1261. of the following values:
  1262.  
  1263. =over 5
  1264.  
  1265. =item B<DB_GET_BOTH>
  1266.  
  1267. When the B<DB_GET_BOTH> flag is specified, B<db_get> checks for the
  1268. existence of B<both> the C<$key> B<and> C<$value> in the database.
  1269.  
  1270. =item B<DB_SET_RECNO>
  1271.  
  1272. TODO.
  1273.  
  1274. =back
  1275.  
  1276. In addition, the following value may be set by bitwise OR'ing it into
  1277. the B<$flags> parameter:
  1278.  
  1279. =over 5
  1280.  
  1281. =item B<DB_RMW>
  1282.  
  1283. TODO
  1284.  
  1285. =back
  1286.  
  1287.  
  1288. =head2 $status = $db->db_put($key, $value [, $flags])
  1289.  
  1290. Stores a key/value pair in the database.
  1291.  
  1292. The B<$flags> parameter is optional. If present it must be set to B<one>
  1293. of the following values:
  1294.  
  1295. =over 5
  1296.  
  1297. =item B<DB_APPEND>
  1298.  
  1299. This flag is only applicable when accessing a B<BerkeleyDB::Recno>
  1300. database.
  1301.  
  1302. TODO.
  1303.  
  1304.  
  1305. =item B<DB_NOOVERWRITE>
  1306.  
  1307. If this flag is specified and C<$key> already exists in the database,
  1308. the call to B<db_put> will return B<DB_KEYEXIST>.
  1309.  
  1310. =back
  1311.  
  1312. =head2 $status = $db->db_del($key [, $flags])
  1313.  
  1314. Deletes a key/value pair in the database associated with C<$key>.
  1315. If duplicate keys are enabled in the database, B<db_del> will delete
  1316. B<all> key/value pairs with key C<$key>.
  1317.  
  1318. The B<$flags> parameter is optional and is currently unused.
  1319.  
  1320. =head2 $status = $db->db_sync()
  1321.  
  1322. If any parts of the database are in memory, write them to the database.
  1323.  
  1324. =head2 $cursor = $db->db_cursor([$flags])
  1325.  
  1326. Creates a cursor object. This is used to access the contents of the
  1327. database sequentially. See L<CURSORS> for details of the methods
  1328. available when working with cursors.
  1329.  
  1330. The B<$flags> parameter is optional. If present it must be set to B<one>
  1331. of the following values:
  1332.  
  1333. =over 5
  1334.  
  1335. =item B<DB_RMW>
  1336.  
  1337. TODO.
  1338.  
  1339. =back
  1340.  
  1341. =head2 ($flag, $old_offset, $old_length) = $db->partial_set($offset, $length) ;
  1342.  
  1343. TODO
  1344.  
  1345. =head2 ($flag, $old_offset, $old_length) = $db->partial_clear() ;
  1346.  
  1347. TODO
  1348.  
  1349. =head2 $db->byteswapped()
  1350.  
  1351. TODO
  1352.  
  1353. =head2 $db->type()
  1354.  
  1355. Returns the type of the database. The possible return code are B<DB_HASH>
  1356. for a B<BerkeleyDB::Hash> database, B<DB_BTREE> for a B<BerkeleyDB::Btree>
  1357. database and B<DB_RECNO> for a B<BerkeleyDB::Recno> database. This method
  1358. is typically used when a database has been opened with
  1359. B<BerkeleyDB::Unknown>.
  1360.  
  1361. =head2 $lock = $db->cds_lock();
  1362.  
  1363. TODO.
  1364.  
  1365. =item $ref = $db->db_stat()
  1366.  
  1367. Returns a reference to an associative array containing information about
  1368. the database. The keys of the associative array correspond directly to the
  1369. names of the fields defined in the Berkeley DB documentation. For example,
  1370. in the DB documentation, the field B<bt_version> stores the version of the
  1371. Btree database. Assuming you called B<db_stat> on a Btree database the
  1372. equivalent field would be accessed as follows:
  1373.  
  1374.     $version = $ref->{'bt_version'} ;
  1375.  
  1376. If you are using Berkeley DB 3.x or better, this method will work will
  1377. all database formats. When DB 2.x is used, it only works with
  1378. B<BerkeleyDB::Btree>.
  1379.  
  1380. =head2 $status = $db->status()
  1381.  
  1382. Returns the status of the last C<$db> method called.
  1383.  
  1384. =head2 $status = $db->truncate($count)
  1385.  
  1386. Truncates the datatabase and returns the number or records deleted
  1387. in C<$count>.
  1388.  
  1389. =head1 CURSORS
  1390.  
  1391. A cursor is used whenever you want to access the contents of a database
  1392. in sequential order.
  1393. A cursor object is created with the C<db_cursor>
  1394.  
  1395. A cursor object has the following methods available:
  1396.  
  1397. =head2 $newcursor = $cursor->c_dup($flags)
  1398.  
  1399. Creates a duplicate of C<$cursor>. This method needs Berkeley DB 3.0.x or better.
  1400.  
  1401. The C<$flags> parameter is optional and can take the following value:
  1402.  
  1403. =over 5
  1404.  
  1405. =item DB_POSITION
  1406.  
  1407. When present this flag will position the new cursor at the same place as the
  1408. existing cursor.
  1409.  
  1410. =back
  1411.  
  1412. =head2 $status = $cursor->c_get($key, $value, $flags)
  1413.  
  1414. Reads a key/value pair from the database, returning the data in C<$key>
  1415. and C<$value>. The key/value pair actually read is controlled by the
  1416. C<$flags> parameter, which can take B<one> of the following values:
  1417.  
  1418. =over 5
  1419.  
  1420. =item B<DB_FIRST>
  1421.  
  1422. Set the cursor to point to the first key/value pair in the
  1423. database. Return the key/value pair in C<$key> and C<$value>.
  1424.  
  1425. =item B<DB_LAST>
  1426.  
  1427. Set the cursor to point to the last key/value pair in the database. Return
  1428. the key/value pair in C<$key> and C<$value>.
  1429.  
  1430. =item B<DB_NEXT>
  1431.  
  1432. If the cursor is already pointing to a key/value pair, it will be
  1433. incremented to point to the next key/value pair and return its contents.
  1434.  
  1435. If the cursor isn't initialised, B<DB_NEXT> works just like B<DB_FIRST>.
  1436.  
  1437. If the cursor is already positioned at the last key/value pair, B<c_get>
  1438. will return B<DB_NOTFOUND>.
  1439.  
  1440. =item B<DB_NEXT_DUP>
  1441.  
  1442. This flag is only valid when duplicate keys have been enabled in
  1443. a database.
  1444. If the cursor is already pointing to a key/value pair and the key of
  1445. the next key/value pair is identical, the cursor will be incremented to
  1446. point to it and their contents returned.
  1447.  
  1448. =item B<DB_PREV>
  1449.  
  1450. If the cursor is already pointing to a key/value pair, it will be
  1451. decremented to point to the previous key/value pair and return its
  1452. contents.
  1453.  
  1454. If the cursor isn't initialised, B<DB_PREV> works just like B<DB_LAST>.
  1455.  
  1456. If the cursor is already positioned at the first key/value pair, B<c_get>
  1457. will return B<DB_NOTFOUND>.
  1458.  
  1459. =item B<DB_CURRENT>
  1460.  
  1461. If the cursor has been set to point to a key/value pair, return their
  1462. contents.
  1463. If the key/value pair referenced by the cursor has been deleted, B<c_get>
  1464. will return B<DB_KEYEMPTY>.
  1465.  
  1466. =item B<DB_SET>
  1467.  
  1468. Set the cursor to point to the key/value pair referenced by B<$key>
  1469. and return the value in B<$value>.
  1470.  
  1471. =item B<DB_SET_RANGE>
  1472.  
  1473. This flag is a variation on the B<DB_SET> flag. As well as returning
  1474. the value, it also returns the key, via B<$key>.
  1475. When used with a B<BerkeleyDB::Btree> database the key matched by B<c_get>
  1476. will be the shortest key (in length) which is greater than or equal to
  1477. the key supplied, via B<$key>. This allows partial key searches.
  1478. See ??? for an example of how to use this flag.
  1479.  
  1480. =item B<DB_GET_BOTH>
  1481.  
  1482. Another variation on B<DB_SET>. This one returns both the key and
  1483. the value.
  1484.  
  1485. =item B<DB_SET_RECNO>
  1486.  
  1487. TODO.
  1488.  
  1489. =item B<DB_GET_RECNO>
  1490.  
  1491. TODO.
  1492.  
  1493. =back
  1494.  
  1495. In addition, the following value may be set by bitwise OR'ing it into
  1496. the B<$flags> parameter:
  1497.  
  1498. =over 5
  1499.  
  1500. =item B<DB_RMW>
  1501.  
  1502. TODO.
  1503.  
  1504. =back
  1505.  
  1506. =head2  $status = $cursor->c_put($key, $value, $flags)
  1507.  
  1508. Stores the key/value pair in the database. The position that the data is
  1509. stored in the database is controlled by the C<$flags> parameter, which
  1510. must take B<one> of the following values:
  1511.  
  1512. =over 5
  1513.  
  1514. =item B<DB_AFTER>
  1515.  
  1516. When used with a Btree or Hash database, a duplicate of the key referenced
  1517. by the current cursor position will be created and the contents of
  1518. B<$value> will be associated with it - B<$key> is ignored.
  1519. The new key/value pair will be stored immediately after the current
  1520. cursor position.
  1521. Obviously the database has to have been opened with B<DB_DUP>.
  1522.  
  1523. When used with a Recno ... TODO
  1524.  
  1525.  
  1526. =item B<DB_BEFORE>
  1527.  
  1528. When used with a Btree or Hash database, a duplicate of the key referenced
  1529. by the current cursor position will be created and the contents of
  1530. B<$value> will be associated with it - B<$key> is ignored.
  1531. The new key/value pair will be stored immediately before the current
  1532. cursor position.
  1533. Obviously the database has to have been opened with B<DB_DUP>.
  1534.  
  1535. When used with a Recno ... TODO
  1536.  
  1537. =item B<DB_CURRENT>
  1538.  
  1539. If the cursor has been initialised, replace the value of the key/value
  1540. pair stored in the database with the contents of B<$value>.
  1541.  
  1542. =item B<DB_KEYFIRST>
  1543.  
  1544. Only valid with a Btree or Hash database. This flag is only really
  1545. used when duplicates are enabled in the database and sorted duplicates
  1546. haven't been specified.
  1547. In this case the key/value pair will be inserted as the first entry in
  1548. the duplicates for the particular key.
  1549.  
  1550. =item B<DB_KEYLAST>
  1551.  
  1552. Only valid with a Btree or Hash database. This flag is only really
  1553. used when duplicates are enabled in the database and sorted duplicates
  1554. haven't been specified.
  1555. In this case the key/value pair will be inserted as the last entry in
  1556. the duplicates for the particular key.
  1557.  
  1558. =back
  1559.  
  1560. =head2  $status = $cursor->c_del([$flags])
  1561.  
  1562. This method deletes the key/value pair associated with the current cursor
  1563. position. The cursor position will not be changed by this operation, so
  1564. any subsequent cursor operation must first initialise the cursor to
  1565. point to a valid key/value pair.
  1566.  
  1567. If the key/value pair associated with the cursor have already been
  1568. deleted, B<c_del> will return B<DB_KEYEMPTY>.
  1569.  
  1570. The B<$flags> parameter is not used at present.
  1571.  
  1572. =head2 $status = $cursor->c_del($cnt [, $flags])
  1573.  
  1574. Stores the number of duplicates at the current cursor position in B<$cnt>.
  1575.  
  1576. The B<$flags> parameter is not used at present. This method needs 
  1577. Berkeley DB 3.1 or better.
  1578.  
  1579. =head2  $status = $cursor->status()
  1580.  
  1581. Returns the status of the last cursor method as a dual type.
  1582.  
  1583. =head2 Cursor Examples
  1584.  
  1585. TODO
  1586.  
  1587. Iterating from first to last, then in reverse.
  1588.  
  1589. examples of each of the flags.
  1590.  
  1591. =head1 JOIN
  1592.  
  1593. Join support for BerkeleyDB is in progress. Watch this space.
  1594.  
  1595. TODO
  1596.  
  1597. =head1 TRANSACTIONS
  1598.  
  1599. TODO.
  1600.  
  1601. =head1 DBM Filters
  1602.  
  1603. A DBM Filter is a piece of code that is be used when you I<always>
  1604. want to make the same transformation to all keys and/or values in a DBM
  1605. database. All of the database classes (BerkeleyDB::Hash,
  1606. BerkeleyDB::Btree and BerkeleyDB::Recno) support DBM Filters.
  1607.  
  1608. There are four methods associated with DBM Filters. All work
  1609. identically, and each is used to install (or uninstall) a single DBM
  1610. Filter. Each expects a single parameter, namely a reference to a sub.
  1611. The only difference between them is the place that the filter is
  1612. installed.
  1613.  
  1614. To summarise:
  1615.  
  1616. =over 5
  1617.  
  1618. =item B<filter_store_key>
  1619.  
  1620. If a filter has been installed with this method, it will be invoked
  1621. every time you write a key to a DBM database.
  1622.  
  1623. =item B<filter_store_value>
  1624.  
  1625. If a filter has been installed with this method, it will be invoked
  1626. every time you write a value to a DBM database.
  1627.  
  1628.  
  1629. =item B<filter_fetch_key>
  1630.  
  1631. If a filter has been installed with this method, it will be invoked
  1632. every time you read a key from a DBM database.
  1633.  
  1634. =item B<filter_fetch_value>
  1635.  
  1636. If a filter has been installed with this method, it will be invoked
  1637. every time you read a value from a DBM database.
  1638.  
  1639. =back
  1640.  
  1641. You can use any combination of the methods, from none, to all four.
  1642.  
  1643. All filter methods return the existing filter, if present, or C<undef>
  1644. in not.
  1645.  
  1646. To delete a filter pass C<undef> to it.
  1647.  
  1648. =head2 The Filter
  1649.  
  1650. When each filter is called by Perl, a local copy of C<$_> will contain
  1651. the key or value to be filtered. Filtering is achieved by modifying
  1652. the contents of C<$_>. The return code from the filter is ignored.
  1653.  
  1654. =head2 An Example -- the NULL termination problem.
  1655.  
  1656. Consider the following scenario. You have a DBM database that you need
  1657. to share with a third-party C application. The C application assumes
  1658. that I<all> keys and values are NULL terminated. Unfortunately when
  1659. Perl writes to DBM databases it doesn't use NULL termination, so your
  1660. Perl application will have to manage NULL termination itself. When you
  1661. write to the database you will have to use something like this:
  1662.  
  1663.     $hash{"$key\0"} = "$value\0" ;
  1664.  
  1665. Similarly the NULL needs to be taken into account when you are considering
  1666. the length of existing keys/values.
  1667.  
  1668. It would be much better if you could ignore the NULL terminations issue
  1669. in the main application code and have a mechanism that automatically
  1670. added the terminating NULL to all keys and values whenever you write to
  1671. the database and have them removed when you read from the database. As I'm
  1672. sure you have already guessed, this is a problem that DBM Filters can
  1673. fix very easily.
  1674.  
  1675.     use strict ;
  1676.     use BerkeleyDB ;
  1677.  
  1678.     my %hash ;
  1679.     my $filename = "filt.db" ;
  1680.     unlink $filename ;
  1681.  
  1682.     my $db = tie %hash, 'BerkeleyDB::Hash',
  1683.             -Filename   => $filename,
  1684.             -Flags      => DB_CREATE
  1685.       or die "Cannot open $filename: $!\n" ;
  1686.  
  1687.     # Install DBM Filters
  1688.     $db->filter_fetch_key  ( sub { s/\0$//    } ) ;
  1689.     $db->filter_store_key  ( sub { $_ .= "\0" } ) ;
  1690.     $db->filter_fetch_value( sub { s/\0$//    } ) ;
  1691.     $db->filter_store_value( sub { $_ .= "\0" } ) ;
  1692.  
  1693.     $hash{"abc"} = "def" ;
  1694.     my $a = $hash{"ABC"} ;
  1695.     # ...
  1696.     undef $db ;
  1697.     untie %hash ;
  1698.  
  1699. Hopefully the contents of each of the filters should be
  1700. self-explanatory. Both "fetch" filters remove the terminating NULL,
  1701. and both "store" filters add a terminating NULL.
  1702.  
  1703.  
  1704. =head2 Another Example -- Key is a C int.
  1705.  
  1706. Here is another real-life example. By default, whenever Perl writes to
  1707. a DBM database it always writes the key and value as strings. So when
  1708. you use this:
  1709.  
  1710.     $hash{12345} = "something" ;
  1711.  
  1712. the key 12345 will get stored in the DBM database as the 5 byte string
  1713. "12345". If you actually want the key to be stored in the DBM database
  1714. as a C int, you will have to use C<pack> when writing, and C<unpack>
  1715. when reading.
  1716.  
  1717. Here is a DBM Filter that does it:
  1718.  
  1719.     use strict ;
  1720.     use BerkeleyDB ;
  1721.     my %hash ;
  1722.     my $filename = "filt.db" ;
  1723.     unlink $filename ;
  1724.  
  1725.  
  1726.     my $db = tie %hash, 'BerkeleyDB::Btree',
  1727.             -Filename   => $filename,
  1728.             -Flags      => DB_CREATE
  1729.       or die "Cannot open $filename: $!\n" ;
  1730.  
  1731.     $db->filter_fetch_key  ( sub { $_ = unpack("i", $_) } ) ;
  1732.     $db->filter_store_key  ( sub { $_ = pack ("i", $_) } ) ;
  1733.     $hash{123} = "def" ;
  1734.     # ...
  1735.     undef $db ;
  1736.     untie %hash ;
  1737.  
  1738. This time only two filters have been used -- we only need to manipulate
  1739. the contents of the key, so it wasn't necessary to install any value
  1740. filters.
  1741.  
  1742. =head1 Using BerkeleyDB with MLDBM
  1743.  
  1744. Both BerkeleyDB::Hash and BerkeleyDB::Btree can be used with the MLDBM
  1745. module. The code fragment below shows how to open associate MLDBM with
  1746. BerkeleyDB::Btree. To use BerkeleyDB::Hash just replace
  1747. BerkeleyDB::Btree with BerkeleyDB::Hash.
  1748.  
  1749.     use strict ;
  1750.     use BerkeleyDB ;
  1751.     use MLDBM qw(BerkeleyDB::Btree) ;
  1752.     use Data::Dumper;
  1753.  
  1754.     my $filename = 'testmldbm' ;
  1755.     my %o ;
  1756.      
  1757.     unlink $filename ;
  1758.     tie %o, 'MLDBM', -Filename => $filename,
  1759.                      -Flags    => DB_CREATE
  1760.                     or die "Cannot open database '$filename: $!\n";
  1761.  
  1762. See the MLDBM documentation for information on how to use the module
  1763. and for details of its limitations.
  1764.  
  1765. =head1 EXAMPLES
  1766.  
  1767. TODO.
  1768.  
  1769. =head1 HINTS & TIPS
  1770.  
  1771. =head2 Sharing Databases With C Applications
  1772.  
  1773. There is no technical reason why a Berkeley DB database cannot be
  1774. shared by both a Perl and a C application.
  1775.  
  1776. The vast majority of problems that are reported in this area boil down
  1777. to the fact that C strings are NULL terminated, whilst Perl strings
  1778. are not. See L<An Example -- the NULL termination problem.> in the DBM
  1779. FILTERS section for a generic way to work around this problem.
  1780.  
  1781.  
  1782. =head2 The untie Gotcha
  1783.  
  1784. TODO
  1785.  
  1786. =head1 COMMON QUESTIONS
  1787.  
  1788. This section attempts to answer some of the more common questions that
  1789. I get asked.
  1790.  
  1791.  
  1792. =head2 Relationship with DB_File
  1793.  
  1794. Before Berkeley DB 2.x was written there was only one Perl module that
  1795. interfaced to Berkeley DB. That module is called B<DB_File>. Although
  1796. B<DB_File> can be build with Berkeley DB 1.x, 2.x, 3.x or 4.x, it only
  1797. provides an interface to the functionality available in Berkeley DB
  1798. 1.x. That means that it doesn't support transactions, locking or any of
  1799. the other new features available in DB 2.x or better.
  1800.  
  1801. =head2 How do I store Perl data structures with BerkeleyDB?
  1802.  
  1803. See L<Using BerkeleyDB with MLDBM>.
  1804.  
  1805. =head1 HISTORY
  1806.  
  1807. See the Changes file.
  1808.  
  1809. =head1 AVAILABILITY
  1810.  
  1811. The most recent version of B<BerkeleyDB> can always be found
  1812. on CPAN (see L<perlmod/CPAN> for details), in the directory
  1813. F<modules/by-module/BerkeleyDB>.
  1814.  
  1815. The official web site for Berkeley DB is F<http://www.sleepycat.com>.
  1816.  
  1817. =head1 COPYRIGHT
  1818.  
  1819. Copyright (c) 1997-2003 Paul Marquess. All rights reserved. This program
  1820. is free software; you can redistribute it and/or modify it under the
  1821. same terms as Perl itself.
  1822.  
  1823. Although B<BerkeleyDB> is covered by the Perl license, the library it
  1824. makes use of, namely Berkeley DB, is not. Berkeley DB has its own
  1825. copyright and its own license. Please take the time to read it.
  1826.  
  1827. Here are few words taken from the Berkeley DB FAQ (at
  1828. F<http://www.sleepycat.com>) regarding the license:
  1829.  
  1830.     Do I have to license DB to use it in Perl scripts?
  1831.  
  1832.     No. The Berkeley DB license requires that software that uses
  1833.     Berkeley DB be freely redistributable. In the case of Perl, that
  1834.     software is Perl, and not your scripts. Any Perl scripts that you
  1835.     write are your property, including scripts that make use of Berkeley
  1836.     DB. Neither the Perl license nor the Berkeley DB license
  1837.     place any restriction on what you may do with them.
  1838.  
  1839. If you are in any doubt about the license situation, contact either the
  1840. Berkeley DB authors or the author of BerkeleyDB.
  1841. See L<"AUTHOR"> for details.
  1842.  
  1843.  
  1844. =head1 AUTHOR
  1845.  
  1846. Paul Marquess E<lt>pmqs@cpan.orgE<gt>.
  1847.  
  1848. Questions about Berkeley DB may be addressed to E<lt>db@sleepycat.comE<gt>.
  1849.  
  1850. =head1 SEE ALSO
  1851.  
  1852. perl(1), DB_File, Berkeley DB.
  1853.  
  1854. =cut
  1855.