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