Ejemplo n.º 1
0
/*
 * simple_walk_log --
 *	A simple walk of the write-ahead log.
 *	We wrote text messages into the log.  Print them.
 *	This verifies we're decrypting properly.
 */
static void
simple_walk_log(WT_SESSION *session)
{
	WT_CURSOR *cursor;
	WT_ITEM logrec_key, logrec_value;
	uint64_t txnid;
	uint32_t fileid, log_file, log_offset, opcount, optype, rectype;
	int found, ret;

	error_check(session->open_cursor(session, "log:", NULL, NULL, &cursor));

	found = 0;
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(
		    cursor, &log_file, &log_offset, &opcount));
		error_check(cursor->get_value(cursor, &txnid,
		    &rectype, &optype, &fileid, &logrec_key, &logrec_value));

		if (rectype == WT_LOGREC_MESSAGE) {
			found = 1;
			printf("Application Log Record: %s\n",
			    (char *)logrec_value.data);
		}
	}
	scan_end_check(ret == WT_NOTFOUND);

	error_check(cursor->close(cursor));
	if (found == 0) {
		fprintf(stderr, "Did not find log messages.\n");
		exit(EXIT_FAILURE);
	}
}
Ejemplo n.º 2
0
static void
cursor_search_near(WT_CURSOR *cursor)
{
	int exact, ret;
	const char *key = "some key";

	/*! [Search for an exact or adjacent match] */
	cursor->set_key(cursor, key);
	error_check(cursor->search_near(cursor, &exact));
	if (exact == 0) {
		/* an exact match */
	} else if (exact < 0) {
		/* returned smaller key */
	} else if (exact > 0) {
		/* returned larger key */
	}
	/*! [Search for an exact or adjacent match] */

	/*! [Forward scan greater than or equal] */
	cursor->set_key(cursor, key);
	error_check(cursor->search_near(cursor, &exact));
	if (exact >= 0) {
		/* include first key returned in the scan */
	}

	while ((ret = cursor->next(cursor)) == 0) {
		/* the rest of the scan */
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Forward scan greater than or equal] */

	/*! [Backward scan less than] */
	cursor->set_key(cursor, key);
	error_check(cursor->search_near(cursor, &exact));
	if (exact < 0) {
		/* include first key returned in the scan */
	}

	while ((ret = cursor->prev(cursor)) == 0) {
		/* the rest of the scan */
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Backward scan less than] */
}
Ejemplo n.º 3
0
/*! [statistics display function] */
void
print_cursor(WT_CURSOR *cursor)
{
	const char *desc, *pvalue;
	uint64_t value;
	int ret;

	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_value(cursor, &desc, &pvalue, &value));
		if (value != 0)
			printf("%s=%s\n", desc, pvalue);
	}
	scan_end_check(ret == WT_NOTFOUND);
}
Ejemplo n.º 4
0
/*! [cursor prev] */
int
cursor_reverse_scan(WT_CURSOR *cursor)
{
	const char *key, *value;
	int ret;

	while ((ret = cursor->prev(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &key));
		error_check(cursor->get_value(cursor, &value));
	}
	scan_end_check(ret == WT_NOTFOUND);

	return (0);
}
Ejemplo n.º 5
0
static void
take_full_backup(WT_SESSION *session, int i)
{
	WT_CURSOR *cursor;
	int j, ret;
	char buf[1024], h[256];
	const char *filename, *hdir;

	/*
	 * First time through we take a full backup into the incremental
	 * directories.  Otherwise only into the appropriate full directory.
	 */
	if (i != 0) {
		(void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
		hdir = h;
	} else
		hdir = home_incr;
	error_check(
	    session->open_cursor(session, "backup:", NULL, NULL, &cursor));

	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &filename));
		if (i == 0)
			/*
			 * Take a full backup into each incremental directory.
			 */
			for (j = 0; j < MAX_ITERATIONS; j++) {
				(void)snprintf(h, sizeof(h),
				    "%s.%d", home_incr, j);
				(void)snprintf(buf, sizeof(buf),
				    "cp %s/%s %s/%s",
				    home, filename, h, filename);
				error_check(system(buf));
			}
		else {
			(void)snprintf(h, sizeof(h), "%s.%d", home_full, i);
			(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
			    home, filename, hdir, filename);
			error_check(system(buf));
		}
	}
	scan_end_check(ret == WT_NOTFOUND);
	error_check(cursor->close(cursor));
}
Ejemplo n.º 6
0
static void
backup(WT_SESSION *session)
{
	char buf[1024];

	/*! [backup]*/
	WT_CURSOR *cursor;
	const char *filename;
	int ret;

	/* Create the backup directory. */
	error_check(mkdir("/path/database.backup", 077));

	/* Open the backup data source. */
	error_check(session->open_cursor(
	    session, "backup:", NULL, NULL, &cursor));

	/* Copy the list of files. */
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &filename));
		(void)snprintf(buf, sizeof(buf),
		    "cp /path/database/%s /path/database.backup/%s",
		    filename, filename);
		error_check(system(buf));
	}
	scan_end_check(ret == WT_NOTFOUND);

	error_check(cursor->close(cursor));
	/*! [backup]*/

	/*! [incremental backup]*/
	/* Open the backup data source for incremental backup. */
	error_check(session->open_cursor(
	    session, "backup:", NULL, "target=(\"log:\")", &cursor));
	/*! [incremental backup]*/
	error_check(cursor->close(cursor));

	/*! [backup of a checkpoint]*/
	error_check(session->checkpoint(
	    session, "drop=(from=June01),name=June01"));
	/*! [backup of a checkpoint]*/
}
Ejemplo n.º 7
0
/*
 * Read the index by year and print out who was in office that year.
 */
static void
read_index(WT_SESSION *session)
{
	WT_CURSOR *cursor;
	int i, ret;
	char *first_name, *last_name;
	uint16_t rec_year, term_end, term_start, year;

	year = 0;
	srand((unsigned int)getpid());
	error_check(session->open_cursor(
	    session, "index:presidents:term", NULL, NULL, &cursor));

	/*
	 * Pick 10 random years and read the data.
	 */
	for (i = 0; i < 10; i++) {
		year = (uint16_t)((rand() % YEAR_SPAN) + YEAR_BASE);
		printf("Year %" PRIu16 ":\n", year);
		cursor->set_key(cursor, year);
		error_check(cursor->search(cursor));
		error_check(cursor->get_key(cursor, &rec_year));
		error_check(cursor->get_value(cursor,
		    &last_name, &first_name, &term_start, &term_end));

		/* Report all presidents that served during the chosen year */
		ret = 0;
		while (term_start <= year &&
		    year <= term_end && year == rec_year) {
			printf("\t%s %s\n", first_name, last_name);
			if ((ret = cursor->next(cursor)) != 0)
				break;
			error_check(cursor->get_key(cursor, &rec_year));
			error_check(cursor->get_value(cursor,
			    &last_name, &first_name, &term_start, &term_end));
		}
		scan_end_check(ret == 0 || ret == WT_NOTFOUND);
	}

	error_check(cursor->close(cursor));
}
Ejemplo n.º 8
0
static void
take_incr_backup(WT_SESSION *session, int i)
{
	WT_CURSOR *cursor;
	int j, ret;
	char buf[1024], h[256];
	const char *filename;

	error_check(session->open_cursor(
	    session, "backup:", NULL, "target=(\"log:\")", &cursor));

	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &filename));
		/*
		 * Copy into the 0 incremental directory and then each of the
		 * incremental directories for this iteration and later.
		 */
		(void)snprintf(h, sizeof(h), "%s.0", home_incr);
		(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
		    home, filename, h, filename);
		error_check(system(buf));
		for (j = i; j < MAX_ITERATIONS; j++) {
			(void)snprintf(h, sizeof(h), "%s.%d", home_incr, j);
			(void)snprintf(buf, sizeof(buf), "cp %s/%s %s/%s",
			    home, filename, h, filename);
			error_check(system(buf));
		}
	}
	scan_end_check(ret == WT_NOTFOUND);

	/*
	 * With an incremental cursor, we want to truncate on the backup
	 * cursor to archive the logs.  Only do this if the copy process
	 * was entirely successful.
	 */
	error_check(session->truncate(session, "log:", cursor, NULL, NULL));
	error_check(cursor->close(cursor));
}
Ejemplo n.º 9
0
int
main(int argc, char *argv[])
{
	POP_RECORD *p;
	WT_CONNECTION *conn;
	WT_CURSOR *country_cursor, *country_cursor2, *cursor, *join_cursor,
	    *stat_cursor, *subjoin_cursor, *year_cursor;
	WT_SESSION *session;
	const char *country;
	uint64_t recno, population;
	uint16_t year;
	int ret;

	home = example_setup(argc, argv);

	error_check(wiredtiger_open(
	    home, NULL, "create,statistics=(fast)", &conn));

	error_check(conn->open_session(conn, NULL, NULL, &session));

	/*! [Create a table with column groups] */
	/*
	 * Create the population table.
	 * Keys are record numbers, the format for values is (5-byte string,
	 * uint16_t, uint64_t).
	 * See ::wiredtiger_struct_pack for details of the format strings.
	 */
	error_check(session->create(session, "table:poptable",
	    "key_format=r,"
	    "value_format=5sHQ,"
	    "columns=(id,country,year,population),"
	    "colgroups=(main,population)"));

	/*
	 * Create two column groups: a primary column group with the country
	 * code, year and population (named "main"), and a population column
	 * group with the population by itself (named "population").
	 */
	error_check(session->create(session,
	    "colgroup:poptable:main", "columns=(country,year,population)"));
	error_check(session->create(session,
	    "colgroup:poptable:population", "columns=(population)"));
	/*! [Create a table with column groups] */

	/*! [Create an index] */
	/* Create an index with a simple key. */
	error_check(session->create(session,
	    "index:poptable:country", "columns=(country)"));
	/*! [Create an index] */

	/*! [Create an index with a composite key] */
	/* Create an index with a composite key (country,year). */
	error_check(session->create(session,
	    "index:poptable:country_plus_year", "columns=(country,year)"));
	/*! [Create an index with a composite key] */

	/*! [Create an immutable index] */
	/* Create an immutable index. */
	error_check(session->create(session,
	    "index:poptable:immutable_year", "columns=(year),immutable"));
	/*! [Create an immutable index] */

	/* Insert the records into the table. */
	error_check(session->open_cursor(
	    session, "table:poptable", NULL, "append", &cursor));
	for (p = pop_data; p->year != 0; p++) {
		cursor->set_value(cursor, p->country, p->year, p->population);
		error_check(cursor->insert(cursor));
	}
	error_check(cursor->close(cursor));

	/* Update records in the table. */
	error_check(session->open_cursor(session,
	    "table:poptable", NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &recno));
		error_check(cursor->get_value(
		    cursor, &country, &year, &population));
		cursor->set_value(cursor, country, year, population + 1);
		error_check(cursor->update(cursor));
	}
	scan_end_check(ret == WT_NOTFOUND);
	error_check(cursor->close(cursor));

	/* List the records in the table. */
	error_check(session->open_cursor(session,
	    "table:poptable", NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &recno));
		error_check(cursor->get_value(
		    cursor, &country, &year, &population));
		printf("ID %" PRIu64, recno);
		printf(
		    ": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
		    country, year, population);
	}
	scan_end_check(ret == WT_NOTFOUND);
	error_check(cursor->close(cursor));

	/*! [List the records in the table using raw mode.] */
	/* List the records in the table using raw mode. */
	error_check(session->open_cursor(session,
	    "table:poptable", NULL, "raw", &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		WT_ITEM key, value;

		error_check(cursor->get_key(cursor, &key));
		error_check(wiredtiger_struct_unpack(
		    session, key.data, key.size, "r", &recno));
		printf("ID %" PRIu64, recno);

		error_check(cursor->get_value(cursor, &value));
		error_check(wiredtiger_struct_unpack(session,
		    value.data, value.size,
		    "5sHQ", &country, &year, &population));
		printf(
		    ": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
		    country, year, population);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [List the records in the table using raw mode.] */
	error_check(cursor->close(cursor));

	/*! [Read population from the primary column group] */
	/*
	 * Open a cursor on the main column group, and return the information
	 * for a particular country.
	 */
	error_check(session->open_cursor(
	    session, "colgroup:poptable:main", NULL, NULL, &cursor));
	cursor->set_key(cursor, 2);
	error_check(cursor->search(cursor));
	error_check(cursor->get_value(cursor, &country, &year, &population));
	printf(
	    "ID 2: country %s, year %" PRIu16 ", population %" PRIu64 "\n",
	    country, year, population);
	/*! [Read population from the primary column group] */
	error_check(cursor->close(cursor));

	/*! [Read population from the standalone column group] */
	/*
	 * Open a cursor on the population column group, and return the
	 * population of a particular country.
	 */
	error_check(session->open_cursor(session,
	    "colgroup:poptable:population", NULL, NULL, &cursor));
	cursor->set_key(cursor, 2);
	error_check(cursor->search(cursor));
	error_check(cursor->get_value(cursor, &population));
	printf("ID 2: population %" PRIu64 "\n", population);
	/*! [Read population from the standalone column group] */
	error_check(cursor->close(cursor));

	/*! [Search in a simple index] */
	/* Search in a simple index. */
	error_check(session->open_cursor(session,
	    "index:poptable:country", NULL, NULL, &cursor));
	cursor->set_key(cursor, "AU\0\0\0");
	error_check(cursor->search(cursor));
	error_check(cursor->get_value(cursor, &country, &year, &population));
	printf("AU: country %s, year %" PRIu16 ", population %" PRIu64 "\n",
	    country, year, population);
	/*! [Search in a simple index] */
	error_check(cursor->close(cursor));

	/*! [Search in a composite index] */
	/* Search in a composite index. */
	error_check(session->open_cursor(session,
	    "index:poptable:country_plus_year", NULL, NULL, &cursor));
	cursor->set_key(cursor, "USA\0\0", (uint16_t)1900);
	error_check(cursor->search(cursor));
	error_check(cursor->get_value(cursor, &country, &year, &population));
	printf(
	    "US 1900: country %s, year %" PRIu16 ", population %" PRIu64 "\n",
	    country, year, population);
	/*! [Search in a composite index] */
	error_check(cursor->close(cursor));

	/*! [Return a subset of values from the table] */
	/*
	 * Use a projection to return just the table's country and year
	 * columns.
	 */
	error_check(session->open_cursor(session,
	    "table:poptable(country,year)", NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_value(cursor, &country, &year));
		printf("country %s, year %" PRIu16 "\n", country, year);
	}
	/*! [Return a subset of values from the table] */
	scan_end_check(ret == WT_NOTFOUND);
	error_check(cursor->close(cursor));

	/*! [Return a subset of values from the table using raw mode] */
	/*
	 * Use a projection to return just the table's country and year
	 * columns, using raw mode.
	 */
	error_check(session->open_cursor(session,
	    "table:poptable(country,year)", NULL, "raw", &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		WT_ITEM value;

		error_check(cursor->get_value(cursor, &value));
		error_check(wiredtiger_struct_unpack(
		    session, value.data, value.size, "5sH", &country, &year));
		printf("country %s, year %" PRIu16 "\n", country, year);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Return a subset of values from the table using raw mode] */
	error_check(cursor->close(cursor));

	/*! [Return the table's record number key using an index] */
	/*
	 * Use a projection to return just the table's record number key
	 * from an index.
	 */
	error_check(session->open_cursor(session,
	    "index:poptable:country_plus_year(id)", NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &country, &year));
		error_check(cursor->get_value(cursor, &recno));
		printf("row ID %" PRIu64 ": country %s, year %" PRIu16 "\n",
		    recno, country, year);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Return the table's record number key using an index] */
	error_check(cursor->close(cursor));

	/*! [Return a subset of the value columns from an index] */
	/*
	 * Use a projection to return just the population column from an
	 * index.
	 */
	error_check(session->open_cursor(session,
	    "index:poptable:country_plus_year(population)",
	    NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &country, &year));
		error_check(cursor->get_value(cursor, &population));
		printf("population %" PRIu64 ": country %s, year %" PRIu16 "\n",
		    population, country, year);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Return a subset of the value columns from an index] */
	error_check(cursor->close(cursor));

	/*! [Access only the index] */
	/*
	 * Use a projection to avoid accessing any other column groups when
	 * using an index: supply an empty list of value columns.
	 */
	error_check(session->open_cursor(session,
	    "index:poptable:country_plus_year()", NULL, NULL, &cursor));
	while ((ret = cursor->next(cursor)) == 0) {
		error_check(cursor->get_key(cursor, &country, &year));
		printf("country %s, year %" PRIu16 "\n", country, year);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Access only the index] */
	error_check(cursor->close(cursor));

	/*! [Join cursors] */
	/* Open cursors needed by the join. */
	error_check(session->open_cursor(session,
	    "join:table:poptable", NULL, NULL, &join_cursor));
	error_check(session->open_cursor(session,
	    "index:poptable:country", NULL, NULL, &country_cursor));
	error_check(session->open_cursor(session,
	    "index:poptable:immutable_year", NULL, NULL, &year_cursor));

	/* select values WHERE country == "AU" AND year > 1900 */
	country_cursor->set_key(country_cursor, "AU\0\0\0");
	error_check(country_cursor->search(country_cursor));
	error_check(session->join(
	    session, join_cursor, country_cursor, "compare=eq,count=10"));
	year_cursor->set_key(year_cursor, (uint16_t)1900);
	error_check(year_cursor->search(year_cursor));
	error_check(session->join(session,
	    join_cursor, year_cursor, "compare=gt,count=10,strategy=bloom"));

	/* List the values that are joined */
	while ((ret = join_cursor->next(join_cursor)) == 0) {
		error_check(join_cursor->get_key(join_cursor, &recno));
		error_check(join_cursor->get_value(
		    join_cursor, &country, &year, &population));
		printf("ID %" PRIu64, recno);
		printf(
		    ": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
		    country, year, population);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Join cursors] */

	/*! [Statistics cursor join cursor] */
	error_check(session->open_cursor(session,
	    "statistics:join",
	    join_cursor, NULL, &stat_cursor));
	/*! [Statistics cursor join cursor] */

	error_check(stat_cursor->close(stat_cursor));
	error_check(join_cursor->close(join_cursor));
	error_check(year_cursor->close(year_cursor));
	error_check(country_cursor->close(country_cursor));

	/*! [Complex join cursors] */
	/* Open cursors needed by the join. */
	error_check(session->open_cursor(session,
	    "join:table:poptable", NULL, NULL, &join_cursor));
	error_check(session->open_cursor(session,
	    "join:table:poptable", NULL, NULL, &subjoin_cursor));
	error_check(session->open_cursor(session,
	    "index:poptable:country", NULL, NULL, &country_cursor));
	error_check(session->open_cursor(session,
	    "index:poptable:country", NULL, NULL, &country_cursor2));
	error_check(session->open_cursor(session,
	    "index:poptable:immutable_year", NULL, NULL, &year_cursor));

	/*
	 * select values WHERE (country == "AU" OR country == "UK")
	 *                     AND year > 1900
	 *
	 * First, set up the join representing the country clause.
	 */
	country_cursor->set_key(country_cursor, "AU\0\0\0");
	error_check(country_cursor->search(country_cursor));
	error_check(session->join(session, subjoin_cursor,
	    country_cursor, "operation=or,compare=eq,count=10"));
	country_cursor2->set_key(country_cursor2, "UK\0\0\0");
	error_check(country_cursor2->search(country_cursor2));
	error_check(session->join(session, subjoin_cursor,
	    country_cursor2, "operation=or,compare=eq,count=10"));

	/* Join that to the top join, and add the year clause */
	error_check(session->join(session, join_cursor, subjoin_cursor, NULL));
	year_cursor->set_key(year_cursor, (uint16_t)1900);
	error_check(year_cursor->search(year_cursor));
	error_check(session->join(session,
	    join_cursor, year_cursor, "compare=gt,count=10,strategy=bloom"));

	/* List the values that are joined */
	while ((ret = join_cursor->next(join_cursor)) == 0) {
		error_check(join_cursor->get_key(join_cursor, &recno));
		error_check(join_cursor->get_value(
		    join_cursor, &country, &year, &population));
		printf("ID %" PRIu64, recno);
		printf(
		    ": country %s, year %" PRIu16 ", population %" PRIu64 "\n",
		    country, year, population);
	}
	scan_end_check(ret == WT_NOTFOUND);
	/*! [Complex join cursors] */

	error_check(join_cursor->close(join_cursor));
	error_check(subjoin_cursor->close(subjoin_cursor));
	error_check(country_cursor->close(country_cursor));
	error_check(country_cursor2->close(country_cursor2));
	error_check(year_cursor->close(year_cursor));

	error_check(conn->close(conn, NULL));

	return (EXIT_SUCCESS);
}