summaryrefslogtreecommitdiff
path: root/src/support/simple_setup.c
blob: a4464fead69a1f00682204d63893380f2eed2c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008-2011 WiredTiger, Inc.
 *	All rights reserved.
 *
 * $Id$
 */

#include <stdlib.h>

#include "wiredtiger.h"

extern const char *progname;

static ENV *__env;

/*
 * wiredtiger_simple_setup --
 *	Standard setup for simple applications.
 */
int
wiredtiger_simple_setup(
    const char *progname, DB **dbp, u_int32_t cache_size, u_int32_t flags)
{
	DB *db;
	ENV *env;
	int ret;

	db = *dbp = NULL;

	if ((ret = wiredtiger_env_init(&env, flags)) != 0) {
		fprintf(stderr,
		    "%s: wiredtiger_env_init: %s\n",
		    progname, wiredtiger_strerror(ret));
		return (ret);
	}
	__env = env;

	if (cache_size != 0 &&
	    (ret = env->cache_size_set(env, cache_size)) != 0) {
		env->err(env, ret, "Env.cache_size_set");
		goto err;
	}

	if ((ret = env->open(env, NULL, 0, 0)) != 0) {
		env->err(env, ret, "%s: Env.open", progname);
		goto err;
	}
	if ((ret = env->db(env, 0, &db)) != 0) {
		env->err(env, ret, "%s: Env.db", progname);
		goto err;
	}
	if ((ret = db->errpfx_set(db, progname)) != 0) {
		db->err(db, ret, "%s: Db.errpfx_set", progname);
		goto err;
	}

	*dbp = db;
	return (EXIT_SUCCESS);

err:	wiredtiger_simple_teardown(progname, db);
	return (ret);
}

/*
 * wiredtiger_simple_teardown --
 *	Standard teardown for simple applications.
 */
int
wiredtiger_simple_teardown(const char *progname, DB *db)
{
	int ret, tret;

	ret = 0;
	if (db != NULL && (tret = db->close(db, 0)) != 0) {
		fprintf(stderr,
		    "%s: Db.close: %s\n", progname, wiredtiger_strerror(ret));
		if (ret == 0)
			ret = tret;
	}

	if (__env != NULL) {
		if ((tret = __env->close(__env, 0)) != 0) {
			fprintf(stderr, "%s: Env.close: %s\n",
			    progname, wiredtiger_strerror(ret));
			if (ret == 0)
				ret = tret;
		}
		__env = NULL;
	}

	return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}