summaryrefslogtreecommitdiff
path: root/hash/hash_method.c
blob: b13ca4310d265fa03746c2b72dc5eba3dcebd635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1999-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/hash.h"

static int __ham_set_h_ffactor __P((DB *, u_int32_t));
static int __ham_get_h_hash
	       __P((DB *, u_int32_t(**)(DB *, const void *, u_int32_t)));
static int __ham_set_h_hash
	       __P((DB *, u_int32_t(*)(DB *, const void *, u_int32_t)));
static int __ham_set_h_nelem __P((DB *, u_int32_t));

static int __ham_get_h_compare
	__P((DB *, int (**)(DB *, const DBT *, const DBT *)));

/*
 * __ham_db_create --
 *	Hash specific initialization of the DB structure.
 *
 * PUBLIC: int __ham_db_create __P((DB *));
 */
int
__ham_db_create(dbp)
	DB *dbp;
{
	HASH *hashp;
	int ret;

	if ((ret = __os_malloc(dbp->env,
	    sizeof(HASH), &dbp->h_internal)) != 0)
		return (ret);

	hashp = dbp->h_internal;

	hashp->h_nelem = 0;			/* Defaults. */
	hashp->h_ffactor = 0;
	hashp->h_hash = NULL;
	hashp->h_compare = NULL;

	dbp->get_h_ffactor = __ham_get_h_ffactor;
	dbp->set_h_ffactor = __ham_set_h_ffactor;
	dbp->get_h_hash = __ham_get_h_hash;
	dbp->set_h_hash = __ham_set_h_hash;
	dbp->get_h_compare = __ham_get_h_compare;
	dbp->set_h_compare = __ham_set_h_compare;
	dbp->get_h_nelem = __ham_get_h_nelem;
	dbp->set_h_nelem = __ham_set_h_nelem;

	return (0);
}

/*
 * PUBLIC: int __ham_db_close __P((DB *));
 */
int
__ham_db_close(dbp)
	DB *dbp;
{
	if (dbp->h_internal == NULL)
		return (0);
	__os_free(dbp->env, dbp->h_internal);
	dbp->h_internal = NULL;
	return (0);
}

/*
 * __ham_get_h_ffactor --
 *
 * PUBLIC: int __ham_get_h_ffactor __P((DB *, u_int32_t *));
 */
int
__ham_get_h_ffactor(dbp, h_ffactorp)
	DB *dbp;
	u_int32_t *h_ffactorp;
{
	HASH *hashp;

	hashp = dbp->h_internal;
	*h_ffactorp = hashp->h_ffactor;
	return (0);
}

/*
 * __ham_set_h_ffactor --
 *	Set the fill factor.
 */
static int
__ham_set_h_ffactor(dbp, h_ffactor)
	DB *dbp;
	u_int32_t h_ffactor;
{
	HASH *hashp;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_ffactor");
	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	hashp = dbp->h_internal;
	hashp->h_ffactor = h_ffactor;
	return (0);
}

/*
 * __ham_get_h_hash --
 *	Get the hash function.
 */
static int
__ham_get_h_hash(dbp, funcp)
	DB *dbp;
	u_int32_t (**funcp) __P((DB *, const void *, u_int32_t));
{
	HASH *hashp;

	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	hashp = dbp->h_internal;
	if (funcp != NULL)
		*funcp = hashp->h_hash;
	return (0);
}

/*
 * __ham_set_h_hash --
 *	Set the hash function.
 */
static int
__ham_set_h_hash(dbp, func)
	DB *dbp;
	u_int32_t (*func) __P((DB *, const void *, u_int32_t));
{
	HASH *hashp;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_hash");
	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	hashp = dbp->h_internal;
	hashp->h_hash = func;
	return (0);
}

/*
 * __ham_get_h_compare --
 *	Get the comparison function.
 */
static int
__ham_get_h_compare(dbp, funcp)
	DB *dbp;
	int (**funcp) __P((DB *, const DBT *, const DBT *));
{
	HASH *t;

	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	t = dbp->h_internal;
	if (funcp != NULL)
		*funcp = t->h_compare;

	return (0);
}

/*
 * __ham_set_h_compare --
 *	Set the comparison function.
 *
 * PUBLIC: int __ham_set_h_compare
 * PUBLIC:         __P((DB *, int (*)(DB *, const DBT *, const DBT *)));
 */
int
__ham_set_h_compare(dbp, func)
	DB *dbp;
	int (*func) __P((DB *, const DBT *, const DBT *));
{
	HASH *t;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_compare");
	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	t = dbp->h_internal;

	t->h_compare = func;

	return (0);
}

/*
 * __db_get_h_nelem --
 *
 * PUBLIC: int __ham_get_h_nelem __P((DB *, u_int32_t *));
 */
int
__ham_get_h_nelem(dbp, h_nelemp)
	DB *dbp;
	u_int32_t *h_nelemp;
{
	HASH *hashp;

	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	hashp = dbp->h_internal;
	*h_nelemp = hashp->h_nelem;
	return (0);
}

/*
 * __ham_set_h_nelem --
 *	Set the table size.
 */
static int
__ham_set_h_nelem(dbp, h_nelem)
	DB *dbp;
	u_int32_t h_nelem;
{
	HASH *hashp;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_h_nelem");
	DB_ILLEGAL_METHOD(dbp, DB_OK_HASH);

	hashp = dbp->h_internal;
	hashp->h_nelem = h_nelem;
	return (0);
}

/*
 * __ham_copy_config
 *	Copy the configuration of one DB handle to another.
 * PUBLIC: void __ham_copy_config __P((DB *, DB*, u_int32_t));
 */
void
__ham_copy_config(src, dst, nparts)
	DB *src, *dst;
	u_int32_t nparts;
{
	HASH *s, *d;

	s = src->h_internal;
	d = dst->h_internal;

	d->h_ffactor = s->h_ffactor;
	d->h_nelem = s->h_nelem / nparts;
	d->h_hash = s->h_hash;
	d->h_compare = s->h_compare;
}