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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2010-2013 Kentoku SHIBA
Copyright(C) 2011-2015 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*/
#include <mrn_mysql.h>
#include "mrn_database_manager.hpp"
#include "mrn_encoding.hpp"
#include "mrn_lock.hpp"
#include "mrn_path_mapper.hpp"
#include <groonga/plugin.h>
// for debug
#define MRN_CLASS_NAME "mrn::DatabaseManager"
#ifdef WIN32
# include <direct.h>
# define MRN_MKDIR(pathname, mode) _mkdir((pathname))
#else
# include <dirent.h>
# include <unistd.h>
# define MRN_MKDIR(pathname, mode) mkdir((pathname), (mode))
#endif
extern "C" {
grn_rc GRN_PLUGIN_IMPL_NAME_TAGGED(init, normalizers_mysql)(grn_ctx *ctx);
grn_rc GRN_PLUGIN_IMPL_NAME_TAGGED(register, normalizers_mysql)(grn_ctx *ctx);
}
namespace mrn {
DatabaseManager::DatabaseManager(grn_ctx *ctx, mysql_mutex_t *mutex)
: ctx_(ctx),
cache_(NULL),
mutex_(mutex) {
}
DatabaseManager::~DatabaseManager(void) {
if (cache_) {
void *db_address;
GRN_HASH_EACH(ctx_, cache_, id, NULL, 0, &db_address, {
Database *db;
memcpy(&db, db_address, sizeof(grn_obj *));
delete db;
});
grn_hash_close(ctx_, cache_);
}
}
bool DatabaseManager::init(void) {
MRN_DBUG_ENTER_METHOD();
cache_ = grn_hash_create(ctx_,
NULL,
GRN_TABLE_MAX_KEY_SIZE,
sizeof(grn_obj *),
GRN_OBJ_KEY_VAR_SIZE);
if (!cache_) {
GRN_LOG(ctx_, GRN_LOG_ERROR,
"failed to initialize hash table for caching opened databases");
DBUG_RETURN(false);
}
DBUG_RETURN(true);
}
int DatabaseManager::open(const char *path, Database **db) {
MRN_DBUG_ENTER_METHOD();
int error = 0;
*db = NULL;
mrn::PathMapper mapper(path);
mrn::Lock lock(mutex_);
error = mrn::encoding::set(ctx_, system_charset_info);
if (error) {
DBUG_RETURN(error);
}
grn_id id;
void *db_address;
id = grn_hash_get(ctx_, cache_,
mapper.db_name(), strlen(mapper.db_name()),
&db_address);
if (id == GRN_ID_NIL) {
grn_obj *grn_db;
struct stat db_stat;
if (stat(mapper.db_path(), &db_stat)) {
GRN_LOG(ctx_, GRN_LOG_INFO,
"database not found. creating...: <%s>", mapper.db_path());
if (path[0] == FN_CURLIB &&
mrn_is_directory_separator(path[1])) {
ensure_database_directory();
}
grn_db = grn_db_create(ctx_, mapper.db_path(), NULL);
if (ctx_->rc) {
error = ER_CANT_CREATE_TABLE;
my_message(error, ctx_->errbuf, MYF(0));
DBUG_RETURN(error);
}
} else {
grn_db = grn_db_open(ctx_, mapper.db_path());
if (ctx_->rc) {
error = ER_CANT_OPEN_FILE;
my_message(error, ctx_->errbuf, MYF(0));
DBUG_RETURN(error);
}
}
*db = new Database(ctx_, grn_db);
grn_hash_add(ctx_, cache_,
mapper.db_name(), strlen(mapper.db_name()),
&db_address, NULL);
memcpy(db_address, db, sizeof(Database *));
error = ensure_normalizers_registered((*db)->get());
if (!error) {
if ((*db)->is_broken()) {
error = ER_CANT_OPEN_FILE;
char error_message[MRN_MESSAGE_BUFFER_SIZE];
snprintf(error_message, MRN_MESSAGE_BUFFER_SIZE,
"mroonga: database: open: "
"The database maybe broken. "
"We recommend you to recreate the database. "
"If the database isn't broken, "
"you can remove this error by running "
"'groonga %s table_remove mroonga_operations' "
"on server. But the latter isn't recommended.",
mapper.db_path());
my_message(error, error_message, MYF(0));
}
}
} else {
memcpy(db, db_address, sizeof(Database *));
grn_ctx_use(ctx_, (*db)->get());
}
DBUG_RETURN(error);
}
void DatabaseManager::close(const char *path) {
MRN_DBUG_ENTER_METHOD();
mrn::PathMapper mapper(path);
mrn::Lock lock(mutex_);
grn_id id;
void *db_address;
id = grn_hash_get(ctx_, cache_,
mapper.db_name(), strlen(mapper.db_name()),
&db_address);
if (id == GRN_ID_NIL) {
DBUG_VOID_RETURN;
}
Database *db = NULL;
memcpy(&db, db_address, sizeof(Database *));
grn_ctx_use(ctx_, db->get());
if (db) {
delete db;
}
grn_hash_delete_by_id(ctx_, cache_, id, NULL);
DBUG_VOID_RETURN;
}
bool DatabaseManager::drop(const char *path) {
MRN_DBUG_ENTER_METHOD();
mrn::PathMapper mapper(path);
mrn::Lock lock(mutex_);
grn_id id;
void *db_address;
id = grn_hash_get(ctx_, cache_,
mapper.db_name(), strlen(mapper.db_name()),
&db_address);
Database *db = NULL;
if (id == GRN_ID_NIL) {
struct stat dummy;
if (stat(mapper.db_path(), &dummy) == 0) {
grn_obj *grn_db = grn_db_open(ctx_, mapper.db_path());
db = new Database(ctx_, grn_db);
}
} else {
memcpy(&db, db_address, sizeof(Database *));
grn_ctx_use(ctx_, db->get());
}
if (!db) {
DBUG_RETURN(false);
}
if (db->remove() == GRN_SUCCESS) {
if (id != GRN_ID_NIL) {
grn_hash_delete_by_id(ctx_, cache_, id, NULL);
}
delete db;
DBUG_RETURN(true);
} else {
GRN_LOG(ctx_, GRN_LOG_ERROR,
"failed to drop database: <%s>: <%s>",
mapper.db_path(), ctx_->errbuf);
if (id == GRN_ID_NIL) {
delete db;
}
DBUG_RETURN(false);
}
}
int DatabaseManager::clear(void) {
MRN_DBUG_ENTER_METHOD();
int error = 0;
mrn::Lock lock(mutex_);
grn_hash_cursor *cursor;
cursor = grn_hash_cursor_open(ctx_, cache_,
NULL, 0, NULL, 0,
0, -1, 0);
if (ctx_->rc) {
my_message(ER_ERROR_ON_READ, ctx_->errbuf, MYF(0));
DBUG_RETURN(ER_ERROR_ON_READ);
}
while (grn_hash_cursor_next(ctx_, cursor) != GRN_ID_NIL) {
if (ctx_->rc) {
error = ER_ERROR_ON_READ;
my_message(error, ctx_->errbuf, MYF(0));
break;
}
void *db_address;
Database *db;
grn_hash_cursor_get_value(ctx_, cursor, &db_address);
memcpy(&db, db_address, sizeof(Database *));
grn_ctx_use(ctx_, db->get());
grn_rc rc = grn_hash_cursor_delete(ctx_, cursor, NULL);
if (rc) {
error = ER_ERROR_ON_READ;
my_message(error, ctx_->errbuf, MYF(0));
break;
}
delete db;
}
grn_hash_cursor_close(ctx_, cursor);
DBUG_RETURN(error);
}
const char *DatabaseManager::error_message() {
MRN_DBUG_ENTER_METHOD();
DBUG_RETURN(ctx_->errbuf);
}
void DatabaseManager::mkdir_p(const char *directory) {
MRN_DBUG_ENTER_METHOD();
int i = 0;
char sub_directory[MRN_MAX_PATH_SIZE];
sub_directory[0] = '\0';
while (true) {
if (mrn_is_directory_separator(directory[i]) ||
directory[i] == '\0') {
sub_directory[i] = '\0';
struct stat directory_status;
if (stat(sub_directory, &directory_status) != 0) {
DBUG_PRINT("info", ("mroonga: creating directory: <%s>", sub_directory));
GRN_LOG(ctx_, GRN_LOG_INFO, "creating directory: <%s>", sub_directory);
if (MRN_MKDIR(sub_directory, S_IRWXU) == 0) {
DBUG_PRINT("info",
("mroonga: created directory: <%s>", sub_directory));
GRN_LOG(ctx_, GRN_LOG_INFO, "created directory: <%s>", sub_directory);
} else {
DBUG_PRINT("error",
("mroonga: failed to create directory: <%s>: <%s>",
sub_directory, strerror(errno)));
GRN_LOG(ctx_, GRN_LOG_ERROR,
"failed to create directory: <%s>: <%s>",
sub_directory, strerror(errno));
DBUG_VOID_RETURN;
}
}
}
if (directory[i] == '\0') {
break;
}
sub_directory[i] = directory[i];
++i;
}
DBUG_VOID_RETURN;
}
void DatabaseManager::ensure_database_directory(void) {
MRN_DBUG_ENTER_METHOD();
const char *path_prefix = mrn::PathMapper::default_path_prefix;
if (!path_prefix)
DBUG_VOID_RETURN;
const char *last_path_separator;
last_path_separator = strrchr(path_prefix, FN_LIBCHAR);
#ifdef FN_LIBCHAR2
if (!last_path_separator)
last_path_separator = strrchr(path_prefix, FN_LIBCHAR2);
#endif
if (!last_path_separator)
DBUG_VOID_RETURN;
if (path_prefix == last_path_separator)
DBUG_VOID_RETURN;
char database_directory[MRN_MAX_PATH_SIZE];
size_t database_directory_length = last_path_separator - path_prefix;
strncpy(database_directory, path_prefix, database_directory_length);
database_directory[database_directory_length] = '\0';
mkdir_p(database_directory);
DBUG_VOID_RETURN;
}
int DatabaseManager::ensure_normalizers_registered(grn_obj *db) {
MRN_DBUG_ENTER_METHOD();
int error = 0;
#ifdef WITH_GROONGA_NORMALIZER_MYSQL
{
# ifdef MRN_GROONGA_NORMALIZER_MYSQL_EMBEDDED
GRN_PLUGIN_IMPL_NAME_TAGGED(init, normalizers_mysql)(ctx_);
GRN_PLUGIN_IMPL_NAME_TAGGED(register, normalizers_mysql)(ctx_);
# else
grn_obj *mysql_normalizer;
mysql_normalizer = grn_ctx_get(ctx_, "NormalizerMySQLGeneralCI", -1);
if (mysql_normalizer) {
grn_obj_unlink(ctx_, mysql_normalizer);
} else {
grn_plugin_register(ctx_, GROONGA_NORMALIZER_MYSQL_PLUGIN_NAME);
}
# endif
}
#endif
DBUG_RETURN(error);
}
}
|