1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: hash_conv.c,v 12.10 2008/01/30 12:18:22 mjc Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12#include "dbinc/db_page.h"
13#include "dbinc/db_swap.h"
14#include "dbinc/hash.h"
15
16/*
17 * __ham_pgin --
18 *	Convert host-specific page layout from the host-independent format
19 *	stored on disk.
20 *
21 * PUBLIC: int __ham_pgin __P((DB *, db_pgno_t, void *, DBT *));
22 */
23int
24__ham_pgin(dbp, pg, pp, cookie)
25	DB *dbp;
26	db_pgno_t pg;
27	void *pp;
28	DBT *cookie;
29{
30	DB_PGINFO *pginfo;
31	PAGE *h;
32
33	h = pp;
34	pginfo = (DB_PGINFO *)cookie->data;
35
36	/*
37	 * The hash access method does blind reads of pages, causing them
38	 * to be created.  If the type field isn't set it's one of them,
39	 * initialize the rest of the page and return.
40	 */
41	if (h->type != P_HASHMETA && h->pgno == PGNO_INVALID) {
42		P_INIT(pp, (db_indx_t)pginfo->db_pagesize,
43		    pg, PGNO_INVALID, PGNO_INVALID, 0, P_HASH);
44		return (0);
45	}
46
47	if (!F_ISSET(pginfo, DB_AM_SWAP))
48		return (0);
49
50	return (h->type == P_HASHMETA ? __ham_mswap(dbp->env, pp) :
51	    __db_byteswap(dbp, pg, pp, pginfo->db_pagesize, 1));
52}
53
54/*
55 * __ham_pgout --
56 *	Convert host-specific page layout to the host-independent format
57 *	stored on disk.
58 *
59 * PUBLIC: int __ham_pgout __P((DB *, db_pgno_t, void *, DBT *));
60 */
61int
62__ham_pgout(dbp, pg, pp, cookie)
63	DB *dbp;
64	db_pgno_t pg;
65	void *pp;
66	DBT *cookie;
67{
68	DB_PGINFO *pginfo;
69	PAGE *h;
70
71	pginfo = (DB_PGINFO *)cookie->data;
72	if (!F_ISSET(pginfo, DB_AM_SWAP))
73		return (0);
74
75	h = pp;
76	return (h->type == P_HASHMETA ?  __ham_mswap(dbp->env, pp) :
77	    __db_byteswap(dbp, pg, pp, pginfo->db_pagesize, 0));
78}
79
80/*
81 * __ham_mswap --
82 *	Swap the bytes on the hash metadata page.
83 *
84 * PUBLIC: int __ham_mswap __P((ENV *, void *));
85 */
86int
87__ham_mswap(env, pg)
88	ENV *env;
89	void *pg;
90{
91	u_int8_t *p;
92	int i;
93
94	COMPQUIET(env, NULL);
95
96	__db_metaswap(pg);
97	p = (u_int8_t *)pg + sizeof(DBMETA);
98
99	SWAP32(p);		/* max_bucket */
100	SWAP32(p);		/* high_mask */
101	SWAP32(p);		/* low_mask */
102	SWAP32(p);		/* ffactor */
103	SWAP32(p);		/* nelem */
104	SWAP32(p);		/* h_charkey */
105	for (i = 0; i < NCACHED; ++i)
106		SWAP32(p);	/* spares */
107	p += 59 * sizeof(u_int32_t); /* unused */
108	SWAP32(p);		/* crypto_magic */
109	return (0);
110}
111