drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
wiredtiger_engine.cc
1#include "config.h"
2#include <drizzled/field/varstring.h>
3#include <drizzled/session/table_messages.h>
4#include <drizzled/data_home.h>
5
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;
11
12#include "wiredtiger_drizzle.h"
13#include "wiredtiger_checkpoint_function.h"
14#include "wt_to_drizzle_error.h"
15
16#include <stdlib.h>
17#include <string>
18#include <sstream>
19
20using namespace drizzled;
21using namespace std;
22
23typedef std::vector<std::pair<std::string, std::string> > StringPairVector;
24
25static int store_table_proto(WT_SESSION *session,
26 const identifier::Table &identifier,
27 const message::Table &create_proto);
28
29static const string engine_name("WIREDTIGER");
30WiredTigerEngine *wiredtiger_storage_engine= NULL;
31
32boost::mutex THR_LOCK_wiredtiger;
33
34Cursor *WiredTigerEngine::create(Table &table)
35{
36 return new WiredTigerCursor(*this, table);
37}
38
39/* Per-Session engine state: the WiredTiger session and whether a
40 WiredTiger transaction is running on it. WiredTiger errors on nested
41 begin_transaction and on commit without begin, so every begin/commit/
42 rollback must go through this state. */
44{
45 WT_SESSION *wt_session;
46 bool txn_active;
47};
48
49static WiredTigerSessionData *get_session_data(Session &session)
50{
51 void **s = session.getEngineData(wiredtiger_storage_engine);
53 if (data == NULL) {
54 WT_CONNECTION *conn = wiredtiger_storage_engine->conn;
55 WT_SESSION *wt_session;
56 int r = conn->open_session(conn, NULL, NULL, &wt_session);
57 assert(r == 0);
58 data = new WiredTigerSessionData();
59 data->wt_session = wt_session;
60 data->txn_active = false;
61 *s = data;
62 }
63 return data;
64}
65
66WT_SESSION *get_wt_session(Session &session)
67{
68 return get_session_data(session)->wt_session;
69}
70
71void WiredTigerEngine::closeConnection(Session &session)
72{
73 void **s = session.getEngineData(wiredtiger_storage_engine);
74 WiredTigerSessionData *data = (WiredTigerSessionData *)*s;
75 if (data != NULL) {
76 int r = data->wt_session->close(data->wt_session, NULL);
77 assert(r == 0);
78 delete data;
79 *s = NULL;
80 }
81}
82
83WiredTigerTableShare *WiredTigerEngine::findOpenTable(const identifier::Table &identifier)
84{
85 WTShareMap::iterator find_iter=
86 wt_shares.find(identifier.getPath());
87
88 if (find_iter != wt_shares.end())
89 return (*find_iter).second;
90 else
91 return NULL;
92}
93
94void WiredTigerEngine::addOpenTable(const identifier::Table &identifier, WiredTigerTableShare *share)
95{
96 // called with wt_share_mutex held
97 wt_shares[identifier.getPath()]= share;
98}
99
100void WiredTigerEngine::deleteOpenTable(const identifier::Table &identifier)
101{
102 // called with wt_share_mutex held
103 wt_shares.erase(identifier.getPath());
104}
105
106/* eng_session is shared by every connection for schema operations and
107 dictionary reads, and a WT_SESSION is not thread-safe: every use of
108 eng_session must hold this mutex. */
109static boost::mutex eng_session_mutex;
110
111void WiredTigerEngine::doGetTableIdentifiers(CachedDirectory&,
112 const identifier::Schema& schema,
113 identifier::table::vector& identifiers)
114{
115 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
116
117 string search_string(schema.getPath());
118 search_string.append("/");
119 WT_CURSOR *cursor;
120 WT_SESSION *session = eng_session;
121 int ret;
122
123 ret= session->open_cursor(session, "table:table_definitions", NULL, NULL, &cursor);
124 assert(ret==0);
125
126 cursor->set_key(cursor, search_string.c_str());
127
128 /* search_near() may land on the key just before the schema prefix;
129 step forward so the scan starts inside it. */
130 int exact;
131 ret = cursor->search_near(cursor, &exact);
132 if (ret == 0 && exact < 0)
133 ret = cursor->next(cursor);
134
135 while (ret == 0)
136 {
137 const char* table_path;
138 cursor->get_key(cursor, &table_path);
139
140 if (strncmp(table_path, search_string.c_str(), search_string.length()) != 0)
141 break;
142
143 identifier::Table i(schema, table_path + search_string.length());
144
145 identifiers.push_back(i);
146
147 ret = cursor->next(cursor);
148 }
149
150 cursor->close(cursor);
151}
152
153bool WiredTigerEngine::doDoesTableExist(Session&, const identifier::Table &identifier)
154{
155 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
156
157 WT_CURSOR *cursor;
158 WT_SESSION *session = eng_session;
159 int ret;
160 bool exists= false;
161
162 ret= session->open_cursor(session, "table:table_definitions", NULL, NULL, &cursor);
163 assert(ret==0);
164
165 string search_str(identifier.getPath());
166 cursor->set_key(cursor, search_str.c_str());
167
168 ret = cursor->search(cursor);
169
170 if (ret == 0)
171 exists= true;
172 else if (ret == WT_NOTFOUND)
173 exists= false;
174
175 ret = cursor->close(cursor);
176
177 return exists;
178}
179
180int WiredTigerEngine::doGetTableDefinition(Session &,
181 const identifier::Table &identifier,
182 message::Table &table_proto)
183{
184 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
185
186 return readTableProto(identifier, table_proto);
187}
188
189/* Read a stored table proto. Caller holds eng_session_mutex. */
190int WiredTigerEngine::readTableProto(const identifier::Table &identifier,
191 message::Table &table_proto)
192{
193 WT_CURSOR *cursor;
194 WT_SESSION *session = eng_session;
195 int ret;
196 int result= ENOENT;
197
198 ret= session->open_cursor(session, "table:table_definitions", NULL, NULL, &cursor);
199 assert(ret==0);
200
201 cursor->set_key(cursor, identifier.getPath().c_str());
202
203 if ((ret = cursor->search(cursor)) == 0)
204 {
205 WT_ITEM message;
206 ret = cursor->get_value(cursor, &message);
207 if (table_proto.ParseFromArray(message.data, message.size) == false)
208 goto cleanup;
209
210 result= EEXIST;
211 }
212
213 cleanup:
214 ret = cursor->close(cursor);
215
216
217 return result;
218}
219
220static int delete_table_proto(WT_SESSION *session,
221 const identifier::Table &identifier)
222{
223 WT_CURSOR *cursor;
224 int ret= 0;
225 ret = session->open_cursor(session, "table:table_definitions",
226 NULL, NULL, &cursor);
227
228 if (ret != 0)
229 return wt_to_drizzle_error(ret);
230
231 cursor->set_key(cursor, identifier.getPath().c_str());
232 ret = cursor->search(cursor);
233
234 if (ret == 0)
235 ret= cursor->remove(cursor);
236 else if (ret == WT_NOTFOUND)
237 goto cleanup;
238
239cleanup:
240 cursor->close(cursor);
241
242 return wt_to_drizzle_error(ret);
243}
244
245int WiredTigerEngine::getTableURI(const drizzled::identifier::Table &identifier,
246 string &table_uri)
247{
248 table_uri.append("table:");
249 table_uri.append(identifier.getPath());
250 return 0;
251}
252
253int WiredTigerEngine::doDropTable(Session &session,
254 const identifier::Table &identifier)
255{
256 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
257
258 int error;
259 string wt_table_name;
260 error = getTableURI(identifier, wt_table_name);
261
262 if (error)
263 return HA_ERR_GENERIC; // FIXME error mapping
264
265 /* Checkpoint the table before dropping it. WiredTiger requires
266 exclusive access to an object's data handle to drop it, and a
267 handle with dirty, never-checkpointed data cannot be released:
268 drop returns EBUSY for any table that has been written to since
269 the last checkpoint (and this engine configures no periodic
270 checkpoints). A targeted checkpoint covers the table's index
271 files as well, so the index drops below are also unblocked. If
272 the checkpoint fails, fall through: the drop is the operation of
273 record and reports its own error. */
274 {
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());
278 if (ckpt_error)
279 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
280 ER_CANT_DELETE_FILE,
281 _("Pre-drop checkpoint of WiredTiger table '%s' failed: %d %s\n"),
282 wt_table_name.c_str(),
283 ckpt_error,
284 wiredtiger_strerror(ckpt_error));
285 }
286
287 /* Drop the table's indexes individually before the table itself.
288 Dropping only the table leaves stale in-memory index schema state
289 in wiredtiger 3.2.1, which crashes the next open of a same-named
290 table. */
291 message::Table table_proto;
292 if (readTableProto(identifier, table_proto) == EEXIST)
293 {
294 const message::Table::Index *primary = NULL;
295 for (int indexnr= 0; indexnr < table_proto.indexes_size(); indexnr++)
296 {
297 const message::Table::Index &index = table_proto.indexes(indexnr);
298 if (index.is_primary()) {
299 primary = &index;
300 break;
301 } else if (index.is_unique()) {
302 primary = &index;
303 }
304 }
305 for (int indexnr= 0; indexnr < table_proto.indexes_size(); indexnr++)
306 {
307 const message::Table::Index &index = table_proto.indexes(indexnr);
308 if (&index == primary)
309 continue;
310
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");
315 }
316 }
317
318 error= eng_session->drop(eng_session, wt_table_name.c_str(), NULL);
319 if (error)
320 {
321 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
322 ER_CANT_DELETE_FILE,
323 _("Cannot drop WiredTiger Table '%s' because %d %s\n"),
324 wt_table_name.c_str(),
325 error,
326 wiredtiger_strerror(error));
327
328 return wt_to_drizzle_error(error);
329 }
330
331 error= delete_table_proto(eng_session, identifier);
332
333 if (error)
334 {
335 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
336 ER_CANT_DELETE_FILE,
337 _("Cannot drop WiredTiger table protobuf for '%s' because %d %s\n"),
338 wt_table_name.c_str(),
339 error,
340 wiredtiger_strerror(error));
341
342 return wt_to_drizzle_error(error);
343 }
344
345 return 0;
346}
347
348int WiredTigerEngine::doRenameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
349{
350 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
351
352 message::Table table_proto;
353
354 if (readTableProto(from, table_proto) != EEXIST)
355 return ENOENT;
356
357 string from_uri;
358 string to_uri;
359 getTableURI(from, from_uri);
360 getTableURI(to, to_uri);
361
362 /* Same reasoning as doDropTable: rename needs exclusive access to
363 the data handle, and a handle with un-checkpointed writes returns
364 EBUSY. Checkpoint the source table (and thereby its indexes)
365 first; on failure fall through and let rename report. */
366 {
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());
370 if (ckpt_error)
371 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
372 ER_ERROR_ON_RENAME,
373 _("Pre-rename checkpoint of WiredTiger table '%s' failed: %d %s\n"),
374 from_uri.c_str(),
375 ckpt_error,
376 wiredtiger_strerror(ckpt_error));
377 }
378
379 int error= eng_session->rename(eng_session, from_uri.c_str(),
380 to_uri.c_str(), NULL);
381 if (error)
382 {
383 push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
384 ER_ERROR_ON_RENAME,
385 _("Cannot rename WiredTiger table '%s' to '%s' because %d %s\n"),
386 from_uri.c_str(),
387 to_uri.c_str(),
388 error,
389 wiredtiger_strerror(error));
390
391 return wt_to_drizzle_error(error);
392 }
393
394 table_proto.set_name(to.getTableName());
395 table_proto.set_schema(to.getSchemaName());
396
397 if ((error= store_table_proto(eng_session, to, table_proto)) != 0)
398 return error;
399
400 return delete_table_proto(eng_session, from);
401}
402
403static int store_table_proto(WT_SESSION *session,
404 const identifier::Table &identifier,
405 const message::Table &create_proto)
406{
407 WT_CURSOR *cursor;
408 string serialized_message;
409 WT_ITEM serialized_message_item;
410 int ret= 0;
411
412 try {
413 create_proto.SerializeToString(&serialized_message);
414 }
415 catch (...)
416 {
417 goto cleanup;
418 }
419
420 serialized_message_item.data= serialized_message.c_str();
421 serialized_message_item.size= serialized_message.length();
422
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);
428 assert(ret == 0);
429 cursor->close(cursor);
430
431 cleanup:
432 return wt_to_drizzle_error(ret);
433}
434
435bool WiredTigerEngine::validateCreateTableOption(const std::string &key,
436 const std::string &state)
437{
438 if (boost::iequals(key, "allocation_size"))
439 {
440 int allocation_size= atoi(state.c_str());
441 if (allocation_size < 512)
442 return false;
443 if (allocation_size > (128*1024*1024))
444 return false;
445
446 return true;
447 }
448
449 if (boost::iequals(key, "leaf_page_max"))
450 return true;
451
452 if (boost::iequals(key, "internal_page_max"))
453 return true;
454
455 return false;
456}
457
458
459static int create_table_add_field(std::string *value_format,
460 const message::Table::Field &field)
461{
462 bool nullable= true;
463 const char *encoding = NULL;
464
465 if (field.has_constraints() && field.constraints().is_notnull())
466 nullable= false;
467
468 switch (field.type())
469 {
470 case message::Table::Field::VARCHAR:
471 encoding = "u"; // WT_ITEM
472 break;
473 case message::Table::Field::INTEGER:
474 encoding = "l"; // int32_t
475 break;
476 case message::Table::Field::BIGINT:
477 encoding = "q"; // int64_t
478 break;
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);
483 break;
484 case message::Table::Field::ENUM:
485 encoding = "H"; // uint16_t
486 break;
487 case message::Table::Field::DATE:
488 encoding = "q"; // int64_t
489 break;
490 case message::Table::Field::EPOCH:
491 encoding = "Q"; // uint64_t
492 break;
493 case message::Table::Field::BLOB:
494 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "Column Type");
495 return(HA_ERR_UNSUPPORTED);
496 break;
497 case message::Table::Field::DECIMAL:
498 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "Column Type");
499 return(HA_ERR_UNSUPPORTED);
500 break;
501 default:
502 my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "Column Type");
503 return(HA_ERR_UNSUPPORTED);
504 }
505
506 // Columns that support NULL are always encoded in a WT_ITEM - we store
507 // a leading NULL byte.
508 if (nullable && encoding != NULL)
509 encoding = "u";
510 value_format->append(encoding);
511
512 return 0;
513}
514
515int WiredTigerEngine::doCreateTable(Session &session,
516 Table &,
517 const identifier::Table &identifier,
518 const message::Table &create_proto)
519{
520 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
521
522 int error;
523 string wt_table_name("table:");
524 string wt_table_def;
525
526 const size_t num_engine_options= create_proto.engine().options_size();
527 for (size_t x= 0; x < num_engine_options; x++)
528 {
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(",");
534 }
535 wt_table_name.append(identifier.getPath());
536
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());
543 int field_err;
544
545 /* Figure out the primary key. */
546 const message::Table::Index *primary = NULL;
547 for (int indexnr= 0; indexnr < create_proto.indexes_size() ; indexnr++)
548 {
549 const message::Table::Index &index = create_proto.indexes(indexnr);
550 if (index.is_primary()) {
551 primary = &index;
552 break;
553 } else if (index.is_unique()) {
554 primary = &index;
555 }
556 }
557 if (primary == NULL) {
558 /* Use a row store if there is no primary index. */
559 wt_key_format.append("r");
560 wt_column_key_names.append("dummy");
561 } else {
562 for (int partnr= 0; partnr < primary->index_part_size(); partnr++)
563 {
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)
568 return field_err;
569 wt_column_key_names.append(field.name());
570 if (partnr < primary->index_part_size() -1 )
571 wt_column_key_names.append(",");
572 }
573 }
574
575 // Setup columns
576 for (int colnr= 0; colnr < create_proto.field_size() ; colnr++)
577 {
578 if (primary_bitset.test(colnr))
579 continue;
580
581 const message::Table::Field field = create_proto.field(colnr);
582
583 wt_column_value_names.append(",");
584 if ((field_err= create_table_add_field(&wt_value_format, field)) != 0)
585 return field_err;
586 wt_column_value_names.append(field.name());
587 }
588
589 /*
590 * Setup non-primary indexes. This is split from the primary case for
591 * code clarity, no technical reason they couldn't happen in the same
592 * loop.
593 */
594 for (int indexnr= 0; indexnr < create_proto.indexes_size() ; indexnr++)
595 {
596 const message::Table::Index &index = create_proto.indexes(indexnr);
597 if (&index == primary)
598 continue;
599
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=(");
604
605 for (int partnr= 0; partnr < index.index_part_size(); partnr++)
606 {
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(",");
611 }
612 wt_index_defn.append(")");
613 indexes.push_back(make_pair(wt_index_name, wt_index_defn));
614 }
615
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(")");
622
623 errmsg_printf(error::INSPECT, "wiredtiger: CREATE TABLE %s\n", wt_table_def.c_str());
624
625 error= eng_session->create(eng_session, wt_table_name.c_str(),
626 wt_table_def.c_str());
627
628 if (error != 0)
629 {
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));
635
636 return wt_to_drizzle_error(error);
637 }
638
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());
643 if (error != 0)
644 {
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"),
648 (*it).first.c_str(),
649 error, wiredtiger_strerror(error));
650
651 return -1;
652 }
653 }
654 error= store_table_proto(eng_session, identifier, create_proto);
655
656 return error;
657}
658
659int WiredTigerEngine::doStartTransaction(Session *session,
660 start_transaction_option_t)
661{
662 WiredTigerSessionData *data= get_session_data(*session);
663
664 if (data->txn_active)
665 return 0;
666
667 /* Always snapshot isolation, regardless of the session's SQL-level
668 setting. WiredTiger 11 rejects any write operation in a
669 read-committed or read-uncommitted transaction with ENOTSUP
670 (src/include/txn_inline.h, "write operations are not supported in
671 read-committed or read-uncommitted transactions"), so the weaker
672 levels are only usable read-only. Rather than pick isolation by
673 guessing whether a transaction will write, provide snapshot -- a
674 strictly stronger level than either -- for everything. */
675 std::stringstream ss;
676 ss << "name=" << session->getSessionId() << ",isolation=snapshot";
677 string txn_config(ss.str());
678
679 int r= data->wt_session->begin_transaction(data->wt_session,
680 txn_config.c_str());
681 if (r == 0)
682 data->txn_active= true;
683
684 return wt_to_drizzle_error(r);
685}
686
687void WiredTigerEngine::doStartStatement(Session *session)
688{
689 /* The kernel only calls doStartTransaction() for an explicit BEGIN.
690 Autocommit and implicit (autocommit=0) transactions still get
691 doCommit(all=true) at the end, so the transaction must start on
692 first use. */
693 doStartTransaction(session, START_TRANS_NO_OPTIONS);
694}
695
696int WiredTigerEngine::doCommit(Session *session, bool all)
697{
698 /* A statement commit inside a multi-statement transaction is a no-op:
699 WiredTiger cannot partially commit and the transaction stays open. */
700 if (!all)
701 return 0;
702
703 WiredTigerSessionData *data= get_session_data(*session);
704
705 if (!data->txn_active)
706 return 0;
707
708 data->txn_active= false;
709
710 return wt_to_drizzle_error(
711 data->wt_session->commit_transaction(data->wt_session, NULL));
712}
713
714int WiredTigerEngine::doRollback(Session *session, bool all)
715{
716 /* WiredTiger has no statement-level rollback (no savepoints); a failed
717 statement inside a multi-statement transaction leaves its changes in
718 the transaction. */
719 if (!all)
720 return 0;
721
722 WiredTigerSessionData *data= get_session_data(*session);
723
724 if (!data->txn_active)
725 return 0;
726
727 data->txn_active= false;
728
729 return wt_to_drizzle_error(
730 data->wt_session->rollback_transaction(data->wt_session, NULL));
731}
732
733
735static cache_size_constraint wt_cache_size;
736static string wt_config_str;
737
738static void wiredtiger_options(drizzled::module::option_context &context)
739{
740 context("cache-size",
741 po::value<cache_size_constraint>(&wt_cache_size)->default_value(128*1024*1024L),
742 N_("The WiredTiger cache size."));
743}
744
745static int create_table_definition_table(WT_SESSION *session)
746{
747 int ret= session->create(session, "table:table_definitions",
748 "key_format=S,value_format=u");
749
750 /* The table already existing means this is a restart against an
751 existing WiredTiger home, not an error. */
752 if (ret == EEXIST)
753 ret= 0;
754
755 return ret;
756}
757
758static int wiredtiger_init(module::Context &context)
759{
760 wt_config_str.assign("create");
761 wt_config_str.append(",statistics=(all)");
762 wt_config_str.append(",cache_size=");
763 std::stringstream ss;
764 ss << wt_cache_size;
765 wt_config_str.append(ss.str());
766
767 WT_CONNECTION *conn;
768 int r= wiredtiger_open(getDataHome().string().c_str(),
769 NULL,
770 wt_config_str.c_str(), &conn);
771
772 if (r != 0)
773 {
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);
777 }
778
779 WT_SESSION *session;
780 r= conn->open_session(conn, NULL, NULL, &session);
781 if (r != 0)
782 {
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);
787 }
788
789 r= create_table_definition_table(session);
790 if (r != 0)
791 {
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);
796 }
797
798 wiredtiger_storage_engine= new WiredTigerEngine(engine_name);
799 wiredtiger_storage_engine->conn = conn;
800 wiredtiger_storage_engine->eng_session = session;
801 context.add(wiredtiger_storage_engine);
802
803 context.registerVariable(new sys_var_constrained_value_readonly<size_t>("cache_size", wt_cache_size));
804
805 int wt_major, wt_minor, wt_patch;
806
807 context.registerVariable(new sys_var_const_string_val("configuration-string",
808 wt_config_str));
809 context.registerVariable(new sys_var_const_string_val("version",
810 string(WIREDTIGER_VERSION_STRING)));
811 context.registerVariable(new sys_var_const_string_val("library-version",
812 string(wiredtiger_version(&wt_major, &wt_minor, &wt_patch))));
813
814 init_wiredtiger_checkpoint_function(context);
815
816 return 0;
817}
818
819int WiredTigerEngine::checkpoint(const std::string &config)
820{
821 boost::mutex::scoped_lock scopedLock(eng_session_mutex);
822
823 return eng_session->checkpoint(eng_session, config.c_str());
824}
825
826DRIZZLE_DECLARE_PLUGIN
827{
828 DRIZZLE_VERSION_ID,
829 "wiredtiger",
830 "1.0",
831 "WiredTiger Inc",
832 N_("WiredTiger storage engine"),
833 PLUGIN_LICENSE_GPL,
834 wiredtiger_init,
835 NULL,
836 wiredtiger_options
837}
838DRIZZLE_DECLARE_PLUGIN_END;
session_id_t getSessionId() const
Definition session.h:640
TODO: Rename this file - func.h is stupid.
STL namespace.
const char * name
Definition dict0mem.h:331