xref: /netbsd-src/lib/libc/db/man/hash.3 (revision fe6e1d361b416a4e84364af0ea6e9710a4747125)
1.\"	$NetBSD: hash.3,v 1.14 2010/12/16 11:57:20 jruoho 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 December 16, 2010
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.In db.h
56header as follows:
57.Bd -literal -offset indent
58typedef struct {
59	u_int bsize;
60	u_int ffactor;
61	u_int nelem;
62	u_int cachesize;
63	uint32_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 defaults to 4096 for in-memory tables.
73If
74.Fa bsize
75is 0 (no bucket size is specified) a bucket size is chosen based on the
76underlying file system I/O block size.
77It may be preferable to increase the page size for disk-resident
78tables and tables with large data items.
79.It Fa ffactor
80.Fa ffactor
81indicates a desired density within the hash table.
82It is an approximation of the number of keys allowed to accumulate in
83any one bucket, determining when the hash table grows or shrinks.
84The default value is 8.
85.It Fa nelem
86.Fa nelem
87is an estimate of the final size of the hash table.
88If not set or set too low, hash tables will expand gracefully as keys
89are entered, although a slight performance degradation may be
90noticed.
91The default value is 1.
92.It Fa cachesize
93A suggested maximum size, in bytes, of the memory cache.
94This value is
95.Em only
96advisory, and the access method will allocate more memory rather
97than fail.
98.It Fa hash
99.Fa hash
100is a user defined hash function.
101Since no hash function performs equally well on all possible data, the
102user may find that the built-in hash function does poorly on a
103particular data set.
104User specified hash functions must take two arguments (a pointer to a
105byte string and a length) and return a 32-bit quantity to be used as
106the hash value.
107.It Fa lorder
108The byte order for integers in the stored database metadata.
109The number should represent the order as an integer; for example,
110big endian order would be the number 4,321.
111If
112.Fa lorder
113is 0 (no order is specified) the current host order is used.
114If the file already exists, the specified value is ignored and the
115value specified when the tree was created is used.
116.El
117.Pp
118If the file already exists (and the
119.Dv O_TRUNC
120flag is not specified), the values specified for the parameters
121.Fa bsize ,
122.Fa ffactor ,
123.Fa lorder ,
124and
125.Fa nelem
126are ignored and the values specified when the tree was created are
127used.
128.Pp
129If a hash function is specified,
130.Fn hash_open
131will attempt to determine if the hash function specified is the same
132as the one with which the database was created, and will fail if it is
133not.
134.\".Pp
135.\"Backward compatible interfaces to the routines described in
136.\".Xr dbm 3 ,
137.\"and
138.\".Xr ndbm 3
139.\"are provided, however these interfaces are not compatible with
140.\"previous file formats.
141.Sh ERRORS
142The
143.Nm
144access method routines may fail and set
145.Va errno
146for any of the errors specified for the library routine
147.Xr dbopen 3 .
148.Sh SEE ALSO
149.Xr btree 3 ,
150.Xr dbopen 3 ,
151.Xr mpool 3 ,
152.Xr recno 3
153.Pp
154.Rs
155.%T Dynamic Hash Tables
156.%A Per-Ake Larson
157.%J Communications of the ACM
158.%D April 1988
159.%N Issue 4
160.%V Volume 31
161.Re
162.Rs
163.%T A New Hash Package for UNIX
164.%A Margo Seltzer
165.%I USENIX Association
166.%B Proceedings of the 1991 Winter USENIX Technical Conference
167.%D January 1991
168.%P 173-184
169.%U http://www.usenix.org/publications/library/proceedings/seltzer2.pdf
170.Re
171.Sh BUGS
172Only big and little endian byte order is supported.
173