libc/db: Sync with FreeBSD
[dragonfly.git] / lib / libc / db / man / hash.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. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)hash.3      8.6 (Berkeley) 8/18/94
29 .\" $FreeBSD: head/lib/libc/db/man/hash.3 231564 2012-02-12 18:29:56Z ed $
30 .\"
31 .Dd August 18, 1994
32 .Dt HASH 3
33 .Os
34 .Sh NAME
35 .Nm hash
36 .Nd "hash database access method"
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/types.h
41 .In db.h
42 .Sh DESCRIPTION
43 The routine
44 .Fn dbopen
45 is the library interface to database files.
46 One of the supported file formats is
47 .Nm
48 files.
49 The general description of the database access methods is in
50 .Xr dbopen 3 ,
51 this manual page describes only the
52 .Nm
53 specific information.
54 .Pp
55 The
56 .Nm
57 data structure is an extensible, dynamic hashing scheme.
58 .Pp
59 The access method specific data structure provided to
60 .Fn dbopen
61 is defined in the
62 .In db.h
63 include file as follows:
64 .Bd -literal
65 typedef struct {
66         unsigned int bsize;
67         unsigned int ffactor;
68         unsigned int nelem;
69         unsigned int cachesize;
70         uint32_t (*hash)(const void *, size_t);
71         int lorder;
72 } HASHINFO;
73 .Ed
74 .Pp
75 The elements of this structure are as follows:
76 .Bl -tag -width indent
77 .It Va bsize
78 The
79 .Va bsize
80 element
81 defines the
82 .Nm
83 table bucket size, and is, by default, 4096 bytes.
84 It may be preferable to increase the page size for disk-resident tables
85 and tables with large data items.
86 .It Va ffactor
87 The
88 .Va ffactor
89 element
90 indicates a desired density within the
91 .Nm
92 table.
93 It is an approximation of the number of keys allowed to accumulate in any
94 one bucket, determining when the
95 .Nm
96 table grows or shrinks.
97 The default value is 8.
98 .It Va nelem
99 The
100 .Va nelem
101 element
102 is an estimate of the final size of the
103 .Nm
104 table.
105 If not set or set too low,
106 .Nm
107 tables will expand gracefully as keys
108 are entered, although a slight performance degradation may be noticed.
109 The default value is 1.
110 .It Va cachesize
111 A suggested maximum size, in bytes, of the memory cache.
112 This value is
113 .Em only
114 advisory, and the access method will allocate more memory rather
115 than fail.
116 .It Va hash
117 The
118 .Va hash
119 element
120 is a user defined
121 .Nm
122 function.
123 Since no
124 .Nm
125 function performs equally well on all possible data, the
126 user may find that the built-in
127 .Nm
128 function does poorly on a particular
129 data set.
130 User specified
131 .Nm
132 functions must take two arguments (a pointer to a byte
133 string and a length) and return a 32-bit quantity to be used as the
134 .Nm
135 value.
136 .It Va lorder
137 The byte order for integers in the stored database metadata.
138 The number should represent the order as an integer; for example,
139 big endian order would be the number 4,321.
140 If
141 .Va lorder
142 is 0 (no order is specified) the current host order is used.
143 If the file already exists, the specified value is ignored and the
144 value specified when the tree was created is used.
145 .El
146 .Pp
147 If the file already exists (and the
148 .Dv O_TRUNC
149 flag is not specified), the
150 values specified for the
151 .Va bsize , ffactor , lorder
152 and
153 .Va nelem
154 arguments
155 are
156 ignored and the values specified when the tree was created are used.
157 .Pp
158 If a
159 .Nm
160 function is specified,
161 .Fn __hash_open
162 will attempt to determine if the
163 .Nm
164 function specified is the same as
165 the one with which the database was created, and will fail if it is not.
166 .Pp
167 Backward compatible interfaces to the older
168 .Em dbm
169 and
170 .Em ndbm
171 routines are provided, however these interfaces are not compatible with
172 previous file formats.
173 .Sh ERRORS
174 The
175 .Nm
176 access method routines may fail and set
177 .Va errno
178 for any of the errors specified for the library routine
179 .Xr dbopen 3 .
180 .Sh SEE ALSO
181 .Xr btree 3 ,
182 .Xr dbopen 3 ,
183 .Xr mpool 3 ,
184 .Xr recno 3
185 .Rs
186 .%T "Dynamic Hash Tables"
187 .%A Per-Ake Larson
188 .%R "Communications of the ACM"
189 .%D April 1988
190 .Re
191 .Rs
192 .%T "A New Hash Package for UNIX"
193 .%A Margo Seltzer
194 .%R "USENIX Proceedings"
195 .%D Winter 1991
196 .Re
197 .Sh BUGS
198 Only big and little endian byte order is supported.