]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/dbopen.3
spfix
[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.\"
0ed55ece 34.TH DBOPEN 3 1994-01-02 "" "Linux Programmer's Manual"
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)
d9a10d9d
MK
76routine, however, only the
77.BR O_CREAT ,
78.BR O_EXCL ,
79.BR O_EXLOCK ,
80.BR O_NONBLOCK ,
81.BR O_RDONLY ,
82.BR O_RDWR ,
83.BR O_SHLOCK ,
84and
c8fe3fa2
MK
85.B O_TRUNC
86flags are meaningful.
d9a10d9d
MK
87(Note, opening a database file
88.B O_WRONLY
89is not possible.)
fea681da
MK
90.\"Three additional options may be specified by
91.\".IR or 'ing
92.\"them into the
93.\".I flags
94.\"argument.
95.\".TP
96.\"DB_LOCK
97.\"Do the necessary locking in the database to support concurrent access.
98.\"If concurrent access isn't needed or the database is read-only this
99.\"flag should not be set, as it tends to have an associated performance
100.\"penalty.
101.\".TP
102.\"DB_SHMEM
103.\"Place the underlying memory pool used by the database in shared
104.\"memory.
105.\"Necessary for concurrent access.
106.\".TP
107.\"DB_TXN
108.\"Support transactions in the database.
109.\"The DB_LOCK and DB_SHMEM flags must be set as well.
110.PP
111The
112.I type
d9a10d9d
MK
113argument is of type
114.I DBTYPE
115(as defined in the
a9a13a50
MK
116.I <db.h>
117include file) and
628d8d62
MK
118may be set to
119.BR DB_BTREE ,
120.BR DB_HASH ,
121or
122.BR DB_RECNO .
fea681da
MK
123.PP
124The
125.I openinfo
126argument is a pointer to an access method specific structure described
127in the access method's manual page.
128If
129.I openinfo
130is NULL, each access method will use defaults appropriate for the system
131and the access method.
132.PP
8a9648b9 133.BR dbopen ()
fea681da 134returns a pointer to a DB structure on success and NULL on error.
a9a13a50
MK
135The DB structure is defined in the
136.I <db.h>
137include file, and contains at
fea681da
MK
138least the following fields:
139.sp
628d8d62 140.in +0.5i
fea681da
MK
141.nf
142typedef struct {
b9f02710
MK
143 DBTYPE type;
144 int (*close)(const DB *db);
145 int (*del)(const DB *db, const DBT *key, u_int flags);
146 int (*fd)(const DB *db);
147 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
148 int (*put)(const DB *db, DBT *key, const DBT *data,
149 u_int flags);
150 int (*sync)(const DB *db, u_int flags);
151 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
fea681da
MK
152} DB;
153.fi
628d8d62 154.in
fea681da
MK
155.PP
156These elements describe a database type and a set of functions performing
157various actions.
158These functions take a pointer to a structure as returned by
8a9648b9 159.BR dbopen (),
fea681da
MK
160and sometimes one or more pointers to key/data structures and a flag value.
161.TP
162type
163The type of the underlying access method (and file format).
164.TP
165close
166A pointer to a routine to flush any cached information to disk, free any
167allocated resources, and close the underlying file(s).
168Since key/data pairs may be cached in memory, failing to sync the file
169with a
170.I close
171or
172.I sync
173function may result in inconsistent or lost information.
174.I Close
175routines return \-1 on error (setting
176.IR errno )
177and 0 on success.
178.TP
179del
180A pointer to a routine to remove key/data pairs from the database.
181.IP
182The parameter
183.I flag
184may be set to the following value:
185.RS
186.TP
628d8d62 187.B R_CURSOR
fea681da
MK
188Delete the record referenced by the cursor.
189The cursor must have previously been initialized.
190.RE
191.IP
192.I Delete
193routines return \-1 on error (setting
194.IR errno ),
1950 on success, and 1 if the specified
196.I key
197was not in the file.
198.TP
199fd
200A pointer to a routine which returns a file descriptor representative
201of the underlying database.
202A file descriptor referencing the same file will be returned to all
203processes which call
8a9648b9 204.BR dbopen ()
fea681da
MK
205with the same
206.I file
207name.
208This file descriptor may be safely used as an argument to the
31e9a9ec 209.BR fcntl (2)
fea681da 210and
31e9a9ec 211.BR flock (2)
fea681da
MK
212locking functions.
213The file descriptor is not necessarily associated with any of the
214underlying files used by the access method.
215No file descriptor is available for in memory databases.
216.I Fd
217routines return \-1 on error (setting
218.IR errno ),
219and the file descriptor on success.
220.TP
221get
222A pointer to a routine which is the interface for keyed retrieval from
223the database.
224The address and length of the data associated with the specified
225.I key
226are returned in the structure referenced by
227.IR data .
228.I Get
229routines return \-1 on error (setting
230.IR errno ),
2310 on success, and 1 if the
232.I key
233was not in the file.
234.TP
235put
236A pointer to a routine to store key/data pairs in the database.
237.IP
238The parameter
239.I flag
240may be set to one of the following values:
241.RS
242.TP
628d8d62 243.B R_CURSOR
fea681da
MK
244Replace the key/data pair referenced by the cursor.
245The cursor must have previously been initialized.
246.TP
628d8d62 247.B R_IAFTER
fea681da
MK
248Append the data immediately after the data referenced by
249.IR key ,
250creating a new key/data pair.
251The record number of the appended key/data pair is returned in the
252.I key
253structure.
628d8d62
MK
254(Applicable only to the
255.B DB_RECNO
256access method.)
fea681da 257.TP
628d8d62 258.B R_IBEFORE
fea681da
MK
259Insert the data immediately before the data referenced by
260.IR key ,
261creating a new key/data pair.
262The record number of the inserted key/data pair is returned in the
263.I key
264structure.
628d8d62
MK
265(Applicable only to the
266.B DB_RECNO
267access method.)
fea681da 268.TP
628d8d62 269.B R_NOOVERWRITE
fea681da
MK
270Enter the new key/data pair only if the key does not previously exist.
271.TP
628d8d62 272.B R_SETCURSOR
fea681da
MK
273Store the key/data pair, setting or initializing the position of the
274cursor to reference it.
628d8d62
MK
275(Applicable only to the
276.B DB_BTREE
277and
278.B DB_RECNO
279access methods.)
fea681da
MK
280.RE
281.IP
628d8d62
MK
282.B R_SETCURSOR
283is available only for the
284.B DB_BTREE
285and
286.B DB_RECNO
287access
fea681da
MK
288methods because it implies that the keys have an inherent order
289which does not change.
290.IP
628d8d62
MK
291.B R_IAFTER
292and
293.B R_IBEFORE
294are available only for the
295.B DB_RECNO
fea681da
MK
296access method because they each imply that the access method is able to
297create new keys.
298This is only true if the keys are ordered and independent, record numbers
299for example.
300.IP
301The default behavior of the
302.I put
303routines is to enter the new key/data pair, replacing any previously
304existing key.
305.IP
306.I Put
307routines return \-1 on error (setting
308.IR errno ),
628d8d62
MK
3090 on success, and 1 if the
310.B R_NOOVERWRITE
fea681da
MK
311.I flag
312was set and the key already exists in the file.
313.TP
314seq
315A pointer to a routine which is the interface for sequential
316retrieval from the database.
317The address and length of the key are returned in the structure
318referenced by
319.IR key ,
320and the address and length of the data are returned in the
321structure referenced
322by
323.IR data .
324.IP
325Sequential key/data pair retrieval may begin at any time, and the
326position of the ``cursor'' is not affected by calls to the
327.IR del ,
328.IR get ,
329.IR put ,
330or
331.I sync
332routines.
333Modifications to the database during a sequential scan will be reflected
988db661 334in the scan, that is,
75b94dc3 335records inserted behind the cursor will not be returned
fea681da
MK
336while records inserted in front of the cursor will be returned.
337.IP
338The flag value
339.B must
340be set to one of the following values:
341.RS
342.TP
628d8d62 343.B R_CURSOR
fea681da
MK
344The data associated with the specified key is returned.
345This differs from the
346.I get
347routines in that it sets or initializes the cursor to the location of
348the key as well.
628d8d62
MK
349(Note, for the
350.B DB_BTREE
351access method, the returned key is not necessarily an
fea681da
MK
352exact match for the specified key.
353The returned key is the smallest key greater than or equal to the specified
354key, permitting partial key matches and range searches.)
355.TP
628d8d62 356.B R_FIRST
fea681da
MK
357The first key/data pair of the database is returned, and the cursor
358is set or initialized to reference it.
359.TP
628d8d62 360.B R_LAST
fea681da
MK
361The last key/data pair of the database is returned, and the cursor
362is set or initialized to reference it.
628d8d62
MK
363(Applicable only to the
364.B DB_BTREE
365and
366.B DB_RECNO
367access methods.)
fea681da 368.TP
628d8d62 369.B R_NEXT
fea681da 370Retrieve the key/data pair immediately after the cursor.
628d8d62
MK
371If the cursor is not yet set, this is the same as the
372.B R_FIRST
373flag.
fea681da 374.TP
628d8d62 375.B R_PREV
fea681da 376Retrieve the key/data pair immediately before the cursor.
628d8d62
MK
377If the cursor is not yet set, this is the same as the
378.B R_LAST
379flag.
380(Applicable only to the
381.B DB_BTREE
382and
383.B DB_RECNO
384access methods.)
fea681da
MK
385.RE
386.IP
628d8d62
MK
387.B R_LAST
388and
389.B R_PREV
390are available only for the
391.B DB_BTREE
392and
393.B DB_RECNO
fea681da
MK
394access methods because they each imply that the keys have an inherent
395order which does not change.
396.IP
397.I Seq
398routines return \-1 on error (setting
399.IR errno ),
4000 on success and 1 if there are no key/data pairs less than or greater
401than the specified or current key.
628d8d62
MK
402If the
403.B DB_RECNO
404access method is being used, and if the database file
fea681da
MK
405is a character special file and no complete key/data pairs are currently
406available, the
407.I seq
408routines return 2.
409.TP
410sync
411A pointer to a routine to flush any cached information to disk.
412If the database is in memory only, the
413.I sync
414routine has no effect and will always succeed.
415.IP
416The flag value may be set to the following value:
417.RS
418.TP
628d8d62
MK
419.B R_RECNOSYNC
420If the
421.B DB_RECNO
422access method is being used, this flag causes
fea681da
MK
423the sync routine to apply to the btree file which underlies the
424recno file, not the recno file itself.
425(See the
426.I bfname
427field of the
31e9a9ec 428.BR recno (3)
fea681da
MK
429manual page for more information.)
430.RE
431.IP
432.I Sync
433routines return \-1 on error (setting
434.IR errno )
435and 0 on success.
8af1ba10 436.SS "Key/Data Pairs"
fea681da
MK
437Access to all file types is based on key/data pairs.
438Both keys and data are represented by the following data structure:
628d8d62
MK
439.in +0.5i
440.nf
441
fea681da 442typedef struct {
628d8d62
MK
443 void *data;
444 size_t size;
fea681da 445} DBT;
628d8d62
MK
446.fi
447.in
fea681da 448.PP
628d8d62
MK
449The elements of the
450.I DBT
451structure are defined as follows:
fea681da
MK
452.TP
453data
454A pointer to a byte string.
455.TP
456size
457The length of the byte string.
458.PP
459Key and data byte strings may reference strings of essentially unlimited
460length although any two of them must fit into available memory at the same
461time.
462It should be noted that the access methods provide no guarantees about
463byte string alignment.
464.SH ERRORS
465The
8a9648b9 466.BR dbopen ()
fea681da
MK
467routine may fail and set
468.I errno
469for any of the errors specified for the library routines
31e9a9ec 470.BR open (2)
fea681da 471and
31e9a9ec 472.BR malloc (3)
fea681da
MK
473or the following:
474.TP
747944fe 475.B EFTYPE
fea681da
MK
476A file is incorrectly formatted.
477.TP
747944fe 478.B EINVAL
fea681da
MK
479A parameter has been specified (hash function, pad byte etc.) that is
480incompatible with the current file specification or which is not
481meaningful for the function (for example, use of the cursor without
482prior initialization) or there is a mismatch between the version
483number of file and the software.
484.PP
485The
486.I close
487routines may fail and set
488.I errno
489for any of the errors specified for the library routines
31e9a9ec
MK
490.BR close (2),
491.BR read (2),
492.BR write (2),
493.BR free (3),
fea681da 494or
31e9a9ec 495.BR fsync (2).
fea681da
MK
496.PP
497The
498.IR del ,
499.IR get ,
500.I put
501and
502.I seq
503routines may fail and set
504.I errno
505for any of the errors specified for the library routines
31e9a9ec
MK
506.BR read (2),
507.BR write (2),
508.BR free (3)
fea681da 509or
31e9a9ec 510.BR malloc (3).
fea681da
MK
511.PP
512The
513.I fd
514routines will fail and set
515.I errno
628d8d62
MK
516to
517.B ENOENT
518for in memory databases.
fea681da
MK
519.PP
520The
521.I sync
522routines may fail and set
523.I errno
524for any of the errors specified for the library routine
31e9a9ec 525.BR fsync (2).
fea681da 526.SH BUGS
628d8d62
MK
527The typedef
528.I DBT
529is a mnemonic for ``data base thang'', and was used
3f1c1b0a 530because no-one could think of a reasonable name that wasn't already used.
fea681da
MK
531.PP
532The file descriptor interface is a kludge and will be deleted in a
533future version of the interface.
534.PP
535None of the access methods provide any form of concurrent access,
536locking, or transactions.
e37e3282
MK
537.SH "SEE ALSO"
538.BR btree (3),
539.BR hash (3),
540.BR mpool (3),
541.BR recno (3)
542.sp
543.IR "LIBTP: Portable, Modular Transactions for UNIX" ,
544Margo Seltzer, Michael Olson, USENIX proceedings, Winter 1992.