Esempio n. 1
0
/*
 * dump_json_table_begin --
 *	Output the JSON syntax that starts a table, along with its config.
 */
static int
dump_json_table_begin(
    WT_SESSION *session, WT_CURSOR *cursor, const char *uri, const char *config)
{
	WT_DECL_RET;
	const char *name;
	char *jsonconfig, *stripped;

	jsonconfig = NULL;

	/* Get the table name. */
	if ((name = strchr(uri, ':')) == NULL) {
		fprintf(stderr, "%s: %s: corrupted uri\n", progname, uri);
		return (1);
	}
	++name;

	if ((ret = 
	    __wt_session_create_strip(session, config, NULL, &stripped)) != 0)
		return (util_err(ret, NULL));
	ret = dup_json_string(stripped, &jsonconfig);
	free(stripped);
	if (ret != 0)
		return (util_cerr(uri, "config dup", ret));
	if (printf("    \"%s\" : [\n        {\n", uri) < 0)
		goto eio;
	if (printf("            \"config\" : \"%s\",\n", jsonconfig) < 0)
		goto eio;

	if ((ret = dump_json_table_cg(
	    session, cursor, uri, name, "colgroup:", "colgroups")) == 0) {
		if (printf(",\n") < 0)
			goto eio;
		ret = dump_json_table_cg(
		    session, cursor, uri, name, "index:", "indices");
	}

	if (printf("\n        },\n        {\n            \"data\" : [") < 0)
		goto eio;

	if (0) {
eio:		ret = util_err(EIO, NULL);
	}

	free(jsonconfig);
	return (ret);
}
Esempio n. 2
0
/*
 * print_config --
 *	Output a key/value URI pair by combining v1 and v2.
 */
static int
print_config(
    WT_SESSION *session, const char *key, char *cfg[], bool json, bool toplevel)
{
	WT_DECL_RET;
	char *jsonconfig, *value_ret;

	/*
	 * We have all of the object configuration, but don't have the default
	 * session.create configuration. Have the underlying library add in the
	 * defaults and collapse it all into one load configuration string.
	 */
	jsonconfig = NULL;
	if ((ret = __wt_schema_create_final(
	    (WT_SESSION_IMPL *)session, cfg, &value_ret)) != 0)
		return (util_err(session, ret, NULL));
	if (json && (ret = dup_json_string(value_ret, &jsonconfig)) != 0) {
		free(value_ret);
		return (util_err(session, ret, NULL));
	}
	if (json) {
		if (toplevel)
			ret = printf(
			    "    \"%s\" : [\n        {\n            "
			    "\"config\" : \"%s\",\n", key, jsonconfig);
		else
			ret = printf(
			    "                {\n"
			    "                    \"uri\" : \"%s\",\n"
			    "                    \"config\" : \"%s\"\n"
			    "                }", key, jsonconfig);
	} else
		ret = printf("%s\n%s\n", key, value_ret);
	free(value_ret);
	free(jsonconfig);
	if (ret < 0)
		return (util_err(session, EIO, NULL));
	return (0);
}
Esempio n. 3
0
/*
 * dump_json_table_cg --
 *	Dump the column groups or indices for a table.
 */
static int
dump_json_table_cg(WT_SESSION *session, WT_CURSOR *cursor,
    const char *name, const char *entry, const char *header)
{
	WT_DECL_RET;
	const char *key, *skip, *value;
	int exact, once;
	char *jsonconfig;
	static const char * const indent = "                ";

	once = 0;
	if (printf("            \"%s\" : [", header) < 0)
		return (util_err(session, EIO, NULL));

	/*
	 * For table dumps, we're done.
	 */
	if (cursor == NULL) {
		if (printf("]") < 0)
			return (util_err(session, EIO, NULL));
		else
			return (0);
	}

	/*
	 * Search the file looking for column group and index key/value pairs:
	 * for each one, look up the related source information and append it
	 * to the base record.
	 */
	cursor->set_key(cursor, entry);
	if ((ret = cursor->search_near(cursor, &exact)) != 0) {
		if (ret == WT_NOTFOUND)
			return (0);
		return (util_cerr(cursor, "search_near", ret));
	}
	if (exact >= 0)
		goto match;
	while ((ret = cursor->next(cursor)) == 0) {
match:		if ((ret = cursor->get_key(cursor, &key)) != 0)
			return (util_cerr(cursor, "get_key", ret));

		/* Check if we've finished the list of entries. */
		if (!WT_PREFIX_MATCH(key, entry))
			break;

		/* Check for a table name match. */
		skip = key + strlen(entry);
		if (strncmp(
		    skip, name, strlen(name)) != 0 || skip[strlen(name)] != ':')
			continue;

		/* Get the value. */
		if ((ret = cursor->get_value(cursor, &value)) != 0)
			return (util_cerr(cursor, "get_value", ret));

		if ((ret = dup_json_string(value, &jsonconfig)) != 0)
			return (util_cerr(cursor, "config dup", ret));
		ret = printf("%s\n"
		    "%s{\n"
		    "%s    \"uri\" : \"%s\",\n"
		    "%s    \"config\" : \"%s\"\n"
		    "%s}",
		    (once == 0 ? "" : ","),
		    indent, indent, key, indent, jsonconfig, indent);
		free(jsonconfig);
		if (ret < 0)
			return (util_err(session, EIO, NULL));

		once = 1;
	}
	if (printf("%s]", (once == 0 ? "" : "\n            ")) < 0)
		return (util_err(session, EIO, NULL));
	if (ret == 0 || ret == WT_NOTFOUND)
		return (0);
	return (util_cerr(cursor, "next", ret));
}