MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbApiDriver.cpp
1 /* -*- mode: java; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:smarttab:
3  *
4  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <cassert>
24 
25 #include "helpers.hpp"
26 #include "string_helpers.hpp"
27 
28 #include "NdbApiDriver.hpp"
29 #include "CrundNdbApiOperations.hpp"
30 
31 using std::cout;
32 using std::flush;
33 using std::endl;
34 using std::ios_base;
35 using std::ostringstream;
36 using std::string;
37 using std::wstring;
38 
39 using utils::toString;
40 
41 // ----------------------------------------------------------------------
42 
43 CrundNdbApiOperations* NdbApiDriver::ops = NULL;
44 
45 // ISO C++ 98 does not allow for a string literal as a template argument
46 // for a non-type template parameter, because string literals are objects
47 // with internal linkage. This restriction maybe lifted in C++0x.
48 //
49 // Until then, we have to allocate the operation names as variables
50 // (which are external at file scope by default).
51 const char* delAByPK_s = "delAByPK";
52 const char* delB0ByPK_s = "delB0ByPK";
53 const char* setAByPK_s = "setAByPK";
54 const char* setB0ByPK_s = "setB0ByPK";
55 const char* getAByPK_bb_s = "getAByPK_bb";
56 const char* getB0ByPK_bb_s = "getB0ByPK_bb";
57 const char* getAByPK_ar_s = "getAByPK_ar";
58 const char* getB0ByPK_ar_s = "getB0ByPK_ar";
59 
60 const char* setVarbinary_s = "setVarbinary";
61 const char* getVarbinary_s = "getVarbinary";
62 const char* clearVarbinary_s = "clearVarbinary";
63 const char* setVarchar_s = "setVarchar";
64 const char* getVarchar_s = "getVarchar";
65 const char* clearVarchar_s = "clearVarchar";
66 
67 const char* setB0ToA_s = "setB0->A";
68 const char* navB0ToA_s = "navB0->A";
69 const char* navB0ToAalt_s = "navB0->A_alt";
70 const char* navAToB0_s = "navA->B0";
71 const char* navAToB0alt_s = "navA->B0_alt";
72 const char* nullB0ToA_s = "nullB0->A";
73 
74 //---------------------------------------------------------------------------
75 
76 void
77 NdbApiDriver::init() {
78  CrundDriver::init();
79 
80  // initialize the benchmark's resources
81  ops = new CrundNdbApiOperations();
82  assert(!mgmdConnect.empty());
83  ops->init(mgmdConnect.c_str());
84 }
85 
86 void
87 NdbApiDriver::close() {
88  // release the benchmark's resources
89  assert(ops);
90  ops->close();
91  delete ops;
92  ops = NULL;
93 
94  CrundDriver::close();
95 }
96 
97 void
98 NdbApiDriver::initProperties() {
99  CrundDriver::initProperties();
100 
101  cout << "setting ndb properties ..." << flush;
102 
103  ostringstream msg;
104 
105  mgmdConnect = toString(props[L"ndb.mgmdConnect"]);
106  if (mgmdConnect.empty()) {
107  mgmdConnect = string("localhost");
108  }
109 
110  catalog = toString(props[L"ndb.catalog"]);
111  if (catalog.empty()) {
112  catalog = string("crunddb");
113  }
114 
115  schema = toString(props[L"ndb.schema"]);
116  if (schema.empty()) {
117  schema = string("def");
118  }
119 
120  //if (msg.tellp() == 0) {
121  if (msg.str().empty()) {
122  cout << " [ok]" << endl;
123  } else {
124  cout << endl << msg.str() << endl;
125  }
126 
127  descr = "ndbapi(" + mgmdConnect + ")";
128 }
129 
130 void
131 NdbApiDriver::printProperties() {
132  CrundDriver::printProperties();
133 
134  const ios_base::fmtflags f = cout.flags();
135  // no effect calling manipulator function, not sure why
136  //cout << ios_base::boolalpha;
137  cout.flags(ios_base::boolalpha);
138 
139  cout << endl << "ndb settings ..." << endl;
140  cout << "ndb.mgmdConnect: \"" << mgmdConnect << "\"" << endl;
141  cout << "ndb.catalog: \"" << catalog << "\"" << endl;
142  cout << "ndb.schema: \"" << schema << "\"" << endl;
143 
144  cout.flags(f);
145 }
146 
147 //---------------------------------------------------------------------------
148 
149 void
150 NdbApiDriver::initOperations() {
151  cout << "initializing operations ..." << flush;
152 
153  const bool feat = true;
154  initOperationsFeat< !feat >();
155  initOperationsFeat< feat >();
156 
157  cout << " [Op: " << operations.size() << "]" << endl;
158 }
159 
160 // the operation invocation templates look a bit complex, but they help
161 // a lot to factorize code over the operations' parameter signatures
162 
163 template< bool OB >
165  ADelAllOp() : Op(string("delAllA")
166  + (OB ? "_batch" : "")) {
167  }
168 
169  virtual void run(int nOps) const {
170  int count;
171  ops->delByScan(ops->model->table_A, count, OB);
172  assert(count == nOps);
173  }
174 };
175 
176 template< bool OB >
178  B0DelAllOp() : Op(string("delAllB0")
179  + (OB ? "_batch" : "")) {
180  }
181 
182  virtual void run(int nOps) const {
183  int count;
184  ops->delByScan(ops->model->table_B0, count, OB);
185  assert(count == nOps);
186  }
187 };
188 
189 template< bool OSA, bool OB >
191  AInsOp() : Op(string("insA")
192  + (OSA ? "_attr" : "")
193  + (OB ? "_batch" : "")) {
194  }
195 
196  virtual void run(int nOps) const {
197  ops->ins(ops->model->table_A, 1, nOps, OSA, OB);
198  }
199 };
200 
201 template< bool OSA, bool OB >
203  B0InsOp() : Op(string("insB0")
204  + (OSA ? "_attr" : "")
205  + (OB ? "_batch" : "")) {
206  }
207 
208  virtual void run(int nOps) const {
209  ops->ins(ops->model->table_B0, 1, nOps, OSA, OB);
210  }
211 };
212 
213 template< const char** ON,
214  void (CrundNdbApiOperations::*OF)(NdbTable,int,int,bool),
215  bool OB >
217  AByPKOp() : Op(string(*ON)
218  + (OB ? "_batch" : "")) {
219  }
220 
221  virtual void run(int nOps) const {
222  (ops->*OF)(ops->model->table_A, 1, nOps, OB);
223  }
224 };
225 
226 template< const char** ON,
227  void (CrundNdbApiOperations::*OF)(NdbTable,int,int,bool),
228  bool OB >
230  B0ByPKOp() : Op(string(*ON)
231  + (OB ? "_batch" : "")) {
232  }
233 
234  virtual void run(int nOps) const {
235  (ops->*OF)(ops->model->table_B0, 1, nOps, OB);
236  }
237 };
238 
239 template< const char** ON,
240  void (CrundNdbApiOperations::*OF)(NdbTable,int,int,bool,int),
241  bool OB >
243  const int length;
244 
245  LengthOp(int length) : Op(string(*ON)
246  + toString(length)
247  + (OB ? "_batch" : "")),
248  length(length) {
249  }
250 
251  virtual void run(int nOps) const {
252  (ops->*OF)(ops->model->table_B0, 1, nOps, OB, length);
253  }
254 };
255 
256 template< const char** ON,
257  void (CrundNdbApiOperations::*OF)(NdbTable,int,int,bool,int),
258  bool OB >
260  ZeroLengthOp(int length) : LengthOp< ON, OF, OB >(length) {
261  }
262 
263  virtual void run(int nOps) const {
264  (ops->*OF)(ops->model->table_B0, 1, nOps, OB, 0);
265  }
266 };
267 
268 template< const char** ON,
269  void (CrundNdbApiOperations::*OF)(int,bool),
270  bool OFS >
272  RelOp() : Op(string(*ON)
273  + (OFS ? "_forceSend" : "")) {
274  }
275 
276  virtual void run(int nOps) const {
277  (ops->*OF)(nOps, OFS);
278  }
279 };
280 
281 template< bool feat > void
282 NdbApiDriver::initOperationsFeat() {
283 
284  const bool setAttr = true;
285  operations.push_back(
287 
288  operations.push_back(
290 
291  operations.push_back(
293 
294  operations.push_back(
296 
297  operations.push_back(
299 
300  operations.push_back(
302 
303  operations.push_back(
305 
306  operations.push_back(
308 
309  for (int i = 1; i <= maxVarbinaryBytes; i *= 10) {
310  const int length = i;
311 
312  operations.push_back(
314 
315  operations.push_back(
317 
318  operations.push_back(
320  }
321 
322  for (int i = 1; i <= maxVarcharChars; i *= 10) {
323  const int length = i;
324 
325  operations.push_back(
326  new LengthOp< &setVarchar_s, &CrundNdbApiOperations::setVarchar, feat >(length));
327 
328  operations.push_back(
329  new LengthOp< &getVarchar_s, &CrundNdbApiOperations::getVarchar, feat >(length));
330 
331  operations.push_back(
332  new ZeroLengthOp< &clearVarchar_s, &CrundNdbApiOperations::setVarchar, feat >(length));
333  }
334 
335  operations.push_back(
336  new RelOp< &setB0ToA_s, &CrundNdbApiOperations::setB0ToA, feat >());
337 
338  operations.push_back(
339  new RelOp< &navB0ToA_s, &CrundNdbApiOperations::navB0ToA, feat >());
340 
341  operations.push_back(
342  new RelOp< &navB0ToAalt_s, &CrundNdbApiOperations::navB0ToAalt, feat >());
343 
344  operations.push_back(
345  new RelOp< &navAToB0_s, &CrundNdbApiOperations::navAToB0, feat >());
346 
347  operations.push_back(
348  new RelOp< &navAToB0alt_s, &CrundNdbApiOperations::navAToB0alt, feat >());
349 
350  operations.push_back(
351  new RelOp< &nullB0ToA_s, &CrundNdbApiOperations::nullB0ToA, feat >());
352 
353  operations.push_back(
354  new B0ByPKOp< &setAByPK_s, &CrundNdbApiOperations::delByPK, feat >());
355 
356  operations.push_back(
357  new AByPKOp< &setB0ByPK_s, &CrundNdbApiOperations::delByPK, feat >());
358 
359  operations.push_back(
360  new AInsOp< setAttr, feat >());
361 
362  operations.push_back(
363  new B0InsOp< setAttr, feat >());
364 
365  operations.push_back(
366  new ADelAllOp< feat >());
367 
368  operations.push_back(
369  new B0DelAllOp< feat >());
370 }
371 
372 void
373 NdbApiDriver::closeOperations() {
374  cout << "closing operations ..." << flush;
375  for (Operations::const_iterator i = operations.begin();
376  i != operations.end(); ++i) {
377  delete *i;
378  }
379  operations.clear();
380  cout << " [ok]" << endl;
381 }
382 
383 //---------------------------------------------------------------------------
384 
385 void
386 NdbApiDriver::initConnection() {
387  NdbOperation::LockMode ndbOpLockMode;
388  switch (lockMode) {
389  case READ_COMMITTED:
390  ndbOpLockMode = NdbOperation::LM_CommittedRead;
391  break;
392  case SHARED:
393  ndbOpLockMode = NdbOperation::LM_Read;
394  break;
395  case EXCLUSIVE:
396  ndbOpLockMode = NdbOperation::LM_Exclusive;
397  break;
398  default:
399  ndbOpLockMode = NdbOperation::LM_CommittedRead;
400  assert(false);
401  }
402 
403  ops->initConnection(catalog.c_str(), schema.c_str(), ndbOpLockMode);
404 }
405 
406 void
407 NdbApiDriver::closeConnection() {
408  ops->closeConnection();
409 }
410 
411 void
412 NdbApiDriver::clearPersistenceContext() {
413  assert(false); // XXX not implemented yet
414 }
415 
416 void
417 NdbApiDriver::clearData()
418 {
419  ops->clearData();
420 }
421 
422 //---------------------------------------------------------------------------
423 
424 int
425 main(int argc, const char* argv[])
426 {
427  TRACE("main()");
428 
429  NdbApiDriver::parseArguments(argc, argv);
430  NdbApiDriver d;
431  d.run();
432 
433  return 0;
434 }
435 
436 //---------------------------------------------------------------------------