summaryrefslogtreecommitdiffstats
path: root/src/plugins/kdb/db2/libdb2/man/db_open.3
blob: f988ef92459404a8c9a314b2b23a7a847c40ed7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
.\" Copyright (c) 1990, 1993, 1994, 1995
.\"	The Regents of the University of California.  All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed by the University of
.\"	California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\"    may be used to endorse or promote products derived from this software
.\"    without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\"	@(#)db_open.3	8.15 (Berkeley) 8/1/95
.\"
.TH DB_OPEN 3 "August 1, 1995"
.UC 7
.SH NAME
db_open \- database access methods
.SH SYNOPSIS
.nf
.ft B
#include <db.h>

DB *
db_open(const char *file, int flags, int mode,
.ti +5
DBTYPE type, DBINFO *dbinfo, const void *openinfo);
.ft R
.fi
.SH DESCRIPTION
.so db.so
.GN
the overall structure of the available access methods.
.PP
The currently supported file formats are btree, hashed, log and recno
(i.e. flat-file oriented).
The btree format is a representation of a sorted, balanced tree structure.
The hashed format is an extensible, dynamic hashing scheme.
The log format is a general-purpose logging facility.
The recno format is a byte stream file with fixed or variable length
records.
The formats and other, format specific information are described in detail
in their respective manual pages:
.IR db_btree (3),
.IR db_hash (3),
.IR db_log (3),
and
.IR db_recno (3).
.PP
Db_open opens
.I file
for reading and/or writing.
Files never intended to be preserved on disk may be created by setting
the file parameter to NULL.
(Note, while most of the access methods use
.I file
as the name of an underlying file on disk, this is not guaranteed.
See the manual pages for the individual access methods for more
information.)
.PP
The
.I flags
and
.I mode arguments
are as specified to the
.IR open (2)
function, however, only the O_CREAT, O_EXCL, O_EXLOCK, O_NONBLOCK,
O_RDONLY, O_RDWR, O_SHLOCK and O_TRUNC flags are meaningful.
(Note, opening a database file O_WRONLY is not possible.)
.\"Three additional options may be specified by
.\".IR or 'ing
.\"them into the
.\".I flags
.\"argument.
.\".TP
.\"DB_LOCK
.\"Do the necessary locking in the database to support concurrent access.
.\"If concurrent access isn't needed or the database is read-only this
.\"flag should not be set, as it tends to have an associated performance
.\"penalty.
.\".TP
.\"DB_SHMEM
.\"Place the underlying memory pool used by the database in shared
.\"memory.
.\"Necessary for concurrent access.
.\".TP
.\"DB_TXN
.\"Support transactions in the database.
.\"The DB_LOCK and DB_SHMEM flags must be set as well.
.PP
The
.I type
argument is of type DBTYPE (as defined in the <db.h> include file) and
may be set to DB_BTREE, DB_HASH, DB_LOG or DB_RECNO.
.PP
The
.I dbinfo 
argument is a pointer to a structure containing references to locking,
logging, transaction, and shared-memory buffer pool information.
If 
.I dbinfo
is NULL, then the access method may still use these subsystems,
but the usage will be private to the application and managed by DB.
If
.I dbinfo
is non-NULL,
then the module referenced by each of the non-NULL fields is used by DB
as necessary.
The fields of the DBINFO structure are defined as follows:
.TP 5
const char *errpfx;
A prefix to prepend to error messages; used only if
.I errfile
is non-NULL.
.TP 5
FILE *errfile;
The
.IR stdio (3)
file stream to which error messages are logged.
.sp
When any error occurs in the
.I db_open
function, or in any function called using a field of the returned DB
structure, an error value is returned by the function,
and the global variable
.I errno
is set appropriately.
In some cases, however, the
.I errno
value may be insufficient to describe the cause of the error.
In these cases, if
.I errfile
is non-NULL, additional error information will be written to the file
stream it represents, preceded by the string, if any, specified by
.IR errpfx .
This error logging facility should not be required for normal operation,
but may be useful in debugging applications.
.TP 5
char *errbuf;
The buffer to which error messages are copied.
If non-NULL,
.I errbuf
behaves as described for
.IR errfile ,
except that the
.I errpfx
field is ignored and the error message is copied into the specified
buffer instead of being written to the FILE stream.
The DB routines assume that the associated buffer is at least 1024 bytes
in length.
.TP 5
LOCK_TABLE_T *lockinfo;
If locking is required for the file being opened (as in the case of
buffers being maintained in a shared memory buffer pool),
the
.I lockinfo
field contains a return value from the function
.I lock_open
that should be used
(see
.IR db_lock (3)).
If
.I lockinfo
is NULL, no locking is done.
.TP 5
DB *loginfo;
If modifications to the file being opened should be logged, the
.I loginfo
field contains a return value from the function
.IR dbopen ,
when opening a DB file of type DB_LOG.
If
.I loginfo
is NULL, no logging is done.
.TP 5
MPOOL *mpoolinfo;
If the cache for the file being opened should be maintained in a shared
buffer pool, the
.I mpoolinfo
field contains a return value from the function 
.I mpool_open
that should be used
(see
.IR db_mpool (3)).
If
.I mpoolinfo
is NULL, a memory pool may still be created,
but it will be private to the application and managed by DB.
.TP 5
TXNMGR *txninfo;
If the accesses to the file being opened should take place in the context
of transactions (providing atomicity and complete error recovery), the
.I txninfo
field contains a return value from the function
.I txn_open
(see
.IR db_txn (3)).
If transactions are specified,
the application is responsible for making suitable calls to 
.IR txn_begin ,
.IR txn_abort ,
and
.IR txn_commit .
If
.I txninfo
is NULL, no transaction support is done.
.PP
The
.I openinfo
argument is a pointer to an access method specific structure described
in the access method's manual page.
If
.I openinfo
is NULL, each access method will use defaults appropriate for the system
and the access method.
.SH "KEY/DATA PAIRS"
Access to all access methods is based on key/data pairs.
Both keys and data are represented by the following data structure:
.PP
typedef struct {
.RS
void *data;
.br
size_t size;
.RE
} DBT;
.PP
The elements of the DBT structure are defined as follows:
.TP 5
data
A pointer to a byte string.
.ns
.br
.TP 5
size
The length of
.IR data ,
in bytes.
.PP
Key and data byte strings may reference strings of essentially unlimited
length, although any two of them must fit into available memory at the
same time.
.PP
The access methods provide no guarantees about byte string alignment,
and applications are responsible for maintaining any necessary alignment.
.SH "DB OPERATIONS"
.I Db_open
returns a pointer to a DB structure (as defined in the <db.h> include file)
on success, and NULL on error.
The DB structure describes a database type, and includes a set of functions
to perform various actions, as described below.
Each of these functions takes a pointer to a DB structure, and may take
one or more DBT *'s and a flag value as well.
Individual access methods specify additional functions and flags which
are specific to the method.
The fields of the DB structure are as follows:
.TP 5
DBTYPE type;
The type of the underlying access method (and file format).
.TP 5
int (*close)(const DB *db);
A pointer to a function to flush any cached information to disk,
free any allocated resources, and close any underlying files.
Since key/data pairs are cached in memory, failing to sync the
file with the
.I close
or
.I sync
function may result in inconsistent or lost information.
.IP
The
.I close
functions return -1 on failure, setting
.IR errno ,
and 0 on success.
.TP 5
int (*del)(const DB *db, TXN *txnid,
.ti +5
const DBT *key, u_int flags);
.br
A pointer to a function to remove key/data pairs from the database.
The key/data pair associated with the specified
.I key
are discarded from the database.
.IP
The
.I txnid
parameter contains a transaction ID returned from
.IR txn_begin ,
if the file is being accessed under transaction protection,
or NULL if transactions are not in effect.
.IP
The parameter
.I flag
must be set to 0 or exactly one of the following values:
.RS
.TP 5
R_CURSOR
Delete the record referenced by the cursor.
The cursor must have previously been initialized.
.RE
.IP
The
.I delete
functions return -1 on error, setting
.IR errno ,
0 on success, and 1 if the specified
.I key
did not exist in the file.
.TP 5
int (*fd)(const DB *db);
A pointer to a function which returns a file descriptor representative
of the underlying database.
A file descriptor referencing the same file will be returned to all
processes which call
.I db_open
with the same
.I file
name.
This file descriptor may be safely used as an argument to the
.IR fcntl (2)
and
.IR flock (2)
locking functions.
The file descriptor is not necessarily associated with any of the
underlying files used by the access method.
No file descriptor is available for in memory databases.
.IP
The
.I fd
functions return -1 on error, setting
.IR errno ,
and the file descriptor on success.
.TP 5
int (*get)(const DB *db, TXN *txnid,
.ti +5
const DBT *key, DBT *data, u_int flags);
.br
A pointer to a function which is the interface for keyed retrieval from
the database.
The address and length of the data associated with the specified
.I key
are returned in the structure referenced by
.IR data .
.IP
The
.I txnid
parameter contains a transaction ID returned from
.IR txn_begin ,
if the file is being accessed under transaction protection,
or NULL if transactions are not in effect.
.IP
The
.I get
functions return -1 on error, setting
.IR errno ,
0 on success, and 1 if the
.I key
was not found.
.TP 5
int (*put)(const DB *db, TXN *txnid,
.ti +5
DBT *key, const DBT *data, u_int flags);
.br
A pointer to a function to store key/data pairs in the database.
.IP
The
.I txnid
parameter contains a transaction ID returned from
.IR txn_begin ,
if the file is being accessed under transaction protection,
or NULL if transactions are not in effect.
.IP
The parameter
.I flag
must be set to 0 or exactly one of the following values:
.RS
.TP 5
R_CURSOR
Replace the key/data pair referenced by the cursor.
The cursor must have previously been initialized.
.TP 5
R_NOOVERWRITE
Enter the new key/data pair only if the key does not previously exist.
.RE
.IP
The default behavior of the
.I put
functions is to enter the new key/data pair, replacing any previously
existing key.
.IP
The
.I put
functions return -1 on error, setting
.IR errno ,
0 on success, and 1 if the R_NOOVERWRITE
.I flag
was set and the key already exists in the file.
.TP 5
int (*seq)(const DB *db, TXN *txnid,
.ti +5
DBT *key, DBT *data, u_int flags);
.br
A pointer to a function which is the interface for sequential
retrieval from the database.
The address and length of the key are returned in the structure
referenced by
.IR key ,
and the address and length of the data are returned in the
structure referenced
by
.IR data .
.IP
The
.I txnid
parameter contains a transaction ID returned from
.IR txn_begin ,
if the file is being accessed under transaction protection,
or NULL if transactions are not in effect.
.IP
Sequential key/data pair retrieval may begin at any time, and the
logical position of the ``cursor'' is not affected by calls to the
.IR del ,
.IR get ,
.IR put ,
or
.I sync
functions.
Modifications to the database during a sequential scan will be reflected
in the scan, i.e. records inserted behind the cursor will not be returned
while records inserted in front of the cursor will be returned.
.IP
The parameter
.I flag
must be set to 0 or exactly one of the following values:
.RS
.TP 5
R_CURSOR
The data associated with the specified key is returned.
This differs from the
.I get
functions in that it sets or initializes the cursor to the location of
the key as well.
.TP 5
R_FIRST
The first key/data pair of the database is returned, and the cursor
is set or initialized to reference it.
.TP 5
R_NEXT
Retrieve the key/data pair immediately after the cursor.
If the cursor is not yet set, this is the same as the R_FIRST flag.
.RE
.IP
The
.I seq
functions return -1 on error, setting
.IR errno ,
0 on success,
and 1 if there are no key/data pairs less than or greater than the
specified or current key.
.TP 5
int (*sync)(const DB *db, u_int flags);
A pointer to a function to flush any cached information to disk.
If the database is in memory only, the
.I sync
function has no effect and will always succeed.
.IP
The parameter
.I flag
must be set to 0 or a value specified by an access method specific
manual page.
.IP
The
.I sync
functions return -1 on failure, setting
.IR errno ,
and 0 on success.
.SH ERRORS
The
.I db_open
function may fail and set
.I errno
for any of the errors specified for the library functions
.IR open (2),
.IR malloc (3)
or the following:
.TP 5
[EFTYPE]
A file is incorrectly formatted.
.TP 5
[EINVAL]
A parameter has been specified (hash function, recno pad byte etc.)
that is incompatible with the current file specification or, a flag
to a function which is not meaningful for the function (for example,
use of the cursor without prior initialization) or there is a mismatch
between the version number of file and the software.
.PP
The
.I close
functions may fail and set
.I errno
for any of the errors specified for the library functions
.IR close (2),
.IR read (2),
.IR write (2),
.IR free (3),
or
.IR fsync (2).
.PP
The
.IR del ,
.IR get ,
.I put
and
.I seq
functions may fail and set
.I errno
for any of the errors specified for the library functions
.IR read (2),
.IR write (2),
.IR free (3)
or
.IR malloc (3).
.PP
The
.I fd
functions will fail and set
.I errno
to ENOENT for in memory databases.
.PP
The
.I sync
functions may fail and set
.I errno
for any of the errors specified for the library function
.IR fsync (2).
.SH "SEE ALSO"
.IR db_btree (3),
.IR db_hash (3),
.IR db_lock (3),
.IR db_log (3),
.IR db_mpool (3),
.IR db_recno (3),
.IR db_txn (3)
.SH BUGS
The name DBT is a mnemonic for ``data base thang'', and was used
because noone could think of a reasonable name that wasn't already
in use somewhere else.
.PP
The
.I fd
function interface is a kluge,
and will be deleted in a future version of the interface.
.PP
Only big and little endian byte order is supported.