db.3 -> dbopen.3
[unix-history] / usr / src / lib / libc / db / man / dbopen.3
CommitLineData
bcd70bc9
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
4e3894e7 6.\" @(#)dbopen.3 5.20 (Berkeley) %G%
bcd70bc9 7.\"
4e3894e7 8.TH DBOPEN 3 ""
bcd70bc9
KB
9.UC 7
10.SH NAME
f52ca292 11dbopen \- database access methods
bcd70bc9
KB
12.SH SYNOPSIS
13.nf
14.ft B
ad422b65 15#include <sys/types.h>
f52ca292 16#include <limits.h>
bcd70bc9
KB
17#include <db.h>
18
19DB *
f52ca292 20dbopen(const char *file, int flags, int mode, DBTYPE type,
16d71a3a 21.ti +5
f52ca292 22const void *openinfo);
bcd70bc9
KB
23.ft R
24.fi
25.SH DESCRIPTION
f52ca292
KB
26.IR Dbopen
27is the library interface to database files.
28The supported file formats are btree, hashed and record oriented.
8e982eee
KB
29The btree format is a representation of a sorted, balanced tree structure.
30The hashed format is an extensible, dynamic hashing scheme.
f52ca292
KB
31The flat-file format is a byte stream file with fixed or variable length
32records.
33The formats and file format specific information are described in detail
34in their respective manual pages
35.IR btree (3),
36.IR hash (3)
37and
38.IR recno (3).
bcd70bc9 39.PP
f52ca292 40Dbopen opens
bcd70bc9
KB
41.I file
42for reading and/or writing.
f52ca292 43Files never intended to be preserved on disk may be created by setting
ad422b65 44the file parameter to NULL.
f52ca292 45.PP
bcd70bc9
KB
46The
47.I flags
48and
49.I mode arguments
50are as specified to the
51.IR open (2)
ad422b65 52routine, however, only the O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC
bcd70bc9 53and O_WRONLY flags are meaningful.
f52ca292
KB
54.PP
55The
56.I type
57argument is of type DBTYPE (as defined in the <db.h> include file) and
58may be set to DB_BTREE, DB_HASH or DB_RECNO.
59.PP
60The
8e982eee 61.I openinfo
f52ca292
KB
62argument is a pointer to an access method specific structure described
63in the access method's manual page.
64If
65.I openinfo
66is NULL, each access method will use defaults appropriate for the system
67and the access method.
bcd70bc9 68.PP
f52ca292
KB
69.I Dbopen
70returns a pointer to a DB structure on success and NULL on error.
71The DB structure is defined in the <db.h> include file, and contains at
72least the following fields:
16d71a3a
KB
73.sp
74.nf
bcd70bc9
KB
75typedef struct {
76.RS
f52ca292 77DBTYPE type;
8e982eee 78int (*close)(const DB *db);
96a0c789
KB
79int (*del)(const DB *db, const DBT *key, u_int flags);
80int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
16d71a3a
KB
81int (*put)(const DB *db, const DBT *key, const DBT *data,
82.ti +5
96a0c789 83u_int flags);
f52ca292 84int (*sync)(const DB *db);
96a0c789 85int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
bcd70bc9
KB
86.RE
87} DB;
16d71a3a 88.fi
bcd70bc9 89.PP
f52ca292
KB
90These elements a database type and a set of functions performing various
91actions.
92These functions take a pointer to a structure as returned by
93.IR dbopen ,
94and sometimes one or more pointers to key/data structures and a flag value.
bcd70bc9 95.TP
189ff9f0 96type
f52ca292 97The type of the underlying access method (and file format).
189ff9f0 98.TP
bcd70bc9
KB
99close
100A pointer to a routine to flush any cached information to disk, free any
f52ca292
KB
101allocated resources, and close the underlying file(s).
102Since key/data pairs may be cached in memory, failing to sync the file
103with a
bcd70bc9 104.I close
f52ca292
KB
105or
106.I sync
107function may result in inconsistent or lost information.
8e982eee
KB
108.I Close
109routines return -1 on error (setting
110.IR errno )
111and 0 on success.
bcd70bc9 112.TP
16d71a3a 113del
932a5d4e 114A pointer to a routine to remove key/data pairs from the database.
f52ca292
KB
115.IP
116The parameter
117.I flag
118may be set to the following value:
119.RS
120.TP
121R_CURSOR
122Delete the record referenced by the cursor.
123.RE
124.IP
8e982eee
KB
125.I Delete
126routines return -1 on error (setting
127.IR errno ),
1280 on success, and 1 if the specified
bcd70bc9
KB
129.I key
130was not in the file.
131.TP
132get
133A pointer to a routine which is the interface for keyed retrieval from
932a5d4e 134the database.
bcd70bc9
KB
135The address and length of the data associated with the specified
136.I key
137are returned in the structure referenced by
138.IR data .
8e982eee
KB
139.I Get
140routines return -1 on error (setting
141.IR errno ),
1420 on success, and 1 if the
bcd70bc9
KB
143.I key
144was not in the file.
145.TP
146put
932a5d4e 147A pointer to a routine to store key/data pairs in the database.
ad422b65 148.IP
44c9ff50
KB
149The parameter
150.I flag
8a7be813 151may be set to one of the following values:
44c9ff50 152.RS
bcd70bc9 153.TP
8a7be813
KB
154R_APPEND
155Append the data to the tree, creating a new key/data pair.
156(Applicable only to the DB_RECNO access method.)
157.TP
158R_CURSOR
159Replace the key/data pair referenced by the cursor.
160.TP
8e982eee 161R_IAFTER
bcd70bc9
KB
162Append the data immediately after the data referenced by
163.IR key ,
8e982eee 164creating a new key/data pair.
8a7be813 165(Applicable only to the DB_RECNO access method.)
ad422b65 166.TP
8e982eee 167R_IBEFORE
bcd70bc9
KB
168Insert the data immediately before the data referenced by
169.IR key ,
8e982eee 170creating a new key/data pair.
8a7be813 171(Applicable only to the DB_RECNO access method.)
bcd70bc9
KB
172.TP
173R_NOOVERWRITE
174Enter the new key/data pair only if the key does not previously exist.
175.RE
ad422b65 176.IP
8a7be813
KB
177R_APPEND, R_IAFTER and R_IBEFORE are available only for the DB_RECNO access
178method because they each imply that the access method is able to create new
179keys.
180This is only true if the keys are ordered and independent, record numbers
181for example.
182.IP
f52ca292
KB
183The default behavior of the
184.I put
185routines is to enter the new key/data pair, replacing any previously
186existing key.
187.IP
8e982eee
KB
188.I Put
189routines return -1 on error (setting
190.IR errno ),
1910 on success, and 1 if the R_NOOVERWRITE
ad422b65 192.I flag
8e982eee 193was set and the key already exists in the file.
bcd70bc9
KB
194.TP
195seq
196A pointer to a routine which is the interface for sequential
932a5d4e 197retrieval from the database.
bcd70bc9
KB
198The address and length of the key are returned in the structure
199referenced by
200.IR key ,
201and the address and length of the data are returned in the
202structure referenced
203by
204.IR data .
ad422b65 205.IP
7226de19
KB
206Sequential key/data pair retrieval may begin at any time, and the
207position of the ``cursor'' is not affected by calls to the
16d71a3a 208.IR del ,
7226de19
KB
209.IR get ,
210.IR put ,
211or
212.I sync
213routines.
214Modifications to the database during a sequential scan will be reflected
215in the scan, i.e. records inserted behind the cursor will not be returned
216while records inserted in front of the cursor will be returned.
217.IP
8a7be813
KB
218The flag value
219.B must
220be set to one of the following values:
bcd70bc9
KB
221.RS
222.TP
ad422b65 223R_CURSOR
8e982eee
KB
224The data associated with the specified key is returned.
225This differs from the
226.I get
227routines in that it sets the ``cursor'' to the location of the
228key as well.
8a7be813
KB
229(Note, for the DB_BTREE access method, the returned key is not necessarily an
230exact match for the specified key.
231The returned key is the smallest key greater than or equal to the specified
232key, permitting partial key matches and range searches.)
ad422b65 233.TP
bcd70bc9 234R_FIRST
8e982eee 235The first key/data pair of the database is returned.
bcd70bc9
KB
236.TP
237R_LAST
8e982eee 238The last key/data pair of the database is returned.
8a7be813 239(Applicable only to the DB_BTREE and DB_RECNO access methods.)
bcd70bc9
KB
240.TP
241R_NEXT
8e982eee
KB
242Retrieve the key/data pair immediately after the key/data pair most recently
243retrieved using the
244.I seq
245routine.
246The cursor is moved to the returned key/data pair.
247If
248.I flag
249is set to R_NEXT the first time the
250.I seq
251routine is called, the first key/data pair of the database is returned.
bcd70bc9
KB
252.TP
253R_PREV
8e982eee
KB
254Retrieve the key/data pair immediately before the key/data pair most recently
255retrieved using the
256.I seq
257routine.
258The cursor is moved to the returned key/data pair.
7226de19
KB
259If
260.I flag
261is set to R_PREV the first time the
262.I seq
263routine is called, the last key/data pair of the database is returned.
8a7be813 264(Applicable only to the DB_BTREE and DB_RECNO access methods.)
bcd70bc9 265.RE
ad422b65 266.IP
8a7be813
KB
267R_LAST and R_PREV are available only for the DB_BTREE and DB_RECNO
268access methods because they each imply that the keys have an inherent order
269which does not change.
270.IP
8e982eee
KB
271.I Seq
272routines return -1 on error (setting
273.IR errno ),
8a7be813
KB
2740 on success and 1 if there are no key/data pairs less than or greater
275than the specified or current key.
f52ca292
KB
276If the DB_RECNO access method is being used, and if the database file
277is a character special file and no complete key/data pairs are currently
278available, the
7226de19
KB
279.I seq
280routines return 2.
bcd70bc9
KB
281.TP
282sync
932a5d4e 283A pointer to a routine to flush any cached information to disk.
bcd70bc9
KB
284If the database is in memory only, the
285.I sync
8e982eee
KB
286routine has no effect and will always succeed.
287.I Sync
288routines return -1 on error (setting
289.IR errno )
290and 0 on success.
ad422b65 291.SH "KEY/DATA PAIRS"
8e982eee 292Access to all file types is based on key/data pairs.
ad422b65 293Both keys and data are represented by the following data structure:
bcd70bc9 294.PP
bcd70bc9
KB
295typedef struct {
296.RS
189ff9f0 297void *data;
bcd70bc9
KB
298.br
299size_t size;
300.RE
932a5d4e 301} DBT;
bcd70bc9 302.PP
ad422b65 303The elements of the DBT structure are defined as follows:
bcd70bc9
KB
304.TP
305data
306A pointer to a byte string.
307.TP
308size
309The length of the byte string.
ad422b65 310.PP
f52ca292
KB
311Key and data byte strings may reference strings of essentially unlimited
312length although any two of them must fit into available memory at the same
313time.
314It should be noted that the access methods provide no guarantees about
315byte string alignment.
bcd70bc9
KB
316.SH ERRORS
317The
f52ca292
KB
318.I dbopen
319routine may fail and set
8e982eee
KB
320.I errno
321for any of the errors specified for the library routines
bcd70bc9
KB
322.IR open (2)
323and
324.IR malloc (3)
325or the following:
326.TP
189ff9f0 327[EFTYPE]
f52ca292 328A file is incorrectly formatted.
189ff9f0 329.TP
bcd70bc9
KB
330[EINVAL]
331A parameter has been specified (hash function, pad byte etc.) that is
332incompatible with the current file specification or there is a mismatch
333between the version number of file and the software.
334.PP
335The
bcd70bc9 336.I close
8e982eee
KB
337routines may fail and set
338.I errno
339for any of the errors specified for the library routines
bcd70bc9 340.IR close (2),
8e982eee
KB
341.IR read (2),
342.IR write (2),
bcd70bc9
KB
343.IR free (3),
344or
345.IR fsync (2).
346.PP
347The
16d71a3a 348.IR del ,
8e982eee
KB
349.IR get ,
350.I put
351and
352.I seq
353routines may fail and set
354.I errno
355for any of the errors specified for the library routines
356.IR read (2),
357.IR write (2),
358.IR free (3)
359or
360.IR malloc (3).
361.PP
362The
bcd70bc9 363.I sync
8e982eee
KB
364routines may fail and set
365.I errno
366for any of the errors specified for the library routine
bcd70bc9 367.IR fsync (2).
8e982eee 368.SH "SEE ALSO"
f52ca292
KB
369.IR btree (3),
370.IR hash (3),
371.IR mpool (3),
372.IR recno (3)
932a5d4e
KB
373.SH BUGS
374The typedef DBT is a mnemonic for ``data base thang'', and was used
5cbd5d23 375because noone could think of a reasonable name that wasn't already used.
ad422b65 376.PP
8e982eee
KB
377None of the access methods provide any form of concurrent access,
378locking, or transactions.