示例#1
0
int
main(void)
{
	WT_CONNECTION *wt_conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	int i, record_count, ret;
	char cmd_buf[256], k[16], v[16];

	snprintf(cmd_buf, sizeof(cmd_buf), "rm -rf %s %s && mkdir %s %s",
	    home1, home2, home1, home2);
	if ((ret = system(cmd_buf)) != 0) {
		fprintf(stderr, "%s: failed ret %d\n", cmd_buf, ret);
		return (ret);
	}
	if ((ret = wiredtiger_open(home1, NULL, CONN_CONFIG, &wt_conn)) != 0) {
		fprintf(stderr, "Error connecting to %s: %s\n",
		    home1, wiredtiger_strerror(ret));
		return (ret);
	}

	ret = wt_conn->open_session(wt_conn, NULL, NULL, &session);
	ret = session->create(session, uri, "key_format=S,value_format=S");

	ret = session->open_cursor(session, uri, NULL, NULL, &cursor);
	/*
	 * Perform some operations with individual auto-commit transactions.
	 */
	for (record_count = 0, i = 0; i < MAX_KEYS; i++, record_count++) {
		snprintf(k, sizeof(k), "key%d", i);
		snprintf(v, sizeof(v), "value%d", i);
		cursor->set_key(cursor, k);
		cursor->set_value(cursor, v);
		ret = cursor->insert(cursor);
	}
	ret = session->begin_transaction(session, NULL);
	/*
	 * Perform some operations within a single transaction.
	 */
	for (i = MAX_KEYS; i < MAX_KEYS+5; i++, record_count++) {
		snprintf(k, sizeof(k), "key%d", i);
		snprintf(v, sizeof(v), "value%d", i);
		cursor->set_key(cursor, k);
		cursor->set_value(cursor, v);
		ret = cursor->insert(cursor);
	}
	ret = session->commit_transaction(session, NULL);
	ret = cursor->close(cursor);

	/*! [log cursor printf] */
	ret = session->log_printf(session, "Wrote %d records", record_count);
	/*! [log cursor printf] */

	/*
	 * Close and reopen the connection so that the log ends up with
	 * a variety of records such as file sync and checkpoint.  We
	 * have archiving turned off.
	 */
	ret = wt_conn->close(wt_conn, NULL);
	if ((ret = wiredtiger_open(home1, NULL, CONN_CONFIG, &wt_conn)) != 0) {
		fprintf(stderr, "Error connecting to %s: %s\n",
		    home1, wiredtiger_strerror(ret));
		return (ret);
	}

	ret = wt_conn->open_session(wt_conn, NULL, NULL, &session);
	ret = simple_walk_log(session);
	ret = walk_log(session);
	ret = wt_conn->close(wt_conn, NULL);
	return (ret);
}
示例#2
0
文件: ex_encrypt.c 项目: DINKIN/mongo
int
main(int argc, char *argv[])
{
	WT_CONNECTION *conn;
	WT_CURSOR *c1, *c2, *nc;
	WT_SESSION *session;
	int i, ret;
	char keybuf[32], valbuf[32];
	char *key1, *key2, *key3, *val1, *val2, *val3;

	home = example_setup(argc, argv);

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

	/*
	 * Write a log record that is larger than the base 128 bytes and
	 * also should compress well.
	 */
	error_check(session->log_printf(session,
	    COMP_A COMP_B COMP_C COMP_A COMP_B COMP_C
	    COMP_A COMP_B COMP_C COMP_A COMP_B COMP_C
	    "The quick brown fox jumps over the lazy dog "));
	simple_walk_log(session);

	/*
	 * Create and open some encrypted and not encrypted tables.
	 * Also use column store and compression for some tables.
	 */
	error_check(session->create(session, "table:crypto1",
	    "encryption=(name=rotn,keyid=" USER1_KEYID"),"
	    "columns=(key0,value0),"
	    "key_format=S,value_format=S"));
	error_check(session->create(session, "index:crypto1:byvalue",
	    "encryption=(name=rotn,keyid=" USER1_KEYID"),"
	    "columns=(value0,key0)"));
	error_check(session->create(session, "table:crypto2",
	    "encryption=(name=rotn,keyid=" USER2_KEYID"),"
	    "key_format=S,value_format=S"));
	error_check(session->create(session, "table:nocrypto",
	    "key_format=S,value_format=S"));

	/*
	 * Send in an unknown keyid.  WiredTiger will try to add in the
	 * new keyid, but the customize function above will return an
	 * error since it is unrecognized.
	 */
	ret = session->create(session, "table:cryptobad",
	    "encryption=(name=rotn,keyid=" USERBAD_KEYID"),"
	    "key_format=S,value_format=S");
	if (ret == 0) {
		fprintf(stderr, "Did not detect bad/unknown keyid error\n");
		exit(EXIT_FAILURE);
	}

	error_check(session->open_cursor(
	    session, "table:crypto1", NULL, NULL, &c1));
	error_check(session->open_cursor(
	    session, "table:crypto2", NULL, NULL, &c2));
	error_check(session->open_cursor(
	    session, "table:nocrypto", NULL, NULL, &nc));

	/*
	 * Insert a set of keys and values.  Insert the same data into
	 * all tables so that we can verify they're all the same after
	 * we decrypt on read.
	 */
	for (i = 0; i < MAX_KEYS; i++) {
		(void)snprintf(keybuf, sizeof(keybuf), "key%d", i);
		c1->set_key(c1, keybuf);
		c2->set_key(c2, keybuf);
		nc->set_key(nc, keybuf);

		(void)snprintf(valbuf, sizeof(valbuf), "value%d", i);
		c1->set_value(c1, valbuf);
		c2->set_value(c2, valbuf);
		nc->set_value(nc, valbuf);

		error_check(c1->insert(c1));
		error_check(c2->insert(c2));
		error_check(nc->insert(nc));
		if (i % 5 == 0)
			error_check(session->log_printf(
			    session, "Wrote %d records", i));
	}
	error_check(session->log_printf(
	    session, "Done. Wrote %d total records", i));

	while (c1->next(c1) == 0) {
		error_check(c1->get_key(c1, &key1));
		error_check(c1->get_value(c1, &val1));

		printf("Read key %s; value %s\n", key1, val1);
	}
	simple_walk_log(session);
	printf("CLOSE\n");
	error_check(conn->close(conn, NULL));

	/*
	 * We want to close and reopen so that we recreate the cache
	 * by reading the data from disk, forcing decryption.
	 */
	printf("REOPEN and VERIFY encrypted data\n");

	error_check(wiredtiger_open(home, NULL, WT_OPEN_CONFIG_GOOD, &conn));

	error_check(conn->open_session(conn, NULL, NULL, &session));
	/*
	 * Verify we can read the encrypted log after restart.
	 */
	simple_walk_log(session);
	error_check(session->open_cursor(
	    session, "table:crypto1", NULL, NULL, &c1));
	error_check(session->open_cursor(
	    session, "table:crypto2", NULL, NULL, &c2));
	error_check(session->open_cursor(
	    session, "table:nocrypto", NULL, NULL, &nc));

	/*
	 * Read the same data from each cursor.  All should be identical.
	 */
	while (c1->next(c1) == 0) {
		error_check(c2->next(c2));
		error_check(nc->next(nc));
		error_check(c1->get_key(c1, &key1));
		error_check(c1->get_value(c1, &val1));
		error_check(c2->get_key(c2, &key2));
		error_check(c2->get_value(c2, &val2));
		error_check(nc->get_key(nc, &key3));
		error_check(nc->get_value(nc, &val3));

		if (strcmp(key1, key2) != 0)
			fprintf(stderr, "Key1 %s and Key2 %s do not match\n",
			    key1, key2);
		if (strcmp(key1, key3) != 0)
			fprintf(stderr, "Key1 %s and Key3 %s do not match\n",
			    key1, key3);
		if (strcmp(key2, key3) != 0)
			fprintf(stderr, "Key2 %s and Key3 %s do not match\n",
			    key2, key3);
		if (strcmp(val1, val2) != 0)
			fprintf(stderr, "Val1 %s and Val2 %s do not match\n",
			    val1, val2);
		if (strcmp(val1, val3) != 0)
			fprintf(stderr, "Val1 %s and Val3 %s do not match\n",
			    val1, val3);
		if (strcmp(val2, val3) != 0)
			fprintf(stderr, "Val2 %s and Val3 %s do not match\n",
			    val2, val3);

		printf("Verified key %s; value %s\n", key1, val1);
	}

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

	return (EXIT_SUCCESS);
}