summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/schema/schema_alter.c
blob: 346f09a1a64400d29ffd0996d6d89b94ae8bfb20 (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
/*-
 * Copyright (c) 2014-2017 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __alter_file --
 *	Alter a file.
 */
static int
__alter_file(WT_SESSION_IMPL *session, const char *uri, const char *newcfg[])
{
	WT_DECL_RET;
	const char *cfg[4], *filename;
	char *config, *newconfig;

	filename = uri;
	newconfig = NULL;
	if (!WT_PREFIX_SKIP(filename, "file:"))
		return (__wt_unexpected_object_type(session, uri, "file:"));

	/* Find the URI */
	WT_RET(__wt_metadata_search(session, uri, &config));

	WT_ASSERT(session, newcfg[0] != NULL);
	/*
	 * Start with the base configuration because collapse is like
	 * a projection and if we are reading older metadata, it may not
	 * have all the components.
	 */
	cfg[0] = WT_CONFIG_BASE(session, file_meta);
	cfg[1] = config;
	cfg[2] = newcfg[0];
	cfg[3] = NULL;
	WT_ERR(__wt_config_collapse(session, cfg, &newconfig));
	/*
	 * Only rewrite if there are changes.
	 */
	if (strcmp(config, newconfig) != 0)
		WT_ERR(__wt_metadata_update(session, uri, newconfig));
	else
		WT_STAT_CONN_INCR(session, session_table_alter_skip);

err:	__wt_free(session, config);
	__wt_free(session, newconfig);
	return (ret);
}

/*
 * __alter_colgroup --
 *	WT_SESSION::alter for a colgroup.
 */
static int
__alter_colgroup(
    WT_SESSION_IMPL *session, const char *uri, const char *cfg[])
{
	WT_COLGROUP *colgroup;
	WT_DECL_RET;

	WT_ASSERT(session, F_ISSET(session, WT_SESSION_LOCKED_TABLE));

	/* If we can get the colgroup, perform any potential alterations. */
	if ((ret = __wt_schema_get_colgroup(
	    session, uri, false, NULL, &colgroup)) == 0)
		WT_TRET(__wt_schema_alter(session, colgroup->source, cfg));

	return (ret);
}

/*
 * __alter_index --
 *	WT_SESSION::alter for an index.
 */
static int
__alter_index(
    WT_SESSION_IMPL *session, const char *uri, const char *cfg[])
{
	WT_INDEX *idx;
	WT_DECL_RET;

	/* If we can get the index, perform any potential alterations. */
	if ((ret = __wt_schema_get_index(
	    session, uri, false, NULL, &idx)) == 0)
		WT_TRET(__wt_schema_alter(session, idx->source, cfg));

	return (ret);
}

/*
 * __alter_table --
 *	WT_SESSION::alter for a table.
 */
static int
__alter_table(WT_SESSION_IMPL *session, const char *uri, const char *cfg[])
{
	WT_COLGROUP *colgroup;
	WT_DECL_RET;
	WT_TABLE *table;
	const char *name;
	u_int i;

	name = uri;
	(void)WT_PREFIX_SKIP(name, "table:");

	WT_RET(__wt_schema_get_table(
	    session, name, strlen(name), true, &table));

	/*
	 * Alter the column groups only if we are using the default
	 * column group.  Otherwise the user should alter each
	 * index or column group explicitly.
	 */
	if (table->ncolgroups == 0)
		for (i = 0; i < WT_COLGROUPS(table); i++) {
			if ((colgroup = table->cgroups[i]) == NULL)
				continue;
			/*
			 * Alter the column group before updating the metadata
			 * to avoid the metadata for the table becoming
			 * inconsistent if we can't get exclusive access.
			 */
			WT_ERR(__wt_schema_alter(
			    session, colgroup->source, cfg));
		}
err:	__wt_schema_release_table(session, table);
	return (ret);
}

/*
 * __wt_schema_alter --
 *	Process a WT_SESSION::alter operation for all supported types.
 */
int
__wt_schema_alter(WT_SESSION_IMPL *session, const char *uri, const char *cfg[])
{
	WT_DATA_SOURCE *dsrc;
	WT_DECL_RET;

	WT_RET(__wt_meta_track_on(session));

	/* Paranoia: clear any handle from our caller. */
	session->dhandle = NULL;

	if (WT_PREFIX_MATCH(uri, "colgroup:"))
		ret = __alter_colgroup(session, uri, cfg);
	else if (WT_PREFIX_MATCH(uri, "file:"))
		ret = __alter_file(session, uri, cfg);
	else if (WT_PREFIX_MATCH(uri, "index:"))
		ret = __alter_index(session, uri, cfg);
	else if (WT_PREFIX_MATCH(uri, "lsm:"))
		ret = __wt_lsm_tree_alter(session, uri, cfg);
	else if (WT_PREFIX_MATCH(uri, "table:"))
		ret = __alter_table(session, uri, cfg);
	else if ((dsrc = __wt_schema_get_source(session, uri)) != NULL)
		ret = dsrc->alter == NULL ?
		    __wt_object_unsupported(session, uri) :
		    dsrc->alter(dsrc,
		    &session->iface, uri, (WT_CONFIG_ARG *)cfg);
	else
		ret = __wt_bad_object_type(session, uri);

	/*
	 * Map WT_NOTFOUND to ENOENT, based on the assumption WT_NOTFOUND means
	 * there was no metadata entry.
	 */
	if (ret == WT_NOTFOUND)
		ret = ENOENT;

	/* Bump the schema generation so that stale data is ignored. */
	(void)__wt_gen_next(session, WT_GEN_SCHEMA);

	WT_TRET(__wt_meta_track_off(session, true, ret != 0));

	return (ret);
}