2#include <drizzled/field/varstring.h>
3#include <drizzled/session/table_messages.h>
4#include <drizzled/data_home.h>
6#include <boost/algorithm/string.hpp>
7#include <boost/dynamic_bitset.hpp>
8#include <boost/thread/mutex.hpp>
9#include <boost/program_options.hpp>
10namespace po= boost::program_options;
12#include "wiredtiger_drizzle.h"
13#include "wiredtiger_checkpoint_function.h"
14#include "wt_to_drizzle_error.h"
23typedef std::vector<std::pair<std::string, std::string> > StringPairVector;
25static int store_table_proto(WT_SESSION *session,
27 const message::Table &create_proto);
29static const string engine_name(
"WIREDTIGER");
32boost::mutex THR_LOCK_wiredtiger;
36 return new WiredTigerCursor(*
this, table);
45 WT_SESSION *wt_session;
51 void **s = session.getEngineData(wiredtiger_storage_engine);
54 WT_CONNECTION *conn = wiredtiger_storage_engine->conn;
55 WT_SESSION *wt_session;
56 int r = conn->open_session(conn, NULL, NULL, &wt_session);
59 data->wt_session = wt_session;
60 data->txn_active =
false;
66WT_SESSION *get_wt_session(
Session &session)
68 return get_session_data(session)->wt_session;
71void WiredTigerEngine::closeConnection(
Session &session)
73 void **s = session.getEngineData(wiredtiger_storage_engine);
74 WiredTigerSessionData *data = (WiredTigerSessionData *)*s;
76 int r = data->wt_session->close(data->wt_session, NULL);
85 WTShareMap::iterator find_iter=
86 wt_shares.find(identifier.getPath());
88 if (find_iter != wt_shares.end())
89 return (*find_iter).second;
97 wt_shares[identifier.getPath()]= share;
103 wt_shares.erase(identifier.getPath());
109static boost::mutex eng_session_mutex;
113 identifier::table::vector& identifiers)
115 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
117 string search_string(schema.getPath());
118 search_string.append(
"/");
120 WT_SESSION *session = eng_session;
123 ret= session->open_cursor(session,
"table:table_definitions", NULL, NULL, &cursor);
126 cursor->set_key(cursor, search_string.c_str());
131 ret = cursor->search_near(cursor, &exact);
132 if (ret == 0 && exact < 0)
133 ret = cursor->next(cursor);
137 const char* table_path;
138 cursor->get_key(cursor, &table_path);
140 if (strncmp(table_path, search_string.c_str(), search_string.length()) != 0)
143 identifier::Table i(schema, table_path + search_string.length());
145 identifiers.push_back(i);
147 ret = cursor->next(cursor);
150 cursor->close(cursor);
155 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
158 WT_SESSION *session = eng_session;
162 ret= session->open_cursor(session,
"table:table_definitions", NULL, NULL, &cursor);
165 string search_str(identifier.getPath());
166 cursor->set_key(cursor, search_str.c_str());
168 ret = cursor->search(cursor);
172 else if (ret == WT_NOTFOUND)
175 ret = cursor->close(cursor);
180int WiredTigerEngine::doGetTableDefinition(
Session &,
182 message::Table &table_proto)
184 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
186 return readTableProto(identifier, table_proto);
191 message::Table &table_proto)
194 WT_SESSION *session = eng_session;
198 ret= session->open_cursor(session,
"table:table_definitions", NULL, NULL, &cursor);
201 cursor->set_key(cursor, identifier.getPath().c_str());
203 if ((ret = cursor->search(cursor)) == 0)
206 ret = cursor->get_value(cursor, &message);
207 if (table_proto.ParseFromArray(message.data, message.size) ==
false)
214 ret = cursor->close(cursor);
220static int delete_table_proto(WT_SESSION *session,
225 ret = session->open_cursor(session,
"table:table_definitions",
226 NULL, NULL, &cursor);
229 return wt_to_drizzle_error(ret);
231 cursor->set_key(cursor, identifier.getPath().c_str());
232 ret = cursor->search(cursor);
235 ret= cursor->remove(cursor);
236 else if (ret == WT_NOTFOUND)
240 cursor->close(cursor);
242 return wt_to_drizzle_error(ret);
248 table_uri.append(
"table:");
249 table_uri.append(identifier.getPath());
253int WiredTigerEngine::doDropTable(
Session &session,
256 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
259 string wt_table_name;
260 error = getTableURI(identifier, wt_table_name);
263 return HA_ERR_GENERIC;
275 string ckpt_config(
"target=(\"");
276 ckpt_config.append(wt_table_name).append(
"\")");
277 int ckpt_error= eng_session->checkpoint(eng_session, ckpt_config.c_str());
279 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
281 _(
"Pre-drop checkpoint of WiredTiger table '%s' failed: %d %s\n"),
282 wt_table_name.c_str(),
284 wiredtiger_strerror(ckpt_error));
291 message::Table table_proto;
292 if (readTableProto(identifier, table_proto) == EEXIST)
294 const message::Table::Index *primary = NULL;
295 for (
int indexnr= 0; indexnr < table_proto.indexes_size(); indexnr++)
297 const message::Table::Index &index = table_proto.
indexes(indexnr);
298 if (index.is_primary()) {
301 }
else if (index.is_unique()) {
305 for (
int indexnr= 0; indexnr < table_proto.indexes_size(); indexnr++)
307 const message::Table::Index &index = table_proto.
indexes(indexnr);
308 if (&index == primary)
311 string wt_index_name(
"index:");
312 wt_index_name.append(identifier.getPath()).append(
":");
313 wt_index_name.append(index.
name());
314 eng_session->drop(eng_session, wt_index_name.c_str(),
"force");
318 error= eng_session->drop(eng_session, wt_table_name.c_str(), NULL);
321 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
323 _(
"Cannot drop WiredTiger Table '%s' because %d %s\n"),
324 wt_table_name.c_str(),
326 wiredtiger_strerror(error));
328 return wt_to_drizzle_error(error);
331 error= delete_table_proto(eng_session, identifier);
335 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
337 _(
"Cannot drop WiredTiger table protobuf for '%s' because %d %s\n"),
338 wt_table_name.c_str(),
340 wiredtiger_strerror(error));
342 return wt_to_drizzle_error(error);
350 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
352 message::Table table_proto;
354 if (readTableProto(from, table_proto) != EEXIST)
359 getTableURI(from, from_uri);
360 getTableURI(to, to_uri);
367 string ckpt_config(
"target=(\"");
368 ckpt_config.append(from_uri).append(
"\")");
369 int ckpt_error= eng_session->checkpoint(eng_session, ckpt_config.c_str());
371 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
373 _(
"Pre-rename checkpoint of WiredTiger table '%s' failed: %d %s\n"),
376 wiredtiger_strerror(ckpt_error));
379 int error= eng_session->rename(eng_session, from_uri.c_str(),
380 to_uri.c_str(), NULL);
383 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
385 _(
"Cannot rename WiredTiger table '%s' to '%s' because %d %s\n"),
389 wiredtiger_strerror(error));
391 return wt_to_drizzle_error(error);
394 table_proto.set_name(to.getTableName());
395 table_proto.set_schema(to.getSchemaName());
397 if ((error= store_table_proto(eng_session, to, table_proto)) != 0)
400 return delete_table_proto(eng_session, from);
403static int store_table_proto(WT_SESSION *session,
405 const message::Table &create_proto)
408 string serialized_message;
409 WT_ITEM serialized_message_item;
413 create_proto.SerializeToString(&serialized_message);
420 serialized_message_item.data= serialized_message.c_str();
421 serialized_message_item.size= serialized_message.length();
423 ret = session->open_cursor(session,
"table:table_definitions",
424 NULL, NULL, &cursor);
425 cursor->set_key(cursor, identifier.getPath().c_str());
426 cursor->set_value(cursor, &serialized_message_item);
427 ret = cursor->insert(cursor);
429 cursor->close(cursor);
432 return wt_to_drizzle_error(ret);
435bool WiredTigerEngine::validateCreateTableOption(
const std::string &key,
436 const std::string &state)
438 if (boost::iequals(key,
"allocation_size"))
440 int allocation_size= atoi(state.c_str());
441 if (allocation_size < 512)
443 if (allocation_size > (128*1024*1024))
449 if (boost::iequals(key,
"leaf_page_max"))
452 if (boost::iequals(key,
"internal_page_max"))
459static int create_table_add_field(std::string *value_format,
460 const message::Table::Field &field)
463 const char *encoding = NULL;
465 if (field.has_constraints() && field.constraints().is_notnull())
468 switch (field.type())
470 case message::Table::Field::VARCHAR:
473 case message::Table::Field::INTEGER:
476 case message::Table::Field::BIGINT:
479 case message::Table::Field::DOUBLE:
480 case message::Table::Field::DATETIME:
481 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0),
"Column Type");
482 return(HA_ERR_UNSUPPORTED);
484 case message::Table::Field::ENUM:
487 case message::Table::Field::DATE:
490 case message::Table::Field::EPOCH:
493 case message::Table::Field::BLOB:
494 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0),
"Column Type");
495 return(HA_ERR_UNSUPPORTED);
497 case message::Table::Field::DECIMAL:
498 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0),
"Column Type");
499 return(HA_ERR_UNSUPPORTED);
502 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0),
"Column Type");
503 return(HA_ERR_UNSUPPORTED);
508 if (nullable && encoding != NULL)
510 value_format->append(encoding);
515int WiredTigerEngine::doCreateTable(
Session &session,
518 const message::Table &create_proto)
520 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
523 string wt_table_name(
"table:");
526 const size_t num_engine_options= create_proto.engine().options_size();
527 for (
size_t x= 0; x < num_engine_options; x++)
529 const message::Engine::Option &engine_option= create_proto.engine().options(x);
530 wt_table_def.append(engine_option.name());
531 wt_table_def.append(
"=");
532 wt_table_def.append(engine_option.state());
533 wt_table_def.append(
",");
535 wt_table_name.append(identifier.getPath());
537 string wt_key_format(
"key_format=");
538 string wt_value_format(
"value_format=");
539 string wt_column_key_names(
"");
540 string wt_column_value_names(
"");
541 StringPairVector indexes;
542 boost::dynamic_bitset<> primary_bitset(create_proto.field_size());
546 const message::Table::Index *primary = NULL;
547 for (
int indexnr= 0; indexnr < create_proto.indexes_size() ; indexnr++)
549 const message::Table::Index &index = create_proto.
indexes(indexnr);
550 if (index.is_primary()) {
553 }
else if (index.is_unique()) {
557 if (primary == NULL) {
559 wt_key_format.append(
"r");
560 wt_column_key_names.append(
"dummy");
562 for (
int partnr= 0; partnr < primary->index_part_size(); partnr++)
564 const message::Table::Index::IndexPart part= primary->index_part(partnr);
565 const message::Table::Field field = create_proto.field(part.fieldnr());
566 primary_bitset.set(part.fieldnr());
567 if ((field_err= create_table_add_field(&wt_key_format, field)) != 0)
569 wt_column_key_names.append(field.name());
570 if (partnr < primary->index_part_size() -1 )
571 wt_column_key_names.append(
",");
576 for (
int colnr= 0; colnr < create_proto.field_size() ; colnr++)
578 if (primary_bitset.test(colnr))
581 const message::Table::Field field = create_proto.field(colnr);
583 wt_column_value_names.append(
",");
584 if ((field_err= create_table_add_field(&wt_value_format, field)) != 0)
586 wt_column_value_names.append(field.name());
594 for (
int indexnr= 0; indexnr < create_proto.indexes_size() ; indexnr++)
596 const message::Table::Index &index = create_proto.
indexes(indexnr);
597 if (&index == primary)
600 string wt_index_name(
"index:");
601 wt_index_name.append(identifier.getPath()).append(
":");
602 wt_index_name.append(index.
name());
603 string wt_index_defn(
"columns=(");
605 for (
int partnr= 0; partnr < index.index_part_size(); partnr++)
607 const message::Table::Index::IndexPart part= index.index_part(partnr);
608 wt_index_defn.append(create_proto.field(part.fieldnr()).name());
609 if (partnr < index.index_part_size() -1 )
610 wt_index_defn.append(
",");
612 wt_index_defn.append(
")");
613 indexes.push_back(make_pair(wt_index_name, wt_index_defn));
616 wt_table_def.append(wt_key_format).append(
",").append(wt_value_format);
617 wt_table_def.append(
",columns=(");
618 wt_table_def.append(wt_column_key_names);
619 if (wt_column_value_names !=
"")
620 wt_table_def.append(wt_column_value_names);
621 wt_table_def.append(
")");
623 errmsg_printf(error::INSPECT,
"wiredtiger: CREATE TABLE %s\n", wt_table_def.c_str());
625 error= eng_session->create(eng_session, wt_table_name.c_str(),
626 wt_table_def.c_str());
630 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
631 ER_CANT_CREATE_TABLE,
632 _(
"Cannot create table %s. WiredTiger Error %d (%s)\n"),
633 wt_table_name.c_str(),
634 error, wiredtiger_strerror(error));
636 return wt_to_drizzle_error(error);
639 for (StringPairVector::iterator it = indexes.begin();
640 it != indexes.end(); ++it) {
641 error= eng_session->create(eng_session,
642 (*it).first.c_str(), (*it).second.c_str());
645 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
646 ER_CANT_CREATE_TABLE,
647 _(
"Cannot create index %s. WiredTiger Error %d (%s)\n"),
649 error, wiredtiger_strerror(error));
654 error= store_table_proto(eng_session, identifier, create_proto);
659int WiredTigerEngine::doStartTransaction(
Session *session,
660 start_transaction_option_t)
662 WiredTigerSessionData *data= get_session_data(*session);
664 if (data->txn_active)
675 std::stringstream ss;
676 ss <<
"name=" << session->
getSessionId() <<
",isolation=snapshot";
677 string txn_config(ss.str());
679 int r= data->wt_session->begin_transaction(data->wt_session,
682 data->txn_active=
true;
684 return wt_to_drizzle_error(r);
687void WiredTigerEngine::doStartStatement(
Session *session)
693 doStartTransaction(session, START_TRANS_NO_OPTIONS);
696int WiredTigerEngine::doCommit(
Session *session,
bool all)
703 WiredTigerSessionData *data= get_session_data(*session);
705 if (!data->txn_active)
708 data->txn_active=
false;
710 return wt_to_drizzle_error(
711 data->wt_session->commit_transaction(data->wt_session, NULL));
714int WiredTigerEngine::doRollback(
Session *session,
bool all)
722 WiredTigerSessionData *data= get_session_data(*session);
724 if (!data->txn_active)
727 data->txn_active=
false;
729 return wt_to_drizzle_error(
730 data->wt_session->rollback_transaction(data->wt_session, NULL));
735static cache_size_constraint wt_cache_size;
736static string wt_config_str;
740 context(
"cache-size",
741 po::value<cache_size_constraint>(&wt_cache_size)->default_value(128*1024*1024L),
742 N_(
"The WiredTiger cache size."));
745static int create_table_definition_table(WT_SESSION *session)
747 int ret= session->create(session,
"table:table_definitions",
748 "key_format=S,value_format=u");
760 wt_config_str.assign(
"create");
761 wt_config_str.append(
",statistics=(all)");
762 wt_config_str.append(
",cache_size=");
763 std::stringstream ss;
765 wt_config_str.append(ss.str());
768 int r= wiredtiger_open(getDataHome().
string().c_str(),
770 wt_config_str.c_str(), &conn);
774 errmsg_printf(error::ERROR, _(
"Error connecting to %s: %s\n"),
775 getDataHome().
string().c_str(), wiredtiger_strerror(r));
776 return wt_to_drizzle_error(r);
780 r= conn->open_session(conn, NULL, NULL, &session);
783 errmsg_printf(error::ERROR, _(
"Error opening WiredTiger session: %s\n"),
784 wiredtiger_strerror(r));
785 conn->close(conn, NULL);
786 return wt_to_drizzle_error(r);
789 r= create_table_definition_table(session);
792 errmsg_printf(error::ERROR, _(
"Error creating WiredTiger table definition table: %s\n"),
793 wiredtiger_strerror(r));
794 conn->close(conn, NULL);
795 return wt_to_drizzle_error(r);
799 wiredtiger_storage_engine->conn = conn;
800 wiredtiger_storage_engine->eng_session = session;
801 context.add(wiredtiger_storage_engine);
805 int wt_major, wt_minor, wt_patch;
810 string(WIREDTIGER_VERSION_STRING)));
812 string(wiredtiger_version(&wt_major, &wt_minor, &wt_patch))));
814 init_wiredtiger_checkpoint_function(context);
819int WiredTigerEngine::checkpoint(
const std::string &config)
821 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
823 return eng_session->checkpoint(eng_session, config.c_str());
826DRIZZLE_DECLARE_PLUGIN
832 N_(
"WiredTiger storage engine"),
838DRIZZLE_DECLARE_PLUGIN_END;
session_id_t getSessionId() const
TODO: Rename this file - func.h is stupid.