📄 berkeleydb.pod
字号:
-Flags => DB_CREATE, -Compare => sub { lc $_[0] cmp lc $_[1] } or die "Cannot open $filename: $!\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. mouse Smith WallThere are a few point to bear in mind if you want to change theordering in a BTREE database:=over 5=item 1.The new compare function must be specified when you create the database.=item 2.You cannot change the ordering once the database has been created. Thusyou must use the same compare function every time you access thedatabase.=back =head2 Using db_statTODO=head1 BerkeleyDB::RecnoEquivalent to calling B<db_open> with type B<DB_RECNO> in Berkeley DB 2.x andcalling B<db_create> followed by B<DB-E<gt>open> with type B<DB_RECNO> inBerkeley DB 3.x or greater. Two forms of constructor are supported: $db = new BerkeleyDB::Recno [ -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::Recno specific [ -Delim => byte,] [ -Len => number,] [ -Pad => byte,] [ -Source => filename,]and this [$db =] tie @arry, 'BerkeleyDB::Recno', [ -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::Recno specific [ -Delim => byte,] [ -Len => number,] [ -Pad => byte,] [ -Source => filename,]=head2 A Recno ExampleHere is a simple example that uses RECNO (if you are using a version of Perl earlier than 5.004_57 this example won't work -- see L<Extra RECNO Methods> for a workaround). use strict ; use BerkeleyDB ; my $filename = "text" ; unlink $filename ; my @h ; tie @h, 'BerkeleyDB::Recno', -Filename => $filename, -Flags => DB_CREATE, -Property => DB_RENUMBER or die "Cannot open $filename: $!\n" ; # Add a few key/value pairs to the file $h[0] = "orange" ; $h[1] = "blue" ; $h[2] = "yellow" ; push @h, "green", "black" ; my $elements = scalar @h ; print "The array contains $elements entries\n" ; my $last = pop @h ; print "popped $last\n" ; unshift @h, "white" ; my $first = shift @h ; print "shifted $first\n" ; # Check for existence of a key print "Element 1 Exists with value $h[1]\n" if $h[1] ; untie @h ;Here is the output from the script: The array contains 5 entries popped black shifted white Element 1 Exists with value blue The last element is green The 2nd last element is yellow=head1 BerkeleyDB::QueueEquivalent to calling B<db_create> followed by B<DB-E<gt>open> withtype B<DB_QUEUE> in Berkeley DB 3.x or greater. This database formatisn't available if you use Berkeley DB 2.x.Two forms of constructor are supported: $db = new BerkeleyDB::Queue [ -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::Queue specific [ -Len => number,] [ -Pad => byte,] [ -ExtentSize => number, ]and this [$db =] tie @arry, 'BerkeleyDB::Queue', [ -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::Queue specific [ -Len => number,] [ -Pad => byte,]=head1 BerkeleyDB::UnknownThis class is used to open an existing database. Equivalent to calling B<db_open> with type B<DB_UNKNOWN> in Berkeley DB 2.x andcalling B<db_create> followed by B<DB-E<gt>open> with type B<DB_UNKNOWN> inBerkeley DB 3.x or greater. The constructor looks like this: $db = new BerkeleyDB::Unknown [ -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 }, ],=head2 An example =head1 COMMON OPTIONSAll database access class constructors support the common set ofoptions defined below. All are optional.=over 5=item -FilenameThe database filename. If no filename is specified, a temporary file willbe created and removed once the program terminates.=item -SubnameSpecifies the name of the sub-database to open.This option is only valid if you are using Berkeley DB 3.x or greater.=item -FlagsSpecify how the database will be opened/created. The valid flags are:B<DB_CREATE>Create any underlying files, as necessary. If the files do not alreadyexist and the B<DB_CREATE> flag is not specified, the call will fail.B<DB_NOMMAP>Not supported by BerkeleyDB.B<DB_RDONLY>Opens the database in read-only mode.B<DB_THREAD>Not supported by BerkeleyDB.B<DB_TRUNCATE>If the database file already exists, remove all the data beforeopening it.=item -ModeDetermines the file protection when the database is created. Defaultsto 0666.=item -Cachesize=item -Lorder=item -Pagesize=item -EnvWhen working under a Berkeley DB environment, this parameterDefaults to no environment.=item -EncryptIf present, this parameter will enable encryption of all data beforeit is written to the database. This parameters must be given a hashreference. The format is shown below. -Encrypt => { -Password => "abc", Flags => DB_ENCRYPT_AES }Valid values for the Flags are 0 or C<DB_ENCRYPT_AES>.This option requires Berkeley DB 4.1 or better.=item -TxnTODO.=back=head1 COMMON DATABASE METHODSAll the database interfaces support the common set of methods definedbelow.All the methods below return 0 to indicate success.=head2 $status = $db->db_get($key, $value [, $flags])Given a key (C<$key>) this method reads the value associated with itfrom the database. If it exists, the value read from the database isreturned in the C<$value> parameter.The B<$flags> parameter is optional. If present, it must be set to B<one>of the following values:=over 5=item B<DB_GET_BOTH>When the B<DB_GET_BOTH> flag is specified, B<db_get> checks for theexistence of B<both> the C<$key> B<and> C<$value> in the database.=item B<DB_SET_RECNO>TODO.=backIn addition, the following value may be set by bitwise OR'ing it intothe B<$flags> parameter:=over 5=item B<DB_RMW>TODO=back=head2 $status = $db->db_put($key, $value [, $flags])Stores a key/value pair in the database.The B<$flags> parameter is optional. If present it must be set to B<one>of the following values:=over 5=item B<DB_APPEND>This flag is only applicable when accessing a B<BerkeleyDB::Recno>database.TODO.=item B<DB_NOOVERWRITE>If this flag is specified and C<$key> already exists in the database,the call to B<db_put> will return B<DB_KEYEXIST>.=back=head2 $status = $db->db_del($key [, $flags])Deletes a key/value pair in the database associated with C<$key>.If duplicate keys are enabled in the database, B<db_del> will deleteB<all> key/value pairs with key C<$key>.The B<$flags> parameter is optional and is currently unused.=head2 $status = $db->db_sync()If any parts of the database are in memory, write them to the database.=head2 $cursor = $db->db_cursor([$flags])Creates a cursor object. This is used to access the contents of thedatabase sequentially. See L<CURSORS> for details of the methodsavailable when working with cursors.The B<$flags> parameter is optional. If present it must be set to B<one>of the following values:=over 5=item B<DB_RMW>TODO.=back=head2 ($flag, $old_offset, $old_length) = $db->partial_set($offset, $length) ;TODO=head2 ($flag, $old_offset, $old_length) = $db->partial_clear() ;TODO=head2 $db->byteswapped()TODO=head2 $db->type()Returns the type of the database. The possible return code are B<DB_HASH>for a B<BerkeleyDB::Hash> database, B<DB_BTREE> for a B<BerkeleyDB::Btree>database and B<DB_RECNO> for a B<BerkeleyDB::Recno> database. This methodis typically used when a database has been opened withB<BerkeleyDB::Unknown>.=head2 $bool = $env->cds_enabled();Returns true if the Berkeley DB environment C<$env> has been opened onCDS mode.=head2 $bool = $db->cds_enabled();Returns true if the database C<$db> has been opened on CDS mode.=head2 $lock = $db->cds_lock();Creates a CDS write lock object C<$lock>.It is a fatal error to attempt to create a cds_lock if the Berkeley DBenvironment has not been opened in CDS mode.=head2 $lock->cds_unlock();Removes a CDS lock. The destruction of the CDS lock object automaticallycalls this method.Note that if multiple CDS lock objects are created, the underlying writelock will not be released until all CDS lock objects are either explictlyunlocked with this method, or the CDS lock objects have been destroyed.=head2 $ref = $db->db_stat()Returns a reference to an associative array containing information aboutthe database. The keys of the associative array correspond directly to thenames of the fields defined in the Berkeley DB documentation. For example,in the DB documentation, the field B<bt_version> stores the version of theBtree database. Assuming you called B<db_stat> on a Btree database theequivalent field would be accessed as follows: $version = $ref->{'bt_version'} ;If you are using Berkeley DB 3.x or better, this method will work willall database formats. When DB 2.x is used, it only works withB<BerkeleyDB::Btree>.=head2 $status = $db->status()Returns the status of the last C<$db> method called.=head2 $status = $db->truncate($count)Truncates the datatabase and returns the number or records deletedin C<$count>.=head2 $status = $db->compact($start, $stop, $c_data, $flags, $end);Compacts the database C<$db>. All the parameters are optional - if only want to make use of some of them,use C<undef> for those you don't want. Trailing unusused parameters can beomitted. For example, if you only want to use the C<$c_data> parameter toset the C<compact_fillpercent>, write you code like this my %hash; $hash{compact_fillpercent} = 50; $db->commit(undef, undef, \%hash);The parameters operate identically to the C equivalent of this method.The C<$c_data> needs a bit of explanation - it must be a hash reference.The values of the following keys can be set before calling C<compact> andwill affect the operation of the compaction.=over 5=item * compact_fillpercent=item * compact_timeout =backThe following keys, along with associated values, will be created in thehash reference if the C<compact> operation was successful.=over 5=item * compact_deadlock=item * compact_levels=item * compact_pages_free
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -