xref: /freebsd/lib/libc/db/btree/bt_overflow.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
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  * Mike Olson.
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 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)bt_overflow.c	8.5 (Berkeley) 7/16/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include <db.h>
48 #include "btree.h"
49 
50 /*
51  * Big key/data code.
52  *
53  * Big key and data entries are stored on linked lists of pages.  The initial
54  * reference is byte string stored with the key or data and is the page number
55  * and size.  The actual record is stored in a chain of pages linked by the
56  * nextpg field of the PAGE header.
57  *
58  * The first page of the chain has a special property.  If the record is used
59  * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
60  * in the header.
61  *
62  * XXX
63  * A single DBT is written to each chain, so a lot of space on the last page
64  * is wasted.  This is a fairly major bug for some data sets.
65  */
66 
67 /*
68  * __OVFL_GET -- Get an overflow key/data item.
69  *
70  * Parameters:
71  *	t:	tree
72  *	p:	pointer to { pgno_t, u_int32_t }
73  *	buf:	storage address
74  *	bufsz:	storage size
75  *
76  * Returns:
77  *	RET_ERROR, RET_SUCCESS
78  */
79 int
80 __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)
81 {
82 	PAGE *h;
83 	pgno_t pg;
84 	size_t nb, plen;
85 	u_int32_t sz;
86 
87 	memmove(&pg, p, sizeof(pgno_t));
88 	memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
89 	*ssz = sz;
90 
91 #ifdef DEBUG
92 	if (pg == P_INVALID || sz == 0)
93 		abort();
94 #endif
95 	/* Make the buffer bigger as necessary. */
96 	if (*bufsz < sz) {
97 		*buf = reallocf(*buf, sz);
98 		if (*buf == NULL)
99 			return (RET_ERROR);
100 		*bufsz = sz;
101 	}
102 
103 	/*
104 	 * Step through the linked list of pages, copying the data on each one
105 	 * into the buffer.  Never copy more than the data's length.
106 	 */
107 	plen = t->bt_psize - BTDATAOFF;
108 	for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
109 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
110 			return (RET_ERROR);
111 
112 		nb = MIN(sz, plen);
113 		memmove(p, (char *)h + BTDATAOFF, nb);
114 		mpool_put(t->bt_mp, h, 0);
115 
116 		if ((sz -= nb) == 0)
117 			break;
118 	}
119 	return (RET_SUCCESS);
120 }
121 
122 /*
123  * __OVFL_PUT -- Store an overflow key/data item.
124  *
125  * Parameters:
126  *	t:	tree
127  *	data:	DBT to store
128  *	pgno:	storage page number
129  *
130  * Returns:
131  *	RET_ERROR, RET_SUCCESS
132  */
133 int
134 __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)
135 {
136 	PAGE *h, *last;
137 	void *p;
138 	pgno_t npg;
139 	size_t nb, plen;
140 	u_int32_t sz;
141 
142 	/*
143 	 * Allocate pages and copy the key/data record into them.  Store the
144 	 * number of the first page in the chain.
145 	 */
146 	plen = t->bt_psize - BTDATAOFF;
147 	for (last = NULL, p = dbt->data, sz = dbt->size;;
148 	    p = (char *)p + plen, last = h) {
149 		if ((h = __bt_new(t, &npg)) == NULL)
150 			return (RET_ERROR);
151 
152 		h->pgno = npg;
153 		h->nextpg = h->prevpg = P_INVALID;
154 		h->flags = P_OVERFLOW;
155 		h->lower = h->upper = 0;
156 
157 		nb = MIN(sz, plen);
158 		memmove((char *)h + BTDATAOFF, p, nb);
159 
160 		if (last) {
161 			last->nextpg = h->pgno;
162 			mpool_put(t->bt_mp, last, MPOOL_DIRTY);
163 		} else
164 			*pg = h->pgno;
165 
166 		if ((sz -= nb) == 0) {
167 			mpool_put(t->bt_mp, h, MPOOL_DIRTY);
168 			break;
169 		}
170 	}
171 	return (RET_SUCCESS);
172 }
173 
174 /*
175  * __OVFL_DELETE -- Delete an overflow chain.
176  *
177  * Parameters:
178  *	t:	tree
179  *	p:	pointer to { pgno_t, u_int32_t }
180  *
181  * Returns:
182  *	RET_ERROR, RET_SUCCESS
183  */
184 int
185 __ovfl_delete(BTREE *t, void *p)
186 {
187 	PAGE *h;
188 	pgno_t pg;
189 	size_t plen;
190 	u_int32_t sz;
191 
192 	memmove(&pg, p, sizeof(pgno_t));
193 	memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));
194 
195 #ifdef DEBUG
196 	if (pg == P_INVALID || sz == 0)
197 		abort();
198 #endif
199 	if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
200 		return (RET_ERROR);
201 
202 	/* Don't delete chains used by internal pages. */
203 	if (h->flags & P_PRESERVE) {
204 		mpool_put(t->bt_mp, h, 0);
205 		return (RET_SUCCESS);
206 	}
207 
208 	/* Step through the chain, calling the free routine for each page. */
209 	for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
210 		pg = h->nextpg;
211 		__bt_free(t, h);
212 		if (sz <= plen)
213 			break;
214 		if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
215 			return (RET_ERROR);
216 	}
217 	return (RET_SUCCESS);
218 }
219