mirror of
https://github.com/MariaDB/server.git
synced 2025-01-28 01:34:17 +01:00
Merge tulin@bk-internal.mysql.com:/home/bk/mysql-5.0-ndb
into poseidon.ndb.mysql.com:/home/tomas/mysql-5.0-ndb
This commit is contained in:
commit
5f0f39d0c6
12 changed files with 291 additions and 221 deletions
|
@ -1,6 +1,5 @@
|
|||
# XXX ndbapi_example4, select_all commented out until fixed
|
||||
BIN_DIRS := ndbapi_example1 ndbapi_example2 ndbapi_example3 $(ndbapi_example4) \
|
||||
ndbapi_example5 $(select_all) ndbapi_scan_example
|
||||
BIN_DIRS := ndbapi_example1 ndbapi_example3 ndbapi_example4 \
|
||||
ndbapi_example5 ndbapi_scan_example
|
||||
|
||||
bins: $(patsubst %, _bins_%, $(BIN_DIRS))
|
||||
|
||||
|
|
|
@ -32,73 +32,95 @@
|
|||
// 9 9
|
||||
|
||||
#include <NdbApi.hpp>
|
||||
|
||||
// Used for cout
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
static void run_application(Ndb_cluster_connection &);
|
||||
|
||||
int main()
|
||||
{
|
||||
// ndb_init must be called first
|
||||
ndb_init();
|
||||
|
||||
// connect to cluster and run application
|
||||
{
|
||||
// Object representing the cluster
|
||||
Ndb_cluster_connection cluster_connection;
|
||||
|
||||
// Connect to cluster management server (ndb_mgmd)
|
||||
if (cluster_connection.connect(4 /* retries */,
|
||||
5 /* delay between retries */,
|
||||
1 /* verbose */))
|
||||
{
|
||||
std::cout << "Cluster management server was not ready within 30 secs.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// Optionally connect and wait for the storage nodes (ndbd's)
|
||||
if (cluster_connection.wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
// run the application code
|
||||
run_application(cluster_connection);
|
||||
}
|
||||
|
||||
// ndb_end should not be called until all "Ndb" objects are deleted
|
||||
ndb_end(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define APIERROR(error) \
|
||||
{ std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \
|
||||
<< error.code << ", msg: " << error.message << "." << std::endl; \
|
||||
exit(-1); }
|
||||
|
||||
int main()
|
||||
static void create_table(Ndb &myNdb);
|
||||
static void do_insert(Ndb &myNdb);
|
||||
static void do_update(Ndb &myNdb);
|
||||
static void do_delete(Ndb &myNdb);
|
||||
static void do_read(Ndb &myNdb);
|
||||
|
||||
static void run_application(Ndb_cluster_connection &cluster_connection)
|
||||
{
|
||||
ndb_init();
|
||||
/********************************************
|
||||
* Connect to database *
|
||||
********************************************/
|
||||
// Object representing the database
|
||||
Ndb myNdb( &cluster_connection, "TEST_DB_1" );
|
||||
if (myNdb.init()) APIERROR(myNdb.getNdbError());
|
||||
|
||||
Ndb_cluster_connection *cluster_connection=
|
||||
new Ndb_cluster_connection(); // Object representing the cluster
|
||||
/*
|
||||
* Do different operations on database
|
||||
*/
|
||||
create_table(myNdb);
|
||||
do_insert(myNdb);
|
||||
do_update(myNdb);
|
||||
do_delete(myNdb);
|
||||
do_read(myNdb);
|
||||
}
|
||||
|
||||
int r= cluster_connection->connect(5 /* retries */,
|
||||
3 /* delay between retries */,
|
||||
1 /* verbose */);
|
||||
if (r > 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed, possibly resolved with more retries.\n";
|
||||
exit(-1);
|
||||
}
|
||||
else if (r < 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/*********************************************************
|
||||
* Create a table named MYTABLENAME if it does not exist *
|
||||
*********************************************************/
|
||||
static void create_table(Ndb &myNdb)
|
||||
{
|
||||
NdbDictionary::Dictionary* myDict = myNdb.getDictionary();
|
||||
|
||||
if (cluster_connection->wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs." << std::endl;
|
||||
if (myDict->getTable("MYTABLENAME") != NULL) {
|
||||
std::cout
|
||||
<< "NDB already has example table: MYTABLENAME.\n"
|
||||
<< "Use ndb_drop_table -d TEST_DB_1 MYTABLENAME\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
Ndb* myNdb = new Ndb( cluster_connection,
|
||||
"TEST_DB_1" ); // Object representing the database
|
||||
NdbDictionary::Table myTable;
|
||||
NdbDictionary::Column myColumn;
|
||||
|
||||
NdbConnection *myConnection; // For other transactions
|
||||
NdbOperation *myOperation; // For other operations
|
||||
NdbRecAttr *myRecAttr; // Result of reading attribute value
|
||||
|
||||
/********************************************
|
||||
* Initialize NDB and wait until it's ready *
|
||||
********************************************/
|
||||
if (myNdb->init()) {
|
||||
APIERROR(myNdb->getNdbError());
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
NdbDictionary::Dictionary* myDict = myNdb->getDictionary();
|
||||
|
||||
/*********************************************************
|
||||
* Create a table named MYTABLENAME if it does not exist *
|
||||
*********************************************************/
|
||||
if (myDict->getTable("MYTABLENAME") != NULL) {
|
||||
std::cout << "NDB already has example table: MYTABLENAME." << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
myTable.setName("MYTABLENAME");
|
||||
|
||||
myColumn.setName("ATTR1");
|
||||
|
@ -115,108 +137,112 @@ int main()
|
|||
myColumn.setNullable(false);
|
||||
myTable.addColumn(myColumn);
|
||||
|
||||
if (myDict->createTable(myTable) == -1)
|
||||
APIERROR(myDict->getNdbError());
|
||||
if (myDict->createTable(myTable) == -1) APIERROR(myDict->getNdbError());
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
|
||||
**************************************************************************/
|
||||
/**************************************************************************
|
||||
* Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
|
||||
**************************************************************************/
|
||||
static void do_insert(Ndb &myNdb)
|
||||
{
|
||||
for (int i = 0; i < 5; i++) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
NdbTransaction *myTransaction= myNdb.startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->insertTuple();
|
||||
myOperation->equal("ATTR1", i);
|
||||
myOperation->setValue("ATTR2", i);
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myOperation= myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->insertTuple();
|
||||
myOperation->equal("ATTR1", i+5);
|
||||
myOperation->setValue("ATTR2", i+5);
|
||||
|
||||
if (myConnection->execute( Commit ) == -1)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if (myTransaction->execute( Commit ) == -1)
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb.closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
* Update the second attribute in half of the tuples (adding 10) *
|
||||
*****************************************************************/
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
* Update the second attribute in half of the tuples (adding 10) *
|
||||
*****************************************************************/
|
||||
static void do_update(Ndb &myNdb)
|
||||
{
|
||||
for (int i = 0; i < 10; i+=2) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
NdbTransaction *myTransaction= myNdb.startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->updateTuple();
|
||||
myOperation->equal( "ATTR1", i );
|
||||
myOperation->setValue( "ATTR2", i+10);
|
||||
|
||||
if( myConnection->execute( Commit ) == -1 )
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if( myTransaction->execute( Commit ) == -1 )
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb.closeTransaction(myTransaction);
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************
|
||||
* Delete one tuple (the one with primary key 3) *
|
||||
*************************************************/
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
/*************************************************
|
||||
* Delete one tuple (the one with primary key 3) *
|
||||
*************************************************/
|
||||
static void do_delete(Ndb &myNdb)
|
||||
{
|
||||
NdbTransaction *myTransaction= myNdb.startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->deleteTuple();
|
||||
myOperation->equal( "ATTR1", 3 );
|
||||
|
||||
if (myConnection->execute(Commit) == -1)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if (myTransaction->execute(Commit) == -1)
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
|
||||
/*****************************
|
||||
* Read and print all tuples *
|
||||
*****************************/
|
||||
myNdb.closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
/*****************************
|
||||
* Read and print all tuples *
|
||||
*****************************/
|
||||
static void do_read(Ndb &myNdb)
|
||||
{
|
||||
std::cout << "ATTR1 ATTR2" << std::endl;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
NdbTransaction *myTransaction= myNdb.startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb.getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
NdbOperation *myOperation= myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->readTuple();
|
||||
myOperation->equal("ATTR1", i);
|
||||
|
||||
NdbRecAttr *myRecAttr= myOperation->getValue("ATTR2", NULL);
|
||||
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myRecAttr = myOperation->getValue("ATTR2", NULL);
|
||||
if (myRecAttr == NULL) APIERROR(myConnection->getNdbError());
|
||||
|
||||
if(myConnection->execute( Commit ) == -1)
|
||||
if(myTransaction->execute( Commit ) == -1)
|
||||
if (i == 3) {
|
||||
std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
|
||||
} else {
|
||||
APIERROR(myConnection->getNdbError());
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
}
|
||||
|
||||
if (i != 3) {
|
||||
printf(" %2d %2d\n", i, myRecAttr->u_32_value());
|
||||
}
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb.closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
delete myNdb;
|
||||
delete cluster_connection;
|
||||
|
||||
ndb_end(0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,12 +7,12 @@ DEBUG =
|
|||
LFLAGS = -Wall
|
||||
INCLUDE_DIR = ../../include
|
||||
LIB_DIR = -L../../src/.libs \
|
||||
-L../../../libmysql/.libs \
|
||||
-L../../../libmysql_r/.libs \
|
||||
-L../../../mysys
|
||||
SYS_LIB =
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient -lmysys $(SYS_LIB) -o $(TARGET)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET)
|
||||
|
||||
$(TARGET).o: $(SRCS)
|
||||
$(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS)
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
<< error.code << ", msg: " << error.message << "." << std::endl; \
|
||||
exit(-1); }
|
||||
|
||||
static void callback(int result, NdbConnection* NdbObject, void* aObject);
|
||||
static void callback(int result, NdbTransaction* NdbObject, void* aObject);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -50,22 +50,36 @@ int main()
|
|||
exit(-1);
|
||||
}
|
||||
|
||||
Ndb* myNdb = new Ndb( cluster_connection,
|
||||
"TEST_DB_2" ); // Object representing the database
|
||||
|
||||
NdbConnection* myNdbConnection[2]; // For transactions
|
||||
NdbOperation* myNdbOperation; // For operations
|
||||
|
||||
/*******************************************
|
||||
* Initialize NDB and wait until its ready *
|
||||
*******************************************/
|
||||
if (myNdb->init(2) == -1) { // Want two parallel insert transactions
|
||||
APIERROR(myNdb->getNdbError());
|
||||
int r= cluster_connection->connect(5 /* retries */,
|
||||
3 /* delay between retries */,
|
||||
1 /* verbose */);
|
||||
if (r > 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed, possibly resolved with more retries.\n";
|
||||
exit(-1);
|
||||
}
|
||||
else if (r < 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (cluster_connection->wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs." << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (myNdb->waitUntilReady(30) != 0) {
|
||||
std::cout << "NDB was not ready within 30 secs." << std::endl;
|
||||
Ndb* myNdb = new Ndb( cluster_connection,
|
||||
"TEST_DB_2" ); // Object representing the database
|
||||
|
||||
NdbTransaction* myNdbTransaction[2]; // For transactions
|
||||
NdbOperation* myNdbOperation; // For operations
|
||||
|
||||
if (myNdb->init(2) == -1) { // Want two parallel insert transactions
|
||||
APIERROR(myNdb->getNdbError());
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
@ -73,19 +87,19 @@ int main()
|
|||
* Insert (we do two insert transactions in parallel) *
|
||||
******************************************************/
|
||||
for (int i = 0; i < 2; i++) {
|
||||
myNdbConnection[i] = myNdb->startTransaction();
|
||||
if (myNdbConnection[i] == NULL) APIERROR(myNdb->getNdbError());
|
||||
myNdbTransaction[i] = myNdb->startTransaction();
|
||||
if (myNdbTransaction[i] == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myNdbOperation = myNdbConnection[i]->getNdbOperation("MYTABLENAME");
|
||||
myNdbOperation = myNdbTransaction[i]->getNdbOperation("MYTABLENAME");
|
||||
// Error check. If error, then maybe table MYTABLENAME is not in database
|
||||
if (myNdbOperation == NULL) APIERROR(myNdbConnection[i]->getNdbError());
|
||||
if (myNdbOperation == NULL) APIERROR(myNdbTransaction[i]->getNdbError());
|
||||
|
||||
myNdbOperation->insertTuple();
|
||||
myNdbOperation->equal("ATTR1", 20 + i);
|
||||
myNdbOperation->setValue("ATTR2", 20 + i);
|
||||
|
||||
// Prepare transaction (the transaction is NOT yet sent to NDB)
|
||||
myNdbConnection[i]->executeAsynchPrepare(Commit, &callback, NULL);
|
||||
myNdbTransaction[i]->executeAsynchPrepare(Commit, &callback, NULL);
|
||||
}
|
||||
|
||||
// Send all transactions to NDB
|
||||
|
@ -96,7 +110,7 @@ int main()
|
|||
|
||||
// Close all transactions
|
||||
for (int i = 0; i < 2; i++)
|
||||
myNdb->closeTransaction(myNdbConnection[i]);
|
||||
myNdb->closeTransaction(myNdbTransaction[i]);
|
||||
|
||||
delete myNdb;
|
||||
delete cluster_connection;
|
||||
|
@ -110,12 +124,12 @@ int main()
|
|||
*
|
||||
* (This function must have three arguments:
|
||||
* - The result of the transaction,
|
||||
* - The NdbConnection object, and
|
||||
* - The NdbTransaction object, and
|
||||
* - A pointer to an arbitrary object.)
|
||||
*/
|
||||
|
||||
static void
|
||||
callback(int result, NdbConnection* myTrans, void* aObject)
|
||||
callback(int result, NdbTransaction* myTrans, void* aObject)
|
||||
{
|
||||
if (result == -1) {
|
||||
std::cout << "Poll error: " << std::endl;
|
||||
|
|
|
@ -7,12 +7,12 @@ DEBUG =
|
|||
LFLAGS = -Wall
|
||||
INCLUDE_DIR = ../../include
|
||||
LIB_DIR = -L../../src/.libs \
|
||||
-L../../../libmysql/.libs \
|
||||
-L../../../libmysql_r/.libs \
|
||||
-L../../../mysys
|
||||
SYS_LIB =
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient -lmysys $(SYS_LIB) -o $(TARGET)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET)
|
||||
|
||||
$(TARGET).o: $(SRCS)
|
||||
$(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS)
|
||||
|
|
|
@ -52,28 +52,28 @@
|
|||
}
|
||||
|
||||
//
|
||||
// CONERROR prints all error info regarding an NdbConnection
|
||||
// TRANSERROR prints all error info regarding an NdbTransaction
|
||||
//
|
||||
#define CONERROR(ndbConnection) \
|
||||
{ NdbError error = ndbConnection->getNdbError(); \
|
||||
std::cout << "CON ERROR: " << error.code << " " << error.message \
|
||||
#define TRANSERROR(ndbTransaction) \
|
||||
{ NdbError error = ndbTransaction->getNdbError(); \
|
||||
std::cout << "TRANS ERROR: " << error.code << " " << error.message \
|
||||
<< std::endl \
|
||||
<< " " << "Status: " << error.status \
|
||||
<< ", Classification: " << error.classification << std::endl \
|
||||
<< " " << "File: " << __FILE__ \
|
||||
<< " (Line: " << __LINE__ << ")" << std::endl \
|
||||
; \
|
||||
printTransactionError(ndbConnection); \
|
||||
printTransactionError(ndbTransaction); \
|
||||
}
|
||||
|
||||
void printTransactionError(NdbConnection *ndbConnection) {
|
||||
void printTransactionError(NdbTransaction *ndbTransaction) {
|
||||
const NdbOperation *ndbOp = NULL;
|
||||
int i=0;
|
||||
|
||||
/****************************************************************
|
||||
* Print NdbError object of every operations in the transaction *
|
||||
****************************************************************/
|
||||
while ((ndbOp = ndbConnection->getNextCompletedOperation(ndbOp)) != NULL) {
|
||||
while ((ndbOp = ndbTransaction->getNextCompletedOperation(ndbOp)) != NULL) {
|
||||
NdbError error = ndbOp->getNdbError();
|
||||
std::cout << " OPERATION " << i+1 << ": "
|
||||
<< error.code << " " << error.message << std::endl
|
||||
|
@ -87,14 +87,14 @@ void printTransactionError(NdbConnection *ndbConnection) {
|
|||
//
|
||||
// Example insert
|
||||
// @param myNdb Ndb object representing NDB Cluster
|
||||
// @param myConnection NdbConnection used for transaction
|
||||
// @param myTransaction NdbTransaction used for transaction
|
||||
// @param error NdbError object returned in case of errors
|
||||
// @return -1 in case of failures, 0 otherwise
|
||||
//
|
||||
int insert(int transactionId, NdbConnection* myConnection) {
|
||||
int insert(int transactionId, NdbTransaction* myTransaction) {
|
||||
NdbOperation *myOperation; // For other operations
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
myOperation = myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) return -1;
|
||||
|
||||
if (myOperation->insertTuple() ||
|
||||
|
@ -104,7 +104,7 @@ int insert(int transactionId, NdbConnection* myConnection) {
|
|||
exit(-1);
|
||||
}
|
||||
|
||||
return myConnection->execute(NoCommit);
|
||||
return myTransaction->execute(NoCommit);
|
||||
}
|
||||
|
||||
|
||||
|
@ -116,7 +116,7 @@ int insert(int transactionId, NdbConnection* myConnection) {
|
|||
int executeInsertTransaction(int transactionId, Ndb* myNdb) {
|
||||
int result = 0; // No result yet
|
||||
int noOfRetriesLeft = 10;
|
||||
NdbConnection *myConnection; // For other transactions
|
||||
NdbTransaction *myTransaction; // For other transactions
|
||||
NdbError ndberror;
|
||||
|
||||
while (noOfRetriesLeft > 0 && !result) {
|
||||
|
@ -124,16 +124,16 @@ int executeInsertTransaction(int transactionId, Ndb* myNdb) {
|
|||
/*********************************
|
||||
* Start and execute transaction *
|
||||
*********************************/
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) {
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) {
|
||||
APIERROR(myNdb->getNdbError());
|
||||
ndberror = myNdb->getNdbError();
|
||||
result = -1; // Failure
|
||||
} else if (insert(transactionId, myConnection) ||
|
||||
insert(10000+transactionId, myConnection) ||
|
||||
myConnection->execute(Commit)) {
|
||||
CONERROR(myConnection);
|
||||
ndberror = myConnection->getNdbError();
|
||||
} else if (insert(transactionId, myTransaction) ||
|
||||
insert(10000+transactionId, myTransaction) ||
|
||||
myTransaction->execute(Commit)) {
|
||||
TRANSERROR(myTransaction);
|
||||
ndberror = myTransaction->getNdbError();
|
||||
result = -1; // Failure
|
||||
} else {
|
||||
result = 1; // Success
|
||||
|
@ -164,8 +164,8 @@ int executeInsertTransaction(int transactionId, Ndb* myNdb) {
|
|||
/*********************
|
||||
* Close transaction *
|
||||
*********************/
|
||||
if (myConnection != NULL) {
|
||||
myNdb->closeTransaction(myConnection);
|
||||
if (myTransaction != NULL) {
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,6 +181,22 @@ int main()
|
|||
Ndb_cluster_connection *cluster_connection=
|
||||
new Ndb_cluster_connection(); // Object representing the cluster
|
||||
|
||||
int r= cluster_connection->connect(5 /* retries */,
|
||||
3 /* delay between retries */,
|
||||
1 /* verbose */);
|
||||
if (r > 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed, possibly resolved with more retries.\n";
|
||||
exit(-1);
|
||||
}
|
||||
else if (r < 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (cluster_connection->wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs." << std::endl;
|
||||
|
@ -190,19 +206,11 @@ int main()
|
|||
Ndb* myNdb = new Ndb( cluster_connection,
|
||||
"TEST_DB_1" ); // Object representing the database
|
||||
|
||||
/*******************************************
|
||||
* Initialize NDB and wait until its ready *
|
||||
*******************************************/
|
||||
if (myNdb->init() == -1) {
|
||||
APIERROR(myNdb->getNdbError());
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (myNdb->waitUntilReady(30) != 0) {
|
||||
std::cout << "NDB was not ready within 30 secs." << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/************************************
|
||||
* Execute some insert transactions *
|
||||
************************************/
|
||||
|
|
|
@ -7,12 +7,12 @@ DEBUG =
|
|||
LFLAGS = -Wall
|
||||
INCLUDE_DIR = ../../include
|
||||
LIB_DIR = -L../../src/.libs \
|
||||
-L../../../libmysql/.libs \
|
||||
-L../../../libmysql_r/.libs \
|
||||
-L../../../mysys
|
||||
SYS_LIB =
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient -lmysys $(SYS_LIB) -o $(TARGET)
|
||||
$(CXX) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET)
|
||||
|
||||
$(TARGET).o: $(SRCS)
|
||||
$(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS)
|
||||
|
|
|
@ -49,6 +49,22 @@ int main()
|
|||
Ndb_cluster_connection *cluster_connection=
|
||||
new Ndb_cluster_connection(); // Object representing the cluster
|
||||
|
||||
int r= cluster_connection->connect(5 /* retries */,
|
||||
3 /* delay between retries */,
|
||||
1 /* verbose */);
|
||||
if (r > 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed, possibly resolved with more retries.\n";
|
||||
exit(-1);
|
||||
}
|
||||
else if (r < 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (cluster_connection->wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs." << std::endl;
|
||||
|
@ -61,24 +77,16 @@ int main()
|
|||
NdbDictionary::Column myColumn;
|
||||
NdbDictionary::Index myIndex;
|
||||
|
||||
NdbConnection *myConnection; // For transactions
|
||||
NdbTransaction *myTransaction; // For transactions
|
||||
NdbOperation *myOperation; // For primary key operations
|
||||
NdbIndexOperation *myIndexOperation; // For index operations
|
||||
NdbRecAttr *myRecAttr; // Result of reading attribute value
|
||||
|
||||
/********************************************
|
||||
* Initialize NDB and wait until it's ready *
|
||||
********************************************/
|
||||
if (myNdb->init() == -1) {
|
||||
APIERROR(myNdb->getNdbError());
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (myNdb->waitUntilReady(30) != 0) {
|
||||
std::cout << "NDB was not ready within 30 secs." << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
/*********************************************************
|
||||
* Create a table named MYTABLENAME if it does not exist *
|
||||
*********************************************************/
|
||||
|
@ -130,27 +138,27 @@ int main()
|
|||
* Using 5 transactions, insert 10 tuples in table: (0,0),(1,1),...,(9,9) *
|
||||
**************************************************************************/
|
||||
for (int i = 0; i < 5; i++) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myOperation = myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->insertTuple();
|
||||
myOperation->equal("ATTR1", i);
|
||||
myOperation->setValue("ATTR2", i);
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myOperation = myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->insertTuple();
|
||||
myOperation->equal("ATTR1", i+5);
|
||||
myOperation->setValue("ATTR2", i+5);
|
||||
|
||||
if (myConnection->execute( Commit ) == -1)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if (myTransaction->execute( Commit ) == -1)
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
/*****************************************
|
||||
|
@ -159,63 +167,63 @@ int main()
|
|||
std::cout << "ATTR1 ATTR2" << std::endl;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myIndexOperation = myConnection->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
if (myIndexOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myIndexOperation->readTuple();
|
||||
myIndexOperation->equal("ATTR2", i);
|
||||
|
||||
myRecAttr = myIndexOperation->getValue("ATTR1", NULL);
|
||||
if (myRecAttr == NULL) APIERROR(myConnection->getNdbError());
|
||||
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
if(myConnection->execute( Commit ) != -1)
|
||||
if(myTransaction->execute( Commit ) != -1)
|
||||
printf(" %2d %2d\n", myRecAttr->u_32_value(), i);
|
||||
}
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
|
||||
/*****************************************************************
|
||||
* Update the second attribute in half of the tuples (adding 10) *
|
||||
*****************************************************************/
|
||||
for (int i = 0; i < 10; i+=2) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myIndexOperation = myConnection->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
if (myIndexOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
if (myIndexOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myIndexOperation->updateTuple();
|
||||
myIndexOperation->equal( "ATTR2", i );
|
||||
myIndexOperation->setValue( "ATTR2", i+10);
|
||||
|
||||
if( myConnection->execute( Commit ) == -1 )
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if( myTransaction->execute( Commit ) == -1 )
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
/*************************************************
|
||||
* Delete one tuple (the one with primary key 3) *
|
||||
*************************************************/
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myIndexOperation = myConnection->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
myIndexOperation = myTransaction->getNdbIndexOperation("MYINDEXNAME",
|
||||
"MYTABLENAME");
|
||||
if (myIndexOperation == NULL)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myIndexOperation->deleteTuple();
|
||||
myIndexOperation->equal( "ATTR2", 3 );
|
||||
|
||||
if (myConnection->execute(Commit) == -1)
|
||||
APIERROR(myConnection->getNdbError());
|
||||
if (myTransaction->execute(Commit) == -1)
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
|
||||
/*****************************
|
||||
* Read and print all tuples *
|
||||
|
@ -223,29 +231,29 @@ int main()
|
|||
std::cout << "ATTR1 ATTR2" << std::endl;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
myConnection = myNdb->startTransaction();
|
||||
if (myConnection == NULL) APIERROR(myNdb->getNdbError());
|
||||
myTransaction = myNdb->startTransaction();
|
||||
if (myTransaction == NULL) APIERROR(myNdb->getNdbError());
|
||||
|
||||
myOperation = myConnection->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myConnection->getNdbError());
|
||||
myOperation = myTransaction->getNdbOperation("MYTABLENAME");
|
||||
if (myOperation == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
myOperation->readTuple();
|
||||
myOperation->equal("ATTR1", i);
|
||||
|
||||
myRecAttr = myOperation->getValue("ATTR2", NULL);
|
||||
if (myRecAttr == NULL) APIERROR(myConnection->getNdbError());
|
||||
if (myRecAttr == NULL) APIERROR(myTransaction->getNdbError());
|
||||
|
||||
if(myConnection->execute( Commit ) == -1)
|
||||
if(myTransaction->execute( Commit ) == -1)
|
||||
if (i == 3) {
|
||||
std::cout << "Detected that deleted tuple doesn't exist!" << std::endl;
|
||||
} else {
|
||||
APIERROR(myConnection->getNdbError());
|
||||
APIERROR(myTransaction->getNdbError());
|
||||
}
|
||||
|
||||
if (i != 3) {
|
||||
printf(" %2d %2d\n", i, myRecAttr->u_32_value());
|
||||
}
|
||||
myNdb->closeTransaction(myConnection);
|
||||
myNdb->closeTransaction(myTransaction);
|
||||
}
|
||||
|
||||
/**************
|
||||
|
|
|
@ -9,12 +9,12 @@ LFLAGS = -Wall
|
|||
TOP_SRCDIR = ../../..
|
||||
INCLUDE_DIR = $(TOP_SRCDIR)/ndb/include
|
||||
LIB_DIR = -L$(TOP_SRCDIR)/ndb/src/.libs \
|
||||
-L$(TOP_SRCDIR)/libmysql/.libs \
|
||||
-L$(TOP_SRCDIR)/libmysql_r/.libs \
|
||||
-L$(TOP_SRCDIR)/mysys
|
||||
SYS_LIB =
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient -lmysys $(SYS_LIB) -o $(TARGET)
|
||||
$(CXX) $(CXXFLAGS) $(LFLAGS) $(LIB_DIR) $(OBJS) -lndbclient -lmysqlclient_r -lmysys -lz $(SYS_LIB) -o $(TARGET)
|
||||
|
||||
$(TARGET).o: $(SRCS)
|
||||
$(CXX) $(CFLAGS) -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/ndbapi $(SRCS)
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
*/
|
||||
|
||||
#include <NdbApi.hpp>
|
||||
#include <NdbEventOperation.hpp>
|
||||
|
||||
// Used for cout
|
||||
#include <stdio.h>
|
||||
|
@ -69,6 +68,22 @@ int main()
|
|||
Ndb_cluster_connection *cluster_connection=
|
||||
new Ndb_cluster_connection(); // Object representing the cluster
|
||||
|
||||
int r= cluster_connection->connect(5 /* retries */,
|
||||
3 /* delay between retries */,
|
||||
1 /* verbose */);
|
||||
if (r > 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed, possibly resolved with more retries.\n";
|
||||
exit(-1);
|
||||
}
|
||||
else if (r < 0)
|
||||
{
|
||||
std::cout
|
||||
<< "Cluster connect failed.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (cluster_connection->wait_until_ready(30,30))
|
||||
{
|
||||
std::cout << "Cluster was not ready within 30 secs." << std::endl;
|
||||
|
|
|
@ -122,8 +122,7 @@
|
|||
// 4. Attribute Actions
|
||||
MyRecAttr= MyOperation->getValue("ATTR2", NULL);
|
||||
@endcode
|
||||
For additional examples of this sort, see @ref ndbapi_example1.cpp and
|
||||
@ref ndbapi_example2.cpp.
|
||||
For additional examples of this sort, see @ref ndbapi_example1.cpp.
|
||||
|
||||
The second example uses an NdbIndexOperation:
|
||||
@code
|
||||
|
@ -428,10 +427,12 @@
|
|||
* @include ndbapi_example1.cpp
|
||||
*/
|
||||
|
||||
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
|
||||
/**
|
||||
* @page ndbapi_example2.cpp ndbapi_example2.cpp
|
||||
* @include ndbapi_example2.cpp
|
||||
*/
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @page ndbapi_example3.cpp ndbapi_example3.cpp
|
||||
|
|
|
@ -39,8 +39,7 @@ class NdbOperation;
|
|||
* ndbout << MyRecAttr->u_32_value();
|
||||
* @endcode
|
||||
* For more examples, see
|
||||
* @ref ndbapi_example1.cpp and
|
||||
* @ref ndbapi_example2.cpp.
|
||||
* @ref ndbapi_example1.cpp.
|
||||
*
|
||||
* @note The NdbRecAttr object is instantiated with its value when
|
||||
* NdbTransaction::execute is called. Before this, the value is
|
||||
|
|
Loading…
Add table
Reference in a new issue