xref: /freebsd/lib/libc/db/man/hash.3 (revision 4b9d6057)
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.Dd August 18, 1994
29.Dt HASH 3
30.Os
31.Sh NAME
32.Nm hash
33.Nd "hash database access method"
34.Sh SYNOPSIS
35.In sys/types.h
36.In db.h
37.Sh DESCRIPTION
38The routine
39.Fn dbopen
40is the library interface to database files.
41One of the supported file formats is
42.Nm
43files.
44The general description of the database access methods is in
45.Xr dbopen 3 ,
46this manual page describes only the
47.Nm
48specific information.
49.Pp
50The
51.Nm
52data structure is an extensible, dynamic hashing scheme.
53.Pp
54The access method specific data structure provided to
55.Fn dbopen
56is defined in the
57.In db.h
58include file as follows:
59.Bd -literal
60typedef struct {
61	u_int bsize;
62	u_int ffactor;
63	u_int nelem;
64	u_int cachesize;
65	uint32_t (*hash)(const void *, size_t);
66	int lorder;
67} HASHINFO;
68.Ed
69.Pp
70The elements of this structure are as follows:
71.Bl -tag -width indent
72.It Va bsize
73The
74.Va bsize
75element
76defines the
77.Nm
78table bucket size, and is, by default, 4096 bytes.
79It may be preferable to increase the page size for disk-resident tables
80and tables with large data items.
81.It Va ffactor
82The
83.Va ffactor
84element
85indicates a desired density within the
86.Nm
87table.
88It is an approximation of the number of keys allowed to accumulate in any
89one bucket, determining when the
90.Nm
91table grows or shrinks.
92The default value is 8.
93.It Va nelem
94The
95.Va nelem
96element
97is an estimate of the final size of the
98.Nm
99table.
100If not set or set too low,
101.Nm
102tables will expand gracefully as keys
103are entered, although a slight performance degradation may be noticed.
104The default value is 1.
105.It Va cachesize
106A suggested maximum size, in bytes, of the memory cache.
107This value is
108.Em only
109advisory, and the access method will allocate more memory rather
110than fail.
111.It Va hash
112The
113.Va hash
114element
115is a user defined
116.Nm
117function.
118Since no
119.Nm
120function performs equally well on all possible data, the
121user may find that the built-in
122.Nm
123function does poorly on a particular
124data set.
125User specified
126.Nm
127functions must take two arguments (a pointer to a byte
128string and a length) and return a 32-bit quantity to be used as the
129.Nm
130value.
131.It Va lorder
132The byte order for integers in the stored database metadata.
133The number should represent the order as an integer; for example,
134big endian order would be the number 4,321.
135If
136.Va lorder
137is 0 (no order is specified) the current host order is used.
138If the file already exists, the specified value is ignored and the
139value specified when the tree was created is used.
140.El
141.Pp
142If the file already exists (and the
143.Dv O_TRUNC
144flag is not specified), the
145values specified for the
146.Va bsize , ffactor , lorder
147and
148.Va nelem
149arguments
150are
151ignored and the values specified when the tree was created are used.
152.Pp
153If a
154.Nm
155function is specified,
156.Fn hash_open
157will attempt to determine if the
158.Nm
159function specified is the same as
160the one with which the database was created, and will fail if it is not.
161.Pp
162Backward compatible interfaces to the older
163.Em dbm
164and
165.Em ndbm
166routines are provided, however these interfaces are not compatible with
167previous file formats.
168.Sh ERRORS
169The
170.Nm
171access method routines may fail and set
172.Va errno
173for any of the errors specified for the library routine
174.Xr dbopen 3 .
175.Sh SEE ALSO
176.Xr btree 3 ,
177.Xr dbopen 3 ,
178.Xr mpool 3 ,
179.Xr recno 3
180.Rs
181.%T "Dynamic Hash Tables"
182.%A Per-Ake Larson
183.%R "Communications of the ACM"
184.%D April 1988
185.Re
186.Rs
187.%T "A New Hash Package for UNIX"
188.%A Margo Seltzer
189.%R "USENIX Proceedings"
190.%D Winter 1991
191.Re
192.Sh BUGS
193Only big and little endian byte order is supported.
194