2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
21
#include "dbinc/db_page.h"
22
#include "dbinc/db_shash.h"
23
#include "dbinc/lock.h"
24
#include "dbinc/db_am.h"
28
* Remove an item from a page.
30
* PUBLIC: int __db_ditem __P((DBC *, PAGE *, u_int32_t, u_int32_t));
33
__db_ditem(dbc, pagep, indx, nbytes)
36
u_int32_t indx, nbytes;
40
db_indx_t cnt, *inp, offset;
45
if (DBC_LOGGING(dbc)) {
46
ldbt.data = P_ENTRY(dbp, pagep, indx);
48
if ((ret = __db_addrem_log(dbp, dbc->txn,
49
&LSN(pagep), 0, DB_REM_DUP, PGNO(pagep),
50
(u_int32_t)indx, nbytes, &ldbt, NULL, &LSN(pagep))) != 0)
53
LSN_NOT_LOGGED(LSN(pagep));
56
* If there's only a single item on the page, we don't have to
59
if (NUM_ENT(pagep) == 1) {
61
HOFFSET(pagep) = dbp->pgsize;
65
inp = P_INP(dbp, pagep);
67
* Pack the remaining key/data items at the end of the page. Use
68
* memmove(3), the regions may overlap.
70
from = (u_int8_t *)pagep + HOFFSET(pagep);
71
DB_ASSERT((int)inp[indx] - HOFFSET(pagep) >= 0);
72
memmove(from + nbytes, from, inp[indx] - HOFFSET(pagep));
73
HOFFSET(pagep) += nbytes;
75
/* Adjust the indices' offsets. */
77
for (cnt = 0; cnt < NUM_ENT(pagep); ++cnt)
78
if (inp[cnt] < offset)
81
/* Shift the indices down. */
83
if (indx != NUM_ENT(pagep))
84
memmove(&inp[indx], &inp[indx + 1],
85
sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
92
* Put an item on a page.
94
* PUBLIC: int __db_pitem
95
* PUBLIC: __P((DBC *, PAGE *, u_int32_t, u_int32_t, DBT *, DBT *));
98
__db_pitem(dbc, pagep, indx, nbytes, hdr, data)
113
if (nbytes > P_FREESPACE(dbp, pagep)) {
114
DB_ASSERT(nbytes <= P_FREESPACE(dbp, pagep));
118
* Put a single item onto a page. The logic figuring out where to
119
* insert and whether it fits is handled in the caller. All we do
120
* here is manage the page shuffling. We cheat a little bit in that
121
* we don't want to copy the dbt on a normal put twice. If hdr is
122
* NULL, we create a BKEYDATA structure on the page, otherwise, just
123
* copy the caller's information onto the page.
125
* This routine is also used to put entries onto the page where the
126
* entry is pre-built, e.g., during recovery. In this case, the hdr
127
* will point to the entry, and the data argument will be NULL.
130
* There's a tremendous potential for off-by-one errors here, since
131
* the passed in header sizes must be adjusted for the structure's
132
* placeholder for the trailing variable-length data field.
134
if (DBC_LOGGING(dbc)) {
135
if ((ret = __db_addrem_log(dbp, dbc->txn,
136
&LSN(pagep), 0, DB_ADD_DUP, PGNO(pagep),
137
(u_int32_t)indx, nbytes, hdr, data, &LSN(pagep))) != 0)
140
LSN_NOT_LOGGED(LSN(pagep));
143
B_TSET(bk.type, B_KEYDATA, 0);
144
bk.len = data == NULL ? 0 : data->size;
147
thdr.size = SSZA(BKEYDATA, data);
150
inp = P_INP(dbp, pagep);
152
/* Adjust the index table, then put the item on the page. */
153
if (indx != NUM_ENT(pagep))
154
memmove(&inp[indx + 1], &inp[indx],
155
sizeof(db_indx_t) * (NUM_ENT(pagep) - indx));
156
HOFFSET(pagep) -= nbytes;
157
inp[indx] = HOFFSET(pagep);
160
p = P_ENTRY(dbp, pagep, indx);
161
memcpy(p, hdr->data, hdr->size);
163
memcpy(p + hdr->size, data->data, data->size);
170
* Relink around a deleted page.
172
* PUBLIC: int __db_relink __P((DBC *, u_int32_t, PAGE *, PAGE **, int));
175
__db_relink(dbc, add_rem, pagep, new_next, needlock)
178
PAGE *pagep, **new_next;
184
DB_LSN *nlsnp, *plsnp, ret_lsn;
192
nlsnp = plsnp = NULL;
197
* Retrieve and lock the one/two pages. For a remove, we may need
198
* two pages (the before and after). For an add, we only need one
199
* because, the split took care of the prev.
201
if (pagep->next_pgno != PGNO_INVALID) {
202
if (needlock && (ret = __db_lget(dbc,
203
0, pagep->next_pgno, DB_LOCK_WRITE, 0, &npl)) != 0)
205
if ((ret = mpf->get(mpf, &pagep->next_pgno, 0, &np)) != 0) {
206
__db_pgerr(dbp, pagep->next_pgno, ret);
211
if (add_rem == DB_REM_PAGE && pagep->prev_pgno != PGNO_INVALID) {
212
if (needlock && (ret = __db_lget(dbc,
213
0, pagep->prev_pgno, DB_LOCK_WRITE, 0, &ppl)) != 0)
215
if ((ret = mpf->get(mpf, &pagep->prev_pgno, 0, &pp)) != 0) {
216
__db_pgerr(dbp, pagep->next_pgno, ret);
222
/* Log the change. */
223
if (DBC_LOGGING(dbc)) {
224
if ((ret = __db_relink_log(dbp, dbc->txn, &ret_lsn, 0, add_rem,
225
pagep->pgno, &pagep->lsn, pagep->prev_pgno, plsnp,
226
pagep->next_pgno, nlsnp)) != 0)
229
LSN_NOT_LOGGED(ret_lsn);
234
if (add_rem == DB_REM_PAGE)
235
pagep->lsn = ret_lsn;
238
* Modify and release the two pages.
241
* The parameter new_next gets set to the page following the page we
242
* are removing. If there is no following page, then new_next gets
246
if (add_rem == DB_ADD_PAGE)
247
np->prev_pgno = pagep->pgno;
249
np->prev_pgno = pagep->prev_pgno;
250
if (new_next == NULL)
251
ret = mpf->put(mpf, np, DB_MPOOL_DIRTY);
254
ret = mpf->set(mpf, np, DB_MPOOL_DIRTY);
259
(void)__TLPUT(dbc, npl);
260
} else if (new_next != NULL)
264
pp->next_pgno = pagep->next_pgno;
265
if ((ret = mpf->put(mpf, pp, DB_MPOOL_DIRTY)) != 0)
268
(void)__TLPUT(dbc, ppl);
273
(void)mpf->put(mpf, np, 0);
275
(void)__TLPUT(dbc, npl);
277
(void)mpf->put(mpf, pp, 0);
279
(void)__TLPUT(dbc, ppl);