Commit | Line | Data |
---|---|---|
bcd70bc9 KB |
1 | .\" Copyright (c) 1990 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
4 | .\" %sccs.include.redist.man% | |
5 | .\" | |
6 | .\" @(#)dbopen.3 5.1 (Berkeley) %G% | |
7 | .\" | |
8 | .TH DB 3 "" | |
9 | .UC 7 | |
10 | .SH NAME | |
11 | btree_open, flat_open, hash_open \- database manipulation routines | |
12 | .SH SYNOPSIS | |
13 | .nf | |
14 | .ft B | |
15 | #include <db.h> | |
16 | ||
17 | DB * | |
18 | btree_open(const char *file, int flags, int mode, const BTREEINFO * private); | |
19 | ||
20 | DB * | |
21 | flat_open(const char *file, int flags, int mode, const FLATINFO * private); | |
22 | ||
23 | DB * | |
24 | hash_open(const char *file, int flags, int mode, const HASHINFO * private); | |
25 | .ft R | |
26 | .fi | |
27 | .SH DESCRIPTION | |
28 | .IR Btree_open , | |
29 | .IR flat_open , | |
30 | and | |
31 | .I hash_open | |
32 | are interfaces, respectively, to database files in btree, flat, | |
33 | and hashed record formats. | |
34 | Access to all file types is based on key/data pairs, where both keys | |
35 | and data are of essentially unlimited size. | |
36 | .PP | |
37 | Each routine opens | |
38 | .I file | |
39 | for reading and/or writing. | |
40 | The | |
41 | .I flags | |
42 | and | |
43 | .I mode arguments | |
44 | are as specified to the | |
45 | .IR open (2) | |
46 | routine, however only the O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC | |
47 | and O_WRONLY flags are meaningful. | |
48 | Databases which are temporary, i.e. not intended to be preserved | |
49 | on disk, may be created by setting the file parameter to NULL. | |
50 | The argument | |
51 | .I private | |
52 | is a pointer to a private, access-method specific structure described | |
53 | below. | |
54 | .PP | |
55 | The open routines return a pointer to a structure representing the | |
56 | database on success and NULL on error. | |
57 | This structure is as follows: | |
58 | .sp | |
59 | typedef struct { | |
60 | .RS | |
61 | void *internal; | |
62 | .br | |
63 | int (*close)(), (*delete)(), (*get)(), (*put)(), (*seq)(), (*sync)(); | |
64 | .RE | |
65 | } DB; | |
66 | .sp | |
67 | .PP | |
68 | This structure is as follows: | |
69 | .TP | |
70 | internal | |
71 | A pointer to an internal structure private to the access method. | |
72 | .TP | |
73 | close | |
74 | A pointer to a routine to flush any cached information to disk, free any | |
75 | allocated resources, and close the database file, whose function prototype | |
76 | is: | |
77 | .sp | |
78 | .in +5 | |
79 | close(const DB *db); | |
80 | .in -5 | |
81 | .sp | |
82 | Since key/data pairs may be cached in memory, failing to close the | |
83 | file with the | |
84 | .I close | |
85 | routine may result in inconsistent or lost information. | |
86 | The | |
87 | .I close | |
88 | routine returns 0 on error and 1 on success. | |
89 | .TP | |
90 | delete | |
91 | A pointer to a routine to remove key/data pairs from the database, | |
92 | whose function prototype is: | |
93 | .sp | |
94 | .in +5 | |
95 | delete(const DB *db, const VALUE *key); | |
96 | .in -5 | |
97 | .sp | |
98 | The | |
99 | .I delete | |
100 | routine returns 0 on error, 1 on success, and -1 if the specified | |
101 | .I key | |
102 | was not in the file. | |
103 | .TP | |
104 | get | |
105 | A pointer to a routine which is the interface for keyed retrieval from | |
106 | the database, whose function prototype is: | |
107 | .sp | |
108 | .in +5 | |
109 | get(const DB *db, const VALUE *key, VALUE *data); | |
110 | .in -5 | |
111 | .sp | |
112 | The address and length of the data associated with the specified | |
113 | .I key | |
114 | are returned in the structure referenced by | |
115 | .IR data . | |
116 | The | |
117 | .I get | |
118 | routine returns 0 on error, 1 on success, and -1 if the | |
119 | .I key | |
120 | was not in the file. | |
121 | .TP | |
122 | put | |
123 | A pointer to a routine to store key/data pairs in the database, | |
124 | whose function prototype is: | |
125 | .sp | |
126 | .in +5 | |
127 | put(const DB *db, const VALUE *key, const VALUE *data, u_long flag); | |
128 | .in -5 | |
129 | .sp | |
130 | The parameter flag, if set, should be either R_APPEND or R_INSERT, | |
131 | optionally | |
132 | .IR or 'ed | |
133 | with R_NOOVERWRITE. | |
134 | .RS | |
135 | .TP | |
136 | R_APPEND | |
137 | Append the data immediately after the data referenced by | |
138 | .IR key , | |
139 | creating a new record. | |
140 | (This implies that the access method is able to create new keys itself, | |
141 | i.e. the keys are ordered and independent, for example, record numbers. | |
142 | Currently applicable only to the flat file access method.) | |
143 | .TP | |
144 | R_INSERT | |
145 | Insert the data immediately before the data referenced by | |
146 | .IR key , | |
147 | creating a new record. | |
148 | (This implies that the access method is able to create new keys itself, | |
149 | i.e. the keys are ordered and independent, for example, record numbers. | |
150 | Currently applicable only to the flat file access method.) | |
151 | .TP | |
152 | R_NOOVERWRITE | |
153 | Enter the new key/data pair only if the key does not previously exist. | |
154 | .RE | |
155 | .PP | |
156 | The | |
157 | .I put | |
158 | routine returns 0 on error, 1 on success, and -1 if the | |
159 | R_NOOVERWRITE | |
160 | .I flag | |
161 | is set and the key already exists in the file. | |
162 | .TP | |
163 | seq | |
164 | A pointer to a routine which is the interface for sequential | |
165 | retrieval from the database, whose function prototype is: | |
166 | .sp | |
167 | .in +5 | |
168 | seq(const DB *db, VALUE *key, VALUE *data, int flag); | |
169 | .in -5 | |
170 | .sp | |
171 | The address and length of the key are returned in the structure | |
172 | referenced by | |
173 | .IR key , | |
174 | and the address and length of the data are returned in the | |
175 | structure referenced | |
176 | by | |
177 | .IR data . | |
178 | .PP | |
179 | The flag value, if set, should be one of the following values: | |
180 | .RS | |
181 | .TP | |
182 | R_FIRST | |
183 | The first key of the hash table is returned. | |
184 | .TP | |
185 | R_LAST | |
186 | The last key of the hash table is returned. | |
187 | .TP | |
188 | R_NEXT | |
189 | Retrieve the record immediately after the most recently requested | |
190 | record. | |
191 | .TP | |
192 | R_PREV | |
193 | Retrieve the record immediately before the most recently requested | |
194 | record. | |
195 | .RE | |
196 | .PP | |
197 | The first time the | |
198 | .I seq | |
199 | routine is called, the first record of the database is returned | |
200 | if | |
201 | .I flag | |
202 | is not set or is set to R_FIRST or R_NEXT. | |
203 | .PP | |
204 | The | |
205 | .I seq | |
206 | routine returns 0 on error, 1 on success, -1 if end-of-file is reached, | |
207 | and -2 if the input is a character device and no complete records are | |
208 | available. | |
209 | .TP | |
210 | sync | |
211 | A pointer to a routine to flush any cached information to disk, | |
212 | whose function prototype is: | |
213 | .sp | |
214 | .in +5 | |
215 | sync(const DB *db); | |
216 | .in -5 | |
217 | .sp | |
218 | If the database is in memory only, the | |
219 | .I sync | |
220 | routine is a no-op. | |
221 | The | |
222 | .I sync | |
223 | routine returns 0 on error and 1 on success. | |
224 | .PP | |
225 | Each of the routines take a pointer to a structure as returned by | |
226 | the open routine, one or more pointers to key/data structures, and, | |
227 | optionally, a flag value. | |
228 | .PP | |
229 | Keys (and data) are represented by the following data structure: | |
230 | .sp | |
231 | typedef struct { | |
232 | .RS | |
233 | u_char *data; | |
234 | .br | |
235 | size_t size; | |
236 | .RE | |
237 | } ENTRY; | |
238 | .PP | |
239 | The elements of this structure are as follows: | |
240 | .TP | |
241 | data | |
242 | A pointer to a byte string. | |
243 | .TP | |
244 | size | |
245 | The length of the byte string. | |
246 | .SH BTREE | |
247 | One of the access methods is a btree: a sorted, balanced | |
248 | tree structure with associated key and data pairs. | |
249 | .PP | |
250 | <Mike fill this in?> | |
251 | .PP | |
252 | The private data structure provided to | |
253 | .I btree_open | |
254 | is as follows: | |
255 | .sp | |
256 | typedef struct { | |
257 | .RS | |
258 | u_long flags; | |
259 | .br | |
260 | int cachesize; | |
261 | .br | |
262 | int pagesize; | |
263 | .RE | |
264 | } BTREEINFO; | |
265 | .PP | |
266 | The elements of this structure are as follows: | |
267 | .TP | |
268 | flags | |
269 | The flag value is specified by | |
270 | .IR or 'ing | |
271 | the following values: | |
272 | .RS | |
273 | .TP | |
274 | R_SMALLCACHE | |
275 | A flag informing the routines that they are not expected to be | |
276 | the primary data cache, and to minimize any caching they do. | |
277 | .RE | |
278 | .TP | |
279 | cachesize | |
280 | .TP | |
281 | pagesize | |
282 | .SH HASH | |
283 | One of the access methods is hashed access and storage. | |
284 | The private data structure provided to | |
285 | .I hash_open | |
286 | is as follows: | |
287 | .sp | |
288 | typedef struct { | |
289 | .RS | |
290 | u_long flags; | |
291 | .br | |
292 | int bsize; | |
293 | .br | |
294 | int ffactor; | |
295 | .br | |
296 | int nelem; | |
297 | .br | |
298 | u_long (*hash)(const void *, const size_t); | |
299 | .RE | |
300 | } HASHINFO; | |
301 | .PP | |
302 | The elements of this structure are as follows: | |
303 | .TP | |
304 | flags | |
305 | The flag value is specified by | |
306 | .IR or 'ing | |
307 | the following values: | |
308 | .RS | |
309 | .TP | |
310 | R_SMALLCACHE | |
311 | A flag informing the routines that they are not expected to be | |
312 | the primary cache, and to minimize any caching they do. | |
313 | .RE | |
314 | .TP | |
315 | bsize | |
316 | .I Bsize | |
317 | defines the hash table bucket size, and is, by default 1024, bytes. | |
318 | For tables with large data items, it may be preferable to increase the | |
319 | page size, and, conversely, applications doing exclusively in-memory hashing | |
320 | may want to use a very small bucket size, for example, 256, to minimize | |
321 | hash chain collisions. | |
322 | .TP | |
323 | ffactor | |
324 | .I Ffactor | |
325 | indicates a desired density within the hash table. | |
326 | It is an approximation of the number of keys allowed to accumulate in any | |
327 | one bucket, determining when the hash table grows or shrinks. | |
328 | The default value is 5. | |
329 | .TP | |
330 | hash | |
331 | .I Hash | |
332 | is a user defined hash function. | |
333 | Since no hash function performs equally well on all possible data, the | |
334 | user may find that the built-in hash function does poorly on a particular | |
335 | data set. | |
336 | Any user specified hash function should take two arguments, a pointer to | |
337 | a byte string and a length, and return an unsigned long to be used as | |
338 | the hash value. | |
339 | .TP | |
340 | nelem | |
341 | .I Nelem | |
342 | is an estimate of the final size of the hash table. | |
343 | If not set, the default value is 1. | |
344 | If not set or set too low, hash tables will expand gracefully as keys | |
345 | are entered, although a slight performance degradation may be noticed. | |
346 | .PP | |
347 | If the hash table already exists, and the O_TRUNC flag is not | |
348 | specified to | |
349 | .IR hash_open , | |
350 | the parameters | |
351 | .IR bsize , | |
352 | .IR ffactor , | |
353 | and | |
354 | .I nelem | |
355 | are ignored. | |
356 | .PP | |
357 | If a hash function is specified, | |
358 | .I hash_open | |
359 | will attempt to determine if the hash function specified is the same as | |
360 | the one with which the database was created, and will fail if it is not. | |
361 | .PP | |
362 | Backward compatible interfaces to the routines described in | |
363 | .IR dbm (3), | |
364 | .IR hsearch (3), | |
365 | and | |
366 | .IR ndbm (3) | |
367 | are provided as part of the compatibility library, ``libcompat.a''. | |
368 | .SH "FLAT FILES" | |
369 | One of the access methods is either variable or fixed-length records, | |
370 | the former delimited by a specific byte value. | |
371 | The private data structure provided to | |
372 | .I flat_open | |
373 | is as follows: | |
374 | .sp | |
375 | typedef struct { | |
376 | .RS | |
377 | u_long flags; | |
378 | .br | |
379 | int cachesize; | |
380 | .br | |
381 | size_t reclen; | |
382 | .br | |
383 | u_char bval; | |
384 | .RE | |
385 | } VLENINFO; | |
386 | .PP | |
387 | The elements of this structure are as follows: | |
388 | .TP | |
389 | flags | |
390 | The flag value is specified by | |
391 | .IR or 'ing | |
392 | the following values: | |
393 | .RS | |
394 | .TP | |
395 | R_FIXEDLEN | |
396 | The records are fixed-length, not byte delimited. | |
397 | The structure element | |
398 | .I reclen | |
399 | specifies the length of the record, and the structure element | |
400 | .I bval | |
401 | is used as the pad character. | |
402 | .TP | |
403 | R_SMALLCACHE | |
404 | A flag informing the routines that they are not expected to be | |
405 | the primary cache, and to minimize any caching they do. | |
406 | .RE | |
407 | .TP | |
408 | cachesize | |
409 | The amount of memory to be used as a data cache. | |
410 | .TP | |
411 | reclen | |
412 | The length of a fixed-length record. | |
413 | .TP | |
414 | bval | |
415 | The delimiting byte to be used to mark the end of a record for | |
416 | variable-length records, and the pad character for fixed-length | |
417 | records. | |
418 | .PP | |
419 | Variable-length and fixed-length data files require | |
420 | .I key | |
421 | structures to reference a byte followed by three unsigned longs. | |
422 | The numbers are used as a record number, a byte offset and a record length, | |
423 | respectively, and the byte is a flag value which indicates the validity | |
424 | of the other fields. | |
425 | These access methods do no validity checking as to the correctness of any | |
426 | of these values, nor are they constrained to use the values provided. | |
427 | If any of the record number, byte offset or record length are not specified | |
428 | by the calling routine, and the record retrieval is successful, the correct | |
429 | values are copied into the caller's | |
430 | .I key | |
431 | structure. | |
432 | The flag value is specified by | |
433 | .IR or 'ing | |
434 | the following values: | |
435 | .TP | |
436 | R_LENGTH | |
437 | The record length is valid. | |
438 | .TP | |
439 | R_OFFSET | |
440 | The byte offset is valid. | |
441 | .TP | |
442 | R_RECNO | |
443 | The record number is valid. | |
444 | .SH ERRORS | |
445 | The | |
446 | .I open | |
447 | routines may fail and set errno for any of the errors specified for the | |
448 | library routines | |
449 | .IR open (2) | |
450 | and | |
451 | .IR malloc (3) | |
452 | or the following: | |
453 | .TP | |
454 | [EINVAL] | |
455 | A parameter has been specified (hash function, pad byte etc.) that is | |
456 | incompatible with the current file specification or there is a mismatch | |
457 | between the version number of file and the software. | |
458 | .PP | |
459 | The | |
460 | .I get | |
461 | routines may fail and set errno for any of the errors specified for the | |
462 | library routine | |
463 | .IR malloc (3). | |
464 | .PP | |
465 | The | |
466 | .I close | |
467 | routines may fail and set errno for any of the errors specified for the | |
468 | library routines | |
469 | .IR close (2), | |
470 | .IR free (3), | |
471 | or | |
472 | .IR fsync (2). | |
473 | .PP | |
474 | The | |
475 | .I sync | |
476 | routines may fail and set errno for any of the errors specified for the | |
477 | library routine | |
478 | .IR fsync (2). |