ndb_user_transaction5.cpp

来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 770 行 · 第 1/2 页

CPP
770
字号
/* 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 *///#define DEBUG_ONextern "C" {#include "user_transaction.h"};#include "macros.h"#include "ndb_schema.hpp"#include "ndb_error.hpp"#include <time.h>#include <NdbApi.hpp>/** * Transaction 1 - T1  * * Update location and changed by/time on a subscriber * * Input:  *   SubscriberNumber, *   Location, *   ChangedBy, *   ChangedTime * * Output: */intT1(void * obj,   const SubscriberNumber number,    const Location new_location,    const ChangedBy changed_by,    const ChangedTime changed_time,   BenchmarkTime * transaction_time){  Ndb * pNDB = (Ndb *) obj;  DEBUG2("T1(%.*s):\n", SUBSCRIBER_NUMBER_LENGTH, number);  BenchmarkTime start;  get_time(&start);  int check;  NdbRecAttr * check2;  NdbConnection * MyTransaction = pNDB->startTransaction();  if (MyTransaction == NULL)	      error_handler("T1-1: startTranscation", pNDB->getNdbErrorString(), 0);  NdbOperation *MyOperation = MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);  CHECK_NULL(MyOperation, "T1: getNdbOperation", MyTransaction);    check = MyOperation->updateTuple();  CHECK_MINUS_ONE(check, "T1: updateTuple", 		  MyTransaction);    check = MyOperation->equal(IND_SUBSCRIBER_NUMBER, 			     number);  CHECK_MINUS_ONE(check, "T1: equal subscriber",		  MyTransaction);  check = MyOperation->setValue(IND_SUBSCRIBER_LOCATION, 				(char *)&new_location);  CHECK_MINUS_ONE(check, "T1: setValue location", 		  MyTransaction);  check = MyOperation->setValue(IND_SUBSCRIBER_CHANGED_BY, 				changed_by);  CHECK_MINUS_ONE(check, "T1: setValue changed_by", 		  MyTransaction);  check = MyOperation->setValue(IND_SUBSCRIBER_CHANGED_TIME, 				changed_time);  CHECK_MINUS_ONE(check, "T1: setValue changed_time", 		  MyTransaction);  check = MyTransaction->execute( Commit );   CHECK_MINUS_ONE(check, "T1: Commit", 		  MyTransaction);    pNDB->closeTransaction(MyTransaction);    get_time(transaction_time);  time_diff(transaction_time, &start);  return 0;}/** * Transaction 2 - T2 * * Read from Subscriber: * * Input:  *   SubscriberNumber * * Output: *   Location *   Changed by *   Changed Timestamp *   Name */intT2(void * obj,   const SubscriberNumber number,    Location * readLocation,    ChangedBy changed_by,    ChangedTime changed_time,   SubscriberName subscriberName,   BenchmarkTime * transaction_time){  Ndb * pNDB = (Ndb *) obj;  DEBUG2("T2(%.*s):\n", SUBSCRIBER_NUMBER_LENGTH, number);  BenchmarkTime start;  get_time(&start);  int check;  NdbRecAttr * check2;  NdbConnection * MyTransaction = pNDB->startTransaction();  if (MyTransaction == NULL)	      error_handler("T2-1: startTranscation", pNDB->getNdbErrorString(), 0);  NdbOperation *MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);  CHECK_NULL(MyOperation, "T2: getNdbOperation", 	     MyTransaction);      check = MyOperation->readTuple();  CHECK_MINUS_ONE(check, "T2: readTuple", 		  MyTransaction);    check = MyOperation->equal(IND_SUBSCRIBER_NUMBER, 			     number);  CHECK_MINUS_ONE(check, "T2: equal subscriber",		  MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_LOCATION, 				(char *)readLocation);  CHECK_NULL(check2, "T2: getValue location", 	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY, 				 changed_by);  CHECK_NULL(check2, "T2: getValue changed_by", 	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,                                  changed_time);  CHECK_NULL(check2, "T2: getValue changed_time",	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_NAME, 				subscriberName);  CHECK_NULL(check2, "T2: getValue name", 	     MyTransaction);  check = MyTransaction->execute( Commit );   CHECK_MINUS_ONE(check, "T2: Commit", 		  MyTransaction);    pNDB->closeTransaction(MyTransaction);    get_time(transaction_time);  time_diff(transaction_time, &start);  return 0;}/** * Transaction 3 - T3 * * Read session details * * Input: *   SubscriberNumber *   ServerId *   ServerBit * * Output: *   BranchExecuted *   SessionDetails *   ChangedBy *   ChangedTime *   Location */intT3(void * obj,   const SubscriberNumber   inNumber,   const SubscriberSuffix   inSuffix,   const ServerId           inServerId,   const ServerBit          inServerBit,   SessionDetails     outSessionDetails,   ChangedBy          outChangedBy,   ChangedTime        outChangedTime,   Location         * outLocation,   BranchExecuted   * outBranchExecuted,   BenchmarkTime    * outTransactionTime){    Ndb * pNDB = (Ndb *) obj;  GroupId        groupId;  ActiveSessions sessions;  Permission     permission;  BenchmarkTime start;  get_time(&start);  int check;  NdbRecAttr * check2;  NdbConnection * MyTransaction = pNDB->startTransaction();  if (MyTransaction == NULL)	      error_handler("T3-1: startTranscation", pNDB->getNdbErrorString(), 0);  NdbOperation *MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);  CHECK_NULL(MyOperation, "T3-1: getNdbOperation", 	     MyTransaction);      check = MyOperation->readTuple();  CHECK_MINUS_ONE(check, "T3-1: readTuple", 		  MyTransaction);    check = MyOperation->equal(IND_SUBSCRIBER_NUMBER, 			     inNumber);  CHECK_MINUS_ONE(check, "T3-1: equal subscriber",		  MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_LOCATION, 				 (char *)outLocation);  CHECK_NULL(check2, "T3-1: getValue location", 	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY, 				 outChangedBy);  CHECK_NULL(check2, "T3-1: getValue changed_by", 	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,                                  outChangedTime);  CHECK_NULL(check2, "T3-1: getValue changed_time",	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_GROUP,				 (char *)&groupId);  CHECK_NULL(check2, "T3-1: getValue group", 	     MyTransaction);  check2 = MyOperation->getValue(IND_SUBSCRIBER_SESSIONS,				 (char *)&sessions);  CHECK_NULL(check2, "T3-1: getValue sessions", 	     MyTransaction);    check = MyTransaction->execute( NoCommit );   CHECK_MINUS_ONE(check, "T3-1: NoCommit", 		  MyTransaction);      /* Operation 2 */  MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE);  CHECK_NULL(MyOperation, "T3-2: getNdbOperation", 	     MyTransaction);      check = MyOperation->readTuple();  CHECK_MINUS_ONE(check, "T3-2: readTuple", 		  MyTransaction);    check = MyOperation->equal(IND_GROUP_ID,		     (char*)&groupId);  CHECK_MINUS_ONE(check, "T3-2: equal group",		  MyTransaction);    check2 = MyOperation->getValue(IND_GROUP_ALLOW_READ, 				 (char *)&permission);  CHECK_NULL(check2, "T3-2: getValue allow_read", 	     MyTransaction);  check = MyTransaction->execute( NoCommit );   CHECK_MINUS_ONE(check, "T3-2: NoCommit", 		  MyTransaction);    DEBUG3("T3(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId);  if(((permission & inServerBit) == inServerBit) &&     ((sessions   & inServerBit) == inServerBit)){        DEBUG("reading - ");    /* Operation 3 */    MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE);    CHECK_NULL(MyOperation, "T3-3: getNdbOperation", 	       MyTransaction);        check = MyOperation->simpleRead();    CHECK_MINUS_ONE(check, "T3-3: readTuple", 		    MyTransaction);        check = MyOperation->equal(IND_SESSION_SUBSCRIBER,			       (char*)inNumber);    CHECK_MINUS_ONE(check, "T3-3: equal number",		    MyTransaction);    check = MyOperation->equal(IND_SESSION_SERVER,			       (char*)&inServerId);    CHECK_MINUS_ONE(check, "T3-3: equal server id",		    MyTransaction);        check2 = MyOperation->getValue(IND_SESSION_DATA, 				   (char *)outSessionDetails);    CHECK_NULL(check2, "T3-3: getValue session details", 	       MyTransaction);        /* Operation 4 */    MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE);    CHECK_NULL(MyOperation, "T3-4: getNdbOperation", 	       MyTransaction);        check = MyOperation->interpretedUpdateTuple();    CHECK_MINUS_ONE(check, "T3-4: interpretedUpdateTuple", 		    MyTransaction);        check = MyOperation->equal(IND_SERVER_ID,			       (char*)&inServerId);    CHECK_MINUS_ONE(check, "T3-4: equal serverId",		    MyTransaction);    check = MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX,			       (char*)inSuffix);    CHECK_MINUS_ONE(check, "T3-4: equal suffix",		    MyTransaction);    check = MyOperation->incValue(IND_SERVER_READS, (uint32)1);    CHECK_MINUS_ONE(check, "T3-4: inc value",		    MyTransaction);    (* outBranchExecuted) = 1;  } else {    (* outBranchExecuted) = 0;  }  DEBUG("commit...");  check = MyTransaction->execute( Commit );   CHECK_MINUS_ONE(check, "T3: Commit", 		  MyTransaction);    pNDB->closeTransaction(MyTransaction);    DEBUG("done\n");  get_time(outTransactionTime);  time_diff(outTransactionTime, &start);  return 0;}/** * Transaction 4 - T4 *  * Create session * * Input: *   SubscriberNumber *   ServerId *   ServerBit *   SessionDetails, *   DoRollback * Output: *   ChangedBy *   ChangedTime *   Location *   BranchExecuted */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?