1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1999,2008 Oracle.  All rights reserved.
5 *
6 * $Id: hash_method.c,v 12.9 2008/01/08 20:58:34 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/db_page.h"
13#include "dbinc/hash.h"
14
15static int __ham_set_h_ffactor __P((DB *, u_int32_t));
16static int __ham_set_h_hash
17	       __P((DB *, u_int32_t(*)(DB *, const void *, u_int32_t)));
18static int __ham_set_h_nelem __P((DB *, u_int32_t));
19
20/*
21 * __ham_db_create --
22 *	Hash specific initialization of the DB structure.
23 *
24 * PUBLIC: int __ham_db_create __P((DB *));
25 */
26int
27__ham_db_create(dbp)
28	DB *dbp;
29{
30	HASH *hashp;
31	int ret;
32
33	if ((ret = __os_malloc(dbp->env,
34	    sizeof(HASH), &dbp->h_internal)) != 0)
35		return (ret);
36
37	hashp = dbp->h_internal;
38
39	hashp->h_nelem = 0;			/* Defaults. */
40	hashp->h_ffactor = 0;
41	hashp->h_hash = NULL;
42	hashp->h_compare = NULL;
43
44	dbp->get_h_ffactor = __ham_get_h_ffactor;
45	dbp->set_h_ffactor = __ham_set_h_ffactor;
46	dbp->set_h_hash = __ham_set_h_hash;
47	dbp->set_h_compare = __ham_set_h_compare;
48	dbp->get_h_nelem = __ham_get_h_nelem;
49	dbp->set_h_nelem = __ham_set_h_nelem;
50
51	return (0);
52}
53
54/*
55 * PUBLIC: int __ham_db_close __P((DB *));
56 */
57int
58__ham_db_close(dbp)
59	DB *dbp;
60{
61	if (dbp->h_internal == NULL)
62		return (0);
63	__os_free(dbp->env, dbp->h_internal);
64	dbp->h_internal = NULL;
65	return (0);
66}
67
68/*
69 * __ham_get_h_ffactor --
70 *
71 * PUBLIC: int __ham_get_h_ffactor __P((DB *, u_int32_t *));
72 */
73int
74__ham_get_h_ffactor(dbp, h_ffactorp)
75	DB *dbp;
76	u_int32_t *h_ffactorp;
77{
78	HASH *hashp;
79
80	hashp = dbp->h_internal;
81	*h_ffactorp = hashp->h_ffactor;
82	return (0);
83}
84
85/*
86 * __ham_set_h_ffactor --
87 *	Set the fill factor.
88 */
89static int
90__ham_set_h_ffactor(dbp, h_ffactor)
91	DB *dbp;
92	u_int32_t h_ffactor;
93{
94	HASH *hashp;
95
96	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_ffactor");
97	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
98
99	hashp = dbp->h_internal;
100	hashp->h_ffactor = h_ffactor;
101	return (0);
102}
103
104/*
105 * __ham_set_h_hash --
106 *	Set the hash function.
107 */
108static int
109__ham_set_h_hash(dbp, func)
110	DB *dbp;
111	u_int32_t (*func) __P((DB *, const void *, u_int32_t));
112{
113	HASH *hashp;
114
115	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_hash");
116	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
117
118	hashp = dbp->h_internal;
119	hashp->h_hash = func;
120	return (0);
121}
122
123/*
124 * __ham_set_h_compare --
125 *	Set the comparison function.
126 *
127 * PUBLIC: int __ham_set_h_compare
128 * PUBLIC:         __P((DB *, int (*)(DB *, const DBT *, const DBT *)));
129 */
130int
131__ham_set_h_compare(dbp, func)
132	DB *dbp;
133	int (*func) __P((DB *, const DBT *, const DBT *));
134{
135	HASH *t;
136
137	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_compare");
138	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
139
140	t = dbp->h_internal;
141
142	t->h_compare = func;
143
144	return (0);
145}
146
147/*
148 * __db_get_h_nelem --
149 *
150 * PUBLIC: int __ham_get_h_nelem __P((DB *, u_int32_t *));
151 */
152int
153__ham_get_h_nelem(dbp, h_nelemp)
154	DB *dbp;
155	u_int32_t *h_nelemp;
156{
157	HASH *hashp;
158
159	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
160
161	hashp = dbp->h_internal;
162	*h_nelemp = hashp->h_nelem;
163	return (0);
164}
165
166/*
167 * __ham_set_h_nelem --
168 *	Set the table size.
169 */
170static int
171__ham_set_h_nelem(dbp, h_nelem)
172	DB *dbp;
173	u_int32_t h_nelem;
174{
175	HASH *hashp;
176
177	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_nelem");
178	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
179
180	hashp = dbp->h_internal;
181	hashp->h_nelem = h_nelem;
182	return (0);
183}
184