/*修改表名*/ static int __rename_table(WT_SESSION_IMPL *session, const char *uri, const char *newuri, const char *cfg[]) { WT_DECL_RET; WT_TABLE *table; u_int i; const char *oldname; oldname = uri; (void)WT_PREFIX_SKIP(oldname, "table:"); WT_RET(__wt_schema_get_table(session, oldname, strlen(oldname), 0, &table)); /* Rename the column groups. */ for (i = 0; i < WT_COLGROUPS(table); i++) WT_ERR(__rename_tree(session, table, newuri, table->cgroups[i]->name, cfg)); /* Rename the indices. */ WT_ERR(__wt_schema_open_indices(session, table)); for (i = 0; i < table->nindices; i++) WT_ERR(__rename_tree(session, table, newuri, table->indices[i]->name, cfg)); WT_ERR(__wt_schema_remove_table(session, table)); table = NULL; /* Rename the table. */ WT_ERR(__metadata_rename(session, uri, newuri)); }
/* * __drop_table -- * WT_SESSION::drop for a table. */ static int __drop_table(WT_SESSION_IMPL *session, const char *uri, const char *cfg[]) { WT_COLGROUP *colgroup; WT_DECL_RET; WT_INDEX *idx; WT_TABLE *table; const char *name; u_int i; name = uri; (void)WT_PREFIX_SKIP(name, "table:"); table = NULL; WT_ERR(__wt_schema_get_table( session, name, strlen(name), true, &table)); /* Drop the column groups. */ for (i = 0; i < WT_COLGROUPS(table); i++) { if ((colgroup = table->cgroups[i]) == NULL) continue; /* * Drop 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_drop(session, colgroup->source, cfg)); WT_ERR(__wt_metadata_remove(session, colgroup->name)); } /* Drop the indices. */ WT_ERR(__wt_schema_open_indices(session, table)); for (i = 0; i < table->nindices; i++) { if ((idx = table->indices[i]) == NULL) continue; /* * Drop 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_drop(session, idx->source, cfg)); WT_ERR(__wt_metadata_remove(session, idx->name)); } WT_ERR(__wt_schema_remove_table(session, table)); table = NULL; /* Remove the metadata entry (ignore missing items). */ WT_ERR(__wt_metadata_remove(session, uri)); err: if (table != NULL) __wt_schema_release_table(session, table); return (ret); }
/* * __rename_table -- * WT_SESSION::rename for a table. */ static int __rename_table(WT_SESSION_IMPL *session, const char *uri, const char *newuri, const char *cfg[]) { WT_DECL_ITEM(buf); WT_DECL_RET; WT_TABLE *table; u_int i; const char *oldname, *value; oldname = uri; (void)WT_PREFIX_SKIP(oldname, "table:"); WT_RET(__wt_schema_get_table( session, oldname, strlen(oldname), 0, &table)); /* Rename the column groups. */ for (i = 0; i < WT_COLGROUPS(table); i++) WT_ERR(__rename_tree(session, table, newuri, table->cgroups[i]->name, cfg)); /* Rename the indices. */ WT_ERR(__wt_schema_open_indices(session, table)); for (i = 0; i < table->nindices; i++) WT_ERR(__rename_tree(session, table, newuri, table->indices[i]->name, cfg)); __wt_schema_remove_table(session, table); table = NULL; /* Rename the table. */ WT_ERR(__wt_scr_alloc(session, 0, &buf)); WT_ERR(__wt_metadata_read(session, uri, &value)); WT_ERR(__wt_metadata_remove(session, uri)); WT_ERR(__wt_metadata_insert(session, newuri, value)); err: __wt_scr_free(&buf); if (table != NULL) __wt_schema_release_table(session, table); return (ret); }
/* * __drop_table -- * WT_SESSION::drop for a table. */ static int __drop_table(WT_SESSION_IMPL *session, const char *uri, int force) { WT_BTREE *btree; WT_TABLE *table; int i, ret; const char *name; ret = 0; name = uri; (void)WT_PREFIX_SKIP(name, "table:"); WT_ERR(__wt_schema_get_table(session, name, strlen(name), &table)); /* Drop the column groups. */ for (i = 0; i < WT_COLGROUPS(table); i++) { if ((btree = table->colgroup[i]) == NULL) continue; table->colgroup[i] = NULL; WT_TRET(__drop_tree(session, btree, force)); } /* Drop the indices. */ WT_TRET(__wt_schema_open_index(session, table, NULL, 0)); for (i = 0; i < table->nindices; i++) { btree = table->index[i]; table->index[i] = NULL; WT_TRET(__drop_tree(session, btree, force)); } WT_TRET(__wt_schema_remove_table(session, table)); /* Remove the schema table entry (ignore missing items). */ WT_TRET(__wt_schema_table_remove(session, uri)); err: if (force && ret == WT_NOTFOUND) ret = 0; return (ret); }
/* * __rename_table -- * WT_SESSION::rename for a table. */ static int __rename_table( WT_SESSION_IMPL *session, const char *oldname, const char *newname) { WT_DECL_RET; WT_ITEM *buf; WT_TABLE *table; int i; const char *value; buf = NULL; WT_RET( __wt_schema_get_table(session, oldname, strlen(oldname), &table)); /* Rename the column groups. */ for (i = 0; i < WT_COLGROUPS(table); i++) WT_RET(__rename_tree(session, table->cg_name[i], newname)); /* Rename the indices. */ WT_RET(__wt_schema_open_index(session, table, NULL, 0)); for (i = 0; i < table->nindices; i++) WT_RET(__rename_tree(session, table->idx_name[i], newname)); WT_RET(__wt_schema_remove_table(session, table)); /* Rename the table. */ WT_ERR(__wt_scr_alloc(session, 0, &buf)); WT_ERR(__wt_buf_fmt(session, buf, "table:%s", oldname)); WT_ERR(__wt_metadata_read(session, buf->data, &value)); WT_ERR(__wt_metadata_remove(session, buf->data)); WT_ERR(__wt_buf_fmt(session, buf, "table:%s", newname)); WT_ERR(__wt_metadata_insert(session, buf->data, value)); err: __wt_scr_free(&buf); return (ret); }
/* * __create_table -- * Create a table. */ static int __create_table(WT_SESSION_IMPL *session, const char *name, int exclusive, const char *config) { WT_CONFIG conf; WT_CONFIG_ITEM cgkey, cgval, cval; WT_DECL_RET; WT_TABLE *table; const char *cfg[4] = { WT_CONFIG_BASE(session, table_meta), config, NULL, NULL }; const char *tablename; char *tableconf, *cgname; size_t cgsize; int ncolgroups; cgname = NULL; table = NULL; tableconf = NULL; tablename = name; if (!WT_PREFIX_SKIP(tablename, "table:")) return (EINVAL); if ((ret = __wt_schema_get_table(session, tablename, strlen(tablename), 0, &table)) == 0) { __wt_schema_release_table(session, table); return (exclusive ? EEXIST : 0); } WT_RET_NOTFOUND_OK(ret); WT_ERR(__wt_config_gets(session, cfg, "colgroups", &cval)); WT_ERR(__wt_config_subinit(session, &conf, &cval)); for (ncolgroups = 0; (ret = __wt_config_next(&conf, &cgkey, &cgval)) == 0; ncolgroups++) ; WT_ERR_NOTFOUND_OK(ret); WT_ERR(__wt_config_collapse(session, cfg, &tableconf)); if ((ret = __wt_metadata_insert(session, name, tableconf)) != 0) { /* * If the entry already exists in the metadata, we're done. * This is an error for exclusive creates but okay otherwise. */ if (ret == WT_DUPLICATE_KEY) ret = exclusive ? EEXIST : 0; goto err; } /* Attempt to open the table now to catch any errors. */ WT_ERR(__wt_schema_get_table( session, tablename, strlen(tablename), 1, &table)); if (ncolgroups == 0) { cgsize = strlen("colgroup:") + strlen(tablename) + 1; WT_ERR(__wt_calloc_def(session, cgsize, &cgname)); snprintf(cgname, cgsize, "colgroup:%s", tablename); WT_ERR(__create_colgroup(session, cgname, exclusive, config)); } if (0) { err: if (table != NULL) { WT_TRET(__wt_schema_remove_table(session, table)); table = NULL; } } if (table != NULL) __wt_schema_release_table(session, table); __wt_free(session, cgname); __wt_free(session, tableconf); return (ret); }
/* * __create_table -- * Create a table. */ static int __create_table(WT_SESSION_IMPL *session, const char *name, bool exclusive, const char *config) { WT_CONFIG conf; WT_CONFIG_ITEM cgkey, cgval, cval; WT_DECL_RET; WT_TABLE *table; const char *cfg[4] = { WT_CONFIG_BASE(session, table_meta), config, NULL, NULL }; const char *tablename; char *tableconf, *cgname; size_t cgsize; int ncolgroups; bool exists; cgname = NULL; table = NULL; tableconf = NULL; exists = false; tablename = name; if (!WT_PREFIX_SKIP(tablename, "table:")) return (__wt_unexpected_object_type(session, name, "table:")); if ((ret = __wt_schema_get_table(session, tablename, strlen(tablename), false, &table)) == 0) { if (exclusive) WT_ERR(EEXIST); exists = true; } WT_ERR_NOTFOUND_OK(ret); WT_ERR(__wt_config_gets(session, cfg, "colgroups", &cval)); __wt_config_subinit(session, &conf, &cval); for (ncolgroups = 0; (ret = __wt_config_next(&conf, &cgkey, &cgval)) == 0; ncolgroups++) ; WT_ERR_NOTFOUND_OK(ret); WT_ERR(__wt_config_collapse(session, cfg, &tableconf)); if (!exists) { WT_ERR(__wt_metadata_insert(session, name, tableconf)); /* Attempt to open the table now to catch any errors. */ WT_ERR(__wt_schema_get_table( session, tablename, strlen(tablename), true, &table)); if (ncolgroups == 0) { cgsize = strlen("colgroup:") + strlen(tablename) + 1; WT_ERR(__wt_calloc_def(session, cgsize, &cgname)); snprintf(cgname, cgsize, "colgroup:%s", tablename); WT_ERR(__create_colgroup( session, cgname, exclusive, config)); } } if (0) { err: if (table != NULL) { WT_TRET(__wt_schema_remove_table(session, table)); table = NULL; } } if (table != NULL) __wt_schema_release_table(session, table); __wt_free(session, cgname); __wt_free(session, tableconf); return (ret); }