int TestDbPreOpenSetterAndGetter(CuTest *ct) {
	DB_ENV *dbenv;
	DB *db, *env_db, *hash_db, *heap_db, *queue_db, *recno_db;
	const char **part_dirs;
	u_int32_t encrypt_flags, heap_set_bytes, heap_set_gbytes,
	    heap_get_bytes, heap_get_gbytes;
	int nowait, onoff;
	
	/* ================ General and Btree Configuration =============== */

	CuAssert(ct, "db_create", create_db_handle(&db, NULL) == 0);

	/*
	 * Test DB->set_cachesize(), DB->get_cachesize().
	 * We use specific values to avoid adjustment.
	 */
	CHECK_3_DIGIT_VALUES(db, set_cachesize, get_cachesize,
	    u_int32_t, 3, u_int32_t, 1048576, int, 5);

	/* Test DB->set_encrypt(), DB->get_encrypt_flags(). */
	CuAssert(ct, "db->set_encrypt",
	    db->set_encrypt(db, passwd, DB_ENCRYPT_AES) == 0);
	CuAssert(ct, "db->get_encrypt_flags",
	    db->get_encrypt_flags(db, &encrypt_flags) == 0);
	CuAssert(ct, "check encrypt flags", encrypt_flags == DB_ENCRYPT_AES);

	/* Test DB->set_errfile(), DB->get_errfile(). */
	CHECK_1_PTR_VALUE_VOID(db, set_errfile, get_errfile, FILE, errfile);

	/* Test DB->set_errpfx(), DB->get_errpfx().*/
	CHECK_1_STR_VALUE_VOID(db, set_errpfx, get_errpfx, "dbp1");

	/* Test DB->set_flags(), DB->get_flags(). */
	CHECK_FLAG_VALUE(db, set_flags, get_flags,
	    u_int32_t, DB_CHKSUM | DB_RECNUM | DB_REVSPLITOFF);

	/* Test DB->set_lk_exclusive(), DB->get_lk_exclusive(). */
	CuAssert(ct, "db->set_lk_exclusive", db->set_lk_exclusive(db, 1) == 0);
	CuAssert(ct, "db->get_lk_exclusive",
	    db->get_lk_exclusive(db, &onoff, &nowait) == 0);
	CuAssert(ct, "check lk_exclusive onoff", onoff == 1);
	CuAssert(ct, "check lk_exclusive nowait", nowait == 1);

	/*
	 * Test DB->set_lorder(), DB->get_lorder().
	 * The only acceptable values are 1234 and 4321.
	 */
	CHECK_1_DIGIT_VALUE(db, set_lorder, get_lorder, int, 1234);
	CHECK_1_DIGIT_VALUE(db, set_lorder, get_lorder, int, 4321);

	/* Test DB->set_msgfile(), DB->get_msgfile(). */
	CHECK_1_PTR_VALUE_VOID(db, set_msgfile, get_msgfile, FILE, msgfile);

	/*
	 * Test DB->set_pagesize(), DB->get_pagesize().
	 * The pagesize should be 512-55536, and be power of two.
	 */
	CHECK_1_DIGIT_VALUE(db, set_pagesize, get_pagesize, u_int32_t, 512);
	CHECK_1_DIGIT_VALUE(db, set_pagesize, get_pagesize, u_int32_t, 65536);

	/*
	 * Test DB->set_bt_minkey(), DB->get_bt_minkey().
	 * The minkey value should be 2 at least.
	 */
	CHECK_1_DIGIT_VALUE(db, set_bt_minkey, get_bt_minkey, u_int32_t, 17);

	CuAssert(ct, "db->close", close_db_handle(db) == 0);

	/* =================== Recno-only Configuration ===================== */

	CuAssert(ct, "db_create", create_db_handle(&recno_db, NULL) == 0);

	/* Test DB->set_flags(), DB->get_flags(). */
	CHECK_FLAG_VALUE(recno_db, set_flags, get_flags,
	    u_int32_t, DB_RENUMBER | DB_SNAPSHOT);

	/* Test DB->set_re_delim(), DB->get_re_delim(). */
	CHECK_1_DIGIT_VALUE(recno_db, set_re_delim, get_re_delim,
	    int, rand());

	/* Test DB->set_re_len(), DB->get_re_len(). */
	CHECK_1_DIGIT_VALUE(recno_db, set_re_len, get_re_len,
	    u_int32_t, rand());

	/* Test DB->set_re_pad(), DB->get_re_pad(). */
	CHECK_1_DIGIT_VALUE(recno_db, set_re_pad, get_re_pad, int, rand());

	/* Test DB->set_re_source(), DB->get_re_source(). */
	CHECK_1_STR_VALUE(recno_db, set_re_source, get_re_source, "re_source1");

	CuAssert(ct, "recno_db->close", close_db_handle(recno_db) == 0);

	/* ==================== Hash-only Configuration ===================== */

	CuAssert(ct, "db_create", create_db_handle(&hash_db, NULL) == 0);

	/* Test DB->set_flags(), DB->get_flags(). */
	CHECK_FLAG_VALUE(hash_db, set_flags, get_flags,
	    u_int32_t, DB_DUP | DB_DUPSORT | DB_REVSPLITOFF);

	/* Test DB->set_h_ffactor(), DB->get_h_ffactor(). */
	CHECK_1_DIGIT_VALUE(hash_db, set_h_ffactor, get_h_ffactor,
	    u_int32_t, rand());

	/* Test DB->set_h_nelem(), DB->get_h_nelem(). */
	CHECK_1_DIGIT_VALUE(hash_db, set_h_nelem, get_h_nelem,
	    u_int32_t, rand());

	CuAssert(ct, "hash_db->close", close_db_handle(hash_db) == 0);

	/* =================== Queue-only Configuration ===================== */

	CuAssert(ct, "db_create", create_db_handle(&queue_db, NULL) == 0);

	/* Test DB->set_flags(), DB->get_flags(). */
	CHECK_FLAG_VALUE(queue_db, set_flags, get_flags, u_int32_t, DB_INORDER);

	/* Test DB->set_q_extentsize(), DB->get_q_extentsize(). */
	CHECK_1_DIGIT_VALUE(queue_db, set_q_extentsize, get_q_extentsize,
	    u_int32_t, rand());

	CuAssert(ct, "queue_db->close", close_db_handle(queue_db) == 0);

	/* ==================== Heap-only Configuration ===================== */
	CuAssert(ct, "db_create", create_db_handle(&heap_db, NULL) == 0);

	/* Test DB->set_heapsize(), DB->get_heapsize(). */
	heap_set_gbytes = 3;
	heap_set_bytes = 1048576;
	heap_get_gbytes = heap_get_bytes = 0;
	CuAssert(ct, "DB->set_heapsize", heap_db->set_heapsize(heap_db,
	    heap_set_gbytes, heap_set_bytes, 0) == 0);
	CuAssert(ct, "DB->get_heapsize", heap_db->get_heapsize(heap_db,
	    &heap_get_gbytes, &heap_get_bytes) == 0);
	CuAssert(ct, "Check heap gbytes", heap_set_gbytes == heap_get_gbytes);
	CuAssert(ct, "Check heap bytes", heap_set_bytes == heap_get_bytes);

	/* Test DB->set_heap_regionsize(), DB->get_heap_regionsize(). */
	CHECK_1_DIGIT_VALUE(heap_db, set_heap_regionsize, get_heap_regionsize,
	    u_int32_t, rand());

	CuAssert(ct, "heap_db->close", close_db_handle(heap_db) == 0);

	/*
	 * The following configurations require the database
	 * be opened in an environment.
	 */
	CuAssert(ct, "db_env_create", create_dbenv_handle(&dbenv) == 0);
	CuAssert(ct, "dbenv->set_flags(DB_ENCRYPT)", dbenv->set_encrypt(dbenv,
	    passwd, DB_ENCRYPT_AES) == 0);	
	CuAssert(ct, "add_dirs_to_dbenv",
	    add_dirs_to_dbenv(dbenv, data_dirs) == 0);
	CuAssert(ct, "dbenv->open", dbenv->open(dbenv, TEST_ENV,
	    DB_CREATE | DB_INIT_MPOOL | DB_INIT_TXN, 0644) == 0);
	CuAssert(ct, "db_create", create_db_handle(&env_db, dbenv) == 0);

	/* Test DB->set_flags(), DB->get_flags(). */
	CHECK_FLAG_VALUE(env_db, set_flags, get_flags,
	    u_int32_t, DB_ENCRYPT | DB_TXN_NOT_DURABLE);

	/* Test DB->set_create_dir(), DB->get_create_dir(). */
	CHECK_1_STR_VALUE(env_db, set_create_dir, get_create_dir, data_dirs[0]);

	/* Test DB->set_partition_dirs(), DB->get_partition_dirs(). */
	CuAssert(ct, "env_db->set_partition_dirs",
	    env_db->set_partition_dirs(env_db, &data_dirs[1]) == 0);
	CuAssert(ct, "env_db->get_partition_dirs",
	    env_db->get_partition_dirs(env_db, &part_dirs) == 0);
	CuAssert(ct, "cmp_dirs", cmp_dirs(&data_dirs[1], part_dirs) == 0);

	CuAssert(ct, "env_db->close", close_db_handle(env_db) == 0);
	CuAssert(ct, "dbenv->close", close_dbenv_handle(dbenv) == 0);

	return (0);
}
Example #2
0
int
main(int argc, char *const* argv)
{
	const char *progname = "dbxml_dump";
	DB_ENV *dbenv;
	XmlManager *xmlDb;
	u_int32_t cache;
	int ch, exitval, is_private, keyflag, nflag, ret, Rflag, rflag;
	char *home, *passwd;

	if ((ret = version_check(progname)) != 0)
		return (ret);

	dbenv = NULL;
	xmlDb = NULL;
	exitval = nflag = rflag = Rflag = 0;
	keyflag = 0;
	cache = MEGABYTE;
	is_private = 0;
	home = passwd = NULL;

	while ((ch = getopt(argc, argv, "f:h:NP:rRV")) != EOF)
		switch (ch) {
		case 'f':
			if (freopen(optarg, "wb", stdout) == NULL) {
				fprintf(stderr, "%s: %s: reopen: %s\n",
					progname, optarg, strerror(errno));
				return (EXIT_FAILURE);
			}
			break;
		case 'h':
			home = optarg;
			break;
		case 'N':
			nflag = 1;
			break;
		case 'P':
			passwd = strdup(optarg);
			memset(optarg, 0, strlen(optarg));
			if (passwd == NULL) {
				fprintf(stderr, "%s: strdup: %s\n",
					progname, strerror(errno));
				return (EXIT_FAILURE);
			}
			break;
		case 'R':
			Rflag = 1;
			/* DB_AGGRESSIVE requires DB_SALVAGE */
			/* FALLTHROUGH */
		case 'r':
			rflag = 1;
			break;
		case 'V':
			printf("%s\n", DbXml::dbxml_version(NULL, NULL, NULL));
			printf("%s\n", db_version(NULL, NULL, NULL));
			return (EXIT_SUCCESS);
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		return (usage());

	/* Handle possible interruptions. */
	SigBlock sb;

	/*
	 * Create an environment object and initialize it for error
	 * reporting.
	 */
	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		fprintf(stderr,
			"%s: db_env_create: %s\n", progname, db_strerror(ret));
		goto err;
	}

	dbenv->set_errfile(dbenv, stderr);
	dbenv->set_errpfx(dbenv, progname);
	if (nflag) {
		if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
			dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
			goto err;
		}
		if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
			dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
			goto err;
		}
	}
	if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
				     passwd, DB_ENCRYPT_AES)) != 0) {
		dbenv->err(dbenv, ret, "set_passwd");
		goto err;
	}

	/* Initialize the environment. */
	if ((ret = db_init(dbenv, home, rflag, cache, &is_private)) != 0) {
		dbenv->err(dbenv, ret, "db_init");
		goto err;
	}

	xmlDb = new XmlManager(dbenv);
	
	if (rflag) {
		try {
			xmlDb->verifyContainer(argv[0], &cout, DB_SALVAGE | (Rflag ? DB_AGGRESSIVE : 0));
		} catch (XmlException &e) {
			dbenv->errx(dbenv, "verify %s: %s", argv[0], e.what());
			goto err;
		}
		goto done;
	}
	
	try {
		xmlDb->dumpContainer(argv[0], &cout);
	} catch (XmlException &e) {
		dbenv->errx(dbenv, "dump %s: %s", argv[0], e.what());
		goto err;
	}
	
	if (0) {
	err:
		exitval = 1;
	}
done:
	if (xmlDb)
		delete xmlDb;
		
	if ((ret = dbenv->close(dbenv, 0)) != 0) {
		exitval = 1;
		fprintf(stderr,
			"%s: dbenv->close: %s\n", progname, db_strerror(ret));
	}
	
	return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Example #3
0
int main(int argc, char **argv)
{
    // Deal with command line arguments
    const char *path2DbEnv = 0;
    u_int32_t envFlags = (DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL);
    u_int32_t txnEnvFlags =	(DB_INIT_TXN|DB_INIT_LOCK|DB_INIT_LOG);
    u_int32_t dbxmlFlags = DBXML_ALLOW_EXTERNAL_ACCESS;
    vector<string> scripts;
    int verbose = 0;
    bool transactionMode = false;
    bool dbPrivate = false;
    bool envCreate = false;
    const char *progName = argv[0];
    const char *password = 0;
    int cacheSize = 64;
    int ch;
    int ret = 0;

    while ((ch = getopt(argc, argv, "?h:hs:tvxVP:cz:")) != -1) {
        switch (ch) {
        case 'h': {
            path2DbEnv = optarg;
            break;
        }
        case 'z': {
            cacheSize = atoi(optarg);
            break;
        }
        case 'c': {
            envFlags &= ~DB_PRIVATE;
            envCreate = true;
            break;
        }
        case 'x': {
            dbxmlFlags &= ~DBXML_ALLOW_EXTERNAL_ACCESS;
            break;
        }
        case 't': {
            transactionMode = true;
            envFlags |= txnEnvFlags;
            break;
        }
        case 's': {
            scripts.push_back(optarg);
            break;
        }
        case 'v': {
            ++verbose;
            break;
        }
        case 'V': {
            printf("%s\n", DbXml::dbxml_version(NULL, NULL, NULL));
            printf("%s\n", db_version(NULL, NULL, NULL));
            exit(0);
        }
        case 'P': {
            password = optarg;
            break;
        }
        case '?':
        default: {
            usage(progName, 0);
            break;
        }
        }
    }

    // Turn on logging if extra verbose is specified
    if(verbose > 1) {
        setLogLevel(LEVEL_ALL, true);
        setLogCategory(CATEGORY_ALL, true);
        setLogCategory(CATEGORY_NODESTORE, verbose > 2);
        verboseErrors = true;
    }

    SigBlock sb; // block signals, resend at end of scope
    try {
        // Create a DB environment, and XmlManager
        DB_ENV *dbenv;
        int dberr = 0;
        dberr = db_env_create(&dbenv, 0);
        if (dberr) {
            cout << "Error creating environment: " << dberr << endl;
            exit(-1);
        }
        if (password)
            dbenv->set_encrypt(dbenv, password, DB_ENCRYPT_AES);
        dbenv->set_errcall(dbenv, errcall);
        dbenv->set_cachesize(dbenv, 0, cacheSize * 1024 * 1024, 1);
        dbenv->set_lk_max_lockers(dbenv, 10000);
        dbenv->set_lk_max_locks(dbenv, 10000);
        dbenv->set_lk_max_objects(dbenv, 10000);
        if (!dbPrivate) {
            dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
            if (verbose && !envCreate) {
                cout <<
                     "Attempting to join environment: "
                     << (path2DbEnv ? path2DbEnv : ".")
                     << endl;
            }
            dberr = dbenv->open(dbenv, path2DbEnv, DB_USE_ENVIRON, 0);
            if (dberr != 0) {
                if (dberr == DB_VERSION_MISMATCH) {
                    cerr << "Error opening environment "
                         << (path2DbEnv ? path2DbEnv : ".")
                         << ": " << "environment version mismatch" << endl;
                    exit(-1);
                }
                if (verbose) {
                    if(envCreate) {
                        cerr << "Creating environment: "
                             << (path2DbEnv ? path2DbEnv : ".")
                             << endl;
                    } else {
                        cerr << "Unable to join environment "
                             << (path2DbEnv ? path2DbEnv : ".")
                             << ", creating a DB_PRIVATE environment" << endl;
                    }
                }
                dberr = dbenv->open(dbenv, path2DbEnv,
                                    envFlags, 0);
            } else {
                cout <<	"Joined existing environment"
                     << endl;
                u_int32_t eflags = 0;
                dbenv->get_open_flags(dbenv, &eflags);
                if (eflags & DB_INIT_TXN)
                    transactionMode = true;
                else {
                    if (verbose && (transactionMode == true))
                        cout << "Joined a non-transactional environment, turning off transaction mode" << endl;
                    transactionMode = false;
                }
            }
        } else {
            dberr = dbenv->open(dbenv, path2DbEnv,
                                envFlags, 0);
        }
        if (dberr != 0) {
            cerr << "Error opening environment "
                 << (path2DbEnv ? path2DbEnv : ".")
                 << ", error is " << dberr << endl;
            exit(-1);
        }
        XmlManager db(dbenv, dbxmlFlags|DBXML_ADOPT_DBENV);

        // Create the environment
        Environment env(db, sb);
        env.transactions() = transactionMode;

        // Create the Shell object
        DefaultShell shell;

        // Run scripts, if specified
        if(!scripts.empty()) {
            env.interactive() = false;
            env.verbose() = (verbose != 0);

            for(vector<string>::iterator i = scripts.begin();
                    i != scripts.end() && !env.quit(); ++i) {
                ifstream scriptFile(i->c_str(), ios::in);
                if(!scriptFile) {
                    cerr << progName << ": cannot open script file: " << *i << endl;
                } else {
                    env.streamName() = *i;
                    env.lineNo() = 0;
                    shell.mainLoop(scriptFile, env);
                    scriptFile.close();
                }
            }
        }

        // Perform the queries
        if(!env.quit()) {
            env.interactive() = true;
            env.verbose() = true;
            env.streamName() = "stdin";
            env.lineNo() = 0;

            do {
                shell.mainLoop(cin, env);
                if(env.sigBlock().isInterrupted())
                    env.sigBlock().reset();
            } while(!env.quit() && !cin.eof());
        }
    }
    catch(exception &e) {
        cerr << progName << ": error at lowest level: " << e.what() << endl;
        ret = 1;
    }
    catch(...) {
        cerr << progName << ": error at lowest level: " << endl;
        ret = 1;
    }
    return ret;
}