📄 berkeleydb.pod
字号:
=item * compact_pages_examine=item * compact_pages_truncated=backYou need to be running Berkeley DB 4.4 or better if you wan to make use ofC<compact>.=head1 CURSORSA cursor is used whenever you want to access the contents of a databasein sequential order.A cursor object is created with the C<db_cursor>A cursor object has the following methods available:=head2 $newcursor = $cursor->c_dup($flags)Creates a duplicate of C<$cursor>. This method needs Berkeley DB 3.0.x or better.The C<$flags> parameter is optional and can take the following value:=over 5=item DB_POSITIONWhen present this flag will position the new cursor at the same place as theexisting cursor.=back=head2 $status = $cursor->c_get($key, $value, $flags)Reads a key/value pair from the database, returning the data in C<$key>and C<$value>. The key/value pair actually read is controlled by theC<$flags> parameter, which can take B<one> of the following values:=over 5=item B<DB_FIRST>Set the cursor to point to the first key/value pair in thedatabase. Return the key/value pair in C<$key> and C<$value>.=item B<DB_LAST>Set the cursor to point to the last key/value pair in the database. Returnthe key/value pair in C<$key> and C<$value>.=item B<DB_NEXT>If the cursor is already pointing to a key/value pair, it will beincremented to point to the next key/value pair and return its contents.If the cursor isn't initialised, B<DB_NEXT> works just like B<DB_FIRST>.If the cursor is already positioned at the last key/value pair, B<c_get>will return B<DB_NOTFOUND>.=item B<DB_NEXT_DUP>This flag is only valid when duplicate keys have been enabled ina database.If the cursor is already pointing to a key/value pair and the key ofthe next key/value pair is identical, the cursor will be incremented topoint to it and their contents returned.=item B<DB_PREV>If the cursor is already pointing to a key/value pair, it will bedecremented to point to the previous key/value pair and return itscontents.If the cursor isn't initialised, B<DB_PREV> works just like B<DB_LAST>.If the cursor is already positioned at the first key/value pair, B<c_get>will return B<DB_NOTFOUND>.=item B<DB_CURRENT>If the cursor has been set to point to a key/value pair, return theircontents.If the key/value pair referenced by the cursor has been deleted, B<c_get>will return B<DB_KEYEMPTY>.=item B<DB_SET>Set the cursor to point to the key/value pair referenced by B<$key>and return the value in B<$value>.=item B<DB_SET_RANGE>This flag is a variation on the B<DB_SET> flag. As well as returningthe value, it also returns the key, via B<$key>.When used with a B<BerkeleyDB::Btree> database the key matched by B<c_get>will be the shortest key (in length) which is greater than or equal tothe key supplied, via B<$key>. This allows partial key searches.See ??? for an example of how to use this flag.=item B<DB_GET_BOTH>Another variation on B<DB_SET>. This one returns both the key andthe value.=item B<DB_SET_RECNO>TODO.=item B<DB_GET_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 = $cursor->c_put($key, $value, $flags)Stores the key/value pair in the database. The position that the data isstored in the database is controlled by the C<$flags> parameter, whichmust take B<one> of the following values:=over 5=item B<DB_AFTER>When used with a Btree or Hash database, a duplicate of the key referencedby the current cursor position will be created and the contents ofB<$value> will be associated with it - B<$key> is ignored.The new key/value pair will be stored immediately after the currentcursor position.Obviously the database has to have been opened with B<DB_DUP>.When used with a Recno ... TODO=item B<DB_BEFORE>When used with a Btree or Hash database, a duplicate of the key referencedby the current cursor position will be created and the contents ofB<$value> will be associated with it - B<$key> is ignored.The new key/value pair will be stored immediately before the currentcursor position.Obviously the database has to have been opened with B<DB_DUP>.When used with a Recno ... TODO=item B<DB_CURRENT>If the cursor has been initialised, replace the value of the key/valuepair stored in the database with the contents of B<$value>.=item B<DB_KEYFIRST>Only valid with a Btree or Hash database. This flag is only reallyused when duplicates are enabled in the database and sorted duplicateshaven't been specified.In this case the key/value pair will be inserted as the first entry inthe duplicates for the particular key.=item B<DB_KEYLAST>Only valid with a Btree or Hash database. This flag is only reallyused when duplicates are enabled in the database and sorted duplicateshaven't been specified.In this case the key/value pair will be inserted as the last entry inthe duplicates for the particular key.=back=head2 $status = $cursor->c_del([$flags])This method deletes the key/value pair associated with the current cursorposition. The cursor position will not be changed by this operation, soany subsequent cursor operation must first initialise the cursor topoint to a valid key/value pair.If the key/value pair associated with the cursor have already beendeleted, B<c_del> will return B<DB_KEYEMPTY>.The B<$flags> parameter is not used at present.=head2 $status = $cursor->c_count($cnt [, $flags])Stores the number of duplicates at the current cursor position in B<$cnt>.The B<$flags> parameter is not used at present. This method needs Berkeley DB 3.1 or better.=head2 $status = $cursor->status()Returns the status of the last cursor method as a dual type.=head2 $status = $cursor->c_pget() ;TODO=head2 $status = $cursor->c_close()Closes the cursor B<$cursor>.=head2 Cursor ExamplesTODOIterating from first to last, then in reverse.examples of each of the flags.=head1 JOINJoin support for BerkeleyDB is in progress. Watch this space.TODO=head1 TRANSACTIONSTODO.=head1 CDS ModeThe Berkeley Db Concurrent Data Store is a lightweight locking mechanismthat is useful in scenarios where transactions are overkill. See theaccompanying document .. for details of using this module in CDS mode.=head1 DBM FiltersA DBM Filter is a piece of code that is be used when you I<always>want to make the same transformation to all keys and/or values in a DBMdatabase. All of the database classes (BerkeleyDB::Hash,BerkeleyDB::Btree and BerkeleyDB::Recno) support DBM Filters.There are four methods associated with DBM Filters. All workidentically, and each is used to install (or uninstall) a single DBMFilter. Each expects a single parameter, namely a reference to a sub.The only difference between them is the place that the filter isinstalled.To summarise:=over 5=item B<filter_store_key>If a filter has been installed with this method, it will be invokedevery time you write a key to a DBM database.=item B<filter_store_value>If a filter has been installed with this method, it will be invokedevery time you write a value to a DBM database.=item B<filter_fetch_key>If a filter has been installed with this method, it will be invokedevery time you read a key from a DBM database.=item B<filter_fetch_value>If a filter has been installed with this method, it will be invokedevery time you read a value from a DBM database.=backYou can use any combination of the methods, from none, to all four.All filter methods return the existing filter, if present, or C<undef>in not.To delete a filter pass C<undef> to it.=head2 The FilterWhen each filter is called by Perl, a local copy of C<$_> will containthe key or value to be filtered. Filtering is achieved by modifyingthe contents of C<$_>. The return code from the filter is ignored.=head2 An Example -- the NULL termination problem.Consider the following scenario. You have a DBM database that you needto share with a third-party C application. The C application assumesthat I<all> keys and values are NULL terminated. Unfortunately whenPerl writes to DBM databases it doesn't use NULL termination, so yourPerl application will have to manage NULL termination itself. When youwrite to the database you will have to use something like this: $hash{"$key\0"} = "$value\0" ;Similarly the NULL needs to be taken into account when you are consideringthe length of existing keys/values.It would be much better if you could ignore the NULL terminations issuein the main application code and have a mechanism that automaticallyadded the terminating NULL to all keys and values whenever you write tothe database and have them removed when you read from the database. As I'msure you have already guessed, this is a problem that DBM Filters canfix very easily. use strict ; use BerkeleyDB ; my %hash ; my $filename = "filt.db" ; unlink $filename ; my $db = tie %hash, 'BerkeleyDB::Hash', -Filename => $filename, -Flags => DB_CREATE or die "Cannot open $filename: $!\n" ; # Install DBM Filters $db->filter_fetch_key ( sub { s/\0$// } ) ; $db->filter_store_key ( sub { $_ .= "\0" } ) ; $db->filter_fetch_value( sub { s/\0$// } ) ; $db->filter_store_value( sub { $_ .= "\0" } ) ; $hash{"abc"} = "def" ; my $a = $hash{"ABC"} ; # ... undef $db ; untie %hash ;Hopefully the contents of each of the filters should beself-explanatory. Both "fetch" filters remove the terminating NULL,and both "store" filters add a terminating NULL.=head2 Another Example -- Key is a C int.Here is another real-life example. By default, whenever Perl writes toa DBM database it always writes the key and value as strings. So whenyou use this: $hash{12345} = "something" ;the key 12345 will get stored in the DBM database as the 5 byte string"12345". If you actually want the key to be stored in the DBM databaseas a C int, you will have to use C<pack> when writing, and C<unpack>when reading.Here is a DBM Filter that does it: use strict ; use BerkeleyDB ; my %hash ; my $filename = "filt.db" ; unlink $filename ; my $db = tie %hash, 'BerkeleyDB::Btree', -Filename => $filename, -Flags => DB_CREATE or die "Cannot open $filename: $!\n" ; $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ) ; $db->filter_store_key ( sub { $_ = pack ("i", $_) } ) ; $hash{123} = "def" ; # ... undef $db ; untie %hash ;This time only two filters have been used -- we only need to manipulatethe contents of the key, so it wasn't necessary to install any valuefilters.=head1 Using BerkeleyDB with MLDBMBoth BerkeleyDB::Hash and BerkeleyDB::Btree can be used with the MLDBMmodule. The code fragment below shows how to open associate MLDBM withBerkeleyDB::Btree. To use BerkeleyDB::Hash just replaceBerkeleyDB::Btree with BerkeleyDB::Hash. use strict ; use BerkeleyDB ; use MLDBM qw(BerkeleyDB::Btree) ; use Data::Dumper; my $filename = 'testmldbm' ; my %o ; unlink $filename ; tie %o, 'MLDBM', -Filename => $filename, -Flags => DB_CREATE or die "Cannot open database '$filename: $!\n"; See the MLDBM documentation for information on how to use the moduleand for details of its limitations.=head1 EXAMPLESTODO.=head1 HINTS & TIPS=head2 Sharing Databases With C ApplicationsThere is no technical reason why a Berkeley DB database cannot beshared by both a Perl and a C application.The vast majority of problems that are reported in this area boil downto the fact that C strings are NULL terminated, whilst Perl stringsare not. See L<An Example -- the NULL termination problem.> in the DBMFILTERS section for a generic way to work around this problem.=head2 The untie GotchaTODO=head1 COMMON QUESTIONSThis section attempts to answer some of the more common questions thatI get asked.=head2 Relationship with DB_FileBefore Berkeley DB 2.x was written there was only one Perl module thatinterfaced to Berkeley DB. That module is called B<DB_File>. AlthoughB<DB_File> can be build with Berkeley DB 1.x, 2.x, 3.x or 4.x, it onlyprovides an interface to the functionality available in Berkeley DB1.x. That means that it doesn't support transactions, locking or any ofthe other new features available in DB 2.x or better.=head2 How do I store Perl data structures with BerkeleyDB?See L<Using BerkeleyDB with MLDBM>.=head1 HISTORYSee the Changes file.=head1 AVAILABILITYThe most recent version of B<BerkeleyDB> can always be foundon CPAN (see L<perlmod/CPAN> for details), in the directoryF<modules/by-module/BerkeleyDB>.The official web site for Berkeley DB is F<http://www.sleepycat.com>.=head1 COPYRIGHTCopyright (c) 1997-2004 Paul Marquess. All rights reserved. This programis free software; you can redistribute it and/or modify it under thesame terms as Perl itself.Although B<BerkeleyDB> is covered by the Perl license, the library itmakes use of, namely Berkeley DB, is not. Berkeley DB has its owncopyright and its own license. Please take the time to read it.Here are few words taken from the Berkeley DB FAQ (atF<http://www.sleepycat.com>) regarding the license: Do I have to license DB to use it in Perl scripts? No. The Berkeley DB license requires that software that uses Berkeley DB be freely redistributable. In the case of Perl, that software is Perl, and not your scripts. Any Perl scripts that you write are your property, including scripts that make use of Berkeley DB. Neither the Perl license nor the Berkeley DB license place any restriction on what you may do with them.If you are in any doubt about the license situation, contact either theBerkeley DB authors or the author of BerkeleyDB.See L<"AUTHOR"> for details.=head1 AUTHORPaul Marquess E<lt>pmqs@cpan.orgE<gt>.Questions about Berkeley DB may be addressed to E<lt>db@sleepycat.comE<gt>.=head1 SEE ALSOperl(1), DB_File, Berkeley DB.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -