146137Smao /*-
261196Sbostic * Copyright (c) 1990, 1993
361196Sbostic * The Regents of the University of California. All rights reserved.
446137Smao *
546137Smao * This code is derived from software contributed to Berkeley by
646137Smao * Mike Olson.
746137Smao *
846137Smao * %sccs.include.redist.c%
946137Smao */
1046137Smao
1146137Smao #if defined(LIBC_SCCS) && !defined(lint)
12*66214Sbostic static char sccsid[] = "@(#)bt_utils.c 8.4 (Berkeley) 02/21/94";
1346137Smao #endif /* LIBC_SCCS and not lint */
1446137Smao
1550989Sbostic #include <sys/param.h>
1656740Sbostic
1750989Sbostic #include <stdio.h>
1846561Sbostic #include <stdlib.h>
1946561Sbostic #include <string.h>
2056740Sbostic
2157932Sbostic #include <db.h>
2246137Smao #include "btree.h"
2346137Smao
2446137Smao /*
2550989Sbostic * __BT_RET -- Build return key/data pair as a result of search or scan.
2646137Smao *
2750989Sbostic * Parameters:
2850989Sbostic * t: tree
2950989Sbostic * d: LEAF to be returned to the user.
3051109Sbostic * key: user's key structure (NULL if not to be filled in)
3150989Sbostic * data: user's data structure
3246137Smao *
3350989Sbostic * Returns:
3450989Sbostic * RET_SUCCESS, RET_ERROR.
3546137Smao */
3646137Smao int
__bt_ret(t,e,key,data)3750989Sbostic __bt_ret(t, e, key, data)
3850989Sbostic BTREE *t;
3950989Sbostic EPG *e;
4050989Sbostic DBT *key, *data;
4146137Smao {
4250989Sbostic register BLEAF *bl;
4351109Sbostic register void *p;
4446137Smao
4550989Sbostic bl = GETBLEAF(e->page, e->index);
4650989Sbostic
4764460Sbostic /*
4864460Sbostic * We always copy big keys/data to make them contigous. Otherwise,
4964460Sbostic * we leave the page pinned and don't copy unless the user specified
5064460Sbostic * concurrent access.
5164460Sbostic */
5250989Sbostic if (bl->flags & P_BIGDATA) {
5350989Sbostic if (__ovfl_get(t, bl->bytes + bl->ksize,
5450989Sbostic &data->size, &t->bt_dbuf, &t->bt_dbufsz))
5546137Smao return (RET_ERROR);
5664460Sbostic data->data = t->bt_dbuf;
5764460Sbostic } else if (ISSET(t, B_DB_LOCK)) {
5856707Sbostic /* Use +1 in case the first record retrieved is 0 length. */
5956707Sbostic if (bl->dsize + 1 > t->bt_dbufsz) {
60*66214Sbostic if ((p =
61*66214Sbostic (void *)realloc(t->bt_dbuf, bl->dsize + 1)) == NULL)
6246137Smao return (RET_ERROR);
6351109Sbostic t->bt_dbuf = p;
6456707Sbostic t->bt_dbufsz = bl->dsize + 1;
6546137Smao }
6658017Sbostic memmove(t->bt_dbuf, bl->bytes + bl->ksize, bl->dsize);
6750989Sbostic data->size = bl->dsize;
6864460Sbostic data->data = t->bt_dbuf;
6964460Sbostic } else {
7064460Sbostic data->size = bl->dsize;
7164460Sbostic data->data = bl->bytes + bl->ksize;
7246137Smao }
7346137Smao
7451109Sbostic if (key == NULL)
7551109Sbostic return (RET_SUCCESS);
7651109Sbostic
7751109Sbostic if (bl->flags & P_BIGKEY) {
7851109Sbostic if (__ovfl_get(t, bl->bytes,
7951109Sbostic &key->size, &t->bt_kbuf, &t->bt_kbufsz))
8051109Sbostic return (RET_ERROR);
8164460Sbostic key->data = t->bt_kbuf;
8264460Sbostic } else if (ISSET(t, B_DB_LOCK)) {
8351109Sbostic if (bl->ksize > t->bt_kbufsz) {
84*66214Sbostic if ((p =
85*66214Sbostic (void *)realloc(t->bt_kbuf, bl->ksize)) == NULL)
8651109Sbostic return (RET_ERROR);
8751109Sbostic t->bt_kbuf = p;
8851109Sbostic t->bt_kbufsz = bl->ksize;
8951109Sbostic }
9058017Sbostic memmove(t->bt_kbuf, bl->bytes, bl->ksize);
9151109Sbostic key->size = bl->ksize;
9264460Sbostic key->data = t->bt_kbuf;
9364460Sbostic } else {
9464460Sbostic key->size = bl->ksize;
9564460Sbostic key->data = bl->bytes;
9651109Sbostic }
9746137Smao return (RET_SUCCESS);
9846137Smao }
9946137Smao
10046137Smao /*
10150989Sbostic * __BT_CMP -- Compare a key to a given record.
10246137Smao *
10350989Sbostic * Parameters:
10450989Sbostic * t: tree
10550989Sbostic * k1: DBT pointer of first arg to comparison
10650989Sbostic * e: pointer to EPG for comparison
10746137Smao *
10850989Sbostic * Returns:
10950989Sbostic * < 0 if k1 is < record
11050989Sbostic * = 0 if k1 is = record
11150989Sbostic * > 0 if k1 is > record
11246137Smao */
11346137Smao int
__bt_cmp(t,k1,e)11450989Sbostic __bt_cmp(t, k1, e)
11550989Sbostic BTREE *t;
11650989Sbostic const DBT *k1;
11750989Sbostic EPG *e;
11846137Smao {
11950989Sbostic BINTERNAL *bi;
12050989Sbostic BLEAF *bl;
12150989Sbostic DBT k2;
12250989Sbostic PAGE *h;
12350989Sbostic void *bigkey;
12446137Smao
12546137Smao /*
12650989Sbostic * The left-most key on internal pages, at any level of the tree, is
12750989Sbostic * guaranteed by the following code to be less than any user key.
12850989Sbostic * This saves us from having to update the leftmost key on an internal
12950989Sbostic * page when the user inserts a new key in the tree smaller than
13050989Sbostic * anything we've yet seen.
13146137Smao */
13250989Sbostic h = e->page;
13350989Sbostic if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
13446137Smao return (1);
13546137Smao
13650989Sbostic bigkey = NULL;
13750989Sbostic if (h->flags & P_BLEAF) {
13850989Sbostic bl = GETBLEAF(h, e->index);
13950989Sbostic if (bl->flags & P_BIGKEY)
14050989Sbostic bigkey = bl->bytes;
14150989Sbostic else {
14250989Sbostic k2.data = bl->bytes;
14350989Sbostic k2.size = bl->ksize;
14446137Smao }
14546137Smao } else {
14650989Sbostic bi = GETBINTERNAL(h, e->index);
14750989Sbostic if (bi->flags & P_BIGKEY)
14850989Sbostic bigkey = bi->bytes;
14950989Sbostic else {
15050989Sbostic k2.data = bi->bytes;
15150989Sbostic k2.size = bi->ksize;
15246137Smao }
15346137Smao }
15446137Smao
15550989Sbostic if (bigkey) {
15650989Sbostic if (__ovfl_get(t, bigkey,
15750989Sbostic &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
15850989Sbostic return (RET_ERROR);
15950989Sbostic k2.data = t->bt_dbuf;
16050989Sbostic }
16159627Sbostic return ((*t->bt_cmp)(k1, &k2));
16246137Smao }
16346137Smao
16446137Smao /*
16550989Sbostic * __BT_DEFCMP -- Default comparison routine.
16646137Smao *
16750989Sbostic * Parameters:
16850989Sbostic * a: DBT #1
16950989Sbostic * b: DBT #2
17046137Smao *
17150989Sbostic * Returns:
17250989Sbostic * < 0 if a is < b
17350989Sbostic * = 0 if a is = b
17450989Sbostic * > 0 if a is > b
17546137Smao */
17646137Smao int
__bt_defcmp(a,b)17750989Sbostic __bt_defcmp(a, b)
17850989Sbostic const DBT *a, *b;
17946137Smao {
18066192Sbostic register size_t len;
18150989Sbostic register u_char *p1, *p2;
18246137Smao
18366192Sbostic /*
18466192Sbostic * XXX
18566192Sbostic * If a size_t doesn't fit in an int, this routine can lose.
18666192Sbostic * What we need is a integral type which is guaranteed to be
18766192Sbostic * larger than a size_t, and there is no such thing.
18866192Sbostic */
18950989Sbostic len = MIN(a->size, b->size);
19050989Sbostic for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
19166192Sbostic if (*p1 != *p2)
19266192Sbostic return ((int)*p1 - (int)*p2);
19366192Sbostic return ((int)a->size - (int)b->size);
19446137Smao }
19546137Smao
19650989Sbostic /*
19750989Sbostic * __BT_DEFPFX -- Default prefix routine.
19850989Sbostic *
19950989Sbostic * Parameters:
20050989Sbostic * a: DBT #1
20150989Sbostic * b: DBT #2
20250989Sbostic *
20350989Sbostic * Returns:
20450989Sbostic * Number of bytes needed to distinguish b from a.
20550989Sbostic */
20666192Sbostic size_t
__bt_defpfx(a,b)20750989Sbostic __bt_defpfx(a, b)
20850989Sbostic const DBT *a, *b;
20946137Smao {
21050989Sbostic register u_char *p1, *p2;
21166192Sbostic register size_t cnt, len;
21246137Smao
21350989Sbostic cnt = 1;
21450989Sbostic len = MIN(a->size, b->size);
21550989Sbostic for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
21650989Sbostic if (*p1 != *p2)
21759627Sbostic return (cnt);
21851109Sbostic
21951109Sbostic /* a->size must be <= b->size, or they wouldn't be in this order. */
22051109Sbostic return (a->size < b->size ? a->size + 1 : a->size);
22146137Smao }
222