📄 berkeleydb.pod
字号:
[ -Encrypt => { Password => "string", Flags => number }, ], # BerkeleyDB::Hash specific [ -Ffactor => number,] [ -Nelem => number,] [ -Hash => code reference,] [ -DupCompare => code reference,]and this [$db =] tie %hash, 'BerkeleyDB::Hash', [ -Filename => "filename", ] [ -Subname => "sub-database name", ] [ -Flags => flags,] [ -Property => flags,] [ -Mode => number,] [ -Cachesize => number,] [ -Lorder => number,] [ -Pagesize => number,] [ -Env => $env,] [ -Txn => $txn,] [ -Encrypt => { Password => "string", Flags => number }, ], # BerkeleyDB::Hash specific [ -Ffactor => number,] [ -Nelem => number,] [ -Hash => code reference,] [ -DupCompare => code reference,]When the "tie" interface is used, reading from and writing to the databaseis achieved via the tied hash. In this case the database operates likea Perl associative array that happens to be stored on disk.In addition to the high-level tied hash interface, it is possible tomake use of the underlying methods provided by Berkeley DB=head2 OptionsIn addition to the standard set of options (see L<COMMON OPTIONS>)B<BerkeleyDB::Hash> supports these options:=over 5=item -PropertyUsed to specify extra flags when opening a database. The followingflags may be specified by bitwise OR'ing together one or more of thefollowing values:B<DB_DUP>When creating a new database, this flag enables the storing of duplicatekeys in the database. If B<DB_DUPSORT> is not specified as well, theduplicates are stored in the order they are created in the database.B<DB_DUPSORT>Enables the sorting of duplicate keys in the database. Ignored ifB<DB_DUP> isn't also specified.=item -Ffactor=item -NelemSee the Berkeley DB documentation for details of these options.=item -HashAllows you to provide a user defined hash function. If not specified, a default hash function is used. Here is a template for a user-definedhash function sub hash { my ($data) = shift ; ... # return the hash value for $data return $hash ; } tie %h, "BerkeleyDB::Hash", -Filename => $filename, -Hash => \&hash, ...See L<""> for an example.=item -DupCompareUsed in conjunction with the B<DB_DUPOSRT> flag. sub compare { my ($key, $key2) = @_ ; ... # return 0 if $key1 eq $key2 # -1 if $key1 lt $key2 # 1 if $key1 gt $key2 return (-1 , 0 or 1) ; } tie %h, "BerkeleyDB::Hash", -Filename => $filename, -Property => DB_DUP|DB_DUPSORT, -DupCompare => \&compare, ...=back=head2 MethodsB<BerkeleyDB::Hash> only supports the standard database methods.See L<COMMON DATABASE METHODS>.=head2 A Simple Tied Hash Example use strict ; use BerkeleyDB ; use vars qw( %h $k $v ) ; my $filename = "fruit" ; unlink $filename ; tie %h, "BerkeleyDB::Hash", -Filename => $filename, -Flags => DB_CREATE or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ; # Add a few key/value pairs to the file $h{"apple"} = "red" ; $h{"orange"} = "orange" ; $h{"banana"} = "yellow" ; $h{"tomato"} = "red" ; # Check for existence of a key print "Banana Exists\n\n" if $h{"banana"} ; # Delete a key/value pair. delete $h{"apple"} ; # print the contents of the file while (($k, $v) = each %h) { print "$k -> $v\n" } untie %h ;here is the output: Banana Exists orange -> orange tomato -> red banana -> yellowNote that the like ordinary associative arrays, the order of the keysretrieved from a Hash database are in an apparently random order.=head2 Another Simple Hash ExampleDo the same as the previous example but not using tie. use strict ; use BerkeleyDB ; my $filename = "fruit" ; unlink $filename ; my $db = new BerkeleyDB::Hash -Filename => $filename, -Flags => DB_CREATE or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ; # Add a few key/value pairs to the file $db->db_put("apple", "red") ; $db->db_put("orange", "orange") ; $db->db_put("banana", "yellow") ; $db->db_put("tomato", "red") ; # Check for existence of a key print "Banana Exists\n\n" if $db->db_get("banana", $v) == 0; # Delete a key/value pair. $db->db_del("apple") ; # print the contents of the file my ($k, $v) = ("", "") ; my $cursor = $db->db_cursor() ; while ($cursor->c_get($k, $v, DB_NEXT) == 0) { print "$k -> $v\n" } undef $cursor ; undef $db ;=head2 Duplicate keysThe code below is a variation on the examples above. This time the hash hasbeen inverted. The key this time is colour and the value is the fruit name.The B<DB_DUP> flag has been specified to allow duplicates. use strict ; use BerkeleyDB ; my $filename = "fruit" ; unlink $filename ; my $db = new BerkeleyDB::Hash -Filename => $filename, -Flags => DB_CREATE, -Property => DB_DUP or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ; # Add a few key/value pairs to the file $db->db_put("red", "apple") ; $db->db_put("orange", "orange") ; $db->db_put("green", "banana") ; $db->db_put("yellow", "banana") ; $db->db_put("red", "tomato") ; $db->db_put("green", "apple") ; # print the contents of the file my ($k, $v) = ("", "") ; my $cursor = $db->db_cursor() ; while ($cursor->c_get($k, $v, DB_NEXT) == 0) { print "$k -> $v\n" } undef $cursor ; undef $db ;here is the output: orange -> orange yellow -> banana red -> apple red -> tomato green -> banana green -> apple=head2 Sorting Duplicate KeysIn the previous example, when there were duplicate keys, the values aresorted in the order they are stored in. The code below isidentical to the previous example except the B<DB_DUPSORT> flag isspecified. use strict ; use BerkeleyDB ; my $filename = "fruit" ; unlink $filename ; my $db = new BerkeleyDB::Hash -Filename => $filename, -Flags => DB_CREATE, -Property => DB_DUP | DB_DUPSORT or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ; # Add a few key/value pairs to the file $db->db_put("red", "apple") ; $db->db_put("orange", "orange") ; $db->db_put("green", "banana") ; $db->db_put("yellow", "banana") ; $db->db_put("red", "tomato") ; $db->db_put("green", "apple") ; # print the contents of the file my ($k, $v) = ("", "") ; my $cursor = $db->db_cursor() ; while ($cursor->c_get($k, $v, DB_NEXT) == 0) { print "$k -> $v\n" } undef $cursor ; undef $db ;Notice that in the output below the duplicate values are sorted. orange -> orange yellow -> banana red -> apple red -> tomato green -> apple green -> banana=head2 Custom Sorting Duplicate KeysAnother variation TODO=head2 Changing the hashTODO=head2 Using db_statTODO=head1 BerkeleyDB::BtreeEquivalent to calling B<db_open> with type B<DB_BTREE> in Berkeley DB 2.x andcalling B<db_create> followed by B<DB-E<gt>open> with type B<DB_BTREE> inBerkeley DB 3.x or greater. Two forms of constructor are supported: $db = new BerkeleyDB::Btree [ -Filename => "filename", ] [ -Subname => "sub-database name", ] [ -Flags => flags,] [ -Property => flags,] [ -Mode => number,] [ -Cachesize => number,] [ -Lorder => number,] [ -Pagesize => number,] [ -Env => $env,] [ -Txn => $txn,] [ -Encrypt => { Password => "string", Flags => number }, ], # BerkeleyDB::Btree specific [ -Minkey => number,] [ -Compare => code reference,] [ -DupCompare => code reference,] [ -Prefix => code reference,]and this [$db =] tie %hash, 'BerkeleyDB::Btree', [ -Filename => "filename", ] [ -Subname => "sub-database name", ] [ -Flags => flags,] [ -Property => flags,] [ -Mode => number,] [ -Cachesize => number,] [ -Lorder => number,] [ -Pagesize => number,] [ -Env => $env,] [ -Txn => $txn,] [ -Encrypt => { Password => "string", Flags => number }, ], # BerkeleyDB::Btree specific [ -Minkey => number,] [ -Compare => code reference,] [ -DupCompare => code reference,] [ -Prefix => code reference,]=head2 OptionsIn addition to the standard set of options (see L<COMMON OPTIONS>)B<BerkeleyDB::Btree> supports these options:=over 5=item -PropertyUsed to specify extra flags when opening a database. The followingflags may be specified by bitwise OR'ing together one or more of thefollowing values:B<DB_DUP>When creating a new database, this flag enables the storing of duplicatekeys in the database. If B<DB_DUPSORT> is not specified as well, theduplicates are stored in the order they are created in the database.B<DB_DUPSORT>Enables the sorting of duplicate keys in the database. Ignored ifB<DB_DUP> isn't also specified.=item MinkeyTODO=item CompareAllow you to override the default sort order used in the database. SeeL<"Changing the sort order"> for an example. sub compare { my ($key, $key2) = @_ ; ... # return 0 if $key1 eq $key2 # -1 if $key1 lt $key2 # 1 if $key1 gt $key2 return (-1 , 0 or 1) ; } tie %h, "BerkeleyDB::Hash", -Filename => $filename, -Compare => \&compare, ...=item Prefix sub prefix { my ($key, $key2) = @_ ; ... # return number of bytes of $key2 which are # necessary to determine that it is greater than $key1 return $bytes ; } tie %h, "BerkeleyDB::Hash", -Filename => $filename, -Prefix => \&prefix, ...=item DupCompare sub compare { my ($key, $key2) = @_ ; ... # return 0 if $key1 eq $key2 # -1 if $key1 lt $key2 # 1 if $key1 gt $key2 return (-1 , 0 or 1) ; } tie %h, "BerkeleyDB::Hash", -Filename => $filename, -DupCompare => \&compare, ...=back=head2 MethodsB<BerkeleyDB::Btree> supports the following database methods.See also L<COMMON DATABASE METHODS>.All the methods below return 0 to indicate success.=over 5=item $status = $db->db_key_range($key, $less, $equal, $greater [, $flags])Given a key, C<$key>, this method returns the proportion of keys less than C<$key> in C<$less>, the proportion equal to C<$key> in C<$equal> and theproportion greater than C<$key> in C<$greater>.The proportion is returned as a double in the range 0.0 to 1.0.=back=head2 A Simple Btree ExampleThe code below is a simple example of using a btree database. use strict ; use BerkeleyDB ; my $filename = "tree" ; unlink $filename ; my %h ; tie %h, 'BerkeleyDB::Btree', -Filename => $filename, -Flags => DB_CREATE or die "Cannot open $filename: $! $BerkeleyDB::Error\n" ; # Add a key/value pair to the file $h{'Wall'} = 'Larry' ; $h{'Smith'} = 'John' ; $h{'mouse'} = 'mickey' ; $h{'duck'} = 'donald' ; # Delete delete $h{"duck"} ; # Cycle through the keys printing them in order. # Note it is not necessary to sort the keys as # the btree will have kept them in order automatically. foreach (keys %h) { print "$_\n" } untie %h ;Here is the output from the code above. The keys have been sorted usingBerkeley DB's default sorting algorithm. Smith
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -