1.\" $OpenBSD: hash.3,v 1.19 2015/09/10 10:20:55 jmc Exp $ 2.\" $NetBSD: hash.3,v 1.6 1996/05/03 21:26:50 cgd Exp $ 3.\" 4.\" Copyright (c) 1997, Phillip F Knaack. All rights reserved. 5.\" 6.\" Copyright (c) 1990, 1993 7.\" The Regents of the University of California. All rights reserved. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.\" @(#)hash.3 8.6 (Berkeley) 8/18/94 34.\" 35.Dd $Mdocdate: September 10 2015 $ 36.Dt HASH 3 37.Os 38.Sh NAME 39.Nm hash 40.Nd hash database access method 41.Sh SYNOPSIS 42.In sys/types.h 43.In db.h 44.Sh DESCRIPTION 45The 46.Fn dbopen 47routine is the library interface to database files. 48One of the supported file formats is hash files. 49The general description of the database access methods is in 50.Xr dbopen 3 . 51This manual page describes only the hash specific information. 52.Pp 53The hash data structure is an extensible, dynamic hashing scheme. 54.Pp 55The access method specific data structure provided to 56.Fn dbopen 57is defined in the 58.In db.h 59include file as follows: 60.Bd -literal -offset indent 61typedef struct { 62 unsigned int bsize; 63 unsigned int ffactor; 64 unsigned int nelem; 65 unsigned int cachesize; 66 u_int32_t (*hash)(const void *, size_t); 67 int lorder; 68} HASHINFO; 69.Ed 70.Pp 71The elements of this structure are as follows: 72.Bl -tag -width XXXXXX -offset indent 73.It Fa bsize 74.Fa bsize 75defines the hash table bucket size, and is, by default, 76the block size of the underlying filesystem. 77It may be preferable to increase the page size for disk-resident tables 78and 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 any 83one bucket, determining when the hash table grows or shrinks. 84The default value is the same as 85.Fa bsize . 86.It Fa nelem 87.Fa nelem 88is an estimate of the final size of the hash table. 89If not set or set too low, hash tables will expand gracefully as keys 90are entered, although a slight performance degradation may be noticed. 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 particular 103data set. 104User specified hash functions must take two arguments (a pointer to a byte 105string and a length) and return a 32-bit quantity to be used as the hash 106value. 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 121values specified for the parameters 122.Fa bsize , ffactor , lorder 123and 124.Fa nelem 125are ignored and the values specified when the tree was created are used. 126.Pp 127If a hash function is specified, 128.Fn hash_open 129will attempt to determine if the hash function specified is the same as 130the one with which the database was created, and will fail if it is not. 131.Pp 132Backward compatible interfaces to the routines described in 133.Xr dbm 3 134and 135.Xr ndbm 3 136are provided, although these interfaces are not compatible with 137previous file formats. 138.Sh ERRORS 139The 140.Nm 141access method routines may fail and set 142.Va errno 143for any of the errors specified for the library routine 144.Xr dbopen 3 . 145.Sh SEE ALSO 146.Xr btree 3 , 147.Xr dbopen 3 , 148.Xr recno 3 149.Rs 150.%T "Dynamic Hash Tables" 151.%A Per-Ake Larson 152.%J Communications of the ACM 153.%D April 1988 154.Re 155.Rs 156.%T "A New Hash Package for UNIX" 157.%A Margo Seltzer 158.%J USENIX Proceedings 159.%D Winter 1991 160.Re 161.Sh BUGS 162Only big and little endian byte order is supported. 163