summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/schema/schema_alter.c
blob: 3b52b4bd22271feb21dddbc309aa429bf96f2800 (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
/*-
 * 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"

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

	uri = session->dhandle->name;
	WT_RET(__wt_meta_track_on(session));

	/*
	 * We know that we have exclusive access to the file.  So it will be
	 * closed after we're done with it and the next open will see the
	 * updated metadata.
	 */
	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);
	/*
	 * Map WT_NOTFOUND to ENOENT, based on the assumption WT_NOTFOUND means
	 * there was no metadata entry.
	 */
	if (ret == WT_NOTFOUND)
		ret = ENOENT;

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

	return (ret);
}