int TestPartOneKeyNoData(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	/* Allocate the memory from stack. */
	DBT keys[4];
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	nparts = 5;

	/* Do not assign any data to the first DBT. */
	memset(&keys[0], 0, sizeof(DBT));
	for (i = 1 ; i < (nparts - 1); i++) {
		memset(&keys[i], 0, sizeof(DBT));
		keys[i].data = &content[(i + 1) * (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Do not set any database flags. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	/*
	 * Verify that before the database is opened, DB->set_partition can
	 * be called multiple times regardless of its return code.
	 */
	keys[0].flags = DB_DBT_MALLOC;
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) != 0);
	keys[0].flags = 0;
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUPSORT flags. */
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUP flags. */
	setup_envdir(TEST_ENV, 1);
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	return (0);
}
int TestPartDuplicatedKey(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 5;

	CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL);
	memset(keys, 0, (nparts - 1) * sizeof(DBT));
	/* Assign the same data to the first 2 DBTs. */
	for (i = 0 ; i < (nparts - 1); i++) {
		if (i < 2)
			keys[i].data = &content[strlen(content) / nparts];
		else
			keys[i].data = &content[(i + 1) *
			    (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Do not set any database flags. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUPSORT flags. */
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUPSORT) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set DB_DUP flags. */
	setup_envdir(TEST_ENV, 1);
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->set_flags(dbp, DB_DUP) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	free(keys);
	return (0);
}
Exemplo n.º 3
0
int TestDbHotBackupTestSetup(CuTest *ct) {
	struct handlers *info;

	if ((info = calloc(1, sizeof(*info))) == NULL)
		return (ENOMEM);
	ct->context = info;
	setup_envdir(TEST_ENV, 1);
	setup_envdir(BACKUP_DIR, 1);
	return (0);
}
int TestPartitionTestSetup(CuTest *ct) {
	if (errfp != NULL)
		fclose(errfp);
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	return (0);
}
Exemplo n.º 5
0
int TestEnvConfigTestSetup(CuTest *ct) {
	struct context *info;

	if ((info = calloc(1, sizeof(*info))) == NULL)
		return (ENOMEM);
	ct->context = info;
	setup_envdir(TEST_ENV, 1);
	return (0);
}
int TestPreOpenSetterAndGetterTestSetup(CuTest *ct) {
	char buf[DB_MAXPATHLEN];

	setup_envdir(TEST_ENV, 1);
	sprintf(buf, "%s/%s", TEST_ENV, "errfile");
	errfile = fopen(buf, "w");
	CuAssert(ct, "open errfile", errfile != NULL);
	sprintf(buf, "%s/%s", TEST_ENV, "msgfile");
	msgfile = fopen(buf, "w");
	CuAssert(ct, "open msgfile", msgfile != NULL);

	info.dbenvp = NULL;
	info.dbp = NULL;
	info.mp = NULL;
	info.seqp = NULL;

	return (0);
}
int TestPartKeyCallBothSet(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBT *keys;
	u_int32_t i;

	dbenv = NULL;
	dbp = NULL;
	keys = NULL;
	nparts = 5;

	CuAssertTrue(ct, (keys = malloc((nparts - 1) * sizeof(DBT))) != NULL);
	memset(keys, 0, (nparts - 1) * sizeof(DBT));
	/* Do not assign any data to the first DBT. */
	for (i = 0 ; i < (nparts - 1); i++) {
		keys[i].data = &content[(i + 1) * (strlen(content) / nparts)];
		keys[i].size = sizeof(char);
	}

	/* Set both partition key and callback, expect it fails. */
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct,
	    dbp->set_partition(dbp, nparts, keys, partitionCallback) != 0);

	/* Set partition by key and open the database. */
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, dbp->close(dbp, 0) == 0);

	/* Reconfig the partition with callback, expect it fails. */
	CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0);
	CuAssertTrue(ct,
	    dbp->set_partition(dbp, nparts, NULL, partitionCallback) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, 0, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	fclose(errfp);
	errfp = NULL;

	/* Set partition by callback and open the database. */
	setup_envdir(TEST_ENV, 1);
	errfp = fopen("TESTDIR/errfile", "w");
	CuAssertTrue(ct, create_db(&dbenv, &dbp, 0, ct) == 0);
	CuAssertTrue(ct,
	    dbp->set_partition(dbp, nparts, NULL, partitionCallback) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, DB_CREATE, 0644) == 0);
	CuAssertTrue(ct, put_data(dbp) == 0);
	CuAssertTrue(ct, dbp->close(dbp, 0) == 0);

	/* Reconfig the partition with key, expect it fails. */
	CuAssertTrue(ct, db_create(&dbp, dbenv, 0) == 0);
	CuAssertTrue(ct, dbp->set_partition(dbp, nparts, keys, NULL) == 0);
	CuAssertTrue(ct, dbp->open(dbp, NULL,
	    "test.db", NULL, DB_BTREE, 0, 0644) != 0);
	CuAssertTrue(ct, close_db(dbenv, dbp, ct) == 0);
	free(keys);
	return (0);
}
Exemplo n.º 8
0
int TestBackupSimpleEnvWithConfig(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	DBTYPE dtype;
	struct handlers *info;
	char **names;
	int cnt, has_callback;
	time_t end_time, secs1, secs2, start_time;
	u_int32_t flag, value;

	dtype = DB_BTREE;
	info = ct->context;
	has_callback = 0;
	flag = DB_EXCL;
	end_time = secs1 = secs2 = start_time = 0;

	/* Step 1: set up directories. */
	CuAssert(ct, "setup_dir", setup_dir(0, NULL) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct, "open_dbp", open_dbp(&dbenv,
	    &dbp, dtype, 0, NULL, NULL, NULL, 0, NULL) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/*
	 * Step 3: store records into db so that there is more than
	 * 1 data page in the db.
	 */
	CuAssert(ct, "store_records", store_records(dbp, 10) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/*
	 * Step 4: verify the backup handle is NULL,
	 * since we never configure the backup.
	 */
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == EINVAL);

	/*
	 * Step 5: backup without any backup configs.
	 * 5a: backup only the db file without callbacks and record the time.
	 */
	start_time = time(NULL);
	CuAssert(ct, "backup_db",
	    backup_db(ct, dbenv, BACKUP_DB, flag, has_callback) == 0);
	end_time = time(NULL);
	secs1 = end_time - start_time;

	/* 5b: verify db file is in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 0, NULL, NULL) == 0);

	/* 5c: verify that no other files are in BACKUP_DIR. */
	CuAssert(ct, "__os_dirlist",
	    __os_dirlist(NULL, BACKUP_DIR, 0, &names, &cnt) == 0);
	CuAssert(ct, "too many files in backupdir", cnt == 1);

	/* Clean up the backup directory. */
	setup_envdir(BACKUP_DIR, 1);

	/*
	 * Step 6: backup with backup configs.
	 * 6a: configure the backup handle: use direct I/O to write pages to
	 * the disk, the backup buffer size is 256 bytes (which is smaller
	 * than the db page size), the number of pages
	 * to read before pausing is 1, and the number of seconds to sleep
	 * between batches of reads is 1.
	 */
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_WRITE_DIRECT, 1) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_SIZE, 256) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_READ_COUNT, 1) == 0);
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv,
	    DB_BACKUP_READ_SLEEP, US_PER_SEC / 2) == 0);

	/*
	 * 6b: backup only the db file without callbacks and
	 * record the time.
	 */
	start_time = time(NULL);
	CuAssert(ct, "backup_db",
	    backup_db(ct, dbenv, BACKUP_DB, flag, has_callback) == 0);
	end_time = time(NULL);
	secs2 = end_time - start_time;

	/* 6c: verify db file is in BACKUP_DIR. */
	CuAssert(ct, "verify_db_log",
	    verify_db_log(dtype, 0, 0, NULL, NULL) == 0);

	/* 6d: no other files are in BACKUP_DIR. */
	CuAssert(ct, "__os_dirlist",
	    __os_dirlist(NULL, BACKUP_DIR, 0, &names, &cnt) == 0);
	CuAssert(ct, "too many files in backupdir", cnt == 1);

	/* 6e: verify the backup config. */
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_READ_SLEEP, &value) == 0);
	CuAssertTrue(ct, value == US_PER_SEC / 2);
	/*
	 * Verify the backup config DB_BACKUP_READ_SLEEP works. That is with
	 * the configuration, backup pauses for a number of microseconds
	 * between batches of reads. So for the same backup content, the backup
	 * time with the configuration should be longer than that without it.
	 */
	CuAssertTrue(ct, secs2 > secs1);

	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_READ_COUNT, &value) == 0);
	CuAssertTrue(ct, value == 1);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv, DB_BACKUP_SIZE, &value) == 0);
	CuAssertTrue(ct, value == 256);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == 0);
	CuAssertTrue(ct, value == 1);

	/*
	 * Step 7: re-configure the backup write direct config and
	 * verify the new config value.
	 */
	CuAssert(ct, "DB_ENV->set_backup_config",
	    dbenv->set_backup_config(dbenv, DB_BACKUP_WRITE_DIRECT, 0) == 0);
	CuAssert(ct, "DB_ENV->get_backup_config",
	    dbenv->get_backup_config(dbenv,
	    DB_BACKUP_WRITE_DIRECT, &value) == 0);
	CuAssertTrue(ct, value == 0);

	return (0);
}
Exemplo n.º 9
0
int TestEnvConfigTestSetup(CuTest *ct) {
    setup_envdir(TEST_ENV, 1);
    return (0);
}