]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/dbopen.3
Convert SH to SS and other section-related fixes.
[thirdparty/man-pages.git] / man3 / dbopen.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 1990, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)dbopen.3 8.5 (Berkeley) 1/2/94
33.\"
c13182ef 34.TH DBOPEN 3 1994-01-02
fea681da
MK
35.UC 7
36.SH NAME
37dbopen \- database access methods
38.SH SYNOPSIS
39.nf
b9f02710
MK
40.B #include <sys/types.h>
41.B #include <limits.h>
42.B #include <db.h>
fea681da 43
b9f02710 44.BI "DB *dbopen(const char *" file ", int " flags ", int " mode \
c13182ef 45", DBTYPE " type ,
b9f02710 46.BI " const void *" openinfo );
fea681da
MK
47.fi
48.SH DESCRIPTION
8a9648b9 49.BR dbopen ()
fea681da
MK
50is the library interface to database files.
51The supported file formats are btree, hashed and UNIX file oriented.
52The btree format is a representation of a sorted, balanced tree structure.
53The hashed format is an extensible, dynamic hashing scheme.
54The flat-file format is a byte stream file with fixed or variable length
55records.
56The formats and file format specific information are described in detail
57in their respective manual pages
31e9a9ec
MK
58.BR btree (3),
59.BR hash (3)
fea681da 60and
31e9a9ec 61.BR recno (3).
fea681da 62.PP
8a9648b9
MK
63.BR dbopen ()
64opens
fea681da
MK
65.I file
66for reading and/or writing.
67Files never intended to be preserved on disk may be created by setting
68the file parameter to NULL.
69.PP
70The
71.I flags
72and
73.I mode
74arguments are as specified to the
31e9a9ec 75.BR open (2)
fea681da
MK
76routine, however, only the O_CREAT, O_EXCL, O_EXLOCK, O_NONBLOCK,
77O_RDONLY, O_RDWR, O_SHLOCK and O_TRUNC flags are meaningful.
78(Note, opening a database file O_WRONLY is not possible.)
79.\"Three additional options may be specified by
80.\".IR or 'ing
81.\"them into the
82.\".I flags
83.\"argument.
84.\".TP
85.\"DB_LOCK
86.\"Do the necessary locking in the database to support concurrent access.
87.\"If concurrent access isn't needed or the database is read-only this
88.\"flag should not be set, as it tends to have an associated performance
89.\"penalty.
90.\".TP
91.\"DB_SHMEM
92.\"Place the underlying memory pool used by the database in shared
93.\"memory.
94.\"Necessary for concurrent access.
95.\".TP
96.\"DB_TXN
97.\"Support transactions in the database.
98.\"The DB_LOCK and DB_SHMEM flags must be set as well.
99.PP
100The
101.I type
102argument is of type DBTYPE (as defined in the <db.h> include file) and
103may be set to DB_BTREE, DB_HASH or DB_RECNO.
104.PP
105The
106.I openinfo
107argument is a pointer to an access method specific structure described
108in the access method's manual page.
109If
110.I openinfo
111is NULL, each access method will use defaults appropriate for the system
112and the access method.
113.PP
8a9648b9 114.BR dbopen ()
fea681da
MK
115returns a pointer to a DB structure on success and NULL on error.
116The DB structure is defined in the <db.h> include file, and contains at
117least the following fields:
118.sp
119.nf
120typedef struct {
b9f02710
MK
121 DBTYPE type;
122 int (*close)(const DB *db);
123 int (*del)(const DB *db, const DBT *key, u_int flags);
124 int (*fd)(const DB *db);
125 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
126 int (*put)(const DB *db, DBT *key, const DBT *data,
127 u_int flags);
128 int (*sync)(const DB *db, u_int flags);
129 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
fea681da
MK
130} DB;
131.fi
132.PP
133These elements describe a database type and a set of functions performing
134various actions.
135These functions take a pointer to a structure as returned by
8a9648b9 136.BR dbopen (),
fea681da
MK
137and sometimes one or more pointers to key/data structures and a flag value.
138.TP
139type
140The type of the underlying access method (and file format).
141.TP
142close
143A pointer to a routine to flush any cached information to disk, free any
144allocated resources, and close the underlying file(s).
145Since key/data pairs may be cached in memory, failing to sync the file
146with a
147.I close
148or
149.I sync
150function may result in inconsistent or lost information.
151.I Close
152routines return \-1 on error (setting
153.IR errno )
154and 0 on success.
155.TP
156del
157A pointer to a routine to remove key/data pairs from the database.
158.IP
159The parameter
160.I flag
161may be set to the following value:
162.RS
163.TP
164R_CURSOR
165Delete the record referenced by the cursor.
166The cursor must have previously been initialized.
167.RE
168.IP
169.I Delete
170routines return \-1 on error (setting
171.IR errno ),
1720 on success, and 1 if the specified
173.I key
174was not in the file.
175.TP
176fd
177A pointer to a routine which returns a file descriptor representative
178of the underlying database.
179A file descriptor referencing the same file will be returned to all
180processes which call
8a9648b9 181.BR dbopen ()
fea681da
MK
182with the same
183.I file
184name.
185This file descriptor may be safely used as an argument to the
31e9a9ec 186.BR fcntl (2)
fea681da 187and
31e9a9ec 188.BR flock (2)
fea681da
MK
189locking functions.
190The file descriptor is not necessarily associated with any of the
191underlying files used by the access method.
192No file descriptor is available for in memory databases.
193.I Fd
194routines return \-1 on error (setting
195.IR errno ),
196and the file descriptor on success.
197.TP
198get
199A pointer to a routine which is the interface for keyed retrieval from
200the database.
201The address and length of the data associated with the specified
202.I key
203are returned in the structure referenced by
204.IR data .
205.I Get
206routines return \-1 on error (setting
207.IR errno ),
2080 on success, and 1 if the
209.I key
210was not in the file.
211.TP
212put
213A pointer to a routine to store key/data pairs in the database.
214.IP
215The parameter
216.I flag
217may be set to one of the following values:
218.RS
219.TP
220R_CURSOR
221Replace the key/data pair referenced by the cursor.
222The cursor must have previously been initialized.
223.TP
224R_IAFTER
225Append the data immediately after the data referenced by
226.IR key ,
227creating a new key/data pair.
228The record number of the appended key/data pair is returned in the
229.I key
230structure.
231(Applicable only to the DB_RECNO access method.)
232.TP
233R_IBEFORE
234Insert the data immediately before the data referenced by
235.IR key ,
236creating a new key/data pair.
237The record number of the inserted key/data pair is returned in the
238.I key
239structure.
240(Applicable only to the DB_RECNO access method.)
241.TP
242R_NOOVERWRITE
243Enter the new key/data pair only if the key does not previously exist.
244.TP
245R_SETCURSOR
246Store the key/data pair, setting or initializing the position of the
247cursor to reference it.
248(Applicable only to the DB_BTREE and DB_RECNO access methods.)
249.RE
250.IP
251R_SETCURSOR is available only for the DB_BTREE and DB_RECNO access
252methods because it implies that the keys have an inherent order
253which does not change.
254.IP
255R_IAFTER and R_IBEFORE are available only for the DB_RECNO
256access method because they each imply that the access method is able to
257create new keys.
258This is only true if the keys are ordered and independent, record numbers
259for example.
260.IP
261The default behavior of the
262.I put
263routines is to enter the new key/data pair, replacing any previously
264existing key.
265.IP
266.I Put
267routines return \-1 on error (setting
268.IR errno ),
2690 on success, and 1 if the R_NOOVERWRITE
270.I flag
271was set and the key already exists in the file.
272.TP
273seq
274A pointer to a routine which is the interface for sequential
275retrieval from the database.
276The address and length of the key are returned in the structure
277referenced by
278.IR key ,
279and the address and length of the data are returned in the
280structure referenced
281by
282.IR data .
283.IP
284Sequential key/data pair retrieval may begin at any time, and the
285position of the ``cursor'' is not affected by calls to the
286.IR del ,
287.IR get ,
288.IR put ,
289or
290.I sync
291routines.
292Modifications to the database during a sequential scan will be reflected
293in the scan, i.e. records inserted behind the cursor will not be returned
294while records inserted in front of the cursor will be returned.
295.IP
296The flag value
297.B must
298be set to one of the following values:
299.RS
300.TP
301R_CURSOR
302The data associated with the specified key is returned.
303This differs from the
304.I get
305routines in that it sets or initializes the cursor to the location of
306the key as well.
307(Note, for the DB_BTREE access method, the returned key is not necessarily an
308exact match for the specified key.
309The returned key is the smallest key greater than or equal to the specified
310key, permitting partial key matches and range searches.)
311.TP
312R_FIRST
313The first key/data pair of the database is returned, and the cursor
314is set or initialized to reference it.
315.TP
316R_LAST
317The last key/data pair of the database is returned, and the cursor
318is set or initialized to reference it.
319(Applicable only to the DB_BTREE and DB_RECNO access methods.)
320.TP
321R_NEXT
322Retrieve the key/data pair immediately after the cursor.
323If the cursor is not yet set, this is the same as the R_FIRST flag.
324.TP
325R_PREV
326Retrieve the key/data pair immediately before the cursor.
327If the cursor is not yet set, this is the same as the R_LAST flag.
328(Applicable only to the DB_BTREE and DB_RECNO access methods.)
329.RE
330.IP
331R_LAST and R_PREV are available only for the DB_BTREE and DB_RECNO
332access methods because they each imply that the keys have an inherent
333order which does not change.
334.IP
335.I Seq
336routines return \-1 on error (setting
337.IR errno ),
3380 on success and 1 if there are no key/data pairs less than or greater
339than the specified or current key.
340If the DB_RECNO access method is being used, and if the database file
341is a character special file and no complete key/data pairs are currently
342available, the
343.I seq
344routines return 2.
345.TP
346sync
347A pointer to a routine to flush any cached information to disk.
348If the database is in memory only, the
349.I sync
350routine has no effect and will always succeed.
351.IP
352The flag value may be set to the following value:
353.RS
354.TP
355R_RECNOSYNC
356If the DB_RECNO access method is being used, this flag causes
357the sync routine to apply to the btree file which underlies the
358recno file, not the recno file itself.
359(See the
360.I bfname
361field of the
31e9a9ec 362.BR recno (3)
fea681da
MK
363manual page for more information.)
364.RE
365.IP
366.I Sync
367routines return \-1 on error (setting
368.IR errno )
369and 0 on success.
8af1ba10 370.SS "Key/Data Pairs"
fea681da
MK
371Access to all file types is based on key/data pairs.
372Both keys and data are represented by the following data structure:
373.PP
374typedef struct {
375.RS
376void *data;
377.br
378size_t size;
379.RE
380} DBT;
381.PP
382The elements of the DBT structure are defined as follows:
383.TP
384data
385A pointer to a byte string.
386.TP
387size
388The length of the byte string.
389.PP
390Key and data byte strings may reference strings of essentially unlimited
391length although any two of them must fit into available memory at the same
392time.
393It should be noted that the access methods provide no guarantees about
394byte string alignment.
395.SH ERRORS
396The
8a9648b9 397.BR dbopen ()
fea681da
MK
398routine may fail and set
399.I errno
400for any of the errors specified for the library routines
31e9a9ec 401.BR open (2)
fea681da 402and
31e9a9ec 403.BR malloc (3)
fea681da
MK
404or the following:
405.TP
406[EFTYPE]
407A file is incorrectly formatted.
408.TP
409[EINVAL]
410A parameter has been specified (hash function, pad byte etc.) that is
411incompatible with the current file specification or which is not
412meaningful for the function (for example, use of the cursor without
413prior initialization) or there is a mismatch between the version
414number of file and the software.
415.PP
416The
417.I close
418routines may fail and set
419.I errno
420for any of the errors specified for the library routines
31e9a9ec
MK
421.BR close (2),
422.BR read (2),
423.BR write (2),
424.BR free (3),
fea681da 425or
31e9a9ec 426.BR fsync (2).
fea681da
MK
427.PP
428The
429.IR del ,
430.IR get ,
431.I put
432and
433.I seq
434routines may fail and set
435.I errno
436for any of the errors specified for the library routines
31e9a9ec
MK
437.BR read (2),
438.BR write (2),
439.BR free (3)
fea681da 440or
31e9a9ec 441.BR malloc (3).
fea681da
MK
442.PP
443The
444.I fd
445routines will fail and set
446.I errno
447to ENOENT for in memory databases.
448.PP
449The
450.I sync
451routines may fail and set
452.I errno
453for any of the errors specified for the library routine
31e9a9ec 454.BR fsync (2).
fea681da 455.SH "SEE ALSO"
31e9a9ec
MK
456.BR btree (3),
457.BR hash (3),
458.BR mpool (3),
459.BR recno (3)
fea681da
MK
460.sp
461.IR "LIBTP: Portable, Modular Transactions for UNIX" ,
462Margo Seltzer, Michael Olson, USENIX proceedings, Winter 1992.
463.SH BUGS
464The typedef DBT is a mnemonic for ``data base thang'', and was used
3f1c1b0a 465because no-one could think of a reasonable name that wasn't already used.
fea681da
MK
466.PP
467The file descriptor interface is a kludge and will be deleted in a
468future version of the interface.
469.PP
470None of the access methods provide any form of concurrent access,
471locking, or transactions.