OSDN Git Service

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