xref: /freebsd/lib/libc/db/hash/hash_bigkey.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Margo Seltzer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * PACKAGE: hash
37  * DESCRIPTION:
38  *	Big key/data handling for the hashing package.
39  *
40  * ROUTINES:
41  * External
42  *	__big_keydata
43  *	__big_split
44  *	__big_insert
45  *	__big_return
46  *	__big_delete
47  *	__find_last_page
48  * Internal
49  *	collect_key
50  *	collect_data
51  */
52 
53 #include <sys/param.h>
54 
55 #include <errno.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #ifdef DEBUG
61 #include <assert.h>
62 #endif
63 
64 #include <db.h>
65 #include "hash.h"
66 #include "page.h"
67 #include "extern.h"
68 
69 static int collect_key(HTAB *, BUFHEAD *, int, DBT *, int);
70 static int collect_data(HTAB *, BUFHEAD *, int, int);
71 
72 /*
73  * Big_insert
74  *
75  * You need to do an insert and the key/data pair is too big
76  *
77  * Returns:
78  * 0 ==> OK
79  *-1 ==> ERROR
80  */
81 int
__big_insert(HTAB * hashp,BUFHEAD * bufp,const DBT * key,const DBT * val)82 __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
83 {
84 	u_int16_t *p;
85 	int key_size, n;
86 	unsigned int val_size;
87 	u_int16_t space, move_bytes, off;
88 	char *cp, *key_data, *val_data;
89 
90 	cp = bufp->page;		/* Character pointer of p. */
91 	p = (u_int16_t *)cp;
92 
93 	key_data = (char *)key->data;
94 	key_size = key->size;
95 	val_data = (char *)val->data;
96 	val_size = val->size;
97 
98 	/* First move the Key */
99 	for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
100 	    space = FREESPACE(p) - BIGOVERHEAD) {
101 		move_bytes = MIN(space, key_size);
102 		off = OFFSET(p) - move_bytes;
103 		memmove(cp + off, key_data, move_bytes);
104 		key_size -= move_bytes;
105 		key_data += move_bytes;
106 		n = p[0];
107 		p[++n] = off;
108 		p[0] = ++n;
109 		FREESPACE(p) = off - PAGE_META(n);
110 		OFFSET(p) = off;
111 		p[n] = PARTIAL_KEY;
112 		bufp = __add_ovflpage(hashp, bufp);
113 		if (!bufp)
114 			return (-1);
115 		n = p[0];
116 		if (!key_size) {
117 			space = FREESPACE(p);
118 			if (space) {
119 				move_bytes = MIN(space, val_size);
120 				/*
121 				 * If the data would fit exactly in the
122 				 * remaining space, we must overflow it to the
123 				 * next page; otherwise the invariant that the
124 				 * data must end on a page with FREESPACE
125 				 * non-zero would fail.
126 				 */
127 				if (space == val_size && val_size == val->size)
128 					goto toolarge;
129 				off = OFFSET(p) - move_bytes;
130 				memmove(cp + off, val_data, move_bytes);
131 				val_data += move_bytes;
132 				val_size -= move_bytes;
133 				p[n] = off;
134 				p[n - 2] = FULL_KEY_DATA;
135 				FREESPACE(p) = FREESPACE(p) - move_bytes;
136 				OFFSET(p) = off;
137 			} else {
138 			toolarge:
139 				p[n - 2] = FULL_KEY;
140 			}
141 		}
142 		p = (u_int16_t *)bufp->page;
143 		cp = bufp->page;
144 		bufp->flags |= BUF_MOD;
145 	}
146 
147 	/* Now move the data */
148 	for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
149 	    space = FREESPACE(p) - BIGOVERHEAD) {
150 		move_bytes = MIN(space, val_size);
151 		/*
152 		 * Here's the hack to make sure that if the data ends on the
153 		 * same page as the key ends, FREESPACE is at least one.
154 		 */
155 		if (space == val_size && val_size == val->size)
156 			move_bytes--;
157 		off = OFFSET(p) - move_bytes;
158 		memmove(cp + off, val_data, move_bytes);
159 		val_size -= move_bytes;
160 		val_data += move_bytes;
161 		n = p[0];
162 		p[++n] = off;
163 		p[0] = ++n;
164 		FREESPACE(p) = off - PAGE_META(n);
165 		OFFSET(p) = off;
166 		if (val_size) {
167 			p[n] = FULL_KEY;
168 			bufp = __add_ovflpage(hashp, bufp);
169 			if (!bufp)
170 				return (-1);
171 			cp = bufp->page;
172 			p = (u_int16_t *)cp;
173 		} else
174 			p[n] = FULL_KEY_DATA;
175 		bufp->flags |= BUF_MOD;
176 	}
177 	return (0);
178 }
179 
180 /*
181  * Called when bufp's page  contains a partial key (index should be 1)
182  *
183  * All pages in the big key/data pair except bufp are freed.  We cannot
184  * free bufp because the page pointing to it is lost and we can't get rid
185  * of its pointer.
186  *
187  * Returns:
188  * 0 => OK
189  *-1 => ERROR
190  */
191 int
__big_delete(HTAB * hashp,BUFHEAD * bufp)192 __big_delete(HTAB *hashp, BUFHEAD *bufp)
193 {
194 	BUFHEAD *last_bfp, *rbufp;
195 	u_int16_t *bp, pageno;
196 	int key_done, n;
197 
198 	rbufp = bufp;
199 	last_bfp = NULL;
200 	bp = (u_int16_t *)bufp->page;
201 	pageno = 0;
202 	key_done = 0;
203 
204 	while (!key_done || (bp[2] != FULL_KEY_DATA)) {
205 		if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
206 			key_done = 1;
207 
208 		/*
209 		 * If there is freespace left on a FULL_KEY_DATA page, then
210 		 * the data is short and fits entirely on this page, and this
211 		 * is the last page.
212 		 */
213 		if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
214 			break;
215 		pageno = bp[bp[0] - 1];
216 		rbufp->flags |= BUF_MOD;
217 		rbufp = __get_buf(hashp, pageno, rbufp, 0);
218 		if (last_bfp)
219 			__free_ovflpage(hashp, last_bfp);
220 		last_bfp = rbufp;
221 		if (!rbufp)
222 			return (-1);		/* Error. */
223 		bp = (u_int16_t *)rbufp->page;
224 	}
225 
226 	/*
227 	 * If we get here then rbufp points to the last page of the big
228 	 * key/data pair.  Bufp points to the first one -- it should now be
229 	 * empty pointing to the next page after this pair.  Can't free it
230 	 * because we don't have the page pointing to it.
231 	 */
232 
233 	/* This is information from the last page of the pair. */
234 	n = bp[0];
235 	pageno = bp[n - 1];
236 
237 	/* Now, bp is the first page of the pair. */
238 	bp = (u_int16_t *)bufp->page;
239 	if (n > 2) {
240 		/* There is an overflow page. */
241 		bp[1] = pageno;
242 		bp[2] = OVFLPAGE;
243 		bufp->ovfl = rbufp->ovfl;
244 	} else
245 		/* This is the last page. */
246 		bufp->ovfl = NULL;
247 	n -= 2;
248 	bp[0] = n;
249 	FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
250 	OFFSET(bp) = hashp->BSIZE;
251 
252 	bufp->flags |= BUF_MOD;
253 	if (rbufp)
254 		__free_ovflpage(hashp, rbufp);
255 	if (last_bfp && last_bfp != rbufp)
256 		__free_ovflpage(hashp, last_bfp);
257 
258 	hashp->NKEYS--;
259 	return (0);
260 }
261 /*
262  * Returns:
263  *  0 = key not found
264  * -1 = get next overflow page
265  * -2 means key not found and this is big key/data
266  * -3 error
267  */
268 int
__find_bigpair(HTAB * hashp,BUFHEAD * bufp,int ndx,char * key,int size)269 __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
270 {
271 	u_int16_t *bp;
272 	char *p;
273 	int ksize;
274 	u_int16_t bytes;
275 	char *kkey;
276 
277 	bp = (u_int16_t *)bufp->page;
278 	p = bufp->page;
279 	ksize = size;
280 	kkey = key;
281 
282 	for (bytes = hashp->BSIZE - bp[ndx];
283 	    bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
284 	    bytes = hashp->BSIZE - bp[ndx]) {
285 		if (memcmp(p + bp[ndx], kkey, bytes))
286 			return (-2);
287 		kkey += bytes;
288 		ksize -= bytes;
289 		bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
290 		if (!bufp)
291 			return (-3);
292 		p = bufp->page;
293 		bp = (u_int16_t *)p;
294 		ndx = 1;
295 	}
296 
297 	if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
298 #ifdef HASH_STATISTICS
299 		++hash_collisions;
300 #endif
301 		return (-2);
302 	} else
303 		return (ndx);
304 }
305 
306 /*
307  * Given the buffer pointer of the first overflow page of a big pair,
308  * find the end of the big pair
309  *
310  * This will set bpp to the buffer header of the last page of the big pair.
311  * It will return the pageno of the overflow page following the last page
312  * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
313  * bucket)
314  */
315 u_int16_t
__find_last_page(HTAB * hashp,BUFHEAD ** bpp)316 __find_last_page(HTAB *hashp, BUFHEAD **bpp)
317 {
318 	BUFHEAD *bufp;
319 	u_int16_t *bp, pageno;
320 	int n;
321 
322 	bufp = *bpp;
323 	bp = (u_int16_t *)bufp->page;
324 	for (;;) {
325 		n = bp[0];
326 
327 		/*
328 		 * This is the last page if: the tag is FULL_KEY_DATA and
329 		 * either only 2 entries OVFLPAGE marker is explicit there
330 		 * is freespace on the page.
331 		 */
332 		if (bp[2] == FULL_KEY_DATA &&
333 		    ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
334 			break;
335 
336 		pageno = bp[n - 1];
337 		bufp = __get_buf(hashp, pageno, bufp, 0);
338 		if (!bufp)
339 			return (0);	/* Need to indicate an error! */
340 		bp = (u_int16_t *)bufp->page;
341 	}
342 
343 	*bpp = bufp;
344 	if (bp[0] > 2)
345 		return (bp[3]);
346 	else
347 		return (0);
348 }
349 
350 /*
351  * Return the data for the key/data pair that begins on this page at this
352  * index (index should always be 1).
353  */
354 int
__big_return(HTAB * hashp,BUFHEAD * bufp,int ndx,DBT * val,int set_current)355 __big_return(HTAB *hashp, BUFHEAD *bufp, int ndx, DBT *val, int set_current)
356 {
357 	BUFHEAD *save_p;
358 	u_int16_t *bp, len, off, save_addr;
359 	char *tp;
360 
361 	bp = (u_int16_t *)bufp->page;
362 	while (bp[ndx + 1] == PARTIAL_KEY) {
363 		bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
364 		if (!bufp)
365 			return (-1);
366 		bp = (u_int16_t *)bufp->page;
367 		ndx = 1;
368 	}
369 
370 	if (bp[ndx + 1] == FULL_KEY) {
371 		bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
372 		if (!bufp)
373 			return (-1);
374 		bp = (u_int16_t *)bufp->page;
375 		save_p = bufp;
376 		save_addr = save_p->addr;
377 		off = bp[1];
378 		len = 0;
379 	} else
380 		if (!FREESPACE(bp)) {
381 			/*
382 			 * This is a hack.  We can't distinguish between
383 			 * FULL_KEY_DATA that contains complete data or
384 			 * incomplete data, so we require that if the data
385 			 * is complete, there is at least 1 byte of free
386 			 * space left.
387 			 */
388 			off = bp[bp[0]];
389 			len = bp[1] - off;
390 			save_p = bufp;
391 			save_addr = bufp->addr;
392 			bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
393 			if (!bufp)
394 				return (-1);
395 			bp = (u_int16_t *)bufp->page;
396 		} else {
397 			/* The data is all on one page. */
398 			tp = (char *)bp;
399 			off = bp[bp[0]];
400 			val->data = (u_char *)tp + off;
401 			val->size = bp[1] - off;
402 			if (set_current) {
403 				if (bp[0] == 2) {	/* No more buckets in
404 							 * chain */
405 					hashp->cpage = NULL;
406 					hashp->cbucket++;
407 					hashp->cndx = 1;
408 				} else {
409 					hashp->cpage = __get_buf(hashp,
410 					    bp[bp[0] - 1], bufp, 0);
411 					if (!hashp->cpage)
412 						return (-1);
413 					hashp->cndx = 1;
414 					if (!((u_int16_t *)
415 					    hashp->cpage->page)[0]) {
416 						hashp->cbucket++;
417 						hashp->cpage = NULL;
418 					}
419 				}
420 			}
421 			return (0);
422 		}
423 
424 	val->size = (size_t)collect_data(hashp, bufp, (int)len, set_current);
425 	if (val->size == (size_t)-1)
426 		return (-1);
427 	if (save_p->addr != save_addr) {
428 		/* We are pretty short on buffers. */
429 		errno = EINVAL;			/* OUT OF BUFFERS */
430 		return (-1);
431 	}
432 	memmove(hashp->tmp_buf, (save_p->page) + off, len);
433 	val->data = (u_char *)hashp->tmp_buf;
434 	return (0);
435 }
436 /*
437  * Count how big the total datasize is by recursing through the pages.  Then
438  * allocate a buffer and copy the data as you recurse up.
439  */
440 static int
collect_data(HTAB * hashp,BUFHEAD * bufp,int len,int set)441 collect_data(HTAB *hashp, BUFHEAD *bufp, int len, int set)
442 {
443 	u_int16_t *bp;
444 	char *p;
445 	BUFHEAD *xbp;
446 	u_int16_t save_addr;
447 	int mylen, totlen;
448 
449 	p = bufp->page;
450 	bp = (u_int16_t *)p;
451 	mylen = hashp->BSIZE - bp[1];
452 	save_addr = bufp->addr;
453 
454 	if (bp[2] == FULL_KEY_DATA) {		/* End of Data */
455 		totlen = len + mylen;
456 		if (hashp->tmp_buf)
457 			free(hashp->tmp_buf);
458 		if ((hashp->tmp_buf = (char *)malloc(totlen)) == NULL)
459 			return (-1);
460 		if (set) {
461 			hashp->cndx = 1;
462 			if (bp[0] == 2) {	/* No more buckets in chain */
463 				hashp->cpage = NULL;
464 				hashp->cbucket++;
465 			} else {
466 				hashp->cpage =
467 				    __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
468 				if (!hashp->cpage)
469 					return (-1);
470 				else if (!((u_int16_t *)hashp->cpage->page)[0]) {
471 					hashp->cbucket++;
472 					hashp->cpage = NULL;
473 				}
474 			}
475 		}
476 	} else {
477 		xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
478 		if (!xbp || ((totlen =
479 		    collect_data(hashp, xbp, len + mylen, set)) < 1))
480 			return (-1);
481 	}
482 	if (bufp->addr != save_addr) {
483 		errno = EINVAL;			/* Out of buffers. */
484 		return (-1);
485 	}
486 	memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], mylen);
487 	return (totlen);
488 }
489 
490 /*
491  * Fill in the key and data for this big pair.
492  */
493 int
__big_keydata(HTAB * hashp,BUFHEAD * bufp,DBT * key,DBT * val,int set)494 __big_keydata(HTAB *hashp, BUFHEAD *bufp, DBT *key, DBT *val, int set)
495 {
496 	key->size = (size_t)collect_key(hashp, bufp, 0, val, set);
497 	if (key->size == (size_t)-1)
498 		return (-1);
499 	key->data = (u_char *)hashp->tmp_key;
500 	return (0);
501 }
502 
503 /*
504  * Count how big the total key size is by recursing through the pages.  Then
505  * collect the data, allocate a buffer and copy the key as you recurse up.
506  */
507 static int
collect_key(HTAB * hashp,BUFHEAD * bufp,int len,DBT * val,int set)508 collect_key(HTAB *hashp, BUFHEAD *bufp, int len, DBT *val, int set)
509 {
510 	BUFHEAD *xbp;
511 	char *p;
512 	int mylen, totlen;
513 	u_int16_t *bp, save_addr;
514 
515 	p = bufp->page;
516 	bp = (u_int16_t *)p;
517 	mylen = hashp->BSIZE - bp[1];
518 
519 	save_addr = bufp->addr;
520 	totlen = len + mylen;
521 	if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) {    /* End of Key. */
522 		if (hashp->tmp_key != NULL)
523 			free(hashp->tmp_key);
524 		if ((hashp->tmp_key = (char *)malloc(totlen)) == NULL)
525 			return (-1);
526 		if (__big_return(hashp, bufp, 1, val, set))
527 			return (-1);
528 	} else {
529 		xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
530 		if (!xbp || ((totlen =
531 		    collect_key(hashp, xbp, totlen, val, set)) < 1))
532 			return (-1);
533 	}
534 	if (bufp->addr != save_addr) {
535 		errno = EINVAL;		/* MIS -- OUT OF BUFFERS */
536 		return (-1);
537 	}
538 	memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], mylen);
539 	return (totlen);
540 }
541 
542 /*
543  * Returns:
544  *  0 => OK
545  * -1 => error
546  */
547 int
__big_split(HTAB * hashp,BUFHEAD * op,BUFHEAD * np,BUFHEAD * big_keyp,int addr,u_int32_t obucket,SPLIT_RETURN * ret)548 __big_split(HTAB *hashp,
549     BUFHEAD *op,	/* Pointer to where to put keys that go in old bucket */
550     BUFHEAD *np,	/* Pointer to new bucket page */
551     BUFHEAD *big_keyp,	/* Pointer to first page containing the big key/data */
552     int addr,		/* Address of big_keyp */
553     u_int32_t obucket,	/* Old Bucket */
554     SPLIT_RETURN *ret)
555 {
556 	BUFHEAD *bp, *tmpp;
557 	DBT key, val;
558 	u_int32_t change;
559 	u_int16_t free_space, n, off, *tp;
560 
561 	bp = big_keyp;
562 
563 	/* Now figure out where the big key/data goes */
564 	if (__big_keydata(hashp, big_keyp, &key, &val, 0))
565 		return (-1);
566 	change = (__call_hash(hashp, key.data, key.size) != obucket);
567 
568 	if ( (ret->next_addr = __find_last_page(hashp, &big_keyp)) ) {
569 		if (!(ret->nextp =
570 		    __get_buf(hashp, ret->next_addr, big_keyp, 0)))
571 			return (-1);
572 	} else
573 		ret->nextp = NULL;
574 
575 	/* Now make one of np/op point to the big key/data pair */
576 #ifdef DEBUG
577 	assert(np->ovfl == NULL);
578 #endif
579 	if (change)
580 		tmpp = np;
581 	else
582 		tmpp = op;
583 
584 	tmpp->flags |= BUF_MOD;
585 #ifdef DEBUG1
586 	(void)fprintf(stderr,
587 	    "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
588 	    (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
589 #endif
590 	tmpp->ovfl = bp;	/* one of op/np point to big_keyp */
591 	tp = (u_int16_t *)tmpp->page;
592 #ifdef DEBUG
593 	assert(FREESPACE(tp) >= OVFLSIZE);
594 #endif
595 	n = tp[0];
596 	off = OFFSET(tp);
597 	free_space = FREESPACE(tp);
598 	tp[++n] = (u_int16_t)addr;
599 	tp[++n] = OVFLPAGE;
600 	tp[0] = n;
601 	OFFSET(tp) = off;
602 	FREESPACE(tp) = free_space - OVFLSIZE;
603 
604 	/*
605 	 * Finally, set the new and old return values. BIG_KEYP contains a
606 	 * pointer to the last page of the big key_data pair. Make sure that
607 	 * big_keyp has no following page (2 elements) or create an empty
608 	 * following page.
609 	 */
610 
611 	ret->newp = np;
612 	ret->oldp = op;
613 
614 	tp = (u_int16_t *)big_keyp->page;
615 	big_keyp->flags |= BUF_MOD;
616 	if (tp[0] > 2) {
617 		/*
618 		 * There may be either one or two offsets on this page.  If
619 		 * there is one, then the overflow page is linked on normally
620 		 * and tp[4] is OVFLPAGE.  If there are two, tp[4] contains
621 		 * the second offset and needs to get stuffed in after the
622 		 * next overflow page is added.
623 		 */
624 		n = tp[4];
625 		free_space = FREESPACE(tp);
626 		off = OFFSET(tp);
627 		tp[0] -= 2;
628 		FREESPACE(tp) = free_space + OVFLSIZE;
629 		OFFSET(tp) = off;
630 		tmpp = __add_ovflpage(hashp, big_keyp);
631 		if (!tmpp)
632 			return (-1);
633 		tp[4] = n;
634 	} else
635 		tmpp = big_keyp;
636 
637 	if (change)
638 		ret->newp = tmpp;
639 	else
640 		ret->oldp = tmpp;
641 	return (0);
642 }
643