20 #include "NdbApiTwsDriver.hpp"
26 #include "helpers.hpp"
27 #include "string_helpers.hpp"
28 #include "Properties.hpp"
39 using utils::toString;
48 #define ABORT_NDB_ERROR(error) \
49 do { cout << "!!! error in " << __FILE__ << ", line: " << __LINE__ \
50 << ", code: " << (int)(error).code \
51 << ", msg: " << (error).message << "." << endl; \
59 NdbApiTwsModel::NdbApiTwsModel(
Ndb* ndb) {
63 if ((table_t0 = dict->
getTable(
"mytable")) == NULL)
67 if ((column_c0 = table_t0->
getColumn(
"c0")) == NULL)
69 if ((column_c1 = table_t0->
getColumn(
"c1")) == NULL)
71 if ((column_c2 = table_t0->
getColumn(
"c2")) == NULL)
73 if ((column_c3 = table_t0->
getColumn(
"c3")) == NULL)
75 if ((column_c4 = table_t0->
getColumn(
"c4")) == NULL)
77 if ((column_c5 = table_t0->
getColumn(
"c5")) == NULL)
79 if ((column_c6 = table_t0->
getColumn(
"c6")) == NULL)
81 if ((column_c7 = table_t0->
getColumn(
"c7")) == NULL)
83 if ((column_c8 = table_t0->
getColumn(
"c8")) == NULL)
85 if ((column_c9 = table_t0->
getColumn(
"c9")) == NULL)
87 if ((column_c10 = table_t0->
getColumn(
"c10")) == NULL)
89 if ((column_c11 = table_t0->
getColumn(
"c11")) == NULL)
91 if ((column_c12 = table_t0->
getColumn(
"c12")) == NULL)
93 if ((column_c13 = table_t0->
getColumn(
"c13")) == NULL)
95 if ((column_c14 = table_t0->
getColumn(
"c14")) == NULL)
99 attr_c0 = column_c0->getAttrId();
100 attr_c1 = column_c1->getAttrId();
101 attr_c2 = column_c2->getAttrId();
102 attr_c3 = column_c3->getAttrId();
103 attr_c4 = column_c4->getAttrId();
104 attr_c5 = column_c5->getAttrId();
105 attr_c6 = column_c6->getAttrId();
106 attr_c7 = column_c7->getAttrId();
107 attr_c8 = column_c8->getAttrId();
108 attr_c9 = column_c9->getAttrId();
109 attr_c10 = column_c10->getAttrId();
110 attr_c11 = column_c11->getAttrId();
111 attr_c12 = column_c12->getAttrId();
112 attr_c13 = column_c13->getAttrId();
113 attr_c14 = column_c14->getAttrId();
115 width_c0 = columnWidth(column_c0);
116 width_c1 = columnWidth(column_c1);
117 width_c2 = columnWidth(column_c2);
118 width_c3 = columnWidth(column_c3);
119 width_c4 = columnWidth(column_c4);
120 width_c5 = columnWidth(column_c5);
121 width_c6 = columnWidth(column_c6);
122 width_c7 = columnWidth(column_c7);
123 width_c8 = columnWidth(column_c8);
124 width_c9 = columnWidth(column_c9);
125 width_c10 = columnWidth(column_c10);
126 width_c11 = columnWidth(column_c11);
127 width_c12 = columnWidth(column_c12);
128 width_c13 = columnWidth(column_c13);
129 width_c14 = columnWidth(column_c14);
154 NdbApiTwsDriver::init() {
159 <<
"initializing NDBAPI ..." << flush;
160 int stat = ndb_init();
162 ABORT_ERROR(
"ndb_init() returned: " << stat);
163 cout <<
" [ok]" << endl;
169 NdbApiTwsDriver::close() {
173 cout <<
"closing NDBAPI ... " << flush;
175 cout <<
" [ok]" << endl;
181 NdbApiTwsDriver::initProperties() {
182 TwsDriver::initProperties();
184 cout <<
"setting ndb properties ..." << flush;
188 mgmdConnect = toString(props[L
"ndb.mgmdConnect"]);
189 if (mgmdConnect.empty()) {
190 mgmdConnect = string(
"localhost");
193 catalog = toString(props[L
"ndb.catalog"]);
194 if (catalog.empty()) {
195 catalog = string(
"testdb");
198 schema = toString(props[L
"ndb.schema"]);
199 if (schema.empty()) {
200 schema = string(
"def");
204 if (msg.str().empty()) {
205 cout <<
" [ok]" << endl;
207 cout << endl << msg.str() << endl;
210 descr =
"ndbapi(" + mgmdConnect +
")";
214 NdbApiTwsDriver::printProperties() {
215 TwsDriver::printProperties();
217 const ios_base::fmtflags f = cout.flags();
220 cout.flags(ios_base::boolalpha);
222 cout << endl <<
"ndb settings ..." << endl;
223 cout <<
"ndb.mgmdConnect: \"" << mgmdConnect <<
"\"" << endl;
224 cout <<
"ndb.catalog: \"" << catalog <<
"\"" << endl;
225 cout <<
"ndb.schema: \"" << schema <<
"\"" << endl;
231 NdbApiTwsDriver::initNdbapiBuffers() {
232 assert(model->column_c0 != NULL);
236 cout <<
"allocating ndbapi buffers ..." << flush;
237 bb =
new char[model->width_row * nRows];
239 cout <<
" [ok]" << endl;
243 NdbApiTwsDriver::closeNdbapiBuffers() {
247 cout <<
"releasing ndbapi buffers ..." << flush;
252 cout <<
" [ok]" << endl;
258 NdbApiTwsDriver::runLoadOperations() {
260 <<
"running NDB API operations ..." <<
" [nRows=" << nRows <<
"]"
264 if (doInsert) runNdbapiInsert(SINGLE);
265 if (doLookup) runNdbapiLookup(SINGLE);
266 if (doUpdate) runNdbapiUpdate(SINGLE);
267 if (doDelete) runNdbapiDelete(SINGLE);
270 if (doInsert) runNdbapiInsert(BULK);
271 if (doLookup) runNdbapiLookup(BULK);
272 if (doUpdate) runNdbapiUpdate(BULK);
273 if (doDelete) runNdbapiDelete(BULK);
276 if (doInsert) runNdbapiInsert(BATCH);
277 if (doLookup) runNdbapiLookup(BATCH);
278 if (doUpdate) runNdbapiUpdate(BATCH);
279 if (doDelete) runNdbapiDelete(BATCH);
305 NdbApiTwsDriver::runNdbapiInsert(XMode
mode) {
306 const string name = string(
"insert_") + toStr(mode);
309 if (mode == SINGLE) {
310 for(
int i = 0;
i < nRows;
i++) {
311 ndbapiBeginTransaction();
313 ndbapiCommitTransaction();
314 ndbapiCloseTransaction();
317 ndbapiBeginTransaction();
318 for(
int i = 0;
i < nRows;
i++) {
321 ndbapiExecuteTransaction();
323 ndbapiCommitTransaction();
324 ndbapiCloseTransaction();
331 NdbApiTwsDriver::ndbapiInsert(
int c0) {
341 const int maxlen = 256;
343 snprintf(str, maxlen,
"%d", i);
346 ndbapiToBuffer1blp(bb_pos, str, model->width_c0);
347 if (op->
equal(model->attr_c0, bb_pos) != 0)
349 bb_pos += model->width_c0;
351 ndbapiToBuffer1blp(bb_pos, str, model->width_c1);
352 if (op->
setValue(model->attr_c1, bb_pos) != 0)
354 bb_pos += model->width_c1;
356 if (op->
setValue(model->attr_c2, i) != 0)
358 bb_pos += model->width_c2;
360 if (op->
setValue(model->attr_c3, i) != 0)
362 bb_pos += model->width_c3;
364 if (op->
setValue(model->attr_c4, (
char*)NULL) != 0)
366 bb_pos += model->width_c4;
368 ndbapiToBuffer1blp(bb_pos, str, model->width_c5);
369 if (op->
setValue(model->attr_c5, bb_pos) != 0)
371 bb_pos += model->width_c5;
373 ndbapiToBuffer1blp(bb_pos, str, model->width_c6);
374 if (op->
setValue(model->attr_c6, bb_pos) != 0)
376 bb_pos += model->width_c6;
378 ndbapiToBuffer1blp(bb_pos, str, model->width_c7);
379 if (op->
setValue(model->attr_c7, bb_pos) != 0)
381 bb_pos += model->width_c7;
383 ndbapiToBuffer1blp(bb_pos, str, model->width_c8);
384 if (op->
setValue(model->attr_c8, bb_pos) != 0)
386 bb_pos += model->width_c8;
388 if (op->
setValue(model->attr_c9, (
char*)NULL) != 0)
390 bb_pos += model->width_c9;
392 if (op->
setValue(model->attr_c10, (
char*)NULL) != 0)
394 bb_pos += model->width_c10;
396 if (op->
setValue(model->attr_c11, (
char*)NULL) != 0)
398 bb_pos += model->width_c11;
400 if (op->
setValue(model->attr_c12, (
char*)NULL) != 0)
402 bb_pos += model->width_c12;
404 if (op->
setValue(model->attr_c13, (
char*)NULL) != 0)
406 bb_pos += model->width_c13;
408 if (op->
setValue(model->attr_c14, (
char*)NULL) != 0)
410 bb_pos += model->width_c14;
414 NdbApiTwsDriver::runNdbapiLookup(XMode mode) {
415 const string name = string(
"lookup_") + toStr(mode);
418 if (mode == SINGLE) {
419 for(
int i = 0; i < nRows; i++) {
420 ndbapiBeginTransaction();
422 ndbapiCommitTransaction();
424 ndbapiCloseTransaction();
427 ndbapiBeginTransaction();
428 for(
int i = 0; i < nRows; i++) {
432 ndbapiExecuteTransaction();
434 ndbapiCommitTransaction();
435 for(
int i = 0; i < nRows; i++) {
438 ndbapiCloseTransaction();
445 NdbApiTwsDriver::ndbapiLookup(
int c0) {
455 const int maxlen = 256;
457 snprintf(str, maxlen,
"%d", i);
460 ndbapiToBuffer1blp(bb_pos, str, model->width_c0);
461 if (op->
equal(model->attr_c0, bb_pos) != 0)
463 bb_pos += model->width_c0;
466 if ((*ra_pos = op->
getValue(model->attr_c1, bb_pos)) == NULL)
468 bb_pos += model->width_c1;
471 if ((*ra_pos = op->
getValue(model->attr_c2, bb_pos)) == NULL)
473 bb_pos += model->width_c2;
476 if ((*ra_pos = op->
getValue(model->attr_c3, bb_pos)) == NULL)
478 bb_pos += model->width_c3;
481 if ((*ra_pos = op->
getValue(model->attr_c4, bb_pos)) == NULL)
483 bb_pos += model->width_c4;
486 if ((*ra_pos = op->
getValue(model->attr_c5, bb_pos)) == NULL)
488 bb_pos += model->width_c5;
491 if ((*ra_pos = op->
getValue(model->attr_c6, bb_pos)) == NULL)
493 bb_pos += model->width_c6;
496 if ((*ra_pos = op->
getValue(model->attr_c7, bb_pos)) == NULL)
498 bb_pos += model->width_c7;
501 if ((*ra_pos = op->
getValue(model->attr_c8, bb_pos)) == NULL)
503 bb_pos += model->width_c8;
506 if ((*ra_pos = op->
getValue(model->attr_c9, bb_pos)) == NULL)
508 bb_pos += model->width_c9;
511 if ((*ra_pos = op->
getValue(model->attr_c10, bb_pos)) == NULL)
513 bb_pos += model->width_c10;
516 if ((*ra_pos = op->
getValue(model->attr_c11, bb_pos)) == NULL)
518 bb_pos += model->width_c11;
521 if ((*ra_pos = op->
getValue(model->attr_c12, bb_pos)) == NULL)
523 bb_pos += model->width_c12;
526 if ((*ra_pos = op->
getValue(model->attr_c13, bb_pos)) == NULL)
528 bb_pos += model->width_c13;
531 if ((*ra_pos = op->
getValue(model->attr_c14, bb_pos)) == NULL)
533 bb_pos += model->width_c14;
538 NdbApiTwsDriver::ndbapiRead(
int c0) {
540 const int maxlen = 256;
542 snprintf(str0, maxlen,
"%d", c0);
547 bb_pos += model->width_c0;
549 ndbapiToString1blp(str1, bb_pos, model->width_c1);
551 bb_pos += model->width_c1;
554 memcpy(&i1, bb_pos, model->width_c2);
556 bb_pos += model->width_c2;
559 memcpy(&i1, bb_pos, model->width_c3);
561 bb_pos += model->width_c3;
565 verify(1, (*ra_pos)->isNULL());
566 bb_pos += model->width_c4;
569 ndbapiToString1blp(str1, bb_pos, model->width_c5);
571 bb_pos += model->width_c5;
574 ndbapiToString1blp(str1, bb_pos, model->width_c6);
576 bb_pos += model->width_c6;
579 ndbapiToString1blp(str1, bb_pos, model->width_c7);
581 bb_pos += model->width_c7;
584 ndbapiToString1blp(str1, bb_pos, model->width_c8);
586 bb_pos += model->width_c8;
590 verify(1, (*ra_pos)->isNULL());
591 bb_pos += model->width_c9;
595 verify(1, (*ra_pos)->isNULL());
596 bb_pos += model->width_c10;
600 verify(1, (*ra_pos)->isNULL());
601 bb_pos += model->width_c11;
605 verify(1, (*ra_pos)->isNULL());
606 bb_pos += model->width_c12;
610 verify(1, (*ra_pos)->isNULL());
611 bb_pos += model->width_c13;
615 verify(1, (*ra_pos)->isNULL());
616 bb_pos += model->width_c14;
621 NdbApiTwsDriver::runNdbapiUpdate(XMode mode) {
622 const string name = string(
"update_") + toStr(mode);
625 if (mode == SINGLE) {
626 for(
int i = 0; i < nRows; i++) {
627 ndbapiBeginTransaction();
629 ndbapiCommitTransaction();
630 ndbapiCloseTransaction();
633 ndbapiBeginTransaction();
634 for(
int i = 0; i < nRows; i++) {
637 ndbapiExecuteTransaction();
639 ndbapiCommitTransaction();
640 ndbapiCloseTransaction();
647 NdbApiTwsDriver::ndbapiUpdate(
int c0) {
656 const int maxlen = 256;
658 snprintf(str0, maxlen,
"%d", c0);
661 snprintf(str1, maxlen,
"%d", i);
664 ndbapiToBuffer1blp(bb_pos, str0, model->width_c0);
665 if (op->
equal(model->attr_c0, bb_pos) != 0)
667 bb_pos += model->width_c0;
669 ndbapiToBuffer1blp(bb_pos, str1, model->width_c1);
670 if (op->
setValue(model->attr_c1, bb_pos) != 0)
672 bb_pos += model->width_c1;
674 if (op->
setValue(model->attr_c2, i) != 0)
676 bb_pos += model->width_c2;
678 if (op->
setValue(model->attr_c3, i) != 0)
680 bb_pos += model->width_c3;
682 ndbapiToBuffer1blp(bb_pos, str1, model->width_c5);
683 if (op->
setValue(model->attr_c5, bb_pos) != 0)
685 bb_pos += model->width_c5;
687 ndbapiToBuffer1blp(bb_pos, str1, model->width_c6);
688 if (op->
setValue(model->attr_c6, bb_pos) != 0)
690 bb_pos += model->width_c6;
692 ndbapiToBuffer1blp(bb_pos, str1, model->width_c7);
693 if (op->
setValue(model->attr_c7, bb_pos) != 0)
695 bb_pos += model->width_c7;
697 ndbapiToBuffer1blp(bb_pos, str1, model->width_c8);
698 if (op->
setValue(model->attr_c8, bb_pos) != 0)
700 bb_pos += model->width_c8;
704 NdbApiTwsDriver::runNdbapiDelete(XMode mode) {
705 const string name = string(
"delete_") + toStr(mode);
708 if (mode == SINGLE) {
709 for(
int i = 0; i < nRows; i++) {
710 ndbapiBeginTransaction();
712 ndbapiCommitTransaction();
713 ndbapiCloseTransaction();
716 ndbapiBeginTransaction();
717 for(
int i = 0; i < nRows; i++) {
720 ndbapiExecuteTransaction();
722 ndbapiCommitTransaction();
723 ndbapiCloseTransaction();
730 NdbApiTwsDriver::ndbapiDelete(
int c0) {
740 const int maxlen = 256;
742 snprintf(str, maxlen,
"%d", i);
745 ndbapiToBuffer1blp(bb_pos, str, model->width_c0);
746 if (op->
equal(model->attr_c0, bb_pos) != 0)
748 bb_pos += model->width_c0;
754 NdbApiTwsDriver::ndbapiBeginTransaction() {
768 NdbApiTwsDriver::ndbapiExecuteTransaction() {
778 NdbApiTwsDriver::ndbapiCommitTransaction() {
792 NdbApiTwsDriver::ndbapiCloseTransaction() {
804 NdbApiTwsDriver::initConnection() {
805 assert(mgmd == NULL);
808 assert(model == NULL);
813 cout <<
"creating cluster connection ..." << flush;
814 assert(!mgmdConnect.empty());
816 cout <<
" [ok]" << endl;
819 cout <<
"connecting to mgmd ..." << flush;
820 const int retries = 0;
822 const int verbose = 1;
824 if (mgmd->connect(retries, delay, verbose) != 0)
825 ABORT_ERROR(
"mgmd@" << mgmdConnect <<
" was not ready within "
826 << (retries * delay) <<
"s.");
827 cout <<
" [ok: " << mgmdConnect <<
"]" << endl;
830 cout <<
"waiting for data nodes ..." << flush;
831 const int initial_wait = 10;
832 const int final_wait = 0;
834 if (mgmd->wait_until_ready(initial_wait, final_wait) < 0)
835 ABORT_ERROR(
"data nodes were not ready within "
836 << (initial_wait + final_wait) <<
"s.");
837 cout <<
" [ok]" << endl;
840 cout <<
"connecting to database ..." << flush;
841 ndb =
new Ndb(mgmd, catalog.c_str(), schema.c_str());
842 const int max_no_tx = 10;
845 if (ndb->
init(max_no_tx) != 0)
847 cout <<
" [ok: " << catalog <<
"." << schema <<
"]" << endl;
849 cout <<
"caching metadata ..." << flush;
851 cout <<
" [ok]" << endl;
855 cout <<
"using lock mode for reads ..." << flush;
860 lm =
"LM_CommittedRead";
872 lm =
"LM_CommittedRead";
875 cout <<
" [ok: " + lm +
"]" << endl;
879 NdbApiTwsDriver::closeConnection() {
880 assert(mgmd != NULL);
883 assert(model != NULL);
887 closeNdbapiBuffers();
889 cout <<
"clearing metadata cache ..." << flush;
892 cout <<
" [ok]" << endl;
894 cout <<
"closing database connection ..." << flush;
898 cout <<
" [ok]" << endl;
900 cout <<
"closing cluster connection ..." << flush;
903 cout <<
" [ok]" << endl;
909 NdbApiTwsDriver::ndbapiToBuffer1blp(
void*
to,
const char* from,
size_t width) {
911 size_t n = strlen(from);
912 assert(0 <= n && n < width && width < 256);
914 memcpy(t + 1, from, n);
919 NdbApiTwsDriver::ndbapiToString1blp(
char*
to,
const void* from,
size_t width) {
920 const char* s = (
const char*)from;
922 assert(0 <= n && n < width && width < 256);
924 memcpy(to, s + 1, n);
931 main(
int argc,
const char* argv[])