2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
10
static const char revid[] = "$Id$";
13
#ifndef NO_SYSTEM_INCLUDES
14
#include <sys/types.h>
21
#include "dbinc/db_page.h"
22
#include "dbinc/hash.h"
23
#include "dbinc/db_upgrade.h"
26
* __ham_30_hashmeta --
27
* Upgrade the database from version 4/5 to version 6.
29
* PUBLIC: int __ham_30_hashmeta __P((DB *, char *, u_int8_t *));
32
__ham_30_hashmeta(dbp, real_name, obuf)
40
u_int32_t *o_spares, *n_spares;
41
u_int32_t fillf, maxb, nelem;
42
int i, max_entry, ret;
45
memset(&newmeta, 0, sizeof(newmeta));
47
oldmeta = (HASHHDR *)obuf;
50
* The first 32 bytes are similar. The only change is the version
51
* and that we removed the ovfl_point and have the page type now.
54
newmeta.dbmeta.lsn = oldmeta->lsn;
55
newmeta.dbmeta.pgno = oldmeta->pgno;
56
newmeta.dbmeta.magic = oldmeta->magic;
57
newmeta.dbmeta.version = 6;
58
newmeta.dbmeta.pagesize = oldmeta->pagesize;
59
newmeta.dbmeta.type = P_HASHMETA;
62
newmeta.dbmeta.flags = oldmeta->flags;
64
/* Copy the free list, which has changed its name but works the same. */
65
newmeta.dbmeta.free = oldmeta->last_freed;
67
/* Copy: max_bucket, high_mask, low-mask, ffactor, nelem, h_charkey */
68
newmeta.max_bucket = oldmeta->max_bucket;
69
newmeta.high_mask = oldmeta->high_mask;
70
newmeta.low_mask = oldmeta->low_mask;
71
newmeta.ffactor = oldmeta->ffactor;
72
newmeta.nelem = oldmeta->nelem;
73
newmeta.h_charkey = oldmeta->h_charkey;
76
* There was a bug in 2.X versions where the nelem could go negative.
77
* In general, this is considered "bad." If it does go negative
78
* (that is, very large and positive), we'll die trying to dump and
79
* load this database. So, let's see if we can fix it here.
81
nelem = newmeta.nelem;
82
fillf = newmeta.ffactor;
83
maxb = newmeta.max_bucket;
85
if ((fillf != 0 && fillf * maxb < 2 * nelem) ||
86
(fillf == 0 && nelem > 0x8000000))
90
* We now have to convert the spares array. The old spares array
91
* contained the total number of extra pages allocated prior to
92
* the bucket that begins the next doubling. The new spares array
93
* contains the page number of the first bucket in the next doubling
94
* MINUS the bucket number of that bucket.
96
o_spares = oldmeta->spares;
97
n_spares = newmeta.spares;
98
max_entry = __db_log2(maxb + 1); /* highest spares entry in use */
100
for (i = 1; i < NCACHED && i <= max_entry; i++)
101
n_spares[i] = 1 + o_spares[i - 1];
103
/* Replace the unique ID. */
104
if ((ret = __os_fileid(dbenv, real_name, 1, newmeta.dbmeta.uid)) != 0)
107
/* Overwrite the original. */
108
memcpy(oldmeta, &newmeta, sizeof(newmeta));
114
* __ham_30_sizefix --
115
* Make sure that all hash pages belonging to the current
116
* hash doubling are within the bounds of the file.
118
* PUBLIC: int __ham_30_sizefix __P((DB *, DB_FH *, char *, u_int8_t *));
121
__ham_30_sizefix(dbp, fhp, realname, metabuf)
127
u_int8_t buf[DB_MAX_PGSIZE];
130
db_pgno_t last_actual, last_desired;
136
memset(buf, 0, DB_MAX_PGSIZE);
138
meta = (HMETA30 *)metabuf;
139
pagesize = meta->dbmeta.pagesize;
142
* Get the last page number. To do this, we'll need dbp->pgsize
143
* to be set right, so slam it into place.
145
dbp->pgsize = pagesize;
146
if ((ret = __db_lastpgno(dbp, realname, fhp, &last_actual)) != 0)
150
* The last bucket in the doubling is equal to high_mask; calculate
151
* the page number that implies.
153
last_desired = BS_TO_PAGE(meta->high_mask, meta->spares);
156
* If last_desired > last_actual, we need to grow the file. Write
157
* a zeroed page where last_desired would go.
159
if (last_desired > last_actual) {
160
if ((ret = __os_seek(dbenv,
161
fhp, pagesize, last_desired, 0, 0, DB_OS_SEEK_SET)) != 0)
163
if ((ret = __os_write(dbenv, fhp, buf, pagesize, &nw)) != 0)
171
* __ham_31_hashmeta --
172
* Upgrade the database from version 6 to version 7.
174
* PUBLIC: int __ham_31_hashmeta
175
* PUBLIC: __P((DB *, char *, u_int32_t, DB_FH *, PAGE *, int *));
178
__ham_31_hashmeta(dbp, real_name, flags, fhp, h, dirtyp)
189
COMPQUIET(dbp, NULL);
190
COMPQUIET(real_name, NULL);
191
COMPQUIET(fhp, NULL);
193
newmeta = (HMETA31 *)h;
194
oldmeta = (HMETA30 *)h;
197
* Copy the fields down the page.
198
* The fields may overlap so start at the bottom and use memmove().
200
memmove(newmeta->spares, oldmeta->spares, sizeof(oldmeta->spares));
201
newmeta->h_charkey = oldmeta->h_charkey;
202
newmeta->nelem = oldmeta->nelem;
203
newmeta->ffactor = oldmeta->ffactor;
204
newmeta->low_mask = oldmeta->low_mask;
205
newmeta->high_mask = oldmeta->high_mask;
206
newmeta->max_bucket = oldmeta->max_bucket;
207
memmove(newmeta->dbmeta.uid,
208
oldmeta->dbmeta.uid, sizeof(oldmeta->dbmeta.uid));
209
newmeta->dbmeta.flags = oldmeta->dbmeta.flags;
210
newmeta->dbmeta.record_count = 0;
211
newmeta->dbmeta.key_count = 0;
212
ZERO_LSN(newmeta->dbmeta.unused3);
214
/* Update the version. */
215
newmeta->dbmeta.version = 7;
217
/* Upgrade the flags. */
218
if (LF_ISSET(DB_DUPSORT))
219
F_SET(&newmeta->dbmeta, DB_HASH_DUPSORT);
227
* Upgrade the database hash leaf pages.
229
* PUBLIC: int __ham_31_hash
230
* PUBLIC: __P((DB *, char *, u_int32_t, DB_FH *, PAGE *, int *));
233
__ham_31_hash(dbp, real_name, flags, fhp, h, dirtyp)
242
db_pgno_t pgno, tpgno;
249
for (indx = 0; indx < NUM_ENT(h); indx += 2) {
250
hk = (HKEYDATA *)H_PAIRDATA(dbp, h, indx);
251
if (HPAGE_PTYPE(hk) == H_OFFDUP) {
252
memcpy(&pgno, HOFFDUP_PGNO(hk), sizeof(db_pgno_t));
254
if ((ret = __db_31_offdup(dbp, real_name, fhp,
255
LF_ISSET(DB_DUPSORT) ? 1 : 0, &tpgno)) != 0)
259
memcpy(HOFFDUP_PGNO(hk),
260
&tpgno, sizeof(db_pgno_t));