30 #include <mysqld_error.h>
41 #define PRINT_ERROR(code,msg) \
42 std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \
43 << ", code: " << code \
44 << ", msg: " << msg << "." << std::endl
45 #define MYSQLERROR(mysql) { \
46 PRINT_ERROR(mysql_errno(&mysql),mysql_error(&mysql)); \
48 #define APIERROR(error) { \
49 PRINT_ERROR(error.code,error.message); \
53 const char *text_quote=
54 "Just at this moment, somehow or other, they began to run.\n"
56 " Alice never could quite make out, in thinking it over\n"
57 "afterwards, how it was that they began: all she remembers is,\n"
58 "that they were running hand in hand, and the Queen went so fast\n"
59 "that it was all she could do to keep up with her: and still the\n"
60 "Queen kept crying 'Faster! Faster!' but Alice felt she COULD NOT\n"
61 "go faster, though she had not breath left to say so.\n"
63 " The most curious part of the thing was, that the trees and the\n"
64 "other things round them never changed their places at all:\n"
65 "however fast they went, they never seemed to pass anything. 'I\n"
66 "wonder if all the things move along with us?' thought poor\n"
67 "puzzled Alice. And the Queen seemed to guess her thoughts, for\n"
68 "she cried, 'Faster! Don't try to talk!'\n"
70 " Not that Alice had any idea of doing THAT. She felt as if she\n"
71 "would never be able to talk again, she was getting so much out of\n"
72 "breath: and still the Queen cried 'Faster! Faster!' and dragged\n"
73 "her along. 'Are we nearly there?' Alice managed to pant out at\n"
76 " 'Nearly there!' the Queen repeated. 'Why, we passed it ten\n"
77 "minutes ago! Faster!' And they ran on for a time in silence,\n"
78 "with the wind whistling in Alice's ears, and almost blowing her\n"
79 "hair off her head, she fancied.\n"
81 " 'Now! Now!' cried the Queen. 'Faster! Faster!' And they\n"
82 "went so fast that at last they seemed to skim through the air,\n"
83 "hardly touching the ground with their feet, till suddenly, just\n"
84 "as Alice was getting quite exhausted, they stopped, and she found\n"
85 "herself sitting on the ground, breathless and giddy.\n"
87 " The Queen propped her up against a tree, and said kindly, 'You\n"
88 "may rest a little now.'\n"
90 " Alice looked round her in great surprise. 'Why, I do believe\n"
91 "we've been under this tree the whole time! Everything's just as\n"
94 " 'Of course it is,' said the Queen, 'what would you have it?'\n"
96 " 'Well, in OUR country,' said Alice, still panting a little,\n"
97 "'you'd generally get to somewhere else--if you ran very fast\n"
98 "for a long time, as we've been doing.'\n"
100 " 'A slow sort of country!' said the Queen. 'Now, HERE, you see,\n"
101 "it takes all the running YOU can do, to keep in the same place.\n"
102 "If you want to get somewhere else, you must run at least twice as\n"
105 " 'I'd rather not try, please!' said Alice. 'I'm quite content\n"
106 "to stay here--only I AM so hot and thirsty!'\n"
108 " -- Lewis Carroll, 'Through the Looking-Glass'.";
113 void drop_table(
MYSQL &mysql)
115 if (mysql_query(&mysql,
"DROP TABLE api_blob"))
123 int try_create_table(
MYSQL &mysql)
125 return mysql_query(&mysql,
128 " (my_id INT UNSIGNED NOT NULL,"
129 " my_text TEXT NOT NULL,"
130 " PRIMARY KEY USING HASH (my_id))"
136 if (try_create_table(mysql))
138 if (mysql_errno(&mysql) != ER_TABLE_EXISTS_ERROR)
140 std::cout <<
"MySQL Cluster already has example table: api_blob. "
141 <<
"Dropping it..." << std::endl;
146 if (try_create_table(mysql))
151 int populate(
Ndb *myNdb)
162 NdbOperation *myNdbOperation= myTrans->getNdbOperation(myTable);
163 if (myNdbOperation == NULL)
164 APIERROR(myTrans->getNdbError());
165 myNdbOperation->insertTuple();
166 myNdbOperation->equal(
"my_id", 1);
167 NdbBlob *myBlobHandle= myNdbOperation->getBlobHandle(
"my_text");
168 if (myBlobHandle == NULL)
169 APIERROR(myNdbOperation->getNdbError());
170 myBlobHandle->
setValue(text_quote, strlen(text_quote));
178 int update_key(
Ndb *myNdb)
194 NdbOperation *myNdbOperation= myTrans->getNdbOperation(myTable);
195 if (myNdbOperation == NULL)
196 APIERROR(myTrans->getNdbError());
197 myNdbOperation->updateTuple();
198 myNdbOperation->equal(
"my_id", 1);
199 NdbBlob *myBlobHandle= myNdbOperation->getBlobHandle(
"my_text");
200 if (myBlobHandle == NULL)
201 APIERROR(myNdbOperation->getNdbError());
205 APIERROR(myTrans->getNdbError());
208 if (-1 == myBlobHandle->
getLength(length))
216 #define CHUNK_SIZE 100
218 char buffer[CHUNK_SIZE];
219 for (chunk= (length-1)/CHUNK_SIZE; chunk >=0; chunk--)
221 Uint64 pos= chunk*CHUNK_SIZE;
222 Uint32 chunk_length= CHUNK_SIZE;
223 if (pos + chunk_length > length)
224 chunk_length= length - pos;
227 if (-1 == myBlobHandle->
setPos(pos))
229 if (-1 == myBlobHandle->
readData(buffer, chunk_length))
233 APIERROR(myTrans->getNdbError());
236 for (Uint64 j= 0; j < chunk_length; j++)
237 buffer[j]= toupper(buffer[j]);
239 if (-1 == myBlobHandle->
setPos(pos))
241 if (-1 == myBlobHandle->
writeData(buffer, chunk_length))
244 if (-1 == myTrans->execute(chunk ?
247 APIERROR(myTrans->getNdbError());
256 int update_scan(
Ndb *myNdb)
274 if (myScanOp == NULL)
275 APIERROR(myTrans->getNdbError());
277 NdbBlob *myBlobHandle= myScanOp->getBlobHandle(
"my_text");
278 if (myBlobHandle == NULL)
279 APIERROR(myScanOp->getNdbError());
280 if (myBlobHandle->
getValue(buffer,
sizeof(buffer)))
285 APIERROR(myTrans->getNdbError());
290 res= myScanOp->nextResult(
true);
294 APIERROR(myScanOp->getNdbError());
297 if (myBlobHandle->
getLength(length) == -1)
301 for (Uint64 j= 0; j < length; j++)
302 buffer[j]= tolower(buffer[j]);
304 NdbOperation *myUpdateOp= myScanOp->updateCurrentTuple();
305 if (myUpdateOp == NULL)
306 APIERROR(myTrans->getNdbError());
308 if (myBlobHandle2 == NULL)
310 if (myBlobHandle2->setValue(buffer, length))
311 APIERROR(myBlobHandle2->getNdbError());
314 APIERROR(myTrans->getNdbError());
317 if (-1 == myTrans->execute(NdbTransaction::Commit))
318 APIERROR(myTrans->getNdbError());
331 int myFetchHook(
NdbBlob* myBlobHandle,
void* arg)
335 ahd->readLength=
sizeof(ahd->buffer) - 1;
336 return myBlobHandle->
readData(ahd->buffer, ahd->readLength);
339 int fetch_key(
Ndb *myNdb)
353 NdbOperation *myNdbOperation= myTrans->getNdbOperation(myTable);
354 if (myNdbOperation == NULL)
355 APIERROR(myTrans->getNdbError());
356 myNdbOperation->readTuple();
357 myNdbOperation->equal(
"my_id", 1);
358 NdbBlob *myBlobHandle= myNdbOperation->getBlobHandle(
"my_text");
359 if (myBlobHandle == NULL)
360 APIERROR(myNdbOperation->getNdbError());
369 if (-1 == myTrans->execute(NdbTransaction::Commit))
370 APIERROR(myTrans->getNdbError());
375 ahd.buffer[ahd.readLength]=
'\0';
376 std::cout <<
"Fetched data:" << std::endl << ahd.buffer << std::endl;
382 int update2_key(
Ndb *myNdb)
396 NdbOperation *myNdbOperation= myTrans->getNdbOperation(myTable);
397 if (myNdbOperation == NULL)
398 APIERROR(myTrans->getNdbError());
399 myNdbOperation->updateTuple();
400 myNdbOperation->equal(
"my_id", 1);
401 NdbBlob *myBlobHandle= myNdbOperation->getBlobHandle(
"my_text");
402 if (myBlobHandle == NULL)
403 APIERROR(myNdbOperation->getNdbError());
404 memset(buffer,
' ',
sizeof(buffer));
405 if (myBlobHandle->
setValue(buffer,
sizeof(buffer)) == -1)
408 if (-1 == myTrans->execute(NdbTransaction::Commit))
409 APIERROR(myTrans->getNdbError());
416 int delete_key(
Ndb *myNdb)
428 NdbOperation *myNdbOperation= myTrans->getNdbOperation(myTable);
429 if (myNdbOperation == NULL)
430 APIERROR(myTrans->getNdbError());
431 myNdbOperation->deleteTuple();
432 myNdbOperation->equal(
"my_id", 1);
434 if (-1 == myTrans->execute(NdbTransaction::Commit))
435 APIERROR(myTrans->getNdbError());
442 int main(
int argc,
char**argv)
446 std::cout <<
"Arguments are <socket mysqld> <connect_string cluster>.\n";
449 char *mysqld_sock = argv[1];
450 const char *connectstring = argv[2];
456 if ( !mysql_init(&mysql) ) {
457 std::cout <<
"mysql_init failed.\n";
460 if ( !mysql_real_connect(&mysql,
"localhost",
"root",
"",
"",
464 mysql_query(&mysql,
"CREATE DATABASE ndb_examples");
465 if (mysql_query(&mysql,
"USE ndb_examples") != 0)
474 if (cluster_connection.connect(4, 5, 1))
476 std::cout <<
"Unable to connect to cluster within 30 secs." << std::endl;
480 if (cluster_connection.wait_until_ready(30,0) < 0)
482 std::cout <<
"Cluster was not ready within 30 secs.\n";
486 Ndb myNdb(&cluster_connection,
"ndb_examples");
487 if (myNdb.
init(1024) == -1) {
492 if(populate(&myNdb) > 0)
493 std::cout <<
"populate: Success!" << std::endl;
495 if(update_key(&myNdb) > 0)
496 std::cout <<
"update_key: Success!" << std::endl;
498 if(update_scan(&myNdb) > 0)
499 std::cout <<
"update_scan: Success!" << std::endl;
501 if(fetch_key(&myNdb) > 0)
502 std::cout <<
"fetch_key: Success!" << std::endl;
504 if(update2_key(&myNdb) > 0)
505 std::cout <<
"update2_key: Success!" << std::endl;
507 if(delete_key(&myNdb) > 0)
508 std::cout <<
"delete_key: Success!" << std::endl;