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