xref: /csrg-svn/lib/libc/db/btree/bt_utils.c (revision 50989)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)bt_utils.c	5.4 (Berkeley) 09/04/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 #include <db.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "btree.h"
21 
22 /*
23  * __BT_RET -- Build return key/data pair as a result of search or scan.
24  *
25  * Parameters:
26  *	t:	tree
27  *	d:	LEAF to be returned to the user.
28  *	key:	user's key structure
29  *	data:	user's data structure
30  *
31  * Returns:
32  *	RET_SUCCESS, RET_ERROR.
33  */
34 int
35 __bt_ret(t, e, key, data)
36 	BTREE *t;
37 	EPG *e;
38 	DBT *key, *data;
39 {
40 	register BLEAF *bl;
41 
42 	bl = GETBLEAF(e->page, e->index);
43 
44 	if (bl->flags & P_BIGKEY) {
45 		if (__ovfl_get(t, bl->bytes,
46 		    &key->size, &t->bt_kbuf, &t->bt_kbufsz))
47 			return (RET_ERROR);
48 	} else {
49 		if (bl->ksize > t->bt_kbufsz) {
50 			if ((t->bt_kbuf =
51 			    realloc(t->bt_kbuf, bl->ksize)) == NULL)
52 				return (RET_ERROR);
53 			t->bt_kbufsz = bl->ksize;
54 		}
55 		bcopy(bl->bytes, t->bt_kbuf, t->bt_kbufsz);
56 		key->size = bl->ksize;
57 	}
58 	key->data = t->bt_kbuf;
59 
60 	if (bl->flags & P_BIGDATA) {
61 		if (__ovfl_get(t, bl->bytes + bl->ksize,
62 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
63 			return (RET_ERROR);
64 	} else {
65 		if (bl->dsize > t->bt_dbufsz) {
66 			if ((t->bt_dbuf =
67 			    realloc(t->bt_dbuf, bl->dsize)) == NULL)
68 				return (RET_ERROR);
69 			t->bt_dbufsz = bl->dsize;
70 		}
71 		bcopy(bl->bytes + bl->ksize, t->bt_dbuf, t->bt_dbufsz);
72 		data->size = bl->dsize;
73 	}
74 	data->data = t->bt_dbuf;
75 
76 	return (RET_SUCCESS);
77 }
78 
79 /*
80  * __BT_CMP -- Compare a key to a given record.
81  *
82  * Parameters:
83  *	t:	tree
84  *	k1:	DBT pointer of first arg to comparison
85  *	e:	pointer to EPG for comparison
86  *
87  * Returns:
88  *	< 0 if k1 is < record
89  *	= 0 if k1 is = record
90  *	> 0 if k1 is > record
91  */
92 int
93 __bt_cmp(t, k1, e)
94 	BTREE *t;
95 	const DBT *k1;
96 	EPG *e;
97 {
98 	BINTERNAL *bi;
99 	BLEAF *bl;
100 	DBT k2;
101 	PAGE *h;
102 	void *bigkey;
103 
104 	/*
105 	 * The left-most key on internal pages, at any level of the tree, is
106 	 * guaranteed by the following code to be less than any user key.
107 	 * This saves us from having to update the leftmost key on an internal
108 	 * page when the user inserts a new key in the tree smaller than
109 	 * anything we've yet seen.
110 	 */
111 	h = e->page;
112 	if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
113 		return (1);
114 
115 	bigkey = NULL;
116 	if (h->flags & P_BLEAF) {
117 		bl = GETBLEAF(h, e->index);
118 		if (bl->flags & P_BIGKEY)
119 			bigkey = bl->bytes;
120 		else {
121 			k2.data = bl->bytes;
122 			k2.size = bl->ksize;
123 		}
124 	} else {
125 		bi = GETBINTERNAL(h, e->index);
126 		if (bi->flags & P_BIGKEY)
127 			bigkey = bi->bytes;
128 		else {
129 			k2.data = bi->bytes;
130 			k2.size = bi->ksize;
131 		}
132 	}
133 
134 	if (bigkey) {
135 		if (__ovfl_get(t, bigkey,
136 		    &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
137 			return (RET_ERROR);
138 		k2.data = t->bt_dbuf;
139 	}
140 	return((*t->bt_cmp)(k1, &k2));
141 }
142 
143 /*
144  * __BT_DEFCMP -- Default comparison routine.
145  *
146  * Parameters:
147  *	a:	DBT #1
148  *	b: 	DBT #2
149  *
150  * Returns:
151  *	< 0 if a is < b
152  *	= 0 if a is = b
153  *	> 0 if a is > b
154  */
155 int
156 __bt_defcmp(a, b)
157 	const DBT *a, *b;
158 {
159 	register u_char *p1, *p2;
160 	register int diff, len;
161 
162 	len = MIN(a->size, b->size);
163 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
164 		if (diff = *p1 - *p2)
165 			return(diff);
166 	return(a->size - b->size);
167 }
168 
169 /*
170  * __BT_DEFPFX -- Default prefix routine.
171  *
172  * Parameters:
173  *	a:	DBT #1
174  *	b: 	DBT #2
175  *
176  * Returns:
177  *	Number of bytes needed to distinguish b from a.
178  */
179 int
180 __bt_defpfx(a, b)
181 	const DBT *a, *b;
182 {
183 	register u_char *p1, *p2;
184 	register int len;
185 	int cnt;
186 
187 	cnt = 1;
188 	len = MIN(a->size, b->size);
189 	for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
190 		if (*p1 != *p2)
191 			return(cnt);
192 	if (a->size == b->size)
193 		return (a->size);
194 	return(a->size + 1);
195 }
196