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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009, 2015 Oracle and/or its affiliates. All rights reserved.
*
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using BerkeleyDB;
namespace excs_env {
class Program {
const int EXIT_FAILURE = 1;
const int EXIT_SUCCESS = 0;
const string progName = "excs_env";
static void Main(string[] args) {
string data_dir, home;
/*
* excs_env is meant to be run from build_windows\AnyCPU, in either
* the Debug or Release directory. The required core libraries,
* however, are in either build_windows\Win32 or build_windows\x64,
* depending upon the platform. That location needs to be added to
* the PATH environment variable for the P/Invoke calls to work.
*/
try {
String pwd = Environment.CurrentDirectory;
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "..");
if (IntPtr.Size == 4)
pwd = Path.Combine(pwd, "Win32");
else
pwd = Path.Combine(pwd, "x64");
#if DEBUG
pwd = Path.Combine(pwd, "Debug");
#else
pwd = Path.Combine(pwd, "Release");
#endif
pwd += ";" + Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", pwd);
} catch (Exception e) {
Console.WriteLine(
"Unable to set the PATH environment variable.");
Console.WriteLine(e.Message);
return;
}
data_dir = home = null;
switch (args.Length) {
case 0:
data_dir = "envData";
home = "envHome";
break;
case 2:
home = args[0];
data_dir = args[1];
break;
case 1:
home = args[0];
break;
default:
usage();
return;
}
if (!Directory.Exists(home)) {
Console.WriteLine("Creating home directory: {0}", home);
try {
Directory.CreateDirectory(home);
} catch {
Console.WriteLine("Unable to create {0}", home);
return;
}
}
if (data_dir != null && !Directory.Exists(home + "/" + data_dir)) {
Console.WriteLine(
"Creating home directory: {0}/{1}", home, data_dir);
try {
Directory.CreateDirectory(home + "/" + data_dir);
} catch {
Console.WriteLine(
"Unable to create {0}/{1}", home, data_dir);
return;
}
}
/* Set up environment. */
if (SetUpEnv(home, data_dir) == EXIT_FAILURE) {
Console.WriteLine("Fail to set up the environment.");
return;
}
Console.WriteLine("Set up the environment.");
/* Tear down the environment and remove its files. */
if (TearDownEnv(home) == EXIT_FAILURE) {
Console.WriteLine(
"Fail to tear down the environment.");
return;
}
Console.WriteLine("Tear down the environment.");
}
/*
* Set up environment.
*/
public static int SetUpEnv(string home, string data_dir) {
DatabaseEnvironment env;
DatabaseEnvironmentConfig envConfig;
/* Configure an environment. */
envConfig = new DatabaseEnvironmentConfig();
envConfig.MPoolSystemCfg = new MPoolConfig();
envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(
0, 64 * 1024, 1);
envConfig.Create = true;
envConfig.DataDirs.Add(data_dir);
envConfig.CreationDir = data_dir;
envConfig.ErrorPrefix = progName;
envConfig.UseLogging = true;
envConfig.UseLocking = true;
envConfig.UseMPool = true;
envConfig.UseTxns = true;
/* Create and open the environment. */
try {
env = DatabaseEnvironment.Open(home, envConfig);
} catch (Exception e) {
Console.WriteLine("{0}", e.Message);
return EXIT_FAILURE;
}
Console.ReadLine();
env.Close();
return EXIT_SUCCESS;
}
/*
* Tear down environment and remove its files.
* Any log or database files and the environment
* directory are not removed.
*/
public static int TearDownEnv(string home) {
/* Remove environment regions. */
try {
DatabaseEnvironment.Remove(home);
} catch (Exception e) {
Console.WriteLine("{0}: {1}\n{2}",
e.Source, e.Message, e.StackTrace);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
public static void usage() {
Console.WriteLine("Usage: excs_env [home] [data dir]");
}
}
}
|