cellodb.cpp
来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 1,505 行 · 第 1/3 页
CPP
1,505 行
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *************************************************** BASIC TEST 1 Test basic functions and status of NDB Arguments: none Returns: 0 - Test passed -1 - Test failed 1 - Invalid argumentsflexBench * *************************************************** */#include <NdbApi.hpp>#include <NdbMain.h>#define MAXATTR 4#define MAXTABLES 4#define PAGESIZE 8192#define OVERHEAD 0.02#define NUMBEROFRECORDS 10#define PKSIZE 1#define ATTRNAMELEN 16static void createTable_IPACCT(Ndb*);static void createTable_RPACCT(Ndb*);static void createTable_SBMCALL(Ndb*);static void createTable_TODACCT(Ndb*);static void error_handler(const char*);static bool error_handler2(const char*, int) ;static void setAttrNames(void);static void setTableNames(void);static void create_table(Ndb*);static void insert_rows(Ndb*);static void update_rows(Ndb*);static void delete_rows(Ndb*);static void verify_deleted(Ndb*);static void read_and_verify_rows(Ndb*);static void insert_IPACCT(Ndb*, Uint32, Uint32, Uint32, Uint32, Uint32); //3 for Pk, and two data. just to test;static void read_IPACCT(Ndb* , Uint32 , Uint32 , Uint32 );static int tAttributeSize;static int bTestPassed;static char tableName[MAXTABLES][ATTRNAMELEN];static char attrName[MAXATTR][ATTRNAMELEN];static int attrValue[NUMBEROFRECORDS];static int pkValue[NUMBEROFRECORDS];static int failed = 0 ;#include <NdbOut.hpp>NDB_COMMAND(celloDb, "celloDb", "celloDb", "celloDb", 65535){ ndb_init(); int tTableId; int i; Ndb MyNdb( "CELLO-SESSION-DB" ); MyNdb.init(); // Assume test passed bTestPassed = 0; /* // Initialize global variables for (i = 0; i < NUMBEROFRECORDS; i ++) pkValue[i] = i; for (i = 0; i < NUMBEROFRECORDS; i ++) attrValue[i] = i; */ tAttributeSize = 1; // Wait for Ndb to become ready if (MyNdb.waitUntilReady() == 0) { ndbout << endl << "Cello session db - Starting " << endl; ndbout << "Test basic functions and status of NDB" << endl; createTable_SBMCALL (&MyNdb ); createTable_RPACCT (&MyNdb ); createTable_TODACCT (&MyNdb ); createTable_IPACCT (&MyNdb ); insert_IPACCT(&MyNdb, 1,2,1,2,2); read_IPACCT(&MyNdb, 1, 2 , 1); /* insert_rows(&MyNdb); read_and_verify_rows(&MyNdb); // Create some new values to use for update for (i = 0; i < NUMBEROFRECORDS; i++) attrValue[i] = NUMBEROFRECORDS-i; update_rows(&MyNdb); read_and_verify_rows(&MyNdb); delete_rows(&MyNdb); verify_deleted(&MyNdb); */ } else { bTestPassed = -1; } if (bTestPassed == 0) { // Test passed ndbout << "OK - Test passed" << endl; } else { // Test failed ndbout << "FAIL - Test failed" << endl; exit(-1); } return NULL;}static void error_handler(const char* errorText){ // Test failed ndbout << endl << "ErrorMessage: " << errorText << endl; bTestPassed = -1;}static voidcreateTable_SBMCALL ( Ndb* pMyNdb ){ /**************************************************************** * Create table and attributes. * * create table SBMCALL( * for attribs, see the REQ SPEC for cello session DB * ) * ***************************************************************/ const char* tname = "SBMCALL"; Uint32 recordsize = 244; //including 12 byte overhead Uint32 pksize = 8; //size of total prim. key. in bytes. sum of entire composite PK. Uint32 tTableId = pMyNdb->getTable()->openTable(tname); if (tTableId == -1) { Uint32 check; Uint32 i; NdbSchemaCon *MySchemaTransaction; NdbSchemaOp *MySchemaOp; ndbout << "Creating " << tname << "..." << endl; MySchemaTransaction = pMyNdb->startSchemaTransaction(); if( ( MySchemaTransaction == NULL ) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; MySchemaOp = MySchemaTransaction->getNdbSchemaOp(); if( ( MySchemaOp == NULL ) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Createtable Uint32 tablesize=(recordsize*NUMBEROFRECORDS + OVERHEAD * NUMBEROFRECORDS)/1024; Uint32 noPages=(pksize*NUMBEROFRECORDS)/PAGESIZE; ndbout << "table size " << tablesize << "for table name " << tname << endl; check = MySchemaOp->createTable( tname, tablesize, // Table Size TupleKey, // Key Type noPages, // Nr of Pages All, 6, 78, 80, 1, true ); if( check == -1 ) { error_handler(MySchemaTransaction->getNdbErrorString()); exit(-1); } // Create first column, primary key check = MySchemaOp->createAttribute( "SPBBOARDID", TupleKey, 32, PKSIZE, UnSigned, MMBased, NotNullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create second column, primary key check = MySchemaOp->createAttribute( "CALLID", TupleKey, 32, PKSIZE, UnSigned, MMBased, NotNullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Creat thrid column, RP Session info, byte[16] represented as (String, 16) check = MySchemaOp->createAttribute( "RPSESS", NoKey, 32, 16, String, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Creat fourth column, GRE Tunnel info, byte[16] represented as (String, 16) check = MySchemaOp->createAttribute( "GRETUNNEL", NoKey, 32, 16, String, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Creat fifth column, PPP Session info, byte[24] represented as (String, 24) check = MySchemaOp->createAttribute( "PPPSESS", NoKey, 32, 24, String, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; if( (MySchemaTransaction->execute() == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; pMyNdb->closeSchemaTransaction(MySchemaTransaction); ndbout << "done" << endl; } //if //else table already created , proceed}static voidcreateTable_RPACCT(Ndb*pMyNdb){ /**************************************************************** * Create table and attributes. * * create table RPACCT( * for attribs, see the REQ SPEC for cello session DB * ) * ***************************************************************/ const char* tname = "RPACCT"; Uint32 recordsize = 380; //including 12 byte overhead Uint32 pksize = 8; //size of total prim. key. in bytes. Uint32 tTableId = pMyNdb->getTable()->openTable(tname); if (tTableId == -1) { Uint32 check; Uint32 i; NdbSchemaCon *MySchemaTransaction; NdbSchemaOp *MySchemaOp; ndbout << "Creating " << tname << "..." << endl; MySchemaTransaction = pMyNdb->startSchemaTransaction(); if( MySchemaTransaction == NULL ) error_handler(MySchemaTransaction->getNdbErrorString()); MySchemaOp = MySchemaTransaction->getNdbSchemaOp(); if( MySchemaOp == NULL ) error_handler(MySchemaTransaction->getNdbErrorString()); // Createtable Uint32 tablesize=(recordsize*NUMBEROFRECORDS + OVERHEAD * NUMBEROFRECORDS)/1024; Uint32 noPages=(pksize*NUMBEROFRECORDS)/PAGESIZE; ndbout << "table size " << tablesize << "for table name " << tname << endl; check = MySchemaOp->createTable( tname, tablesize, // Table Size TupleKey, // Key Type noPages // Nr of Pages ); if( check == -1 ) error_handler(MySchemaTransaction->getNdbErrorString()); // Create first column, primary key check = MySchemaOp->createAttribute( "SPBBOARDID", TupleKey, 32, PKSIZE, UnSigned, MMBased, NotNullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create second column, primary key check = MySchemaOp->createAttribute( "CALLID", TupleKey, 32, PKSIZE, UnSigned, MMBased, NotNullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Creat thrid column MS ID, 4 byte unsigned check = MySchemaOp->createAttribute( "MSID", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create PDSN FA Address, 4 byte unsigned check = MySchemaOp->createAttribute( "PDSN", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Serving PCF, 4 byte unsigned check = MySchemaOp->createAttribute( "SPCF", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create BS ID, 12 byte char, represented as String,12 check = MySchemaOp->createAttribute( "BSID", NoKey, 32, 12, String, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create User Zone, 4 byte unsigned check = MySchemaOp->createAttribute( "UZ", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Forward Multiplex, 4 byte unsigned check = MySchemaOp->createAttribute( "FMO", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Reverse Multiplex, 4 byte unsigned check = MySchemaOp->createAttribute( "RMO", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Forward Fund rate, 4 byte unsigned check = MySchemaOp->createAttribute( "FFR", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Reverse Fund rate, 4 byte unsigned check = MySchemaOp->createAttribute( "RFR", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) ) exit (-1) ; // Create Service Option, 4 byte unsigned check = MySchemaOp->createAttribute( "SO", NoKey, 32, 1, UnSigned, MMBased, NullAttribute ); if( (check == -1) && (!error_handler2((const char*)MySchemaTransaction->getNdbErrorString(), MySchemaTransaction->getNdbError())) )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?