1 .\" Copyright (c) 1990 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
32 .\" @(#)db.3 5.16 (Berkeley) 4/2/91
34 .TH DB 3 "April 2, 1991"
37 btree_open, hash_open, recno_open \- database access methods
41 #include <sys/types.h>
45 btree_open(const char *file, int flags, int mode,
47 const BTREEINFO * openinfo);
50 hash_open(const char *file, int flags, int mode,
52 const HASHINFO * openinfo);
55 recno_open(const char *file, int flags, int mode,
57 const RECNOINFO * openinfo);
65 are access method interfaces to database files in btree, hashed, and
66 flat-file formats, respectively.
67 The btree format is a representation of a sorted, balanced tree structure.
68 The hashed format is an extensible, dynamic hashing scheme.
69 The flat-file format is a UNIX file with fixed or variable length
71 These formats are described in more detail below.
73 Access to all file types is based on key/data pairs.
77 for reading and/or writing.
78 Databases never intended to be preserved on disk may be created by setting
79 the file parameter to NULL.
84 are as specified to the
86 routine, however, only the O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC
87 and O_WRONLY flags are meaningful.
90 is a pointer to an access method specific structure described below.
92 The open routines return a pointer to a DB structure on success and NULL
94 The DB structure contains at least the following fields:
99 int (*close)(const DB *db);
100 int (*sync)(const DB *db);
101 int (*del)(const DB *db, const DBT *key, u_int flags);
102 int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
103 int (*put)(const DB *db, const DBT *key, const DBT *data,
106 int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
113 The elements of this structure consist of a pointer to an access method
114 specific structure and a set of routines which perform various functions.
115 All of these routines take a pointer to a structure as returned by
116 one of the open routines, one or more pointers to key/data structures,
117 and, optionally, a flag value.
120 A pointer to an internal structure specific to the access method.
123 The type of the underlying access method; either DB_BTREE, DB_HASH
127 A pointer to a routine to flush any cached information to disk, free any
128 allocated resources, and close the database file.
129 Since key/data pairs may be cached in memory, failing to close the
132 routine may result in inconsistent or lost information.
134 routines return -1 on error (setting
139 A pointer to a routine to remove key/data pairs from the database.
141 routines return -1 on error (setting
143 0 on success, and 1 if the specified
148 A pointer to a routine which is the interface for keyed retrieval from
150 The address and length of the data associated with the specified
152 are returned in the structure referenced by
155 routines return -1 on error (setting
157 0 on success, and 1 if the
162 A pointer to a routine to store key/data pairs in the database.
166 must be set to one of the following values:
170 Append the data immediately after the data referenced by
172 creating a new key/data pair.
173 (This implies that the access method is able to create new keys,
174 i.e. the keys are ordered and independent, for example, record numbers.
175 Applicable only to the
180 Insert the data immediately before the data referenced by
182 creating a new key/data pair.
183 (This implies that the access method is able to create new keys,
184 i.e. the keys are ordered and independent, for example, record numbers.
185 Applicable only to the
190 Enter the new key/data pair only if the key does not previously exist.
193 Enter the new key/data pair and replace any previously existing key.
197 routines return -1 on error (setting
199 0 on success, and 1 if the R_NOOVERWRITE
201 was set and the key already exists in the file.
204 A pointer to a routine which is the interface for sequential
205 retrieval from the database.
206 The address and length of the key are returned in the structure
209 and the address and length of the data are returned in the
214 Sequential key/data pair retrieval may begin at any time, and the
215 position of the ``cursor'' is not affected by calls to the
222 Modifications to the database during a sequential scan will be reflected
223 in the scan, i.e. records inserted behind the cursor will not be returned
224 while records inserted in front of the cursor will be returned.
226 The flag value must be set to one of the following values:
230 The data associated with the specified key is returned.
231 This differs from the
233 routines in that it sets the ``cursor'' to the location of the
235 (This implies that the access method has a implicit order which does
237 Applicable only to the
244 The first key/data pair of the database is returned.
247 The last key/data pair of the database is returned.
248 (This implies that the access method has a implicit order which does
250 Applicable only to the
257 Retrieve the key/data pair immediately after the key/data pair most recently
261 The cursor is moved to the returned key/data pair.
264 is set to R_NEXT the first time the
266 routine is called, the first key/data pair of the database is returned.
269 Retrieve the key/data pair immediately before the key/data pair most recently
273 The cursor is moved to the returned key/data pair.
276 is set to R_PREV the first time the
278 routine is called, the last key/data pair of the database is returned.
279 (This implies that the access method has a implicit order which does
281 Applicable only to the
289 routines return -1 on error (setting
291 0 on success, 1 if there are no more key/data pairs available.
294 access method is being used, and if the database file is a character special
295 file and no complete key/data pairs are currently available, the
300 A pointer to a routine to flush any cached information to disk.
301 If the database is in memory only, the
303 routine has no effect and will always succeed.
305 routines return -1 on error (setting
309 Access to all file types is based on key/data pairs.
310 Both keys and data are represented by the following data structure:
320 The elements of the DBT structure are defined as follows:
323 A pointer to a byte string.
326 The length of the byte string.
328 Key/data strings must fit into available memory.
330 One of the access methods is a btree: a sorted, balanced tree structure
331 with associated key/data pairs.
333 The access method specific data structure provided to
345 int (*compare)(const void *, const void *);
351 The elements of this structure are defined as follows:
354 The flag value is specified by
356 any of the following values:
361 if the key to be inserted already exists,
362 permit insertion anyway.
363 This flag permits duplicate keys in the tree.
365 duplicates are not permitted,
366 and attempts to insert them will fail.
367 Note, the order of retrieval of key/data pairs with duplicate keys is
372 A suggested maximum size, in bytes, of the memory cache.
373 Setting this value to zero specifies that an appropriate amount of memory
375 Since every search examines the root page of the tree, caching the most
376 recently used pages substantially improves access time.
377 In addition, physical writes are delayed as long as possible, so a moderate
378 cache can reduce the number of I/O operations significantly.
379 Obviously, using a cache increases the likelihood of corruption or lost data
380 if the system crashes while a tree is being modified.
382 pages decreases the creation time of a large tree by between two and three
386 Compare is a user defined comparison function.
387 It must return an integer less than, equal to, or greater than zero if the
388 first argument is considered to be respectively less than, equal to, or
389 greater than the second.
390 The same comparison function must be used on a given tree every time it
392 If no comparison function is specified,
397 The byte order for 4-byte integers in the stored database metadata.
398 The number should represent the order as an integer; for example,
399 big endian order would be the number 4,321.
402 is 0 (no order is specified) the current host order is used.
403 If the file already exists, the specified value is ignored and the
404 value specified when the tree was created is used.
405 (Obviously, portability of the data forming the key/data pairs is the
406 concern of the application program.)
409 Page size is the size in bytes of the pages used for nodes in the tree.
410 If the file already exists, the specified value is ignored and the
411 value specified when the tree was created is used.
414 is zero, an appropriate page size is chosen (based on the system memory
415 and/or file system constraints), but will never be less than 512 bytes.
417 If the pointer to the
419 data structure is NULL, the
421 routine will use appropriate values.
423 If the database file already exists, and the O_TRUNC flag is not specified
430 Key structures may reference byte strings of slightly less than one-half the
431 tree's page size only (see
433 Data structures may reference byte strings of essentially unlimited length.
435 Searches, insertions, and deletions in a btree will all complete in
438 Forward sequential scans of a tree are from the least key to the greatest.
440 Space freed up by deleting key/data pairs from a btree is never reclaimed,
441 although it is normally made available for reuse.
442 The exception to this is that space occupied by large data items (those
443 greater than one quarter the size of a page) is neither reclaimed nor reused.
444 This means that the btree storage structure is grow-only.
445 The only solutions are to avoid excessive deletions, or to create a fresh
446 tree periodically from a scan of an existing one.
448 One of the access methods is hashed access and storage.
449 The access method specific data structure provided to
455 u_long (*hash)(const void *, const size_t);
469 The elements of this structure are defined as follows:
473 defines the hash table bucket size, and is, by default, 256 bytes.
474 It may be preferable to increase the page size for disk-resident tables and
475 tables with large data items.
478 A suggested maximum size, in bytes, of the memory cache.
479 Setting this value to zero specifies that an appropriate amount of memory
484 indicates a desired density within the hash table.
485 It is an approximation of the number of keys allowed to accumulate in any
486 one bucket, determining when the hash table grows or shrinks.
487 The default value is 8.
491 is a user defined hash function.
492 Since no hash function performs equally well on all possible data, the
493 user may find that the built-in hash function does poorly on a particular
495 User specified hash functions must take two arguments (a pointer to a byte
496 string and a length) and return an u_long to be used as the hash value.
499 The byte order for 4-byte integers in the stored database metadata.
500 The number should represent the order as an integer; for example,
501 big endian order would be the number 4,321.
504 is 0 (no order is specified) the current host order is used.
505 If the file already exists, the specified value is ignored and the
506 value specified when the tree was created is used.
507 (Obviously, portability of the data forming the key/data pairs is the
508 concern of the application program.)
512 is an estimate of the final size of the hash table.
513 If not set, the default value is 1.
514 If not set or set too low, hash tables will expand gracefully as keys
515 are entered, although a slight performance degradation may be noticed.
517 If the pointer to the
519 data structure is NULL, the
521 routine will use appropriate values.
523 If the hash table already exists, and the O_TRUNC flag is not
533 If a hash function is specified,
535 will attempt to determine if the hash function specified is the same as
536 the one with which the database was created, and will fail if it is not.
538 Both key and data structures may reference byte strings of essentially
541 Backward compatible interfaces to the routines described in
546 are provided, however, these interfaces are not compatible with
547 previous file formats.
549 One of the access methods is either variable or fixed-length records,
550 the former delimited by a specific byte value.
551 The access method specific data structure provided to
567 The elements of this structure are defined as follows:
570 The flag value is specified by
572 any of the following values:
576 The records are fixed-length, not byte delimited.
577 The structure element
579 specifies the length of the record, and the structure element
581 is used as the pad character.
584 This flag requires that a snapshot of the file be taken when
586 is called, instead of permitting any unmodified records to be
587 read from the original file.
591 A suggested maximum size, in bytes, of the memory cache.
592 Setting this value to zero specifies that an appropriate amount of memory
596 The length of a fixed-length record.
599 The delimiting byte to be used to mark the end of a record for
600 variable-length records, and the pad character for fixed-length
603 Variable-length and fixed-length data files require
605 structures to reference the following structure:
619 The elements of this structure are defined as follows:
622 The length of the record.
628 The offset in the file at which the record is located.
631 A flag value which indicates the validity of the other fields in the
633 The flag value is specified by
635 one or more of the following values:
639 The record length is valid.
642 The record number is valid.
645 The byte offset is valid.
648 If the record retrieval is successful, the record number, byte offset and
649 record length are set in the RECNOKEY structure referenced by the caller's
653 Data structures may reference byte strings of essentially unlimited length.
657 routines may fail and set
659 for any of the errors specified for the library routines
666 A file used by one of the
668 routines is incorrectly formatted.
671 A parameter has been specified (hash function, pad byte etc.) that is
672 incompatible with the current file specification or there is a mismatch
673 between the version number of file and the software.
677 routines may fail and set
679 for any of the errors specified for the library routines
693 routines may fail and set
695 for any of the errors specified for the library routines
704 routines may fail and set
706 for any of the errors specified for the library routine
709 .IR "Dynamic Hash Tables" ,
710 Per-Ake Larson, Communications of the ACM, April 1988.
712 .IR "A New Hash Package for UNIX" ,
713 Margo Seltzer, USENIX Proceedings, Winter 1991.
715 The typedef DBT is a mnemonic for ``data base thang'', and was used
716 because noone could think of a reasonable name that wasn't already used.
718 None of the access methods provide any form of concurrent access,
719 locking, or transactions.
721 Only big and little endian byte order is supported.