xref: /csrg-svn/lib/libc/db/btree/bt_conv.c (revision 50990)
1*50990Sbostic /*-
2*50990Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*50990Sbostic  * All rights reserved.
4*50990Sbostic  *
5*50990Sbostic  * This code is derived from software contributed to Berkeley by
6*50990Sbostic  * Mike Olson.
7*50990Sbostic  *
8*50990Sbostic  * %sccs.include.redist.c%
9*50990Sbostic  */
10*50990Sbostic 
11*50990Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*50990Sbostic static char sccsid[] = "@(#)bt_conv.c	5.1 (Berkeley) 09/04/91";
13*50990Sbostic #endif /* LIBC_SCCS and not lint */
14*50990Sbostic 
15*50990Sbostic #include <sys/param.h>
16*50990Sbostic #include <db.h>
17*50990Sbostic #include <stdio.h>
18*50990Sbostic #include "btree.h"
19*50990Sbostic 
20*50990Sbostic /*
21*50990Sbostic  * __BT_BPGIN, __BT_BPGOUT --
22*50990Sbostic  *	Convert host-specific number layout to/from the host-independent
23*50990Sbostic  *	format stored on disk.
24*50990Sbostic  *
25*50990Sbostic  * Parameters:
26*50990Sbostic  *	tree:	tree
27*50990Sbostic  *	h:	page to convert
28*50990Sbostic  *
29*50990Sbostic  * Side Effects:
30*50990Sbostic  *	Layout of tree metadata on the page is changed in place.
31*50990Sbostic  *
32*50990Sbostic  * Warnings:
33*50990Sbostic  *	Everywhere else in the code, the types pgno_t and index_t are
34*50990Sbostic  *	opaque.  These two routines know what they really are.
35*50990Sbostic  */
36*50990Sbostic void
37*50990Sbostic __bt_pgin(t, pg, p)
38*50990Sbostic 	void *t;
39*50990Sbostic 	pgno_t pg;
40*50990Sbostic 	void *p;
41*50990Sbostic {
42*50990Sbostic 	register BINTERNAL *bi;
43*50990Sbostic 	register BLEAF *bl;
44*50990Sbostic 	register int i, top;
45*50990Sbostic 	PAGE *h;
46*50990Sbostic 
47*50990Sbostic 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
48*50990Sbostic 		return;
49*50990Sbostic 
50*50990Sbostic 	h = p;
51*50990Sbostic 	BLSWAP(h->pgno);
52*50990Sbostic 	BLSWAP(h->prevpg);
53*50990Sbostic 	BLSWAP(h->nextpg);
54*50990Sbostic 	BLSWAP(h->flags);
55*50990Sbostic 	BLSWAP(h->lower);
56*50990Sbostic 	BLSWAP(h->upper);
57*50990Sbostic 
58*50990Sbostic 	top = NEXTINDEX(h);
59*50990Sbostic 	if (!(h->flags & (P_BLEAF | P_RLEAF)))
60*50990Sbostic 		for (i = 0; i < top; i++) {
61*50990Sbostic 			BLSWAP(h->linp[i]);
62*50990Sbostic 			bi = GETBINTERNAL(h, i);
63*50990Sbostic 			BLSWAP(bi->ksize);
64*50990Sbostic 			BLSWAP(bi->pgno);
65*50990Sbostic 			BLSWAP(bi->flags);
66*50990Sbostic 			if (bi->flags & P_BIGKEY)
67*50990Sbostic 				BLSWAP(*(long *)bi->bytes);
68*50990Sbostic 		}
69*50990Sbostic 	else if (!(h->flags & P_OVERFLOW))
70*50990Sbostic 		for (i = 0; i < top; i++) {
71*50990Sbostic 			BLSWAP(h->linp[i]);
72*50990Sbostic 			bl = GETBLEAF(h, i);
73*50990Sbostic 			BLSWAP(bl->dsize);
74*50990Sbostic 			BLSWAP(bl->ksize);
75*50990Sbostic 			BLSWAP(bl->flags);
76*50990Sbostic 			if (bl->flags & P_BIGKEY)
77*50990Sbostic 				BLSWAP(*(long *)bl->bytes);
78*50990Sbostic 			if (bl->flags & P_BIGDATA)
79*50990Sbostic 				BLSWAP(*(long *)(bl->bytes + bl->ksize));
80*50990Sbostic 		}
81*50990Sbostic }
82*50990Sbostic 
83*50990Sbostic void
84*50990Sbostic __bt_pgout(t, pg, p)
85*50990Sbostic 	void *t;
86*50990Sbostic 	pgno_t pg;
87*50990Sbostic 	void *p;
88*50990Sbostic {
89*50990Sbostic 	register BINTERNAL *bi;
90*50990Sbostic 	register BLEAF *bl;
91*50990Sbostic 	register int i, top;
92*50990Sbostic 	PAGE *h;
93*50990Sbostic 
94*50990Sbostic 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
95*50990Sbostic 		return;
96*50990Sbostic 
97*50990Sbostic 	h = p;
98*50990Sbostic 	top = NEXTINDEX(h);
99*50990Sbostic 	if (!(h->flags & (P_BLEAF | P_RLEAF)))
100*50990Sbostic 		for (i = 0; i < top; i++) {
101*50990Sbostic 			bi = GETBINTERNAL(h, i);
102*50990Sbostic 			BLSWAP(bi->ksize);
103*50990Sbostic 			BLSWAP(bi->pgno);
104*50990Sbostic 			if (bi->flags & P_BIGKEY)
105*50990Sbostic 				BLSWAP(*(long *)bi->bytes);
106*50990Sbostic 			BLSWAP(h->linp[i]);
107*50990Sbostic 		}
108*50990Sbostic 	else if (!(h->flags & P_OVERFLOW))
109*50990Sbostic 		for (i = 0; i < top; i++) {
110*50990Sbostic 			bl = GETBLEAF(h, i);
111*50990Sbostic 			BLSWAP(bl->ksize);
112*50990Sbostic 			BLSWAP(bl->dsize);
113*50990Sbostic 			if (bl->flags & P_BIGKEY)
114*50990Sbostic 				BLSWAP(*(long *)bl->bytes);
115*50990Sbostic 			if (bl->flags & P_BIGDATA)
116*50990Sbostic 				BLSWAP(*(long *)(bl->bytes + bl->ksize));
117*50990Sbostic 			BLSWAP(h->linp[i]);
118*50990Sbostic 		}
119*50990Sbostic 	BLSWAP(h->pgno);
120*50990Sbostic 	BLSWAP(h->prevpg);
121*50990Sbostic 	BLSWAP(h->nextpg);
122*50990Sbostic 	BLSWAP(h->flags);
123*50990Sbostic 	BLSWAP(h->lower);
124*50990Sbostic 	BLSWAP(h->upper);
125*50990Sbostic }
126