1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1999-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
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_get_h_hash
17	       __P((DB *, u_int32_t(**)(DB *, const void *, u_int32_t)));
18static int __ham_set_h_hash
19	       __P((DB *, u_int32_t(*)(DB *, const void *, u_int32_t)));
20static int __ham_set_h_nelem __P((DB *, u_int32_t));
21
22static int __ham_get_h_compare
23	__P((DB *, int (**)(DB *, const DBT *, const DBT *)));
24
25/*
26 * __ham_db_create --
27 *	Hash specific initialization of the DB structure.
28 *
29 * PUBLIC: int __ham_db_create __P((DB *));
30 */
31int
32__ham_db_create(dbp)
33	DB *dbp;
34{
35	HASH *hashp;
36	int ret;
37
38	if ((ret = __os_malloc(dbp->env,
39	    sizeof(HASH), &dbp->h_internal)) != 0)
40		return (ret);
41
42	hashp = dbp->h_internal;
43
44	hashp->h_nelem = 0;			/* Defaults. */
45	hashp->h_ffactor = 0;
46	hashp->h_hash = NULL;
47	hashp->h_compare = NULL;
48
49	dbp->get_h_ffactor = __ham_get_h_ffactor;
50	dbp->set_h_ffactor = __ham_set_h_ffactor;
51	dbp->get_h_hash = __ham_get_h_hash;
52	dbp->set_h_hash = __ham_set_h_hash;
53	dbp->get_h_compare = __ham_get_h_compare;
54	dbp->set_h_compare = __ham_set_h_compare;
55	dbp->get_h_nelem = __ham_get_h_nelem;
56	dbp->set_h_nelem = __ham_set_h_nelem;
57
58	return (0);
59}
60
61/*
62 * PUBLIC: int __ham_db_close __P((DB *));
63 */
64int
65__ham_db_close(dbp)
66	DB *dbp;
67{
68	if (dbp->h_internal == NULL)
69		return (0);
70	__os_free(dbp->env, dbp->h_internal);
71	dbp->h_internal = NULL;
72	return (0);
73}
74
75/*
76 * __ham_get_h_ffactor --
77 *
78 * PUBLIC: int __ham_get_h_ffactor __P((DB *, u_int32_t *));
79 */
80int
81__ham_get_h_ffactor(dbp, h_ffactorp)
82	DB *dbp;
83	u_int32_t *h_ffactorp;
84{
85	HASH *hashp;
86
87	hashp = dbp->h_internal;
88	*h_ffactorp = hashp->h_ffactor;
89	return (0);
90}
91
92/*
93 * __ham_set_h_ffactor --
94 *	Set the fill factor.
95 */
96static int
97__ham_set_h_ffactor(dbp, h_ffactor)
98	DB *dbp;
99	u_int32_t h_ffactor;
100{
101	HASH *hashp;
102
103	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_ffactor");
104	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
105
106	hashp = dbp->h_internal;
107	hashp->h_ffactor = h_ffactor;
108	return (0);
109}
110
111/*
112 * __ham_get_h_hash --
113 *	Get the hash function.
114 */
115static int
116__ham_get_h_hash(dbp, funcp)
117	DB *dbp;
118	u_int32_t (**funcp) __P((DB *, const void *, u_int32_t));
119{
120	HASH *hashp;
121
122	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
123
124	hashp = dbp->h_internal;
125	if (funcp != NULL)
126		*funcp = hashp->h_hash;
127	return (0);
128}
129
130/*
131 * __ham_set_h_hash --
132 *	Set the hash function.
133 */
134static int
135__ham_set_h_hash(dbp, func)
136	DB *dbp;
137	u_int32_t (*func) __P((DB *, const void *, u_int32_t));
138{
139	HASH *hashp;
140
141	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_hash");
142	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
143
144	hashp = dbp->h_internal;
145	hashp->h_hash = func;
146	return (0);
147}
148
149/*
150 * __ham_get_h_compare --
151 *	Get the comparison function.
152 */
153static int
154__ham_get_h_compare(dbp, funcp)
155	DB *dbp;
156	int (**funcp) __P((DB *, const DBT *, const DBT *));
157{
158	HASH *t;
159
160	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
161
162	t = dbp->h_internal;
163	if (funcp != NULL)
164		*funcp = t->h_compare;
165
166	return (0);
167}
168
169/*
170 * __ham_set_h_compare --
171 *	Set the comparison function.
172 *
173 * PUBLIC: int __ham_set_h_compare
174 * PUBLIC:         __P((DB *, int (*)(DB *, const DBT *, const DBT *)));
175 */
176int
177__ham_set_h_compare(dbp, func)
178	DB *dbp;
179	int (*func) __P((DB *, const DBT *, const DBT *));
180{
181	HASH *t;
182
183	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_compare");
184	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
185
186	t = dbp->h_internal;
187
188	t->h_compare = func;
189
190	return (0);
191}
192
193/*
194 * __db_get_h_nelem --
195 *
196 * PUBLIC: int __ham_get_h_nelem __P((DB *, u_int32_t *));
197 */
198int
199__ham_get_h_nelem(dbp, h_nelemp)
200	DB *dbp;
201	u_int32_t *h_nelemp;
202{
203	HASH *hashp;
204
205	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
206
207	hashp = dbp->h_internal;
208	*h_nelemp = hashp->h_nelem;
209	return (0);
210}
211
212/*
213 * __ham_set_h_nelem --
214 *	Set the table size.
215 */
216static int
217__ham_set_h_nelem(dbp, h_nelem)
218	DB *dbp;
219	u_int32_t h_nelem;
220{
221	HASH *hashp;
222
223	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_nelem");
224	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);
225
226	hashp = dbp->h_internal;
227	hashp->h_nelem = h_nelem;
228	return (0);
229}
230
231/*
232 * __ham_copy_config
233 *	Copy the configuration of one DB handle to another.
234 * PUBLIC: void __ham_copy_config __P((DB *, DB*, u_int32_t));
235 */
236void
237__ham_copy_config(src, dst, nparts)
238	DB *src, *dst;
239	u_int32_t nparts;
240{
241	HASH *s, *d;
242
243	s = src->h_internal;
244	d = dst->h_internal;
245
246	d->h_ffactor = s->h_ffactor;
247	d->h_nelem = s->h_nelem / nparts;
248	d->h_hash = s->h_hash;
249	d->h_compare = s->h_compare;
250}
251