📄 db_file.pm
字号:
$x->seq($key, $value, R_CURSOR) ;Here is the relevant quote from the dbopen man page where it definesthe use of the R_CURSOR flag with seq: Note, for the DB_BTREE access method, the returned key is not necessarily an exact match for the specified key. The returned key is the smallest key greater than or equal to the specified key, permitting partial key matches and range searches.In the example script below, the C<match> sub uses this feature to findand print the first matching key/value pair given a partial key. use warnings ; use strict ; use DB_File ; use Fcntl ; use vars qw($filename $x %h $st $key $value) ; sub match { my $key = shift ; my $value = 0; my $orig_key = $key ; $x->seq($key, $value, R_CURSOR) ; print "$orig_key\t-> $key\t-> $value\n" ; } $filename = "tree" ; unlink $filename ; $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE or die "Cannot open $filename: $!\n"; # Add some key/value pairs to the file $h{'mouse'} = 'mickey' ; $h{'Wall'} = 'Larry' ; $h{'Walls'} = 'Brick' ; $h{'Smith'} = 'John' ; $key = $value = 0 ; print "IN ORDER\n" ; for ($st = $x->seq($key, $value, R_FIRST) ; $st == 0 ; $st = $x->seq($key, $value, R_NEXT) ) { print "$key -> $value\n" } print "\nPARTIAL MATCH\n" ; match "Wa" ; match "A" ; match "a" ; undef $x ; untie %h ;Here is the output: IN ORDER Smith -> John Wall -> Larry Walls -> Brick mouse -> mickey PARTIAL MATCH Wa -> Wall -> Larry A -> Smith -> John a -> mouse -> mickey=head1 DB_RECNODB_RECNO provides an interface to flat text files. Both variable andfixed length records are supported.In order to make RECNO more compatible with Perl, the array offset forall RECNO arrays begins at 0 rather than 1 as in Berkeley DB.As with normal Perl arrays, a RECNO array can be accessed usingnegative indexes. The index -1 refers to the last element of the array,-2 the second last, and so on. Attempting to access an element beforethe start of the array will raise a fatal run-time error.=head2 The 'bval' OptionThe operation of the bval option warrants some discussion. Here is thedefinition of bval from the Berkeley DB 1.85 recno manual page: The delimiting byte to be used to mark the end of a record for variable-length records, and the pad charac- ter for fixed-length records. If no value is speci- fied, newlines (``\n'') are used to mark the end of variable-length records and fixed-length records are padded with spaces.The second sentence is wrong. In actual fact bval will only default toC<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULLopeninfo parameter is used at all, the value that happens to be in bvalwill be used. That means you always have to specify bval when makinguse of any of the options in the openinfo parameter. This documentationerror will be fixed in the next release of Berkeley DB.That clarifies the situation with regards Berkeley DB itself. Whatabout B<DB_File>? Well, the behavior defined in the quote above isquite useful, so B<DB_File> conforms to it.That means that you can specify other options (e.g. cachesize) andstill have bval default to C<"\n"> for variable length records, andspace for fixed length records.=head2 A Simple 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 warnings ; use strict ; use DB_File ; my $filename = "text" ; unlink $filename ; my @h ; tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO or die "Cannot open file 'text': $!\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] ; # use a negative index print "The last element is $h[-1]\n" ; print "The 2nd last element is $h[-2]\n" ; 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=head2 Extra RECNO MethodsIf you are using a version of Perl earlier than 5.004_57, the tiedarray interface is quite limited. In the example script aboveC<push>, C<pop>, C<shift>, C<unshift>or determining the array length will not work with a tied array.To make the interface more useful for older versions of Perl, a numberof methods are supplied with B<DB_File> to simulate the missing arrayoperations. All these methods are accessed via the object returned fromthe tie call.Here are the methods:=over 5=item B<$X-E<gt>push(list) ;>Pushes the elements of C<list> to the end of the array.=item B<$value = $X-E<gt>pop ;>Removes and returns the last element of the array.=item B<$X-E<gt>shift>Removes and returns the first element of the array.=item B<$X-E<gt>unshift(list) ;>Pushes the elements of C<list> to the start of the array.=item B<$X-E<gt>length>Returns the number of elements in the array.=back=head2 Another ExampleHere is a more complete example that makes use of some of the methodsdescribed above. It also makes use of the API interface directly (see L<THE API INTERFACE>). use warnings ; use strict ; use vars qw(@h $H $file $i) ; use DB_File ; use Fcntl ; $file = "text" ; unlink $file ; $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO or die "Cannot open file $file: $!\n" ; # first create a text file to play with $h[0] = "zero" ; $h[1] = "one" ; $h[2] = "two" ; $h[3] = "three" ; $h[4] = "four" ; # Print the records in order. # # The length method is needed here because evaluating a tied # array in a scalar context does not return the number of # elements in the array. print "\nORIGINAL\n" ; foreach $i (0 .. $H->length - 1) { print "$i: $h[$i]\n" ; } # use the push & pop methods $a = $H->pop ; $H->push("last") ; print "\nThe last record was [$a]\n" ; # and the shift & unshift methods $a = $H->shift ; $H->unshift("first") ; print "The first record was [$a]\n" ; # Use the API to add a new record after record 2. $i = 2 ; $H->put($i, "Newbie", R_IAFTER) ; # and a new record before record 1. $i = 1 ; $H->put($i, "New One", R_IBEFORE) ; # delete record 3 $H->del(3) ; # now print the records in reverse order print "\nREVERSE\n" ; for ($i = $H->length - 1 ; $i >= 0 ; -- $i) { print "$i: $h[$i]\n" } # same again, but use the API functions instead print "\nREVERSE again\n" ; my ($s, $k, $v) = (0, 0, 0) ; for ($s = $H->seq($k, $v, R_LAST) ; $s == 0 ; $s = $H->seq($k, $v, R_PREV)) { print "$k: $v\n" } undef $H ; untie @h ;and this is what it outputs: ORIGINAL 0: zero 1: one 2: two 3: three 4: four The last record was [four] The first record was [zero] REVERSE 5: last 4: three 3: Newbie 2: one 1: New One 0: first REVERSE again 5: last 4: three 3: Newbie 2: one 1: New One 0: firstNotes:=over 5=item 1.Rather than iterating through the array, C<@h> like this: foreach $i (@h)it is necessary to use either this: foreach $i (0 .. $H->length - 1) or this: for ($a = $H->get($k, $v, R_FIRST) ; $a == 0 ; $a = $H->get($k, $v, R_NEXT) )=item 2.Notice that both times the C<put> method was used the record index wasspecified using a variable, C<$i>, rather than the literal valueitself. This is because C<put> will return the record number of theinserted line via that parameter.=back=head1 THE API INTERFACEAs well as accessing Berkeley DB using a tied hash or array, it is alsopossible to make direct use of most of the API functions defined in theBerkeley DB documentation.To do this you need to store a copy of the object returned from the tie. $db = tie %hash, "DB_File", "filename" ;Once you have done that, you can access the Berkeley DB API functionsas B<DB_File> methods directly like this: $db->put($key, $value, R_NOOVERWRITE) ;B<Important:> If you have saved a copy of the object returned fromC<tie>, the underlying database file will I<not> be closed until boththe tied variable is untied and all copies of the saved object aredestroyed. use DB_File ; $db = tie %hash, "DB_File", "filename" or die "Cannot tie filename: $!" ; ... undef $db ; untie %hash ;See L<The untie() Gotcha> for more details.All the functions defined in L<dbopen> are available except forclose() and dbopen() itself. The B<DB_File> method interface to thesupported functions have been implemented to mirror the way Berkeley DBworks whenever possible. In particular note that:=over 5=item *The methods return a status value. All return 0 on success.All return -1 to signify an error and set C<$!> to the exacterror code. The return code 1 generally (but not always) means that thekey specified did not exist in the database.Other return codes are defined. See below and in the Berkeley DBdocumentation for details. The Berkeley DB documentation should be usedas the definitive source.=item *Whenever a Berkeley DB function returns data via one of its parameters,the equivalent B<DB_File> method does exactly the same.=item *If you are careful, it is possible to mix API calls with the tiedhash/array interface in the same piece of code. Although only a few ofthe methods used to implement the tied interface currently make use ofthe cursor, you should always assume that the cursor has been changedany time the tied hash/array interface is used. As an example, thiscode will probably not do what you expect: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; # this line will modify the cursor $count = scalar keys %x ; # Get the second key/value pair. # oops, it didn't, it got the last key/value pair! $X->seq($key, $value, R_NEXT) ;The code above can be rearranged to get around the problem, like this: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; # this line will modify the cursor $count = scalar keys %x ; # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; # Get the second key/value pair. # worked this time. $X->seq($key, $value, R_NEXT) ;=backAll the constants defined in L<dbopen> for use in the flags parametersin the methods defined below are also available. Refer to the BerkeleyDB documentation for the precise meaning of the flags values.Below is a list of the methods available.=over 5=item B<$status = $X-E<gt>get($key, $value [, $flags]) ;>Given a key (C<$key>) this method reads the value associated with itfrom the database. The value read from the database is returned in theC<$value> parameter.If the key does not exist the method returns 1.No flags are currently defined for this method.=item B<$status = $X-E<gt>put($key, $value [, $flags]) ;>Stores the key/value pair in the database.If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameterwill have the record number of the inserted key/value pair set.Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE andR_SETCURSOR.=item B<$status = $X-E<gt>del($key [, $flags]) ;>Removes all key/value pairs with key C<$key> from the database.A return code of 1 means that the requested key was not in thedatabase.R_CURSOR is the only valid flag at present.=item B<$status = $X-E<gt>fd ;>Returns the file descriptor for the underlying database.See L<Locking: The Trouble with fd> for an explanation for why you shouldnot use C<fd> to lock your database.=item B<$status = $X-E<gt>seq($key, $value, $flags) ;>This interface allows sequential retrieval from the database. SeeL<dbopen> for full details.Both the C<$key> and C<$value> parameters will be set to the key/valuepair read from the database.The flags parameter is mandatory. The valid flag values are R_CURSOR,R_FIRST, R_LAST, R_NEXT and R_PREV.=item B<$status = $X-E<gt>sync([$flags]) ;>Flushes any cached buffers to disk.R_RECNOSYNC is the only valid flag at present.=back=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 aDBM database.There are four methods associated with DBM Filters. All work identically,and each is used to install (or uninstall) a single DBM Filter. Eachexpects a single parameter, namely a reference to a sub. The onlydifference between them is the place that the filter is installed.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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -