MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndb_user_transaction6.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 //#define DEBUG_ON
20 
21 #include <string.h>
22 #include "userHandle.h"
23 #include "userInterface.h"
24 
25 #include "macros.h"
26 #include "ndb_schema.hpp"
27 #include "ndb_error.hpp"
28 
29 #include <NdbApi.hpp>
30 
31 
32 void
33 userCheckpoint(UserHandle *uh){
34 }
35 
36 inline
38 startTransaction(Ndb * pNDB, ServerId inServerId, const SubscriberNumber inNumber){
39 
40  const int keyDataLenBytes = sizeof(ServerId)+SUBSCRIBER_NUMBER_LENGTH;
41  const int keyDataLen_64Words = keyDataLenBytes >> 3;
42 
43  Uint64 keyDataBuf[keyDataLen_64Words+1]; // The "+1" is for rounding...
44 
45  char * keyDataBuf_charP = (char *)&keyDataBuf[0];
46  Uint32 * keyDataBuf_wo32P = (Uint32 *)&keyDataBuf[0];
47 
48  // Server Id comes first
49  keyDataBuf_wo32P[0] = inServerId;
50  // Then subscriber number
51  memcpy(&keyDataBuf_charP[sizeof(ServerId)], inNumber, SUBSCRIBER_NUMBER_LENGTH);
52 
53  return pNDB->startTransaction(0, keyDataBuf_charP, keyDataLenBytes);
54 }
55 
69 void
70 userTransaction_T1(UserHandle * uh,
71  SubscriberNumber number,
72  Location new_location,
73  ChangedBy changed_by,
74  ChangedTime changed_time){
75  Ndb * pNDB = uh->pNDB;
76 
77  DEBUG2("T1(%.*s):\n", SUBSCRIBER_NUMBER_LENGTH, number);
78 
79  int check;
80  NdbRecAttr * check2;
81 
82  NdbConnection * MyTransaction = pNDB->startTransaction();
83  if (MyTransaction != NULL) {
84  NdbOperation *MyOperation = MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
85  if (MyOperation != NULL) {
86  MyOperation->updateTuple();
87  MyOperation->equal(IND_SUBSCRIBER_NUMBER,
88  number);
89  MyOperation->setValue(IND_SUBSCRIBER_LOCATION,
90  (char *)&new_location);
91  MyOperation->setValue(IND_SUBSCRIBER_CHANGED_BY,
92  changed_by);
93  MyOperation->setValue(IND_SUBSCRIBER_CHANGED_TIME,
94  changed_time);
95  check = MyTransaction->execute( Commit );
96  if (check != -1) {
97  pNDB->closeTransaction(MyTransaction);
98  return;
99  } else {
100  CHECK_MINUS_ONE(check, "T1: Commit",
101  MyTransaction);
102  }//if
103  } else {
104  CHECK_NULL(MyOperation, "T1: getNdbOperation", MyTransaction);
105  }//if
106  } else {
107  error_handler("T1-1: startTranscation", pNDB->getNdbErrorString(), pNDB->getNdbError());
108  }//if
109 }
110 
125 void
126 userTransaction_T2(UserHandle * uh,
127  SubscriberNumber number,
128  Location * readLocation,
129  ChangedBy changed_by,
130  ChangedTime changed_time,
131  SubscriberName subscriberName){
132  Ndb * pNDB = uh->pNDB;
133 
134  DEBUG2("T2(%.*s):\n", SUBSCRIBER_NUMBER_LENGTH, number);
135 
136  int check;
137  NdbRecAttr * check2;
138 
139  NdbConnection * MyTransaction = pNDB->startTransaction();
140  if (MyTransaction == NULL)
141  error_handler("T2-1: startTransaction", pNDB->getNdbErrorString(), pNDB->getNdbError());
142 
143  NdbOperation *MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
144  CHECK_NULL(MyOperation, "T2: getNdbOperation",
145  MyTransaction);
146 
147  MyOperation->readTuple();
148  MyOperation->equal(IND_SUBSCRIBER_NUMBER,
149  number);
150  MyOperation->getValue(IND_SUBSCRIBER_LOCATION,
151  (char *)readLocation);
152  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY,
153  changed_by);
154  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,
155  changed_time);
156  MyOperation->getValue(IND_SUBSCRIBER_NAME,
157  subscriberName);
158  check = MyTransaction->execute( Commit );
159  CHECK_MINUS_ONE(check, "T2: Commit",
160  MyTransaction);
161  pNDB->closeTransaction(MyTransaction);
162 }
163 
181 void
182 userTransaction_T3(UserHandle * uh,
183  SubscriberNumber inNumber,
184  ServerId inServerId,
185  ServerBit inServerBit,
186  SessionDetails outSessionDetails,
187  BranchExecuted * outBranchExecuted){
188  Ndb * pNDB = uh->pNDB;
189 
190  char outChangedBy [sizeof(ChangedBy) +(4-(sizeof(ChangedBy) & 3))];
191  char outChangedTime [sizeof(ChangedTime)+(4-(sizeof(ChangedTime) & 3))];
192  Location outLocation;
193  GroupId groupId;
194  ActiveSessions sessions;
195  Permission permission;
196  SubscriberSuffix inSuffix;
197 
198  DEBUG3("T3(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId);
199 
200  int check;
201  NdbRecAttr * check2;
202 
203  NdbConnection * MyTransaction = startTransaction(pNDB, inServerId, inNumber);
204  if (MyTransaction == NULL)
205  error_handler("T3-1: startTranscation", pNDB->getNdbErrorString(), pNDB->getNdbError());
206 
207  NdbOperation *MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
208  CHECK_NULL(MyOperation, "T3-1: getNdbOperation",
209  MyTransaction);
210 
211  MyOperation->readTuple();
212  MyOperation->equal(IND_SUBSCRIBER_NUMBER,
213  inNumber);
214  MyOperation->getValue(IND_SUBSCRIBER_LOCATION,
215  (char *)&outLocation);
216  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY,
217  outChangedBy);
218  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,
219  outChangedTime);
220  MyOperation->getValue(IND_SUBSCRIBER_GROUP,
221  (char *)&groupId);
222  MyOperation->getValue(IND_SUBSCRIBER_SESSIONS,
223  (char *)&sessions);
224  check = MyTransaction->execute( NoCommit );
225  CHECK_MINUS_ONE(check, "T3-1: NoCommit",
226  MyTransaction);
227 
228  /* Operation 2 */
229 
230  MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE);
231  CHECK_NULL(MyOperation, "T3-2: getNdbOperation",
232  MyTransaction);
233 
234 
235  MyOperation->readTuple();
236  MyOperation->equal(IND_GROUP_ID,
237  (char*)&groupId);
238  MyOperation->getValue(IND_GROUP_ALLOW_READ,
239  (char *)&permission);
240  check = MyTransaction->execute( NoCommit );
241  CHECK_MINUS_ONE(check, "T3-2: NoCommit",
242  MyTransaction);
243 
244  if(((permission & inServerBit) == inServerBit) &&
245  ((sessions & inServerBit) == inServerBit)){
246 
247  memcpy(inSuffix,
248  &inNumber[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH], SUBSCRIBER_NUMBER_SUFFIX_LENGTH);
249  DEBUG2("reading(%.*s) - ", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, inSuffix);
250 
251  /* Operation 3 */
252  MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE);
253  CHECK_NULL(MyOperation, "T3-3: getNdbOperation",
254  MyTransaction);
255 
256  MyOperation->simpleRead();
257 
258  MyOperation->equal(IND_SESSION_SUBSCRIBER,
259  (char*)inNumber);
260  MyOperation->equal(IND_SESSION_SERVER,
261  (char*)&inServerId);
262  MyOperation->getValue(IND_SESSION_DATA,
263  (char *)outSessionDetails);
264  /* Operation 4 */
265  MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE);
266  CHECK_NULL(MyOperation, "T3-4: getNdbOperation",
267  MyTransaction);
268 
269  MyOperation->interpretedUpdateTuple();
270  MyOperation->equal(IND_SERVER_ID,
271  (char*)&inServerId);
272  MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX,
273  (char*)inSuffix);
274  MyOperation->incValue(IND_SERVER_READS, (uint32)1);
275  (* outBranchExecuted) = 1;
276  } else {
277  (* outBranchExecuted) = 0;
278  }
279  DEBUG("commit...");
280  check = MyTransaction->execute( Commit );
281  CHECK_MINUS_ONE(check, "T3: Commit",
282  MyTransaction);
283 
284  pNDB->closeTransaction(MyTransaction);
285 
286  DEBUG("done\n");
287 }
288 
289 
307 void
308 userTransaction_T4(UserHandle * uh,
309  SubscriberNumber inNumber,
310  ServerId inServerId,
311  ServerBit inServerBit,
312  SessionDetails inSessionDetails,
313  DoRollback inDoRollback,
314  BranchExecuted * outBranchExecuted){
315 
316  Ndb * pNDB = uh->pNDB;
317 
318  char outChangedBy [sizeof(ChangedBy) +(4-(sizeof(ChangedBy) & 3))];
319  char outChangedTime [sizeof(ChangedTime)+(4-(sizeof(ChangedTime) & 3))];
320  Location outLocation;
321  GroupId groupId;
322  ActiveSessions sessions;
323  Permission permission;
324  SubscriberSuffix inSuffix;
325 
326  DEBUG3("T4(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId);
327 
328  int check;
329  NdbRecAttr * check2;
330 
331  NdbConnection * MyTransaction = startTransaction(pNDB, inServerId, inNumber);
332  if (MyTransaction == NULL)
333  error_handler("T4-1: startTranscation", pNDB->getNdbErrorString(), pNDB->getNdbError());
334 
335  NdbOperation *MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
336  CHECK_NULL(MyOperation, "T4-1: getNdbOperation",
337  MyTransaction);
338 
339  MyOperation->interpretedUpdateTuple();
340  MyOperation->equal(IND_SUBSCRIBER_NUMBER,
341  inNumber);
342  MyOperation->getValue(IND_SUBSCRIBER_LOCATION,
343  (char *)&outLocation);
344  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY,
345  outChangedBy);
346  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,
347  outChangedTime);
348  MyOperation->getValue(IND_SUBSCRIBER_GROUP,
349  (char *)&groupId);
350  MyOperation->getValue(IND_SUBSCRIBER_SESSIONS,
351  (char *)&sessions);
352  MyOperation->incValue(IND_SUBSCRIBER_SESSIONS,
353  (uint32)inServerBit);
354  check = MyTransaction->execute( NoCommit );
355 
356  /* Operation 2 */
357 
358  MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE);
359  CHECK_NULL(MyOperation, "T4-2: getNdbOperation",
360  MyTransaction);
361 
362  MyOperation->readTuple();
363  MyOperation->equal(IND_GROUP_ID,
364  (char*)&groupId);
365  MyOperation->getValue(IND_GROUP_ALLOW_INSERT,
366  (char *)&permission);
367  check = MyTransaction->execute( NoCommit );
368  CHECK_MINUS_ONE(check, "T4-2: NoCommit",
369  MyTransaction);
370 
371  if(((permission & inServerBit) == inServerBit) &&
372  ((sessions & inServerBit) == 0)){
373 
374  memcpy(inSuffix,
375  &inNumber[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH], SUBSCRIBER_NUMBER_SUFFIX_LENGTH);
376 
377  DEBUG2("inserting(%.*s) - ", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, inSuffix);
378 
379  /* Operation 3 */
380 
381  MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE);
382  CHECK_NULL(MyOperation, "T4-3: getNdbOperation",
383  MyTransaction);
384 
385  MyOperation->insertTuple();
386  MyOperation->equal(IND_SESSION_SUBSCRIBER,
387  (char*)inNumber);
388  MyOperation->equal(IND_SESSION_SERVER,
389  (char*)&inServerId);
390  MyOperation->setValue(SESSION_DATA,
391  (char *)inSessionDetails);
392  /* Operation 4 */
393 
394  /* Operation 5 */
395  MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE);
396  CHECK_NULL(MyOperation, "T4-5: getNdbOperation",
397  MyTransaction);
398 
399  MyOperation->interpretedUpdateTuple();
400  MyOperation->equal(IND_SERVER_ID,
401  (char*)&inServerId);
402  MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX,
403  (char*)inSuffix);
404  MyOperation->incValue(IND_SERVER_INSERTS, (uint32)1);
405  (* outBranchExecuted) = 1;
406  } else {
407  (* outBranchExecuted) = 0;
408  DEBUG1("%s", ((permission & inServerBit) ? "permission - " : "no permission - "));
409  DEBUG1("%s", ((sessions & inServerBit) ? "in session - " : "no in session - "));
410  }
411 
412  if(!inDoRollback && (* outBranchExecuted)){
413  DEBUG("commit\n");
414  check = MyTransaction->execute( Commit );
415  CHECK_MINUS_ONE(check, "T4: Commit",
416  MyTransaction);
417  } else {
418  DEBUG("rollback\n");
419  check = MyTransaction->execute(Rollback);
420  CHECK_MINUS_ONE(check, "T4:Rollback",
421  MyTransaction);
422 
423  }
424 
425  pNDB->closeTransaction(MyTransaction);
426 }
427 
428 
445 void
446 userTransaction_T5(UserHandle * uh,
447  SubscriberNumber inNumber,
448  ServerId inServerId,
449  ServerBit inServerBit,
450  DoRollback inDoRollback,
451  BranchExecuted * outBranchExecuted){
452  Ndb * pNDB = uh->pNDB;
453 
454  DEBUG3("T5(%.*s, %.2d): ", SUBSCRIBER_NUMBER_LENGTH, inNumber, inServerId);
455 
456  NdbConnection * MyTransaction = 0;
457  NdbOperation * MyOperation = 0;
458 
459  char outChangedBy [sizeof(ChangedBy) +(4-(sizeof(ChangedBy) & 3))];
460  char outChangedTime [sizeof(ChangedTime)+(4-(sizeof(ChangedTime) & 3))];
461  Location outLocation;
462  GroupId groupId;
463  ActiveSessions sessions;
464  Permission permission;
465  SubscriberSuffix inSuffix;
466 
467  int check;
468  NdbRecAttr * check2;
469 
470  MyTransaction = pNDB->startTransaction();
471  if (MyTransaction == NULL)
472  error_handler("T5-1: startTranscation", pNDB->getNdbErrorString(), pNDB->getNdbError());
473 
474  MyOperation= MyTransaction->getNdbOperation(SUBSCRIBER_TABLE);
475  CHECK_NULL(MyOperation, "T5-1: getNdbOperation",
476  MyTransaction);
477 
478  MyOperation->interpretedUpdateTuple();
479  MyOperation->equal(IND_SUBSCRIBER_NUMBER,
480  inNumber);
481  MyOperation->getValue(IND_SUBSCRIBER_LOCATION,
482  (char *)&outLocation);
483  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_BY,
484  &outChangedBy[0]);
485  MyOperation->getValue(IND_SUBSCRIBER_CHANGED_TIME,
486  &outChangedTime[0]);
487  MyOperation->getValue(IND_SUBSCRIBER_GROUP,
488  (char *)&groupId);
489  MyOperation->getValue(IND_SUBSCRIBER_SESSIONS,
490  (char *)&sessions);
491  MyOperation->subValue(IND_SUBSCRIBER_SESSIONS,
492  (uint32)inServerBit);
493  MyTransaction->execute( NoCommit );
494  /* Operation 2 */
495 
496  MyOperation = MyTransaction->getNdbOperation(GROUP_TABLE);
497  CHECK_NULL(MyOperation, "T5-2: getNdbOperation",
498  MyTransaction);
499 
500  MyOperation->readTuple();
501  MyOperation->equal(IND_GROUP_ID,
502  (char*)&groupId);
503  MyOperation->getValue(IND_GROUP_ALLOW_DELETE,
504  (char *)&permission);
505  check = MyTransaction->execute( NoCommit );
506  CHECK_MINUS_ONE(check, "T5-2: NoCommit",
507  MyTransaction);
508 
509  if(((permission & inServerBit) == inServerBit) &&
510  ((sessions & inServerBit) == inServerBit)){
511 
512  memcpy(inSuffix,
513  &inNumber[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH], SUBSCRIBER_NUMBER_SUFFIX_LENGTH);
514 
515  DEBUG2("deleting(%.*s) - ", SUBSCRIBER_NUMBER_SUFFIX_LENGTH, inSuffix);
516 
517  /* Operation 3 */
518  MyOperation = MyTransaction->getNdbOperation(SESSION_TABLE);
519  CHECK_NULL(MyOperation, "T5-3: getNdbOperation",
520  MyTransaction);
521 
522  MyOperation->deleteTuple();
523  MyOperation->equal(IND_SESSION_SUBSCRIBER,
524  (char*)inNumber);
525  MyOperation->equal(IND_SESSION_SERVER,
526  (char*)&inServerId);
527  /* Operation 4 */
528 
529  /* Operation 5 */
530  MyOperation = MyTransaction->getNdbOperation(SERVER_TABLE);
531  CHECK_NULL(MyOperation, "T5-5: getNdbOperation",
532  MyTransaction);
533 
534 
535  MyOperation->interpretedUpdateTuple();
536  MyOperation->equal(IND_SERVER_ID,
537  (char*)&inServerId);
538  MyOperation->equal(IND_SERVER_SUBSCRIBER_SUFFIX,
539  (char*)inSuffix);
540  MyOperation->incValue(IND_SERVER_DELETES, (uint32)1);
541  (* outBranchExecuted) = 1;
542  } else {
543  (* outBranchExecuted) = 0;
544  DEBUG1("%s", ((permission & inServerBit) ? "permission - " : "no permission - "));
545  DEBUG1("%s", ((sessions & inServerBit) ? "in session - " : "no in session - "));
546  }
547 
548  if(!inDoRollback && (* outBranchExecuted)){
549  DEBUG("commit\n");
550  check = MyTransaction->execute( Commit );
551  CHECK_MINUS_ONE(check, "T5: Commit",
552  MyTransaction);
553  } else {
554  DEBUG("rollback\n");
555  check = MyTransaction->execute(Rollback);
556  CHECK_MINUS_ONE(check, "T5:Rollback",
557  MyTransaction);
558 
559  }
560 
561  pNDB->closeTransaction(MyTransaction);
562 }
563