Skip to content

Commit

Permalink
WT-6741 Add check for supported data source on import (#6058)
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuo-cpp committed Oct 9, 2020
1 parent fadf9fc commit 700d4fe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/schema/schema_create.c
Expand Up @@ -101,7 +101,8 @@ __check_imported_ts(WT_SESSION_IMPL *session, const char *uri, const char *confi
* Create a new 'file:' object.
*/
static int
__create_file(WT_SESSION_IMPL *session, const char *uri, bool exclusive, const char *config)
__create_file(
WT_SESSION_IMPL *session, const char *uri, bool exclusive, bool import, const char *config)
{
WT_CONFIG_ITEM cval;
WT_DECL_ITEM(val);
Expand All @@ -110,11 +111,10 @@ __create_file(WT_SESSION_IMPL *session, const char *uri, bool exclusive, const c
*filecfg[] = {WT_CONFIG_BASE(session, file_meta), config, NULL, NULL, NULL};
char *fileconf;
uint32_t allocsize;
bool exists, import, import_repair, is_metadata;
bool exists, import_repair, is_metadata;

fileconf = NULL;

import = __wt_config_getones(session, config, "import.enabled", &cval) == 0 && cval.val != 0;
import_repair = false;
is_metadata = strcmp(uri, WT_METAFILE_URI) == 0;

Expand Down Expand Up @@ -653,7 +653,8 @@ __create_index(WT_SESSION_IMPL *session, const char *name, bool exclusive, const
* Create a table.
*/
static int
__create_table(WT_SESSION_IMPL *session, const char *uri, bool exclusive, const char *config)
__create_table(
WT_SESSION_IMPL *session, const char *uri, bool exclusive, bool import, const char *config)
{
WT_CONFIG conf;
WT_CONFIG_ITEM cgkey, cgval, ckey, cval;
Expand All @@ -664,7 +665,7 @@ __create_table(WT_SESSION_IMPL *session, const char *uri, bool exclusive, const
char *tableconf, *cgname;
const char *cfg[4] = {WT_CONFIG_BASE(session, table_meta), config, NULL, NULL};
const char *tablename;
bool import, import_repair;
bool import_repair;

cgname = NULL;
table = NULL;
Expand All @@ -674,7 +675,6 @@ __create_table(WT_SESSION_IMPL *session, const char *uri, bool exclusive, const

tablename = uri;
WT_PREFIX_SKIP_REQUIRED(session, tablename, "table:");
import = __wt_config_getones(session, config, "import.enabled", &cval) == 0 && cval.val != 0;

/* Check if the table already exists. */
if ((ret = __wt_metadata_search(session, uri, &tableconf)) != WT_NOTFOUND) {
Expand Down Expand Up @@ -781,9 +781,14 @@ __schema_create(WT_SESSION_IMPL *session, const char *uri, const char *config)
WT_CONFIG_ITEM cval;
WT_DATA_SOURCE *dsrc;
WT_DECL_RET;
bool exclusive;
bool exclusive, import;

exclusive = __wt_config_getones(session, config, "exclusive", &cval) == 0 && cval.val != 0;
import = __wt_config_getones(session, config, "import.enabled", &cval) == 0 && cval.val != 0;

if (import && !WT_PREFIX_MATCH(uri, "file:") && !WT_PREFIX_MATCH(uri, "table:"))
WT_RET_MSG(session, ENOTSUP,
"%s: import is only supported for 'file' and 'table' data sources", uri);

/*
* We track create operations: if we fail in the middle of creating a complex object, we want to
Expand All @@ -794,13 +799,13 @@ __schema_create(WT_SESSION_IMPL *session, const char *uri, const char *config)
if (WT_PREFIX_MATCH(uri, "colgroup:"))
ret = __create_colgroup(session, uri, exclusive, config);
else if (WT_PREFIX_MATCH(uri, "file:"))
ret = __create_file(session, uri, exclusive, config);
ret = __create_file(session, uri, exclusive, import, config);
else if (WT_PREFIX_MATCH(uri, "lsm:"))
ret = __wt_lsm_tree_create(session, uri, exclusive, config);
else if (WT_PREFIX_MATCH(uri, "index:"))
ret = __create_index(session, uri, exclusive, config);
else if (WT_PREFIX_MATCH(uri, "table:"))
ret = __create_table(session, uri, exclusive, config);
ret = __create_table(session, uri, exclusive, import, config);
else if ((dsrc = __wt_schema_get_source(session, uri)) != NULL)
ret = dsrc->create == NULL ? __wt_object_unsupported(session, uri) :
__create_data_source(session, uri, config, dsrc);
Expand Down
73 changes: 73 additions & 0 deletions test/suite/test_import07.py
@@ -0,0 +1,73 @@
#!/usr/bin/env python
#
# Public Domain 2014-2020 MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# test_import07.py
# Check that importing unsupported data sources returns an error.

import wiredtiger
from test_import01 import test_import_base
from wtscenario import make_scenarios

class test_import07(test_import_base):
original_db_file = 'original_db_file'
create_config = 'allocation_size=512,key_format=u,log=(enabled=true),value_format=u'

ntables = 10
nrows = 100
scenarios = make_scenarios([
('colgroup', dict(prefix='colgroup:')),
('lsm', dict(prefix='lsm:')),
('index', dict(prefix='index:')),
])

def test_import_unsupported_data_source(self):
# Make a bunch of files and fill them with data.
self.populate(self.ntables, self.nrows)
self.session.checkpoint()

# Export the metadata for one of the files we made.
# We just need an example of what a file configuration would typically look like.
cursor = self.session.open_cursor('metadata:', None, None)
for k, v in cursor:
if k.startswith('table:'):
example_db_file_config = cursor[k]
break
cursor.close()

# Contruct the config string.
import_config = 'import=(enabled,repair=false,file_metadata=(' + \
example_db_file_config + '))'

# This uri doesn't exist however, for the purposes of our test, this is fine.
uri = self.prefix + self.original_db_file

# Before importing, we check whether the data source is a 'file' or 'table'.
# If not, we return ENOTSUP.
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.create(uri, import_config),
'/Operation not supported/')

0 comments on commit 700d4fe

Please sign in to comment.