aboutsummaryrefslogtreecommitdiff
path: root/src/modules/kdb/db2/libdb2/hash/hash_bigkey.c
blob: 06210a57ccc94a31867f49ec23903f67321283b0 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*-
 * Copyright (c) 1990, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Margo Seltzer.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)hash_bigkey.c	8.5 (Berkeley) 11/2/95";
#endif /* LIBC_SCCS and not lint */

/*
 * PACKAGE: hash
 * DESCRIPTION:
 *	Big key/data handling for the hashing package.
 *
 * ROUTINES:
 * External
 *	__big_keydata
 *	__big_split
 *	__big_insert
 *	__big_return
 *	__big_delete
 *	__find_last_page
 * Internal
 *	collect_key
 *	collect_data
 */
#include <sys/types.h>

#include <stdlib.h>
#include <string.h>

#ifdef DEBUG
#include <assert.h>
#endif

#include "db-int.h"
#include "hash.h"
#include "page.h"
#include "extern.h"

static int32_t collect_key __P((HTAB *, PAGE16 *, int32_t, db_pgno_t *));
static int32_t collect_data __P((HTAB *, PAGE16 *, int32_t));

/*
 * Big_insert
 *
 * You need to do an insert and the key/data pair is greater than 
 * MINFILL * the bucket size
 *
 * Returns:
 *	 0 ==> OK
 *	-1 ==> ERROR
 */
int32_t
__big_insert(hashp, pagep, key, val)
	HTAB *hashp;
	PAGE16 *pagep;
	const DBT *key, *val;
{
	size_t  key_size, val_size;
	indx_t  key_move_bytes, val_move_bytes;
	int8_t *key_data, *val_data, base_page;

	key_data = (int8_t *)key->data;
	key_size = key->size;
	val_data = (int8_t *)val->data;
	val_size = val->size;

	NUM_ENT(pagep) = NUM_ENT(pagep) + 1;

	for (base_page = 1; key_size + val_size;) {
		/* Add a page! */
		pagep =
		    __add_bigpage(hashp, pagep, NUM_ENT(pagep) - 1, base_page);
		if (!pagep)
			return (-1);

		/* There's just going to be one entry on this page. */
		NUM_ENT(pagep) = 1;

		/* Move the key's data. */
		key_move_bytes = MIN(FREESPACE(pagep), key_size);
		/* Mark the page as to how much key & data is on this page. */
		BIGKEYLEN(pagep) = key_move_bytes;
		val_move_bytes =
		    MIN(FREESPACE(pagep) - key_move_bytes, val_size);
		BIGDATALEN(pagep) = val_move_bytes;

		/* Note big pages build beginning --> end, not vice versa. */
		if (key_move_bytes)
			memmove(BIGKEY(pagep), key_data, key_move_bytes);
		if (val_move_bytes)
			memmove(BIGDATA(pagep), val_data, val_move_bytes);

		key_size -= key_move_bytes;
		key_data += key_move_bytes;
		val_size -= val_move_bytes;
		val_data += val_move_bytes;

		base_page = 0;
	}
	__put_page(hashp, pagep, A_RAW, 1);
	return (0);
}

/*
 * Called when we need to delete a big pair.
 *
 * Returns:
 *	 0 => OK
 *	-1 => ERROR
 */
int32_t
#ifdef __STDC__
__big_delete(HTAB *hashp, PAGE16 *pagep, indx_t ndx)
#else
__big_delete(hashp, pagep, ndx)
	HTAB *hashp;
	PAGE16 *pagep;
	u_int32_t ndx;		/* Index of big pair on base page. */
#endif
{
	PAGE16 *last_pagep;

	/* Get first page with big key/data. */
	pagep = __get_page(hashp, OADDR_TO_PAGE(DATA_OFF(pagep, ndx)), A_RAW);
	if (!pagep)
		return (-1);

	/*
	 * Traverse through the pages, freeing the previous one (except
	 * the first) at each new page.
	 */
	while (NEXT_PGNO(pagep) != INVALID_PGNO) {
		last_pagep = pagep;
		pagep = __get_page(hashp, NEXT_PGNO(pagep), A_RAW);
		if (!pagep)
			return (-1);
		__delete_page(hashp, last_pagep, A_OVFL);
	}

	/* Free the last page in the chain. */
	__delete_page(hashp, pagep, A_OVFL);
	return (0);
}

/*
 * Given a key, indicates whether the big key at cursorp matches the
 * given key.
 *
 * Returns:
 *	 1 = Found!
 *	 0 = Key not found
 *	-1 error
 */
int32_t
__find_bigpair(hashp, cursorp, key, size)
	HTAB *hashp;
	CURSOR *cursorp;
	int8_t *key;
	int32_t size;
{
	PAGE16 *pagep, *hold_pagep;
	db_pgno_t  next_pgno;
	int32_t ksize;
	u_int16_t bytes;
	int8_t *kkey;

	ksize = size;
	kkey = key;
	bytes = 0;

	hold_pagep = NULL;
	/* Chances are, hashp->cpage is the base page. */
	if (cursorp->pagep)
		pagep = hold_pagep = cursorp->pagep;
	else {
		pagep = __get_page(hashp, cursorp->pgno, A_RAW);
		if (!pagep)
			return (-1);
	}

	/*
	 * Now, get the first page with the big stuff on it.
	 *
	 * XXX
	 * KLUDGE: we know that cursor is looking at the _next_ item, so
	 * we have to look at pgndx - 1.
	 */
	next_pgno = OADDR_TO_PAGE(DATA_OFF(pagep, (cursorp->pgndx - 1)));
	if (!hold_pagep)
		__put_page(hashp, pagep, A_RAW, 0);
	pagep = __get_page(hashp, next_pgno, A_RAW);
	if (!pagep)
		return (-1);

	/* While there are both keys to compare. */
	while ((ksize > 0) && (BIGKEYLEN(pagep))) {
		if (ksize < KEY_OFF(pagep, 0) ||
		    memcmp(BIGKEY(pagep), kkey, BIGKEYLEN(pagep))) {
			__put_page(hashp, pagep, A_RAW, 0);
			return (0);
		}
		kkey += BIGKEYLEN(pagep);
		ksize -= BIGKEYLEN(pagep);
		if (NEXT_PGNO(pagep) != INVALID_PGNO) {
			next_pgno = NEXT_PGNO(pagep);
			__put_page(hashp, pagep, A_RAW, 0);
			pagep = __get_page(hashp, next_pgno, A_RAW);
			if (!pagep)
				return (-1);
		}
	}
	__put_page(hashp, pagep, A_RAW, 0);
#ifdef DEBUG
	assert(ksize >= 0);
#endif
	if (ksize != 0) {
#ifdef HASH_STATISTICS
		++hash_collisions;
#endif
		return (0);
	} else
		return (1);
}

/*
 * Fill in the key and data for this big pair.
 */
int32_t
__big_keydata(hashp, pagep, key, val, ndx)
	HTAB *hashp;
	PAGE16 *pagep;
	DBT *key, *val;
	int32_t ndx;
{
	ITEM_INFO ii;
	PAGE16 *key_pagep;
	db_pgno_t last_page;

	key_pagep =
	    __get_page(hashp, OADDR_TO_PAGE(DATA_OFF(pagep, ndx)), A_RAW);
	if (!key_pagep)
		return (-1);
	key->size = collect_key(hashp, key_pagep, 0, &last_page);
	key->data = hashp->bigkey_buf;
	__put_page(hashp, key_pagep, A_RAW, 0);

	if (key->size == -1)
		return (-1);

	/* Create an item_info to direct __big_return to the beginning pgno. */
	ii.pgno = last_page;
	return (__big_return(hashp, &ii, val, 1));
}

/*
 * Return the big key on page, ndx.
 */
int32_t
#ifdef __STDC__
__get_bigkey(HTAB *hashp, PAGE16 *pagep, indx_t ndx, DBT *key)
#else
__get_bigkey(hashp, pagep, ndx, key)
	HTAB *hashp;
	PAGE16 *pagep;
	u_int32_t ndx;
	DBT *key;
#endif
{
	PAGE16 *key_pagep;

	key_pagep =
	    __get_page(hashp, OADDR_TO_PAGE(DATA_OFF(pagep, ndx)), A_RAW);
	if (!pagep)
		return (-1);
	key->size = collect_key(hashp, key_pagep, 0, NULL);
	key->data = hashp->bigkey_buf;

	__put_page(hashp, key_pagep, A_RAW, 0);

	return (0);
}

/*
 * Return the big key and data indicated in item_info.
 */
int32_t
__big_return(hashp, item_info, val, on_bigkey_page)
	HTAB *hashp;
	ITEM_INFO *item_info;
	DBT *val;
	int32_t on_bigkey_page;
{
	PAGE16 *pagep;
	db_pgno_t next_pgno;

	if (!on_bigkey_page) {
		/* Get first page with big pair on it. */
		pagep = __get_page(hashp,
		    OADDR_TO_PAGE(item_info->data_off), A_RAW);
		if (!pagep)
			return (-1);
	} else {
		pagep = __get_page(hashp, item_info->pgno, A_RAW);
		if (!pagep)
			return (-1);
	}

	/* Traverse through the bigkey pages until a page with data is found. */
	while (!BIGDATALEN(pagep)) {
		next_pgno = NEXT_PGNO(pagep);
		__put_page(hashp, pagep, A_RAW, 0);
		pagep = __get_page(hashp, next_pgno, A_RAW);
		if (!pagep)
			return (-1);
	}

	val->size = collect_data(hashp, pagep, 0);
	if (val->size < 1)
		return (-1);
	val->data = (void *)hashp->bigdata_buf;

	__put_page(hashp, pagep, A_RAW, 0);
	return (0);
}

/*
 * Given a page with a big key on it, traverse through the pages counting data
 * length, and collect all of the data on the way up.  Store the key in
 * hashp->bigkey_buf.  last_page indicates to the calling function what the
 * last page with key on it is; this will help if you later want to retrieve
 * the data portion.
 *
 * Does the work for __get_bigkey.
 *
 * Return total length of data; -1 if error.
 */
static int32_t
collect_key(hashp, pagep, len, last_page)
	HTAB *hashp;
	PAGE16 *pagep;
	int32_t len;
	db_pgno_t *last_page;
{
	PAGE16 *next_pagep;
	int32_t totlen, retval;
	db_pgno_t next_pgno;
#ifdef DEBUG
	db_pgno_t save_addr;
#endif

	/* If this is the last page with key. */
	if (BIGDATALEN(pagep)) {
		totlen = len + BIGKEYLEN(pagep);
		if (hashp->bigkey_buf)
			free(hashp->bigkey_buf);
		hashp->bigkey_buf = (u_int8_t *)malloc(totlen);
		if (!hashp->bigkey_buf)
			return (-1);
		memcpy(hashp->bigkey_buf + len,
		    BIGKEY(pagep), BIGKEYLEN(pagep));
		if (last_page)
			*last_page = ADDR(pagep);
		return (totlen);
	}

	/* Key filled up all of last key page, so we've gone 1 too far. */
	if (BIGKEYLEN(pagep) == 0) {
		if (hashp->bigkey_buf)
			free(hashp->bigkey_buf);
		hashp->bigkey_buf = (u_int8_t *)malloc(len);
		return (hashp->bigkey_buf ? len : -1);
	}
	totlen = len + BIGKEYLEN(pagep);

	/* Set pagep to the next page in the chain. */
	if (last_page)
		*last_page = ADDR(pagep);
	next_pgno = NEXT_PGNO(pagep);
	next_pagep = __get_page(hashp, next_pgno, A_RAW);
	if (!next_pagep)
		return (-1);
#ifdef DEBUG
	save_addr = ADDR(pagep);
#endif
	retval = collect_key(hashp, next_pagep, totlen, last_page);

#ifdef DEBUG
	assert(save_addr == ADDR(pagep));
#endif
	memcpy(hashp->bigkey_buf + len, BIGKEY(pagep), BIGKEYLEN(pagep));
	__put_page(hashp, next_pagep, A_RAW, 0);

	return (retval);
}

/*
 * Given a page with big data on it, recur through the pages counting data
 * length, and collect all of the data on the way up.  Store the data in
 * hashp->bigdata_buf.
 *
 * Does the work for __big_return.
 *
 * Return total length of data; -1 if error.
 */
static int32_t
collect_data(hashp, pagep, len)
	HTAB *hashp;
	PAGE16 *pagep;
	int32_t len;
{
	PAGE16 *next_pagep;
	int32_t totlen, retval;
	db_pgno_t next_pgno;
#ifdef DEBUG
	db_pgno_t save_addr;
#endif

	/* If there is no next page. */
	if (NEXT_PGNO(pagep) == INVALID_PGNO) {
		if (hashp->bigdata_buf)
			free(hashp->bigdata_buf);
		totlen = len + BIGDATALEN(pagep);
		hashp->bigdata_buf = (u_int8_t *)malloc(totlen);
		if (!hashp->bigdata_buf)
			return (-1);
		memcpy(hashp->bigdata_buf + totlen - BIGDATALEN(pagep),
		    BIGDATA(pagep), BIGDATALEN(pagep));
		return (totlen);
	}
	totlen = len + BIGDATALEN(pagep);

	/* Set pagep to the next page in the chain. */
	next_pgno = NEXT_PGNO(pagep);
	next_pagep = __get_page(hashp, next_pgno, A_RAW);
	if (!next_pagep)
		return (-1);

#ifdef DEBUG
	save_addr = ADDR(pagep);
#endif
	retval = collect_data(hashp, next_pagep, totlen);
#ifdef DEBUG
	assert(save_addr == ADDR(pagep));
#endif
	memcpy(hashp->bigdata_buf + totlen - BIGDATALEN(pagep),
	    BIGDATA(pagep), BIGDATALEN(pagep));
	__put_page(hashp, next_pagep, A_RAW, 0);

	return (retval);
}