mirror of
https://github.com/MariaDB/server.git
synced 2025-08-31 22:22:30 +03:00
updated ndbapi examples
removed references to example2 (asynch api)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user