1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: bt_upgrade.c,v 12.9 2008/01/08 20:57:59 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/db_page.h"
13#include "dbinc/db_upgrade.h"
14#include "dbinc/btree.h"
15
16/*
17 * __bam_30_btreemeta --
18 *	Upgrade the metadata pages from version 6 to version 7.
19 *
20 * PUBLIC: int __bam_30_btreemeta __P((DB *, char *, u_int8_t *));
21 */
22int
23__bam_30_btreemeta(dbp, real_name, buf)
24	DB *dbp;
25	char *real_name;
26	u_int8_t *buf;
27{
28	BTMETA2X *oldmeta;
29	BTMETA30 *newmeta;
30	ENV *env;
31	int ret;
32
33	env = dbp->env;
34
35	newmeta = (BTMETA30 *)buf;
36	oldmeta = (BTMETA2X *)buf;
37
38	/*
39	 * Move things from the end up, so we do not overwrite things.
40	 * We are going to create a new uid, so we can move the stuff
41	 * at the end of the structure first, overwriting the uid.
42	 */
43
44	newmeta->re_pad = oldmeta->re_pad;
45	newmeta->re_len = oldmeta->re_len;
46	newmeta->minkey = oldmeta->minkey;
47	newmeta->maxkey = oldmeta->maxkey;
48	newmeta->dbmeta.free = oldmeta->free;
49	newmeta->dbmeta.flags = oldmeta->flags;
50	newmeta->dbmeta.type  = P_BTREEMETA;
51
52	newmeta->dbmeta.version = 7;
53	/* Replace the unique ID. */
54	if ((ret = __os_fileid(env, real_name, 1, buf + 36)) != 0)
55		return (ret);
56
57	newmeta->root = 1;
58
59	return (0);
60}
61
62/*
63 * __bam_31_btreemeta --
64 *	Upgrade the database from version 7 to version 8.
65 *
66 * PUBLIC: int __bam_31_btreemeta
67 * PUBLIC:      __P((DB *, char *, u_int32_t, DB_FH *, PAGE *, int *));
68 */
69int
70__bam_31_btreemeta(dbp, real_name, flags, fhp, h, dirtyp)
71	DB *dbp;
72	char *real_name;
73	u_int32_t flags;
74	DB_FH *fhp;
75	PAGE *h;
76	int *dirtyp;
77{
78	BTMETA30 *oldmeta;
79	BTMETA31 *newmeta;
80
81	COMPQUIET(dbp, NULL);
82	COMPQUIET(real_name, NULL);
83	COMPQUIET(fhp, NULL);
84
85	newmeta = (BTMETA31 *)h;
86	oldmeta = (BTMETA30 *)h;
87
88	/*
89	 * Copy the effected fields down the page.
90	 * The fields may overlap each other so we
91	 * start at the bottom and use memmove.
92	 */
93	newmeta->root = oldmeta->root;
94	newmeta->re_pad = oldmeta->re_pad;
95	newmeta->re_len = oldmeta->re_len;
96	newmeta->minkey = oldmeta->minkey;
97	newmeta->maxkey = oldmeta->maxkey;
98	memmove(newmeta->dbmeta.uid,
99	    oldmeta->dbmeta.uid, sizeof(oldmeta->dbmeta.uid));
100	newmeta->dbmeta.flags = oldmeta->dbmeta.flags;
101	newmeta->dbmeta.record_count = 0;
102	newmeta->dbmeta.key_count = 0;
103	ZERO_LSN(newmeta->dbmeta.unused3);
104
105	/* Set the version number. */
106	newmeta->dbmeta.version = 8;
107
108	/* Upgrade the flags. */
109	if (LF_ISSET(DB_DUPSORT))
110		F_SET(&newmeta->dbmeta, BTM_DUPSORT);
111
112	*dirtyp = 1;
113	return (0);
114}
115
116/*
117 * __bam_31_lbtree --
118 *	Upgrade the database btree leaf pages.
119 *
120 * PUBLIC: int __bam_31_lbtree
121 * PUBLIC:      __P((DB *, char *, u_int32_t, DB_FH *, PAGE *, int *));
122 */
123int
124__bam_31_lbtree(dbp, real_name, flags, fhp, h, dirtyp)
125	DB *dbp;
126	char *real_name;
127	u_int32_t flags;
128	DB_FH *fhp;
129	PAGE *h;
130	int *dirtyp;
131{
132	BKEYDATA *bk;
133	db_pgno_t pgno;
134	db_indx_t indx;
135	int ret;
136
137	ret = 0;
138	for (indx = O_INDX; indx < NUM_ENT(h); indx += P_INDX) {
139		bk = GET_BKEYDATA(dbp, h, indx);
140		if (B_TYPE(bk->type) == B_DUPLICATE) {
141			pgno = GET_BOVERFLOW(dbp, h, indx)->pgno;
142			if ((ret = __db_31_offdup(dbp, real_name, fhp,
143			    LF_ISSET(DB_DUPSORT) ? 1 : 0, &pgno)) != 0)
144				break;
145			if (pgno != GET_BOVERFLOW(dbp, h, indx)->pgno) {
146				*dirtyp = 1;
147				GET_BOVERFLOW(dbp, h, indx)->pgno = pgno;
148			}
149		}
150	}
151
152	return (ret);
153}
154