#include #include #include // Needed for apps accessing Cluster data #include #define PRINT_ERROR(code,msg) \ std::cout << "Error in " << __FILE__ << ", line: " << __LINE__ \ << ", code: " << code \ << ", msg: " << msg << "." << std::endl #define APIERROR(error) { \ PRINT_ERROR(error.code,error.message); \ exit(-1); } static void do_read(Ndb *); int main() { int x; Ndb_cluster_connection *c; // Object for the cluster connection int retries, delay, verbose; // connect() parameters int timeoutBefore, timeoutAfter; // wait_until_ready() parameters //-------------------------------------------------------------------------- // The NDB API must be initialised before use //-------------------------------------------------------------------------- if (ndb_init()) return(EXIT_FAILURE); //-------------------------------------------------------------------------- // Attempt to connect to the cluster. Without a connection string parameter // to the Ndb_cluster_connection constructor, we connect to localhost. //-------------------------------------------------------------------------- c = new Ndb_cluster_connection(); retries = 4; // times to retry the connection if it failed delay = 5; // seconds between recoonect attempts verbose = 1; // enable reporting (1) or not (0) if (c->connect(retries, delay, verbose)) { fprintf(stderr, "Unable to connect to cluster within 30 seconds.\n\n"); return(EXIT_FAILURE); } //-------------------------------------------------------------------------- // Wait until the requested connection with one or more data nodes succeeds //-------------------------------------------------------------------------- timeoutBefore = 30; // seconds to wait until 1st "live" node is detected timeoutAfter = 0; // seconds to wait after 1st "live" node is detected // for *all* nodes to become active if (c->wait_until_ready(timeoutBefore, timeoutAfter) < 0) { fprintf(stderr, "Cluster was not ready within 30 seconds.\n\n"); return(EXIT_FAILURE); } printf("Connection Established.\n\n"); Ndb *myNdb = new Ndb(c, "test"); if (myNdb->init()) APIERROR(myNdb->getNdbError()); x = 0; while (x++ < 10000) { printf("iteration %d\n", x); do_read(myNdb); // output all tuples } delete c; ndb_end(0); return(EXIT_SUCCESS); } /* Read contents of test.a by doing a table scan CREATE TABLE a (col1 INT NOT NULL PRIMARY KEY, col2 INT) ENGINE=NDB; INSERT INTO a VALUES (1,10), (2,20), (3,30); */ static void do_read(Ndb *myNdb) { int check; const NdbDictionary::Dictionary *myDict = myNdb->getDictionary(); const NdbDictionary::Table *myTable = myDict->getTable("a"); if (myTable == NULL) APIERROR(myDict->getNdbError()); NdbTransaction *myTransaction = myNdb->startTransaction(); NdbScanOperation *myScanOp = myTransaction->getNdbScanOperation(myTable); if (myScanOp == NULL) APIERROR(myTransaction->getNdbError()); if (myScanOp->readTuples(NdbOperation::LM_CommittedRead) == -1) APIERROR(myTransaction->getNdbError()); NdbRecAttr *myRecAttr[2]; myRecAttr[0] = myScanOp->getValue("col1"); myRecAttr[1] = myScanOp->getValue("col2"); if (myRecAttr[0] == NULL || myRecAttr[1] == NULL) APIERROR(myTransaction->getNdbError()); // Start scan if (myTransaction->execute(NdbTransaction::NoCommit) != 0) APIERROR(myTransaction->getNdbError()); /* printf("col1 col2\n"); printf("---- ----\n"); */ int i=0; while ((check = myScanOp->nextResult(true)) == 0) { do { i++; /* printf("%4d %4d\n", myRecAttr[0]->int32_value(), myRecAttr[1]->int32_value()); */ } while ((check = myScanOp->nextResult(false)) == 0); } // printf("\n"); // Here is the unnecessary call to nextResult() check = myScanOp->nextResult(true); if (myTransaction->execute( NdbTransaction::Commit ) == -1) APIERROR(myTransaction->getNdbError()); myNdb->closeTransaction(myTransaction); }