📄 dbopen.3
字号:
.\" Copyright (c) 1990, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)dbopen.3 8.5 (Berkeley) 1/2/94.\".TH DBOPEN 3 "January 2, 1994".UC 7.SH NAMEdbopen \- database access methods.SH SYNOPSIS.nf.ft B#include <sys/types.h>#include <limits.h>#include <db.h>DB *dbopen(const char *file, int flags, int mode, DBTYPE type,.ti +5const void *openinfo);.ft R.fi.SH DESCRIPTION.IR Dbopenis the library interface to database files.The supported file formats are btree, hashed and UNIX file oriented.The btree format is a representation of a sorted, balanced tree structure.The hashed format is an extensible, dynamic hashing scheme.The flat-file format is a byte stream file with fixed or variable lengthrecords.The formats and file format specific information are described in detailin their respective manual pages.IR btree (3),.IR hash (3)and.IR recno (3)..PPDbopen opens.I filefor reading and/or writing.Files never intended to be preserved on disk may be created by settingthe file parameter to NULL..PPThe.I flagsand.I mode argumentsare as specified to the.IR open (2)routine, however, only the O_CREAT, O_EXCL, O_EXLOCK, O_NONBLOCK,O_RDONLY, O_RDWR, O_SHLOCK and O_TRUNC flags are meaningful.(Note, opening a database file O_WRONLY is not possible.).\"Three additional options may be specified by.\".IR or 'ing.\"them into the.\".I flags.\"argument..\".TP.\"DB_LOCK.\"Do the necessary locking in the database to support concurrent access..\"If concurrent access isn't needed or the database is read-only this.\"flag should not be set, as it tends to have an associated performance.\"penalty..\".TP.\"DB_SHMEM.\"Place the underlying memory pool used by the database in shared.\"memory..\"Necessary for concurrent access..\".TP.\"DB_TXN.\"Support transactions in the database..\"The DB_LOCK and DB_SHMEM flags must be set as well..PPThe.I typeargument is of type DBTYPE (as defined in the <db.h> include file) andmay be set to DB_BTREE, DB_HASH or DB_RECNO..PPThe.I openinfoargument is a pointer to an access method specific structure describedin the access method's manual page.If.I openinfois NULL, each access method will use defaults appropriate for the systemand the access method..PP.I Dbopenreturns a pointer to a DB structure on success and NULL on error.The DB structure is defined in the <db.h> include file, and contains atleast the following fields:.sp.nftypedef struct {.RSDBTYPE type;int (*close)(const DB *db);int (*del)(const DB *db, const DBT *key, u_int flags);int (*fd)(const DB *db);int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);int (*put)(const DB *db, DBT *key, const DBT *data,.ti +5u_int flags);int (*sync)(const DB *db, u_int flags);int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);.RE} DB;.fi.PPThese elements describe a database type and a set of functions performingvarious actions.These functions take a pointer to a structure as returned by.IR dbopen ,and sometimes one or more pointers to key/data structures and a flag value..TPtypeThe type of the underlying access method (and file format)..TPcloseA pointer to a routine to flush any cached information to disk, free anyallocated resources, and close the underlying file(s).Since key/data pairs may be cached in memory, failing to sync the filewith a.I closeor.I syncfunction may result in inconsistent or lost information..I Closeroutines return -1 on error (setting.IR errno )and 0 on success..TPdelA pointer to a routine to remove key/data pairs from the database..IPThe parameter.I flagmay be set to the following value:.RS.TPR_CURSORDelete the record referenced by the cursor.The cursor must have previously been initialized..RE.IP.I Deleteroutines return -1 on error (setting.IR errno ),0 on success, and 1 if the specified.I keywas not in the file..TPfdA pointer to a routine which returns a file descriptor representativeof the underlying database.A file descriptor referencing the same file will be returned to allprocesses which call.I dbopenwith the same.I filename.This file descriptor may be safely used as an argument to the.IR fcntl (2)and.IR flock (2)locking functions.The file descriptor is not necessarily associated with any of theunderlying files used by the access method.No file descriptor is available for in memory databases..I Fdroutines return -1 on error (setting.IR errno ),and the file descriptor on success..TPgetA pointer to a routine which is the interface for keyed retrieval fromthe database.The address and length of the data associated with the specified.I keyare returned in the structure referenced by.IR data ..I Getroutines return -1 on error (setting.IR errno ),0 on success, and 1 if the.I keywas not in the file..TPputA pointer to a routine to store key/data pairs in the database..IPThe parameter.I flagmay be set to one of the following values:.RS.TPR_CURSORReplace the key/data pair referenced by the cursor.The cursor must have previously been initialized..TPR_IAFTERAppend the data immediately after the data referenced by.IR key ,creating a new key/data pair.The record number of the appended key/data pair is returned in the.I keystructure.(Applicable only to the DB_RECNO access method.).TPR_IBEFOREInsert the data immediately before the data referenced by.IR key ,creating a new key/data pair.The record number of the inserted key/data pair is returned in the.I keystructure.(Applicable only to the DB_RECNO access method.).TPR_NOOVERWRITEEnter the new key/data pair only if the key does not previously exist..TPR_SETCURSORStore the key/data pair, setting or initializing the position of thecursor to reference it.(Applicable only to the DB_BTREE and DB_RECNO access methods.).RE.IPR_SETCURSOR is available only for the DB_BTREE and DB_RECNO accessmethods because it implies that the keys have an inherent orderwhich does not change..IPR_IAFTER and R_IBEFORE are available only for the DB_RECNOaccess method because they each imply that the access method is able tocreate new keys.This is only true if the keys are ordered and independent, record numbersfor example..IPThe default behavior of the.I putroutines is to enter the new key/data pair, replacing any previouslyexisting key..IP.I Putroutines return -1 on error (setting.IR errno ),0 on success, and 1 if the R_NOOVERWRITE.I flagwas set and the key already exists in the file..TPseqA pointer to a routine which is the interface for sequentialretrieval from the database.The address and length of the key are returned in the structurereferenced by.IR key ,and the address and length of the data are returned in thestructure referencedby.IR data ..IPSequential key/data pair retrieval may begin at any time, and theposition of the ``cursor'' is not affected by calls to the.IR del ,.IR get ,.IR put ,or.I syncroutines.Modifications to the database during a sequential scan will be reflectedin the scan, i.e. records inserted behind the cursor will not be returnedwhile records inserted in front of the cursor will be returned..IPThe flag value.B mustbe set to one of the following values:.RS.TPR_CURSORThe data associated with the specified key is returned.This differs from the.I getroutines in that it sets or initializes the cursor to the location ofthe key as well.(Note, for the DB_BTREE access method, the returned key is not necessarily anexact match for the specified key.The returned key is the smallest key greater than or equal to the specifiedkey, permitting partial key matches and range searches.).TPR_FIRSTThe first key/data pair of the database is returned, and the cursoris set or initialized to reference it..TPR_LASTThe last key/data pair of the database is returned, and the cursoris set or initialized to reference it.(Applicable only to the DB_BTREE and DB_RECNO access methods.).TPR_NEXTRetrieve the key/data pair immediately after the cursor.If the cursor is not yet set, this is the same as the R_FIRST flag..TPR_PREVRetrieve the key/data pair immediately before the cursor.If the cursor is not yet set, this is the same as the R_LAST flag.(Applicable only to the DB_BTREE and DB_RECNO access methods.).RE.IPR_LAST and R_PREV are available only for the DB_BTREE and DB_RECNOaccess methods because they each imply that the keys have an inherentorder which does not change..IP.I Seqroutines return -1 on error (setting.IR errno ),0 on success and 1 if there are no key/data pairs less than or greaterthan the specified or current key.If the DB_RECNO access method is being used, and if the database fileis a character special file and no complete key/data pairs are currentlyavailable, the.I seqroutines return 2..TPsyncA pointer to a routine to flush any cached information to disk.If the database is in memory only, the.I syncroutine has no effect and will always succeed..IPThe flag value may be set to the following value:.RS.TPR_RECNOSYNCIf the DB_RECNO access method is being used, this flag causesthe sync routine to apply to the btree file which underlies therecno file, not the recno file itself.(See the.I bfnamefield of the.IR recno (3)manual page for more information.).RE.IP.I Syncroutines return -1 on error (setting.IR errno )and 0 on success..SH "KEY/DATA PAIRS"Access to all file types is based on key/data pairs.Both keys and data are represented by the following data structure:.PPtypedef struct {.RSvoid *data;.brsize_t size;.RE} DBT;.PPThe elements of the DBT structure are defined as follows:.TPdataA pointer to a byte string..TPsizeThe length of the byte string..PPKey and data byte strings may reference strings of essentially unlimitedlength although any two of them must fit into available memory at the sametime.It should be noted that the access methods provide no guarantees aboutbyte string alignment..SH ERRORSThe.I dbopenroutine may fail and set.I errnofor any of the errors specified for the library routines.IR open (2)and.IR malloc (3)or the following:.TP[EFTYPE]A file is incorrectly formatted..TP[EINVAL]A parameter has been specified (hash function, pad byte etc.) that isincompatible with the current file specification or which is notmeaningful for the function (for example, use of the cursor withoutprior initialization) or there is a mismatch between the versionnumber of file and the software..PPThe.I closeroutines may fail and set.I errnofor any of the errors specified for the library routines.IR close (2),.IR read (2),.IR write (2),.IR free (3),or.IR fsync (2)..PPThe.IR del ,.IR get ,.I putand.I seqroutines may fail and set.I errnofor any of the errors specified for the library routines.IR read (2),.IR write (2),.IR free (3)or.IR malloc (3)..PPThe.I fdroutines will fail and set.I errnoto ENOENT for in memory databases..PPThe.I syncroutines may fail and set.I errnofor any of the errors specified for the library routine.IR fsync (2)..SH "SEE ALSO".IR btree (3),.IR hash (3),.IR mpool (3),.IR recno (3).sp.IR "LIBTP: Portable, Modular Transactions for UNIX" ,Margo Seltzer, Michael Olson, USENIX proceedings, Winter 1992..SH BUGSThe typedef DBT is a mnemonic for ``data base thang'', and was usedbecause noone could think of a reasonable name that wasn't already used..PPThe file descriptor interface is a kluge and will be deleted in afuture version of the interface..PPNone of the access methods provide any form of concurrent access,locking, or transactions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -