.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "GDBM_File 3perl" .TH GDBM_File 3perl 2024-03-06 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME GDBM_File \- Perl5 access to the gdbm library. .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 4 \& use GDBM_File; \& [$db =] tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640 \& or die "$GDBM_File::gdbm_errno"; \& # Use the %hash... \& \& $e = $db\->errno; \& $e = $db\->syserrno; \& $str = $db\->strerror; \& $bool = $db\->needs_recovery; \& \& $db\->clear_error; \& \& $db\->reorganize; \& $db\->sync; \& \& $n = $db\->count; \& \& $n = $db\->flags; \& \& $str = $db\->dbname; \& \& $db\->cache_size; \& $db\->cache_size($newsize); \& \& $n = $db\->block_size; \& \& $bool = $db\->sync_mode; \& $db\->sync_mode($bool); \& \& $bool = $db\->centfree; \& $db\->centfree($bool); \& \& $bool = $db\->coalesce; \& $db\->coalesce($bool); \& \& $bool = $db\->mmap; \& \& $size = $db\->mmapsize; \& $db\->mmapsize($newsize); \& \& $db\->recover(%args); \& \& untie %hash ; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\fBGDBM_File\fR is a module which allows Perl programs to make use of the facilities provided by the GNU gdbm library. If you intend to use this module you should really have a copy of the \fBGDBM manual\fR at hand. The manual is avaialble online at . .PP Most of the \fBgdbm\fR functions are available through the \fBGDBM_File\fR interface. .PP Unlike Perl's built-in hashes, it is not safe to \f(CW\*(C`delete\*(C'\fR the current item from a GDBM_File tied hash while iterating over it with \f(CW\*(C`each\*(C'\fR. This is a limitation of the gdbm library. .SS Tie .IX Subsection "Tie" Use the Perl built-in \fBtie\fR to associate a \fBGDBM\fR database with a Perl hash: .PP .Vb 1 \& tie %hash, \*(AqGDBM_File\*(Aq, $filename, $flags, $mode; .Ve .PP Here, \fR\f(CI$filename\fR\fI\fR is the name of the database file to open or create. \&\fI\fR\f(CI$flags\fR\fI\fR is a bitwise OR of \fIaccess mode\fR and optional \fImodifiers\fR. Access mode is one of: .IP \fBGDBM_READER\fR 4 .IX Item "GDBM_READER" Open existing database file in read-only mode. .IP \fBGDBM_WRITER\fR 4 .IX Item "GDBM_WRITER" Open existing database file in read-write mode. .IP \fBGDBM_WRCREAT\fR 4 .IX Item "GDBM_WRCREAT" If the database file exists, open it in read-write mode. If it doesn't, create it first and open read-write. .IP \fBGDBM_NEWDB\fR 4 .IX Item "GDBM_NEWDB" Create new database and open it read-write. If the database already exists, truncate it first. .PP A number of modifiers can be OR'd to the access mode. Most of them are rarely needed (see for a complete list), but one is worth mentioning. The \fBGDBM_NUMSYNC\fR modifier, when used with \fBGDBM_NEWDB\fR, instructs \fBGDBM\fR to create the database in \fIextended\fR (so called \fInumsync\fR) format. This format is best suited for crash-tolerant implementations. See \fBCRASH TOLERANCE\fR below for more information. .PP The \fR\f(CI$mode\fR\fI\fR parameter is the file mode for creating new database file. Use an octal constant or a combination of \f(CW\*(C`S_I*\*(C'\fR constants from the \fBFcntl\fR module. This parameter is used if \fI\fR\f(CI$flags\fR\fI\fR is \&\fBGDBM_NEWDB\fR or \fBGDBM_WRCREAT\fR. .PP On success, \fBtie\fR returns an object of class \fBGDBM_File\fR. On failure, it returns \fBundef\fR. It is recommended to always check the return value, to make sure your hash is successfully associated with the database file. See \fBERROR HANDLING\fR below for examples. .SH "STATIC METHODS" .IX Header "STATIC METHODS" .SS GDBM_version .IX Subsection "GDBM_version" .Vb 2 \& $str = GDBM_File\->GDBM_version; \& @ar = GDBM_File\->GDBM_version; .Ve .PP Returns the version number of the underlying \fBlibgdbm\fR library. In scalar context, returns the library version formatted as string: .PP .Vb 1 \& MINOR.MAJOR[.PATCH][ (GUESS)] .Ve .PP where \fIMINOR\fR, \fIMAJOR\fR, and \fIPATCH\fR are version numbers, and \fIGUESS\fR is a guess level (see below). .PP In list context, returns a list: .PP .Vb 1 \& ( MINOR, MAJOR, PATCH [, GUESS] ) .Ve .PP The \fIGUESS\fR component is present only if \fBlibgdbm\fR version is 1.8.3 or earlier. This is because earlier releases of \fBlibgdbm\fR did not include information about their version and the \fBGDBM_File\fR module has to implement certain guesswork in order to determine it. \fIGUESS\fR is a textual description in string context, and a positive number indicating how rough the guess is in list context. Possible values are: .IP "1 \- exact guess" 4 .IX Item "1 - exact guess" The major and minor version numbers are guaranteed to be correct. The actual patchlevel is most probably guessed right, but can be 1\-2 less than indicated. .IP "2 \- approximate" 4 .IX Item "2 - approximate" The major and minor number are guaranteed to be correct. The patchlevel is set to the upper bound. .IP "3 \- rough guess" 4 .IX Item "3 - rough guess" The version is guaranteed to be not newer than \fR\f(BIMAJOR\fR\fB.\fR\f(BIMINOR\fR\fB\fR. .SH "ERROR HANDLING" .IX Header "ERROR HANDLING" .ie n .SS $GDBM_File::gdbm_errno .el .SS \f(CW$GDBM_File::gdbm_errno\fP .IX Subsection "$GDBM_File::gdbm_errno" When referenced in numeric context, retrieves the current value of the \&\fBgdbm_errno\fR variable, i.e. a numeric code describing the state of the most recent operation on any \fBgdbm\fR database. Each numeric code has a symbolic name associated with it. For a comprehensive list of these, see . Notice, that this list includes all error codes defined for the most recent version of \fBgdbm\fR. Depending on the actual version of the library \&\fBGDBM_File\fR is built with, some of these may be missing. .PP In string context, \fR\f(CB$gdbm_errno\fR\fB\fR returns a human-readable description of the error. If necessary, this description includes the value of \fB$!\fR. This makes it possible to use it in diagnostic messages. For example, the usual tying sequence is .PP .Vb 2 \& tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640 \& or die "$GDBM_File::gdbm_errno"; .Ve .PP The following, more complex, example illustrates how you can fall back to read-only mode if the database file permissions forbid read-write access: .PP .Vb 11 \& use Errno qw(EACCES); \& unless (tie(%hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640)) { \& if ($GDBM_File::gdbm_errno == GDBM_FILE_OPEN_ERROR \& && $!{EACCES}) { \& if (tie(%hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_READER, 0640)) { \& die "$GDBM_File::gdbm_errno"; \& } \& } else { \& die "$GDBM_File::gdbm_errno"; \& } \& } .Ve .SS gdbm_check_syserr .IX Subsection "gdbm_check_syserr" .Vb 1 \& if (gdbm_check_syserr(gdbm_errno)) ... .Ve .PP Returns true if the system error number (\fB$!\fR) gives more information on the cause of the error. .SH "DATABASE METHODS" .IX Header "DATABASE METHODS" .SS close .IX Subsection "close" .Vb 1 \& $db\->close; .Ve .PP Closes the database. Normally you would just do \fBuntie\fR. However, you will need to use this function if you have explicitly assigned the result of \fBtie\fR to a variable, and wish to release the database to another users. Consider the following code: .PP .Vb 4 \& $db = tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640; \& # Do something with %hash or $db... \& untie %hash; \& $db\->close; .Ve .PP In this example, doing \fBuntie\fR alone is not enough, since the database would remain referenced by \fR\f(CB$db\fR\fB\fR, and, as a consequence, the database file would remain locked. Calling \fB\fR\f(CB$db\fR\fB\->close\fR ensures the database file is closed and unlocked. .SS errno .IX Subsection "errno" .Vb 1 \& $db\->errno .Ve .PP Returns the last error status associated with this database. In string context, returns a human-readable description of the error. See also \&\fR\f(CB$GDBM_File::gdbm_errno\fR\fB\fR variable above. .SS syserrno .IX Subsection "syserrno" .Vb 1 \& $db\->syserrno .Ve .PP Returns the last system error status (C \f(CW\*(C`errno\*(C'\fR variable), associated with this database, .SS strerror .IX Subsection "strerror" .Vb 1 \& $db\->strerror .Ve .PP Returns textual description of the last error that occurred in this database. .SS clear_error .IX Subsection "clear_error" .Vb 1 \& $db\->clear_error .Ve .PP Clear error status. .SS needs_recovery .IX Subsection "needs_recovery" .Vb 1 \& $db\->needs_recovery .Ve .PP Returns true if the database needs recovery. .SS reorganize .IX Subsection "reorganize" .Vb 1 \& $db\->reorganize; .Ve .PP Reorganizes the database. .SS sync .IX Subsection "sync" .Vb 1 \& $db\->sync; .Ve .PP Synchronizes recent changes to the database with its disk copy. .SS count .IX Subsection "count" .Vb 1 \& $n = $db\->count; .Ve .PP Returns number of keys in the database. .SS flags .IX Subsection "flags" .Vb 1 \& $db\->flags; .Ve .PP Returns flags passed as 4th argument to \fBtie\fR. .SS dbname .IX Subsection "dbname" .Vb 1 \& $db\->dbname; .Ve .PP Returns the database name (i.e. 3rd argument to \fBtie\fR. .SS cache_size .IX Subsection "cache_size" .Vb 2 \& $db\->cache_size; \& $db\->cache_size($newsize); .Ve .PP Returns the size of the internal \fBGDBM\fR cache for that database. .PP Called with argument, sets the size to \fR\f(CI$newsize\fR\fI\fR. .SS block_size .IX Subsection "block_size" .Vb 1 \& $db\->block_size; .Ve .PP Returns the block size of the database. .SS sync_mode .IX Subsection "sync_mode" .Vb 2 \& $db\->sync_mode; \& $db\->sync_mode($bool); .Ve .PP Returns the status of the automatic synchronization mode. Called with argument, enables or disables the sync mode, depending on whether \f(CW$bool\fR is \fBtrue\fR or \&\fBfalse\fR. .PP When synchronization mode is on (\fBtrue\fR), any changes to the database are immediately written to the disk. This ensures database consistency in case of any unforeseen errors (e.g. power failures), at the expense of considerable slowdown of operation. .PP Synchronization mode is off by default. .SS centfree .IX Subsection "centfree" .Vb 2 \& $db\->centfree; \& $db\->centfree($bool); .Ve .PP Returns status of the central free block pool (\fB0\fR \- disabled, \&\fB1\fR \- enabled). .PP With argument, changes its status. .PP By default, central free block pool is disabled. .SS coalesce .IX Subsection "coalesce" .Vb 2 \& $db\->coalesce; \& $db\->coalesce($bool); .Ve .SS mmap .IX Subsection "mmap" .Vb 1 \& $db\->mmap; .Ve .PP Returns true if memory mapping is enabled. .PP This method will \fBcroak\fR if the \fBlibgdbm\fR library is complied without memory mapping support. .SS mmapsize .IX Subsection "mmapsize" .Vb 2 \& $db\->mmapsize; \& $db\->mmapsize($newsize); .Ve .PP If memory mapping is enabled, returns the size of memory mapping. With argument, sets the size to \fR\f(CB$newsize\fR\fB\fR. .PP This method will \fBcroak\fR if the \fBlibgdbm\fR library is complied without memory mapping support. .SS recover .IX Subsection "recover" .Vb 1 \& $db\->recover(%args); .Ve .PP Recovers data from a failed database. \fR\f(CB%args\fR\fB\fR is optional and can contain following keys: .IP "err => sub { ... }" 4 .IX Item "err => sub { ... }" Reference to code for detailed error reporting. Upon encountering an error, \&\fBrecover\fR will call this sub with a single argument \- a description of the error. .IP "backup => \e$str" 4 .IX Item "backup => $str" Creates a backup copy of the database before recovery and returns its filename in \fR\f(CB$str\fR\fB\fR. .ie n .IP "max_failed_keys => $n" 4 .el .IP "max_failed_keys => \f(CW$n\fR" 4 .IX Item "max_failed_keys => $n" Maximum allowed number of failed keys. If the actual number becomes equal to \fR\f(CI$n\fR\fI\fR, \fBrecover\fR aborts and returns error. .ie n .IP "max_failed_buckets => $n" 4 .el .IP "max_failed_buckets => \f(CW$n\fR" 4 .IX Item "max_failed_buckets => $n" Maximum allowed number of failed buckets. If the actual number becomes equal to \fR\f(CI$n\fR\fI\fR, \fBrecover\fR aborts and returns error. .ie n .IP "max_failures => $n" 4 .el .IP "max_failures => \f(CW$n\fR" 4 .IX Item "max_failures => $n" Maximum allowed number of failures during recovery. .IP "stat => \e%hash" 4 .IX Item "stat => %hash" Return recovery statistics in \fR\f(CI%hash\fR\fI\fR. Upon return, the following keys will be present: .RS 4 .IP recovered_keys 8 .IX Item "recovered_keys" Number of successfully recovered keys. .IP recovered_buckets 8 .IX Item "recovered_buckets" Number of successfully recovered buckets. .IP failed_keys 8 .IX Item "failed_keys" Number of keys that failed to be retrieved. .IP failed_buckets 8 .IX Item "failed_buckets" Number of buckets that failed to be retrieved. .RE .RS 4 .RE .SS convert .IX Subsection "convert" .Vb 1 \& $db\->convert($format); .Ve .PP Changes the format of the database file referred to by \fR\f(CB$db\fR\fB\fR. .PP Starting from version 1.20, \fBgdbm\fR supports two database file formats: \&\fIstandard\fR and \fIextended\fR. The former is the traditional database format, used by previous \fBgdbm\fR versions. The \fIextended\fR format contains additional data and is recommended for use in crash tolerant applications. .PP , for the discussion of both formats. .PP The \fR\f(CB$format\fR\fB\fR argument sets the new desired database format. It is \&\fBGDBM_NUMSYNC\fR to convert the database from standard to extended format, and \&\fB0\fR to convert it from extended to standard format. .PP If the database is already in the requested format, the function returns success without doing anything. .SS dump .IX Subsection "dump" .Vb 1 \& $db\->dump($filename, %options) .Ve .PP Creates a dump of the database file in \fR\f(CI$filename\fR\fI\fR. Such file can be used as a backup copy or sent over a wire to recreate the database on another machine. To create a database from the dump file, use the \fBload\fR method. .PP \&\fBGDBM\fR supports two dump formats: old \fIbinary\fR and new \fIascii\fR. The binary format is not portable across architectures and is deprecated. It is supported for backward compatibility. The ascii format is portable and stores additional meta-data about the file. It was introduced with the \&\fBgdbm\fR version 1.11 and is the preferred dump format. The \fBdump\fR method creates ascii dumps by default. .PP If the named file already exists, the function will refuse to overwrite and will croak an error. If it doesn't exist, it will be created with the mode \fB0666\fR modified by the current \fBumask\fR. .PP These defaults can be altered using the following \fR\f(CI%options\fR\fI\fR: .IP "\fBbinary\fR => 1" 4 .IX Item "binary => 1" Create dump in \fIbinary\fR format. .IP "\fBmode\fR => \fIMODE\fR" 4 .IX Item "mode => MODE" Set file mode to \fIMODE\fR. .IP "\fBoverwrite\fR => 1" 4 .IX Item "overwrite => 1" Silently overwrite existing files. .SS load .IX Subsection "load" .Vb 1 \& $db\->load($filename, %options) .Ve .PP Load the data from the dump file \fR\f(CI$filename\fR\fI\fR into the database \fI\fR\f(CI$db\fR\fI\fR. The file must have been previously created using the \fBdump\fR method. File format is recognized automatically. By default, the function will croak if the dump contains a key that already exists in the database. It will silently ignore the failure to restore database mode and/or ownership. These defaults can be altered using the following \fI\fR\f(CI%options\fR\fI\fR: .IP "\fBreplace\fR => 1" 4 .IX Item "replace => 1" Replace existing keys. .IP "\fBrestore_mode\fR => 0 | 1" 4 .IX Item "restore_mode => 0 | 1" If \fI0\fR, don't try to restore the mode of the database file to that stored in the dump. .IP "\fBrestore_owner\fR => 0 | 1" 4 .IX Item "restore_owner => 0 | 1" If \fI0\fR, don't try to restore the owner of the database file to that stored in the dump. .IP "\fBstrict_errors\fR => 1" 4 .IX Item "strict_errors => 1" Croak if failed to restore ownership and/or mode. .PP The usual sequence to recreate a database from the dump file is: .PP .Vb 3 \& my %hash; \& my $db = tie %hash, \*(AqGDBM_File\*(Aq, \*(Aqa.db\*(Aq, GDBM_NEWDB, 0640; \& $db\->load(\*(Aqa.dump\*(Aq); .Ve .SH "CRASH TOLERANCE" .IX Header "CRASH TOLERANCE" Crash tolerance is a new feature that, given appropriate support from the OS and the filesystem, guarantees that a logically consistent recent state of the database can be recovered following a crash, such as power outage, OS kernel panic, or the like. .PP Crash tolerance support appeared in \fBgdbm\fR version 1.21. The theory behind it is explained in "Crashproofing the Original NoSQL Key-Value Store", by Terence Kelly (). A detailed discussion of the \fBgdbm\fR implementation is available in the \&\fBGDBM Manual\fR (). The information below describes the Perl interface. .PP For maximum robustness, we recommend to use \fIextended database format\fR for crash tolerant databases. To create a database in extended format, use the \fBGDBM_NEWDB|GDBM_NUMSYNC\fR when opening the database, e.g.: .PP .Vb 2 \& $db = tie %hash, \*(AqGDBM_File\*(Aq, $filename, \& GDBM_NEWDB|GDBM_NUMSYNC, 0640; .Ve .PP To convert existing database to the extended format, use the \fBconvert\fR method, described above, e.g.: .PP .Vb 1 \& $db\->convert(GDBM_NUMSYNC); .Ve .SS crash_tolerance_status .IX Subsection "crash_tolerance_status" .Vb 1 \& GDBM_File\->crash_tolerance_status; .Ve .PP This static method returns the status of crash tolerance support. A non-zero value means crash tolerance is compiled in and supported by the operating system. .SS failure_atomic .IX Subsection "failure_atomic" .Vb 1 \& $db\->failure_atomic($even, $odd) .Ve .PP Enables crash tolerance for the database \fR\f(CB$db\fR\fB\fR, Arguments are the pathnames of two files that will be created and filled with \&\fIsnapshots\fR of the database file. The two files must not exist when this method is called and must reside on the same filesystem as the database file. This filesystem must be support the \fIreflink\fR operation (https://www.gnu.org.ua/software/gdbm/manual/Filesystems\-supporting\-crash\-tolerance.html>. .PP After a successful call to \fBfailure_atomic\fR, every call to \fR\f(CB$db\fR\fB\-\fRsync> method will make an efficient reflink snapshot of the database file in one of these files; consecutive calls to \fBsync\fR alternate between the two, hence the names. .PP The most recent of these files can be used to recover the database after a crash. To select the right snapshot, use the \fBlatest_snapshot\fR static method. .SS latest_snapshot .IX Subsection "latest_snapshot" .Vb 1 \& $file = GDBM_File\->latest_snapshot($even, $odd); \& \& ($file, $error) = GDBM_File\->latest_snapshot($even, $odd); .Ve .PP Given the two snapshot names (the ones used previously in a call to \&\fBfailure_atomic\fR), this method selects the one suitable for database recovery, i.e. the file which contains the most recent database snapshot. .PP In scalar context, it returns the selected file name or \fBundef\fR in case of failure. .PP In array context, the returns a list of two elements: the file name and status code. On success, the file name is defined and the code is \fBGDBM_SNAPSHOT_OK\fR. On error, the file name is \fBundef\fR, and the status is one of the following: .IP GDBM_SNAPSHOT_BAD 4 .IX Item "GDBM_SNAPSHOT_BAD" Neither snapshot file is applicable. This means that the crash has occurred before a call to \fBfailure_atomic\fR completed. In this case, it is best to fall back on a safe backup copy of the data file. .IP GDBM_SNAPSHOT_ERR 4 .IX Item "GDBM_SNAPSHOT_ERR" A system error occurred. Examine \fB$!\fR for details. See for a comprehensive list of error codes and their meaning. .IP GDBM_SNAPSHOT_SAME 4 .IX Item "GDBM_SNAPSHOT_SAME" The file modes and modification dates of both snapshot files are exactly the same. This can happen only for databases in standard format. .IP GDBM_SNAPSHOT_SUSPICIOUS 4 .IX Item "GDBM_SNAPSHOT_SUSPICIOUS" The \fInumsync\fR counters of the two snapshots differ by more than one. The most probable reason is programmer's error: the two parameters refer to snapshots belonging to different database files. .SH AVAILABILITY .IX Header "AVAILABILITY" gdbm is available from any GNU archive. The master site is \&\f(CW\*(C`ftp.gnu.org\*(C'\fR, but you are strongly urged to use one of the many mirrors. You can obtain a list of mirror sites from . .SH "SECURITY AND PORTABILITY" .IX Header "SECURITY AND PORTABILITY" GDBM files are not portable across platforms. If you wish to transfer a GDBM file over the wire, dump it to a portable format first. .PP \&\fBDo not accept GDBM files from untrusted sources.\fR .PP Robustness of GDBM against corrupted databases depends highly on its version. Versions prior to 1.15 did not implement any validity checking, so that a corrupted or maliciously crafted database file could cause perl to crash or even expose a security vulnerability. Versions between 1.15 and 1.20 were progressively strengthened against invalid inputs. Finally, version 1.21 had undergone extensive fuzzy checking which proved its ability to withstand any kinds of inputs without crashing. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBperl\fR\|(1), \fBDB_File\fR\|(3), perldbmfilter, \&\fBgdbm\fR\|(3), .