summaryrefslogtreecommitdiff
path: root/db/examples_c/ex_env.c
blob: 5490723a31c3af99d812e0553e52131b10d25754 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998, 1999, 2000
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: ex_env.c,v 11.18 2000/10/27 20:32:00 dda Exp $
 */

#include "db_config.h"

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#endif

#include <db.h>

#ifdef macintosh
#define	DATABASE_HOME	":database"
#define	CONFIG_DATA_DIR	":database"
#else
#ifdef DB_WIN32
#define	DATABASE_HOME	"\\tmp\\database"
#define	CONFIG_DATA_DIR	"\\database\\files"
#else
#ifdef HAVE_VXWORKS
#define	DATABASE_HOME	"/ata0/vxtmp/database"
#define	CONFIG_DATA_DIR	"/vxtmp/vxtmp/database/files"
#else
#define	DATABASE_HOME	"/tmp/database"
#define	CONFIG_DATA_DIR	"/database/files"
#endif
#endif
#endif

int	db_setup __P((char *, char *, FILE *, char *));
int	db_teardown __P((char *, char *, FILE *, char *));
#ifdef HAVE_VXWORKS
int	ex_env __P((void));
#define	ERROR_RETURN	ERROR
#define	VXSHM_KEY	11
#else
int	main __P((void));
#define	ERROR_RETURN	1
#endif

/*
 * An example of a program creating/configuring a Berkeley DB environment.
 */
int
#ifdef HAVE_VXWORKS
ex_env()
#else
main()
#endif
{
	int ret;
	char *data_dir, *home;
	char *progname = "ex_env";		/* Program name. */

	/*
	 * All of the shared database files live in DATABASE_HOME, but
	 * data files will live in CONFIG_DATA_DIR.
	 */
	home = DATABASE_HOME;
	data_dir = CONFIG_DATA_DIR;

	printf("Setup env\n");
	if ((ret = db_setup(home, data_dir, stderr, progname)) != 0)
		return (ret);

	printf("Teardown env\n");
	if ((ret = db_teardown(home, data_dir, stderr, progname)) != 0)
		return (ret);

	return (0);
}

int
db_setup(home, data_dir, errfp, progname)
	char *home, *data_dir, *progname;
	FILE *errfp;
{
	DB_ENV *dbenv;
	int ret;

	/*
	 * Create an environment object and initialize it for error
	 * reporting.
	 */
	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
		return (ERROR_RETURN);
	}
	dbenv->set_errfile(dbenv, errfp);
	dbenv->set_errpfx(dbenv, progname);

#ifdef HAVE_VXWORKS
	/* VxWorks needs to specify a base segment ID. */
	if ((ret = dbenv->set_shm_key(dbenv, VXSHM_KEY)) != 0) {
		fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
		return (ERROR_RETURN);
	}
#endif

	/*
	 * We want to specify the shared memory buffer pool cachesize,
	 * but everything else is the default.
	 */
	if ((ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0)) != 0) {
		dbenv->err(dbenv, ret, "set_cachesize");
		dbenv->close(dbenv, 0);
		return (ERROR_RETURN);
	}

	/* Databases are in a subdirectory. */
	(void)dbenv->set_data_dir(dbenv, data_dir);

	/* Open the environment with full transactional support. */
	if ((ret = dbenv->open(dbenv, home,
    DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN,
	    0)) != 0) {
		dbenv->err(dbenv, ret, "environment open: %s", home);
		dbenv->close(dbenv, 0);
		return (ERROR_RETURN);
	}

	/* Do something interesting... */

	/* Close the handle. */
	if ((ret = dbenv->close(dbenv, 0)) != 0) {
		fprintf(stderr, "DBENV->close: %s\n", db_strerror(ret));
		return (ERROR_RETURN);
	}
	return (0);
}

int
db_teardown(home, data_dir, errfp, progname)
	char *home, *data_dir, *progname;
	FILE *errfp;
{
	DB_ENV *dbenv;
	int ret;

	/* Remove the shared database regions. */
	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
		return (ERROR_RETURN);
	}
	dbenv->set_errfile(dbenv, errfp);
	dbenv->set_errpfx(dbenv, progname);
#ifdef HAVE_VXWORKS
	if ((ret = dbenv->set_shm_key(dbenv, VXSHM_KEY)) != 0) {
		fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
		return (ERROR_RETURN);
	}
#endif

	(void)dbenv->set_data_dir(dbenv, data_dir);
	if ((ret = dbenv->remove(dbenv, home, 0)) != 0) {
		fprintf(stderr, "DBENV->remove: %s\n", db_strerror(ret));
		return (ERROR_RETURN);
	}
	return (0);
}