57
54
tcmapdel(blitz_table_cache);
60
virtual drizzled::Cursor *create(drizzled::Table &table) {
57
virtual drizzled::Cursor *create(drizzled::TableShare &table) {
61
58
return new ha_blitz(*this, table);
68
65
int doCreateTable(drizzled::Session &session,
69
66
drizzled::Table &table_arg,
70
const drizzled::identifier::Table &identifier,
67
const drizzled::TableIdentifier &identifier,
71
68
drizzled::message::Table &table_proto);
73
70
int doRenameTable(drizzled::Session &session,
74
const drizzled::identifier::Table &from_identifier,
75
const drizzled::identifier::Table &to_identifier);
71
const drizzled::TableIdentifier &from_identifier,
72
const drizzled::TableIdentifier &to_identifier);
77
74
int doDropTable(drizzled::Session &session,
78
const drizzled::identifier::Table &identifier);
75
const drizzled::TableIdentifier &identifier);
80
77
int doGetTableDefinition(drizzled::Session &session,
81
const drizzled::identifier::Table &identifier,
78
const drizzled::TableIdentifier &identifier,
82
79
drizzled::message::Table &table_proto);
84
81
void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
85
const drizzled::identifier::Schema &schema_identifier,
86
drizzled::identifier::Table::vector &set_of_identifiers);
82
const drizzled::SchemaIdentifier &schema_identifier,
83
drizzled::TableIdentifiers &set_of_identifiers);
88
85
bool doDoesTableExist(drizzled::Session &session,
89
const drizzled::identifier::Table &identifier);
86
const drizzled::TableIdentifier &identifier);
91
88
bool validateCreateTableOption(const std::string &key,
92
89
const std::string &state);
124
121
int BlitzEngine::doCreateTable(drizzled::Session &,
125
122
drizzled::Table &table,
126
const drizzled::identifier::Table &identifier,
123
const drizzled::TableIdentifier &identifier,
127
124
drizzled::message::Table &proto) {
132
129
/* Temporary fix for blocking composite keys. We need to add this
133
130
check because version 1 doesn't handle composite indexes. */
134
for (uint32_t i = 0; i < table.getShare()->keys; i++) {
131
for (uint32_t i = 0; i < table.s->keys; i++) {
135
132
if (table.key_info[i].key_parts > 1)
136
133
return HA_ERR_UNSUPPORTED;
147
144
/* Create b+tree index(es) for this table. */
148
for (uint32_t i = 0; i < table.getShare()->keys; i++) {
145
for (uint32_t i = 0; i < table.s->keys; i++) {
149
146
if ((ecode = btree.create(identifier.getPath().c_str(), i)) != 0)
166
163
int BlitzEngine::doRenameTable(drizzled::Session &,
167
const drizzled::identifier::Table &from,
168
const drizzled::identifier::Table &to) {
164
const drizzled::TableIdentifier &from,
165
const drizzled::TableIdentifier &to) {
171
168
BlitzData blitz_table;
176
/* Write the table definition to system table. */
177
if ((ecode = dict.open_system_table(from.getPath(), HDBOWRITER)) != 0)
180
drizzled::message::Table proto;
182
int proto_string_len;
184
proto_string = dict.get_system_entry(BLITZ_TABLE_PROTO_KEY.c_str(),
185
BLITZ_TABLE_PROTO_KEY.length(),
188
if (proto_string == NULL) {
192
if (!proto.ParseFromArray(proto_string, proto_string_len)) {
194
return HA_ERR_CRASHED_ON_USAGE;
199
proto.set_name(to.getTableName());
200
proto.set_schema(to.getSchemaName());
201
proto.set_catalog(to.getCatalogName());
203
if (!dict.write_table_definition(proto)) {
204
dict.close_system_table();
205
return HA_ERR_CRASHED_ON_USAGE;
208
dict.close_system_table();
210
171
/* Find out the number of indexes in this table. This information
211
172
is required because BlitzDB creates a file for each indexes.*/
212
173
if (blitz_table.open_data_table(from.getPath().c_str(), HDBOREADER) != 0)
286
247
int BlitzEngine::doGetTableDefinition(drizzled::Session &,
287
const drizzled::identifier::Table &identifier,
248
const drizzled::TableIdentifier &identifier,
288
249
drizzled::message::Table &proto) {
289
250
struct stat stat_info;
290
251
std::string path(identifier.getPath());
328
289
void BlitzEngine::doGetTableIdentifiers(drizzled::CachedDirectory &directory,
329
const drizzled::identifier::Schema &schema_id,
330
drizzled::identifier::Table::vector &ids) {
290
const drizzled::SchemaIdentifier &schema_id,
291
drizzled::TableIdentifiers &ids) {
331
292
drizzled::CachedDirectory::Entries entries = directory.getEntries();
333
294
for (drizzled::CachedDirectory::Entries::iterator entry_iter = entries.begin();
346
307
char uname[NAME_LEN + 1];
347
308
uint32_t file_name_len;
349
file_name_len = identifier::Table::filename_to_tablename(filename->c_str(),
310
file_name_len = TableIdentifier::filename_to_tablename(filename->c_str(),
353
314
uname[file_name_len - sizeof(BLITZ_DATA_EXT) + 1]= '\0';
354
ids.push_back(identifier::Table(schema_id, uname));
315
ids.push_back(TableIdentifier(schema_id, uname));
359
320
bool BlitzEngine::doDoesTableExist(drizzled::Session &,
360
const drizzled::identifier::Table &identifier) {
321
const drizzled::TableIdentifier &identifier) {
361
322
std::string proto_path(identifier.getPath());
362
323
proto_path.append(BLITZ_DATA_EXT);
406
367
ha_blitz::ha_blitz(drizzled::plugin::StorageEngine &engine_arg,
407
Table &table_arg) : Cursor(engine_arg, table_arg),
368
TableShare &table_arg) : Cursor(engine_arg, table_arg),
408
369
btree_cursor(NULL),
409
370
table_scan(false),
410
371
table_based(false),
460
421
will use to uniquely identify a row. The actual allocation is
461
422
done by the kernel so all we do here is specify the size of it.*/
462
423
if (share->primary_key_exists) {
463
ref_length = getTable()->key_info[getTable()->getShare()->getPrimaryKey()].key_length;
424
ref_length = table->key_info[table->s->getPrimaryKey()].key_length;
465
426
ref_length = sizeof(held_key_len) + sizeof(uint64_t);
500
461
int ha_blitz::doStartTableScan(bool scan) {
501
462
/* Obtain the query type for this scan */
502
sql_command_type = getTable()->getSession()->getSqlCommand();
463
sql_command_type = session_sql_command(table->getSession());
503
464
table_scan = scan;
504
465
table_based = true;
625
586
int ha_blitz::doStartIndexScan(uint32_t key_num, bool) {
626
587
active_index = key_num;
627
sql_command_type = getTable()->getSession()->getSqlCommand();
588
sql_command_type = session_sql_command(table->getSession());
629
590
/* This is unlikely to happen but just for assurance, re-obtain
630
591
the lock if this thread already has a certain lock. This makes
679
640
if ((row = share->dict.get_row(dict_key, dict_klen, &rlen)) == NULL) {
681
getTable()->status = STATUS_NOT_FOUND;
642
table->status = STATUS_NOT_FOUND;
682
643
return HA_ERR_KEY_NOT_FOUND;
833
794
ha_statistic_increment(&system_status_var::ha_write_count);
835
796
/* Prepare Auto Increment field if one exists. */
836
if (getTable()->next_number_field && drizzle_row == getTable()->getInsertRecord()) {
797
if (table->next_number_field && drizzle_row == table->getInsertRecord()) {
837
798
pthread_mutex_lock(&blitz_utility_mutex);
838
799
if ((rv = update_auto_increment()) != 0) {
839
800
pthread_mutex_unlock(&blitz_utility_mutex);
843
uint64_t next_val = getTable()->next_number_field->val_int();
804
uint64_t next_val = table->next_number_field->val_int();
845
806
if (next_val > share->auto_increment_value) {
846
807
share->auto_increment_value = next_val;
971
932
/* Now write the new key. */
972
933
prefix_len = make_index_key(key_buffer, i, new_row);
974
if (i == getTable()->getShare()->getPrimaryKey()) {
935
if (i == table->s->getPrimaryKey()) {
975
936
key = merge_key(key_buffer, prefix_len, key_buffer, prefix_len, &klen);
976
937
rv = share->btrees[i].write(key, klen);
998
959
if (table_based) {
999
960
rv = share->dict.write_row(held_key, held_key_len, row_buf, row_len);
1001
int klen = make_index_key(key_buffer, getTable()->getShare()->getPrimaryKey(), old_row);
962
int klen = make_index_key(key_buffer, table->s->getPrimaryKey(), old_row);
1003
964
/* Delete with the old key. */
1004
965
share->dict.delete_row(key_buffer, klen);
1006
967
/* Write with the new key. */
1007
klen = make_index_key(key_buffer, getTable()->getShare()->getPrimaryKey(), new_row);
968
klen = make_index_key(key_buffer, table->s->getPrimaryKey(), new_row);
1008
969
rv = share->dict.write_row(key_buffer, klen, row_buf, row_len);
1098
1059
uint32_t ha_blitz::max_row_length(void) {
1099
uint32_t length = (getTable()->getRecordLength() + getTable()->sizeFields() * 2);
1100
uint32_t *pos = getTable()->getBlobField();
1101
uint32_t *end = pos + getTable()->sizeBlobFields();
1060
uint32_t length = (table->getRecordLength() + table->sizeFields() * 2);
1061
uint32_t *pos = table->getBlobField();
1062
uint32_t *end = pos + table->sizeBlobFields();
1103
1064
while (pos != end) {
1104
length += 2 + ((Field_blob *)getTable()->getField(*pos))->get_length();
1065
length += 2 + ((Field_blob *)table->getField(*pos))->get_length();
1118
1079
/* Getting here means that there is a PK in this table. Get the
1119
1080
binary representation of the PK, pack it to BlitzDB's key buffer
1120
1081
and return the size of it. */
1121
return make_index_key(pack_to, getTable()->getShare()->getPrimaryKey(), row);
1082
return make_index_key(pack_to, table->s->getPrimaryKey(), row);
1124
1085
size_t ha_blitz::make_index_key(char *pack_to, int key_num,
1125
1086
const unsigned char *row) {
1126
KeyInfo *key = &getTable()->key_info[key_num];
1087
KeyInfo *key = &table->key_info[key_num];
1127
1088
KeyPartInfo *key_part = key->key_part;
1128
1089
KeyPartInfo *key_part_end = key_part + key->key_parts;
1146
/* Here we normalize VARTEXT1 to VARTEXT2 for simplicity. */
1147
if (key_part->type == HA_KEYTYPE_VARTEXT1) {
1148
/* Extract the length of the string from the row. */
1149
uint16_t data_len = *(uint8_t *)(row + key_part->offset);
1151
/* Copy the length of the string. Use 2 bytes. */
1152
int2store(pos, data_len);
1153
pos += sizeof(data_len);
1155
/* Copy the string data */
1156
memcpy(pos, row + key_part->offset + sizeof(uint8_t), data_len);
1159
end = key_part->field->pack(pos, row + key_part->offset);
1107
end = key_part->field->pack(pos, row + key_part->offset);
1165
1112
return ((char *)pos - pack_to);
1200
1147
size_t ha_blitz::btree_key_length(const char *key, const int key_num) {
1201
KeyInfo *key_info = &getTable()->key_info[key_num];
1148
KeyInfo *key_info = &table->key_info[key_num];
1202
1149
KeyPartInfo *key_part = key_info->key_part;
1203
1150
KeyPartInfo *key_part_end = key_part + key_info->key_parts;
1204
1151
char *pos = (char *)key;
1208
1155
for (; key_part != key_part_end; key_part++) {
1209
1156
if (key_part->null_bit) {
1216
if (key_part->type == HA_KEYTYPE_VARTEXT1 ||
1217
key_part->type == HA_KEYTYPE_VARTEXT2) {
1162
if (key_part->type == HA_KEYTYPE_VARTEXT1) {
1163
len = *(uint8_t *)pos;
1164
rv += len + sizeof(uint8_t);
1165
} else if (key_part->type == HA_KEYTYPE_VARTEXT2) {
1218
1166
len = uint2korr(pos);
1219
1167
rv += len + sizeof(uint16_t);
1237
1185
/* Converts a native Drizzle index key to BlitzDB's format. */
1238
1186
char *ha_blitz::native_to_blitz_key(const unsigned char *native_key,
1239
1187
const int key_num, int *return_key_len) {
1240
KeyInfo *key = &getTable()->key_info[key_num];
1188
KeyInfo *key = &table->key_info[key_num];
1241
1189
KeyPartInfo *key_part = key->key_part;
1242
1190
KeyPartInfo *key_part_end = key_part + key->key_parts;
1261
/* Normalize a VARTEXT1 key to VARTEXT2. */
1209
/* This is a temporary workaround for a bug in Drizzle's VARCHAR
1210
where a 1 byte representable length varchar's actual data is
1211
positioned 2 bytes ahead of the beginning of the buffer. The
1212
correct behavior is to be positioned 1 byte ahead. Furthermore,
1213
this is only applicable with varchar keys on READ. */
1262
1214
if (key_part->type == HA_KEYTYPE_VARTEXT1) {
1263
uint16_t str_len = *(uint16_t *)key_pos;
1265
/* Copy the length of the string over to key buffer. */
1266
int2store(keybuf_pos, str_len);
1267
keybuf_pos += sizeof(str_len);
1269
/* Copy the actual value over to the key buffer. */
1270
memcpy(keybuf_pos, key_pos + sizeof(str_len), str_len);
1271
keybuf_pos += str_len;
1273
/* NULL byte + Length of str (2 byte) + Actual String. */
1274
offset = 1 + sizeof(str_len) + str_len;
1215
/* Dereference the 1 byte length of the value. */
1216
uint8_t varlen = *(uint8_t *)key_pos;
1217
*keybuf_pos++ = varlen;
1219
/* Read the value by skipping 2 bytes. This is the workaround. */
1220
memcpy(keybuf_pos, key_pos + sizeof(uint16_t), varlen);
1221
offset = (sizeof(uint8_t) + varlen);
1222
keybuf_pos += varlen;
1276
1224
end = key_part->field->pack(keybuf_pos, key_pos);
1277
1225
offset = end - keybuf_pos;
1293
1241
/* Nothing special to do if the table is fixed length */
1294
1242
if (share->fixed_length_table) {
1295
memcpy(row_buffer, row_to_pack, getTable()->getShare()->getRecordLength());
1296
return (size_t)getTable()->getShare()->getRecordLength();
1243
memcpy(row_buffer, row_to_pack, table->s->getRecordLength());
1244
return (size_t)table->s->getRecordLength();
1299
1247
/* Copy NULL bits */
1300
memcpy(row_buffer, row_to_pack, getTable()->getShare()->null_bytes);
1301
pos = row_buffer + getTable()->getShare()->null_bytes;
1248
memcpy(row_buffer, row_to_pack, table->s->null_bytes);
1249
pos = row_buffer + table->s->null_bytes;
1303
1251
/* Pack each field into the buffer */
1304
for (Field **field = getTable()->getFields(); *field; field++) {
1252
for (Field **field = table->getFields(); *field; field++) {
1305
1253
if (!((*field)->is_null()))
1306
1254
pos = (*field)->pack(pos, row_to_pack + (*field)->offset(row_to_pack));
1322
1270
/* Start by copying NULL bits which is the beginning block
1323
1271
of a Drizzle row. */
1324
1272
pos = (const unsigned char *)from;
1325
memcpy(to, pos, getTable()->getShare()->null_bytes);
1326
pos += getTable()->getShare()->null_bytes;
1273
memcpy(to, pos, table->s->null_bytes);
1274
pos += table->s->null_bytes;
1328
1276
/* Unpack all fields in the provided row. */
1329
for (Field **field = getTable()->getFields(); *field; field++) {
1277
for (Field **field = table->getFields(); *field; field++) {
1330
1278
if (!((*field)->is_null())) {
1331
pos = (*field)->unpack(to + (*field)->offset(getTable()->getInsertRecord()), pos);
1279
pos = (*field)->unpack(to + (*field)->offset(table->getInsertRecord()), pos);
1361
1309
BlitzShare *ha_blitz::get_share(const char *name) {
1362
1310
BlitzShare *share_ptr;
1363
BlitzEngine *bz_engine = (BlitzEngine *)getEngine();
1311
BlitzEngine *bz_engine = (BlitzEngine *)engine;
1364
1312
std::string table_path(name);
1366
1314
pthread_mutex_lock(&blitz_utility_mutex);
1387
1335
/* Prepare Index Structure(s) */
1388
KeyInfo *curr = &getTable()->getMutableShare()->getKeyInfo(0);
1389
share_ptr->btrees = new BlitzTree[getTable()->getShare()->keys];
1336
KeyInfo *curr = &table->s->getKeyInfo(0);
1337
share_ptr->btrees = new BlitzTree[table->s->keys];
1391
for (uint32_t i = 0; i < getTable()->getShare()->keys; i++, curr++) {
1339
for (uint32_t i = 0; i < table->s->keys; i++, curr++) {
1392
1340
share_ptr->btrees[i].open(table_path.c_str(), i, BDBOWRITER);
1393
1341
share_ptr->btrees[i].parts = new BlitzKeyPart[curr->key_parts];
1395
if (getTable()->key_info[i].flags & HA_NOSAME)
1343
if (table->key_info[i].flags & HA_NOSAME)
1396
1344
share_ptr->btrees[i].unique = true;
1398
1346
share_ptr->btrees[i].length = curr->key_length;
1405
1353
if (f->null_ptr) {
1406
1354
share_ptr->btrees[i].parts[j].null_bitmask = f->null_bit;
1407
1355
share_ptr->btrees[i].parts[j].null_pos
1408
= (uint32_t)(f->null_ptr - (unsigned char *)getTable()->getInsertRecord());
1356
= (uint32_t)(f->null_ptr - (unsigned char *)table->getInsertRecord());
1411
1359
share_ptr->btrees[i].parts[j].flag = curr->key_part[j].key_part_flag;
1423
1371
/* Set Meta Data */
1424
1372
share_ptr->auto_increment_value = share_ptr->dict.read_meta_autoinc();
1425
1373
share_ptr->table_name = table_path;
1426
share_ptr->nkeys = getTable()->getShare()->keys;
1374
share_ptr->nkeys = table->s->keys;
1427
1375
share_ptr->use_count = 1;
1429
share_ptr->fixed_length_table = !(getTable()->getShare()->db_create_options
1377
share_ptr->fixed_length_table = !(table->s->db_create_options
1430
1378
& HA_OPTION_PACK_RECORD);
1432
if (getTable()->getShare()->getPrimaryKey() >= MAX_KEY)
1380
if (table->s->getPrimaryKey() >= MAX_KEY)
1433
1381
share_ptr->primary_key_exists = false;
1435
1383
share_ptr->primary_key_exists = true;
1505
static void blitz_init_options(drizzled::module::option_context &context)
1507
context("estimated-rows",
1508
po::value<uint64_t>(&blitz_estimated_rows)->default_value(0),
1509
N_("Estimated number of rows that a BlitzDB table will store."));
1512
DRIZZLE_PLUGIN(blitz_init, NULL, blitz_init_options);
1451
static DRIZZLE_SYSVAR_ULONGLONG (
1453
blitz_estimated_rows,
1454
PLUGIN_VAR_RQCMDARG,
1455
"Estimated number of rows that a BlitzDB table will store.",
1464
static drizzle_sys_var *blitz_system_variables[] = {
1465
DRIZZLE_SYSVAR(estimated_rows),
1469
DRIZZLE_PLUGIN(blitz_init, blitz_system_variables, NULL);