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