summaryrefslogtreecommitdiff
path: root/db/db_handle.c
blob: b9e244d5ea986cd0511abc59904235817bb3701d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 *
 * $Id$
 */

#include "wt_internal.h"

static int __wt_db_config(DB *);
static int __wt_idb_config(DB *);
static int __wt_idb_destroy(DB *);

/*
 * __wt_env_db --
 *	DB constructor.
 */
int
__wt_env_db(ENV *env, DB **dbp)
{
	DB *db;
	IDB *idb;
	int ret;

	db = NULL;
	idb = NULL;

	/* Create the DB and IDB structures. */
	WT_ERR(__wt_calloc(env, 1, sizeof(DB), &db));
	WT_ERR(__wt_calloc(env, 1, sizeof(IDB), &idb));

	/* Connect everything together. */
	db->idb = idb;
	idb->db = db;
	db->env = env;

	/* Configure the DB and the IDB. */
	WT_ERR(__wt_db_config(db));
	WT_ERR(__wt_idb_config(db));

	*dbp = db;
	return (0);

err:	(void)__wt_db_destroy(db);
	return (ret);
}

/*
 * __wt_db_config --
 *	Set configuration for a just-created DB handle.
 */
static int
__wt_db_config(DB *db)
{
	__wt_methods_db_config_default(db);
	__wt_methods_db_lockout(db);
	__wt_methods_db_init_transition(db);

	return (0);
}

/*
 * __wt_idb_config --
 *	Set configuration for a just-created IDB handle.
 */
static int
__wt_idb_config(DB *db)
{
	ENV *env;
	IDB *idb;
	IENV *ienv;

	env = db->env;
	idb = db->idb;
	ienv = env->ienv;

	idb->db = db;
	idb->root_off.addr = idb->free_addr = WT_ADDR_INVALID;

	__wt_lock(env, ienv->mtx);		/* Add to the ENV's list */
	TAILQ_INSERT_TAIL(&ienv->dbqh, idb, q);
	++ienv->dbqcnt;
	__wt_unlock(env, ienv->mtx);

	WT_RET(__wt_stat_alloc_db_stats(env, &idb->stats));
	WT_RET(__wt_stat_alloc_database_stats(env, &idb->dstats));

	return (0);
}

/*
 * __wt_db_destroy --
 *	DB handle destructor.
 */
int
__wt_db_destroy(DB *db)
{
	ENV *env;
	int ret;

	env = db->env;

	/* Discard the underlying IDB object. */
	ret = __wt_idb_destroy(db);

	/* Discard the DB object. */
	__wt_free(env, db, sizeof(DB));

	return (ret);
}

/*
 * __wt_idb_destroy --
 *	IDB handle destructor.
 */
static int
__wt_idb_destroy(DB *db)
{
	ENV *env;
	IDB *idb;
	IENV *ienv;
	int ret;

	env = db->env;
	idb = db->idb;
	ienv = env->ienv;
	ret = 0;

	/* Check that there's something to close. */
	if (idb == NULL)
		return (0);

	/* Diagnostic check: check flags against approved list. */
	WT_ENV_FCHK_RET(env, "Db.close", idb->flags, WT_APIMASK_IDB, ret);

	__wt_free(env, idb->name, 0);

	if (idb->huffman_key != NULL) {
		/* Key and data may use the same table, only close it once. */
		if (idb->huffman_data == idb->huffman_key)
			idb->huffman_data = NULL;
		__wt_huffman_close(env, idb->huffman_key);
		idb->huffman_key = NULL;
	}
	if (idb->huffman_data != NULL) {
		__wt_huffman_close(env, idb->huffman_data);
		idb->huffman_data = NULL;
	}

	__wt_walk_end(env, &idb->evict_walk);

	__wt_free(env, idb->stats, 0);
	__wt_free(env, idb->dstats, 0);

	__wt_lock(env, ienv->mtx);		/* Delete from the ENV's list */
	TAILQ_REMOVE(&ienv->dbqh, idb, q);
	--ienv->dbqcnt;
	__wt_unlock(env, ienv->mtx);

	__wt_free(env, idb, sizeof(IDB));
	db->idb = NULL;
	return (ret);
}

int
__wt_db_lockout_err(DB *db)
{
	__wt_api_db_errx(db,
	    "This Db handle has failed for some reason, and can no longer "
	    "be used; the only method permitted on it is Db.close which "
	    "discards the handle permanently");
	return (WT_ERROR);
}

int
__wt_db_lockout_open(DB *db)
{
	__wt_api_db_errx(db,
	    "This method may not be called until after the Db.open method has "
	    "been called");
	return (WT_ERROR);
}