📄 gdbm.texinfo
字号:
@node Delete, Sequential, Fetch, Top@chapter Removing records from the database.To remove some data from the database:@exampleret = gdbm_delete(dbf, key);@end exampleThe parameters are:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@item datum keyThe @code{key} data.@end tableThe ret value is -1 if the item is not present or the requester is a reader.The ret value is 0 if there was a successful delete.@code{gdbm_delete} removes the keyed item and the @code{key} from the database@code{dbf}. The file on disk is updated to reflect the structure of the newdatabase before returning from this function.@node Sequential, Reorganization, Delete, Top@chapter Sequential access to records.The next two functions allow for accessing all items in the database. Thisaccess is not @code{key} sequential, but it is guaranteed to visit every@code{key} in the database once. The order has to do with the hash values.@code{gdbm_firstkey} starts the visit of all keys in the database.@code{gdbm_nextkey} finds and reads the next entry in the hash structure for@code{dbf}.@examplekey = gdbm_firstkey(dbf);nextkey = gdbm_nextkey(dbf, key);@end exampleThe parameters are:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@item datum @code{key}@item datum nextkeyThe @code{key} data.@end tableThe return values are both datum. If @code{key}.dptr or nextkey.dptr is NULL,there is no first @code{key} or next @code{key}. Again notice that dptr points todata allocated by malloc and @code{gdbm} will not free it for you.These functions were intended to visit the database in read-only algorithms,for instance, to validate the database or similar operations.File @code{visiting} is based on a @code{hash table}. @code{gdbm_delete}re-arranges the hash table to make sure that any collisions in the table do notleave some item @code{un-findable}. The original key order is NOT guaranteed toremain unchanged in ALL instances. It is possible that some key will not bevisited if a loop like the following is executed:@example@group key = gdbm_firstkey ( dbf ); while ( key.dptr ) @{ nextkey = gdbm_nextkey ( dbf, key ); if ( some condition ) @{ gdbm_delete ( dbf, key ); free ( key.dptr ); @} key = nextkey; @}@end group@end example@node Reorganization, Sync, Sequential, Top@chapter Database reorganization.The following function should be used very seldom.@exampleret = gdbm_reorganize(dbf);@end exampleThe parameter is:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@end tableIf you have had a lot of deletions and would like to shrink the spaceused by the @code{gdbm} file, this function will reorganize the database.@code{gdbm} will not shorten the length of a @code{gdbm} file (deleted file space will bereused) except by using this reorganization.This reorganization requires creating a new file and inserting all the elementsin the old file @code{dbf} into the new file. The new file is then renamed tothe same name as the old file and @code{dbf} is updated to contain all thecorrect information about the new file. If an error is detected, the returnvalue is negative. The value zero is returned after a successfulreorganization.@node Sync, Errors, Reorganization, Top@chapter Database SynchronizationUnless your database was opened with the GDBM_SYNC flag, @code{gdbm} does notwait for writes to be flushed to the disk before continuing. This allowsfaster writing of databases at the risk of having a corrupted database ifthe application terminates in an abnormal fashion. The following functionallows the programmer to make sure the disk version of the database has been completely updated with all changes to the current time.@examplegdbm_sync(dbf);@end exampleThe parameter is:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@end tableThis would usually be called after a complete set of changes have beenmade to the database and before some long waiting time.@code{gdbm_close} automatically calls the equivalent of @code{gdbm_sync}so no call is needed if the database is to be closed immediately afterthe set of changes have been made.@node Errors, Options, Sync, Top@chapter Error strings.To convert a @code{gdbm} error code into English text, use this routine:@exampleret = gdbm_strerror(errno)@end exampleThe parameter is:@table @asis@item gdbm_error errnoThe @code{gdbm} error code, usually @code{gdbm_errno}.@end tableThe appropiate phrase for reading by humans is returned.@node Options, Locking, Errors, top@chapter Seting options.@code{Gdbm} supports the ability to set certain options on an alreadyopen database.@exampleret = gdbm_setopt(dbf, option, value, size);@end exampleThe parameters are:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@item int optionThe option to be set.@item int *valueA pointer to the value to which @code{option} will be set.@item int sizeThe length of the data pointed to by @code{value}.@end tableThe valid options are: GDBM_CACHESIZE - Set the size of the internal bucket cache. This option may only be set once on each GDBM_FILE descriptor, and is set automatically to 100 upon the first access to the database. GDBM_FASTMODE - Set fast mode to either on or off. This allows fast mode to be toggled on an already open and active database. value (see below) should be set to either TRUE or FALSE. @emph{This option is now obsolete.} GDBM_SYNCMODE - Turn on or off file system synchronization operations. This setting defaults to off; value (see below) should be set to either TRUE or FALSE. GDBM_CENTFREE - Set central free block pool to either on or off. The default is off, which is how previous versions of @code{Gdbm} handled free blocks. If set, this option causes all subsequent free blocks to be placed in the @emph{global} pool, allowing (in theory) more file space to be reused more quickly. value (see below) should be set to either TRUE or FALSE. @emph{NOTICE: This feature is still under study.} GDBM_COALESCEBLKS - Set free block merging to either on or off. The default is off, which is how previous versions of @code{Gdbm} handled free blocks. If set, this option causes adjacent free blocks to be merged. This can become a CPU expensive process with time, though, especially if used in conjunction with GDBM_CENTFREE. value (see below) should be set to either TRUE or FALSE. @emph{NOTICE: This feature is still under study.}The return value will be -1 upon failure, or 0 upon success. The globalvariable @code{gdbm_errno} will be set upon failure.For instance, to set a database to use a cache of 10, after opening itwith @code{gdbm_open}, but prior to accessing it in any way, the followingcode could be used:@exampleint value = 10;ret = gdbm_setopt(dbf, GDBM_CACHESIZE, &value, sizeof(int));@end example@node Locking, Variables, Options, Top@chapter File Locking.With locking disabled (if @code{gdbm_open} was called with GDBM_NOLOCK),the user may want to perform their own file locking on the database filein order to prevent multiple writers operating on the same filesimultaneously.In order to support this, the @code{gdbm_fdesc} routine is provided.@exampleret = gdbm_fdesc(dbf);@end exampleThe single valid parameter is:@table @asis@item GDBM_FILE dbfThe pointer returned by @code{gdbm_open}.@end tableThe return value will be the file descriptor of the database.@node Variables, Compatibility, Locking, Top@chapter Two useful variables.The following two variables are variables that may need to be used:@table @asis@item gdbm_error gdbm_errnoThe variable that contains more information about @code{gdbm} errors(@code{gdbm.h} has the definitions of the error values).@item char * gdbm_versionThe string containing the version information.@end table@node Compatibility, Conversion, Variables, Top@chapter Compatibility with standard @code{dbm} and @code{ndbm}.GNU @code{dbm} files are not @code{sparse}. You can copy them with the UNIX@code{cp} command and they will not expand in the copying process.There is a compatibility mode for use with programs that already use UNIX@code{dbm} and UNIX @code{ndbm}.GNU @code{dbm} has compatibility functions for @code{dbm}. For @code{dbm}compatibility functions, you need the include file @code{dbm.h}.In this compatibility mode, no @code{gdbm} file pointer is requiredby the user, and Only one file may be opened at a time. All users incompatibility mode are assumed to be writers. If the @code{gdbm} file is aread only, it will fail as a writer, but will also try to open it as a reader.All returned pointers in datum structures point to data that @code{gdbm} WILLfree. They should be treated as static pointers (as standard UNIX @code{dbm}does). The compatibility function names are the same as the UNIX @code{dbm}function names. Their definitions follow:@exampleint dbminit(name);int store(key, content);datum fetch(key);int delete(key);datum firstkey();datum nextkey(key);int dbmclose();@end exampleStandard UNIX @code{dbm} and GNU @code{dbm} do not have the same dataformat in the file. You cannot access a standard UNIX @code{dbm} file with GNU@code{dbm}! If you want to use an old database with GNU @code{dbm}, you mustuse the @code{conv2gdbm} program.Also, GNU @code{dbm} has compatibility functions for @code{ndbm}. For@code{ndbm} compatibility functions, you need the include file @code{ndbm.h}.Again, just like @code{ndbm}, any returned datum can be assumed to be staticstorage. You do not have to free that memory, the @code{ndbm} compatibilityfunctions will do it for you.The functions are:@exampleDBM *dbm_open(name, flags, mode);void dbm_close(file);datum dbm_fetch(file, key);int dbm_store(file, key, @code{content}, flags);int dbm_delete(file, key);datum dbm_firstkey(file);datum dbm_nextkey(file);int dbm_error(file);int dbm_clearerr(file);int dbm_dirfno(file);int dbm_pagfno(file);int dbm_rdonly(file);@end exampleIf you want to compile an old C program that used UNIX @code{dbm} or @code{ndbm}and want to use @code{gdbm} files, execute the following @code{cc} command:@examplecc ... -L/usr/local/lib -lgdbm -lgdbm_compat@end example@node Conversion, Bugs, Compatibility, Top@chapter Converting @code{dbm} files to @code{gdbm} format.The program @code{conv2gdbm} has been provided to help you convert from @code{dbm}databases to @code{gdbm}. The usage is:@exampleconv2gdbm [-q] [-b block_size] dbm_file [gdbm_file]@end exampleThe options are:@table @asis@item -qCauses @code{conv2gdbm} to work quietly.@item block_sizeIs the same as in @code{gdbm_open}.@item dbm_fileIs the name of the @code{dbm} file without the @code{.pag} or @code{.dir}extensions.@item gdbm_fileIs the complete file name. If not included, the @code{gdbm} file name is thesame as the @code{dbm} file name without any extensions. That is@code{conv2gdbm} @code{dbmfile} converts the files @code{dbmfile.pag} and@code{dbmfile.dir} into a @code{gdbm} file called @code{dbmfile}.@end table@node Bugs, , Conversion, Top@chapter Problems and bugs.If you have problems with GNU @code{dbm} or think you've found a bug,please report it. Before reporting a bug, make sure you've actuallyfound a real bug. Carefully reread the documentation and see if itreally says you can do what you're trying to do. If it's not clearwhether you should be able to do something or not, report that too; it'sa bug in the documentation!Before reporting a bug or trying to fix it yourself, try to isolate itto the smallest possible input file that reproduces the problem. Thensend us the input file and the exact results @code{gdbm} gave you. Alsosay what you expected to occur; this will help us decide whether theproblem was really in the documentation.Once you've got a precise problem, send e-mail to:@exampleInternet: @file{bug-gnu-utils@@prep.ai.mit.edu}.UUCP: @file{mit-eddie!prep.ai.mit.edu!bug-gnu-utils}.@end examplePlease include the version number of GNU @code{dbm} you are using. You can getthis information by printing the variable @code{gdbm_version} (see Variables).Non-bug suggestions are always welcome as well. If you have questionsabout things that are unclear in the documentation or are just obscurefeatures, please report them too.You may contact the author by:@example e-mail: phil@@cs.wwu.edu us-mail: Philip A. Nelson Computer Science Department Western Washington University Bellingham, WA 98226@end exampleYou may contact the current maintainer by:@example e-mail: downsj@@downsj.com@end example@bye
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -