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
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2009 - 2010 Toru Maesaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "ha_blitz.h"
using namespace std;
using namespace drizzled;
int BlitzData::startup(const char *path) {
int rv = 0;
if ((rv = open_data_table(path, HDBOWRITER)) != 0)
return rv;
current_hidden_row_id = read_meta_row_id();
return rv;
}
int BlitzData::shutdown() {
int rv = 0;
/* Copy the latest autogenerated ID back to TC's metadata buffer.
This data will be sync'd by TC. */
write_meta_row_id(current_hidden_row_id);
if ((rv = close_data_table()) != 0)
return rv;
return rv;
}
/* Similar to UNIX touch(1) but generates a tuned TCHDB file. */
int BlitzData::create_data_table(drizzled::message::Table &proto,
drizzled::Table &table_info,
const drizzled::TableIdentifier &identifier) {
std::string path = identifier.getPath() + BLITZ_DATA_EXT;
uint64_t autoinc = (proto.options().has_auto_increment_value())
? proto.options().auto_increment_value() - 1 : 0;
uint64_t hash_buckets = (blitz_estimated_rows == 0) ? BLITZ_TC_BUCKETS
: blitz_estimated_rows;
int n_options = proto.engine().options_size();
for (int i = 0; i < n_options; i++) {
if (proto.engine().options(i).name() == "estimated_rows" ||
proto.engine().options(i).name() == "ESTIMATED_ROWS") {
std::istringstream stream(proto.engine().options(i).state());
stream >> hash_buckets;
if (hash_buckets <= 0)
hash_buckets = BLITZ_TC_BUCKETS;
}
}
if (data_table != NULL)
return HA_ERR_GENERIC;
if ((data_table = tchdbnew()) == NULL)
return HA_ERR_OUT_OF_MEM;
if (!tchdbtune(data_table, hash_buckets, -1, -1, 0)) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
if (!tchdbopen(data_table, path.c_str(), (HDBOWRITER | HDBOCREAT))) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
/* Write the Meta Data for this Table. */
tc_meta_buffer = tchdbopaque(data_table);
write_meta_autoinc(autoinc);
write_meta_keycount(table_info.getShare()->keys);
/* We're Done. */
if (close_data_table() != 0)
return HA_ERR_CRASHED_ON_USAGE;
return 0;
}
int BlitzData::open_data_table(const char *path, const int mode) {
char buf[FN_REFLEN];
if ((data_table = tchdbnew()) == NULL)
return HA_ERR_OUT_OF_MEM;
if (!tchdbsetmutex(data_table)) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
if (!tchdbsetxmsiz(data_table, BLITZ_TC_EXTRA_MMAP_SIZE)) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
snprintf(buf, FN_REFLEN, "%s%s", path, BLITZ_DATA_EXT);
if (!tchdbopen(data_table, buf, mode)) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
tc_meta_buffer = tchdbopaque(data_table);
return 0;
}
bool BlitzData::rename_table(const char *from, const char *to) {
char from_buf[FN_REFLEN];
char to_buf[FN_REFLEN];
snprintf(from_buf, FN_REFLEN, "%s%s", from, BLITZ_DATA_EXT);
snprintf(to_buf, FN_REFLEN, "%s%s", to, BLITZ_DATA_EXT);
if (rename(from_buf, to_buf) != 0)
return false;
snprintf(from_buf, FN_REFLEN, "%s%s", from, BLITZ_SYSTEM_EXT);
snprintf(to_buf, FN_REFLEN, "%s%s", to, BLITZ_SYSTEM_EXT);
if (rename(from_buf, to_buf) != 0)
return false;
return true;
}
int BlitzData::close_data_table(void) {
assert(data_table);
if (!tchdbclose(data_table)) {
tchdbdel(data_table);
return HA_ERR_CRASHED_ON_USAGE;
}
tchdbdel(data_table);
data_table = NULL;
tc_meta_buffer = NULL;
return 0;
}
uint64_t BlitzData::nrecords(void) {
return tchdbrnum(data_table);
}
uint64_t BlitzData::table_size(void) {
return tchdbfsiz(data_table);
}
uint64_t BlitzData::read_meta_row_id(void) {
assert(tc_meta_buffer);
return (uint64_t)uint8korr(tc_meta_buffer);
}
uint64_t BlitzData::read_meta_autoinc(void) {
assert(tc_meta_buffer);
char *pos = tc_meta_buffer + sizeof (current_hidden_row_id);
return (uint64_t)uint8korr(pos);
}
uint32_t BlitzData::read_meta_keycount(void) {
assert(tc_meta_buffer);
char *pos = tc_meta_buffer;
pos += sizeof(current_hidden_row_id) + sizeof(uint64_t);
return (uint32_t)uint4korr(pos);
}
void BlitzData::write_meta_row_id(uint64_t row_id) {
assert(tc_meta_buffer);
int8store(tc_meta_buffer, row_id);
}
void BlitzData::write_meta_autoinc(uint64_t num) {
assert(tc_meta_buffer);
char *pos = tc_meta_buffer + sizeof(current_hidden_row_id);
int8store(pos, num);
}
void BlitzData::write_meta_keycount(uint32_t nkeys) {
assert(tc_meta_buffer);
char *pos = tc_meta_buffer;
pos += sizeof(current_hidden_row_id) + sizeof(uint64_t);
int4store(pos, nkeys);
}
char *BlitzData::get_row(const char *key, const size_t klen, int *vlen) {
return (char *)tchdbget(data_table, key, klen, vlen);
}
/* Fastest way to fetch both key and value from TCHDB since it only
involves one allocation. That is, both key and value are living
on the same block of memory. The return value is a pointer to the
next key. Technically it is a pointer to the region of memory that
holds both key and value. */
char *BlitzData::next_key_and_row(const char *key, const size_t klen,
int *next_key_len, const char **value,
int *value_len) {
return tchdbgetnext3(data_table, key, klen, next_key_len, value, value_len);
}
char *BlitzData::first_row(int *row_len) {
return (char *)tchdbgetnext(data_table, NULL, 0, row_len);
}
uint64_t BlitzData::next_hidden_row_id(void) {
/* current_hidden_row_id is an atomic type */
uint64_t rv = current_hidden_row_id.increment();
return rv;
}
int BlitzData::write_row(const char *key, const size_t klen,
const unsigned char *row, const size_t rlen) {
return (tchdbput(data_table, key, klen, row, rlen)) ? 0 : 1;
}
int BlitzData::write_unique_row(const char *key, const size_t klen,
const unsigned char *row, const size_t rlen) {
int rv = 0;
if (!tchdbputkeep(data_table, key, klen, row, rlen)) {
if (tchdbecode(data_table) == TCEKEEP) {
errno = HA_ERR_FOUND_DUPP_KEY;
rv = HA_ERR_FOUND_DUPP_KEY;
}
}
return rv;
}
int BlitzData::delete_row(const char *key, const size_t klen) {
return (tchdbout(data_table, key, klen)) ? 0 : -1;
}
bool BlitzData::delete_all_rows() {
char buf[BLITZ_MAX_META_LEN];
/* Evacuate the meta data buffer since this will be wiped out. */
memcpy(buf, tc_meta_buffer, BLITZ_MAX_META_LEN);
/* Now it's safe to wipe everything. */
if (!tchdbvanish(data_table))
return false;
/* Copy the evacuated meta buffer back to the fresh TCHDB file. */
tc_meta_buffer = tchdbopaque(data_table);
memcpy(tc_meta_buffer, buf, BLITZ_MAX_META_LEN);
return true;
}
/* Code from here on is for BlitzDB's internal system information management.
It is deliberately separated from the data dictionary code because we
might move to a simple flat file structure in the future. For now we
use a micro Tokyo Cabinet database for this. */
int BlitzData::create_system_table(const std::string &path) {
int rv = 0;
int mode = (HDBOWRITER | HDBOCREAT);
if ((rv = open_system_table(path.c_str(), mode)) != 0)
return rv;
return close_system_table();
}
int BlitzData::open_system_table(const std::string &path, const int mode) {
char buf[FN_REFLEN];
const int BUCKETS = 7;
if ((system_table = tchdbnew()) == NULL)
return HA_ERR_OUT_OF_MEM;
if (!tchdbsetmutex(system_table)) {
tchdbdel(system_table);
return HA_ERR_CRASHED_ON_USAGE;
}
if (!tchdbtune(system_table, BUCKETS, -1, -1, 0)) {
tchdbdel(system_table);
return HA_ERR_CRASHED_ON_USAGE;
}
snprintf(buf, FN_REFLEN, "%s%s", path.c_str(), BLITZ_SYSTEM_EXT);
if (!tchdbopen(system_table, buf, mode)) {
tchdbdel(system_table);
return HA_ERR_CRASHED_ON_USAGE;
}
return 0;
}
int BlitzData::close_system_table(void) {
assert(system_table);
if (!tchdbclose(system_table)) {
tchdbdel(system_table);
return HA_ERR_CRASHED_ON_USAGE;
}
tchdbdel(system_table);
return 0;
}
bool BlitzData::write_table_definition(drizzled::message::Table &proto) {
assert(system_table);
std::string serialized_proto;
proto.SerializeToString(&serialized_proto);
if (!tchdbput(system_table, BLITZ_TABLE_PROTO_KEY.c_str(),
BLITZ_TABLE_PROTO_KEY.length(), serialized_proto.c_str(),
serialized_proto.length())) {
return false;
}
if (proto.options().has_comment()) {
if (!tchdbput(system_table, BLITZ_TABLE_PROTO_COMMENT_KEY.c_str(),
BLITZ_TABLE_PROTO_COMMENT_KEY.length(),
proto.options().comment().c_str(),
proto.options().comment().length())) {
return false;
}
}
return true;
}
char *BlitzData::get_system_entry(const char *key, const size_t klen,
int *vlen) {
assert(system_table);
return (char *)tchdbget(system_table, key, klen, vlen);
}
|