-
Notifications
You must be signed in to change notification settings - Fork 12
/
BerKeley.cpp
184 lines (124 loc) · 2.9 KB
/
BerKeley.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "stdafx.h"
#include "data.h"
#include "chess.h"
#include "Resource.h"
#include "pregen.h"
// db_err_handle
void BD_my_error_handler(const DB_ENV* env, const char *error_prefix, const char *msg){
if(msg != NULL){
//printf("info DB_ERR: %s %s\n",error_prefix,msg);
LogOut(_T("DB_ERR: "));
LogOuts((char*)error_prefix);
LogOuts((char*)msg);
}
}
int BD_initEnv2(){ //初始化环境
#define DATABASE "access4.db"
DB *dbp;
int rret;
rret = db_create(&dbp, NULL, 0);
rret = dbp->open(dbp,
NULL,
"afaefa",
NULL,
DB_HASH,
DB_CREATE,
0);
if (rret) {
dbp->err(dbp, rret, "%s", DATABASE);
}
return 0;
}
// db_init
int BD_initEnv(STOCK_DBS* mst){ //初始化环境
mst->Book_Init_Ok = FALSE;
LogOut(_T("正在初始化棋库数据库。。。!\n"));
memset(mst,0,sizeof(STOCK_DBS));
uint32 env_flag = DB_CREATE
|DB_INIT_MPOOL; //初始化in-memory cache
//|DB_THREAD ; //数据库句柄可以给多个线程使用
mst->book_db_name = "book.db";
mst->good_db_name = "good.db";
int ret;
ret = db_env_create(&mst->Env,0);
if(ret != 0){
LogOut(_T("DB_ERR: Create env: "));
LogOuts(db_strerror(ret));
return ret;
}
ret = mst->Env->open(mst->Env,
CW2A(installDir),
env_flag,
0);
if(ret != 0){
LogOut(_T("DB_ERR: Environment open: "));
LogOuts(db_strerror(ret));
return ret;
}
//建立book数据库连接
//ret = db_create(&mst->book,NULL,0);
ret = db_create(&mst->book,mst->Env,0);
if(ret != 0){
LogOut(_T("DB_ERR: Create Book: "));
LogOuts(db_strerror(ret));
return ret;
}
mst->book->set_errcall(mst->book,BD_my_error_handler);
mst->book->set_errpfx(mst->book,"book->");
//打开book 数据库
ret = mst->book->open(mst->book,
NULL,
mst->book_db_name,
NULL,
DB_HASH,
DB_CREATE,
0);
if(ret != 0){
mst->book->err(mst->book,ret,"book file open failed: %s",mst->book_db_name);
return ret;
}
LogOut(_T("初始化棋库数据库成功!\n"));
mst->Book_Init_Ok = TRUE;
mst->back_db = NULL;
return ret;
}
int BD_openGoodDb(STOCK_DBS* mst){ //这是当前使用的的book;){
int ret;
ret = db_create(&mst->back_db,mst->Env, 0);
if(ret != 0){
LogOut(_T("DB_ERR: Create back db: "));
LogOuts(db_strerror(ret));
return ret;
}
mst->back_db->set_errcall(mst->back_db,BD_my_error_handler);
mst->back_db->set_errpfx(mst->back_db,"back->");
//打开备份数据库
ret = mst->back_db->open(mst->back_db,
NULL,
mst->good_db_name,
NULL,
DB_HASH,
DB_CREATE,
0);
if(ret != 0){
mst->back_db->err(mst->back_db,ret,"book file open failed: %s",mst->good_db_name);
return ret;
}
return ret;
}
void BerKeley_DB_EXIT(STOCK_DBS *mst){ //
if(mst->book != NULL){
mst->book->close(mst->book,0);
}
if(mst->back_db != NULL){
mst->back_db->close(mst->back_db,0);
}
}
void Back_Up_Book(void){
if (BD_openGoodDb(MST) == 0){
Book_Back_Up(MST->back_db, MST->book);
if (MST->back_db != NULL){
MST->back_db->close(MST->back_db, 0);
}
}
}