DbtupTrigger.cpp 41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* 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 DBTUP_C
#include "Dbtup.hpp"
#include <RefConvert.hpp>
#include <ndb_limits.h>
#include <pc.hpp>
#include <AttributeDescriptor.hpp>
#include "AttributeOffset.hpp"
#include <AttributeHeader.hpp>
#include <signaldata/FireTrigOrd.hpp>
#include <signaldata/CreateTrig.hpp>
#include <signaldata/TuxMaint.hpp>

#define ljam() { jamLine(7000 + __LINE__); }
#define ljamEntry() { jamEntryLine(7000 + __LINE__); }

/* **************************************************************** */
/* ---------------------------------------------------------------- */
/* ----------------------- TRIGGER HANDLING ----------------------- */
/* ---------------------------------------------------------------- */
/* **************************************************************** */

39
DLList<Dbtup::TupTriggerData>*
40 41 42 43 44
Dbtup::findTriggerList(Tablerec* table,
                       TriggerType::Value ttype,
                       TriggerActionTime::Value ttime,
                       TriggerEvent::Value tevent)
{
45
  DLList<TupTriggerData>* tlist = NULL;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  switch (ttype) {
  case TriggerType::SUBSCRIPTION:
  case TriggerType::SUBSCRIPTION_BEFORE:
    switch (tevent) {
    case TriggerEvent::TE_INSERT:
      ljam();
      if (ttime == TriggerActionTime::TA_DETACHED)
        tlist = &table->subscriptionInsertTriggers;
      break;
    case TriggerEvent::TE_UPDATE:
      ljam();
      if (ttime == TriggerActionTime::TA_DETACHED)
        tlist = &table->subscriptionUpdateTriggers;
      break;
    case TriggerEvent::TE_DELETE:
      ljam();
      if (ttime == TriggerActionTime::TA_DETACHED)
        tlist = &table->subscriptionDeleteTriggers;
      break;
    default:
      break;
    }
    break;
  case TriggerType::SECONDARY_INDEX:
    switch (tevent) {
    case TriggerEvent::TE_INSERT:
      ljam();
      if (ttime == TriggerActionTime::TA_AFTER)
        tlist = &table->afterInsertTriggers;
      break;
    case TriggerEvent::TE_UPDATE:
      ljam();
      if (ttime == TriggerActionTime::TA_AFTER)
        tlist = &table->afterUpdateTriggers;
      break;
    case TriggerEvent::TE_DELETE:
      ljam();
      if (ttime == TriggerActionTime::TA_AFTER)
        tlist = &table->afterDeleteTriggers;
      break;
    default:
      break;
    }
    break;
  case TriggerType::ORDERED_INDEX:
    switch (tevent) {
    case TriggerEvent::TE_CUSTOM:
      ljam();
      if (ttime == TriggerActionTime::TA_CUSTOM)
        tlist = &table->tuxCustomTriggers;
      break;
    default:
      break;
    }
    break;
  case TriggerType::READ_ONLY_CONSTRAINT:
    switch (tevent) {
    case TriggerEvent::TE_UPDATE:
      ljam();
      if (ttime == TriggerActionTime::TA_AFTER)
        tlist = &table->constraintUpdateTriggers;
      break;
    default:
      break;
    }
    break;
  default:
    break;
  }
  return tlist;
}

// Trigger signals
void
Dbtup::execCREATE_TRIG_REQ(Signal* signal)
{
  ljamEntry();
  BlockReference senderRef = signal->getSendersBlockRef();
  const CreateTrigReq reqCopy = *(const CreateTrigReq*)signal->getDataPtr();
  const CreateTrigReq* const req = &reqCopy;
unknown's avatar
unknown committed
126
  CreateTrigRef::ErrorCode error= CreateTrigRef::NoError;
127 128 129 130 131 132

  // Find table
  TablerecPtr tabPtr;
  tabPtr.i = req->getTableId();
  ptrCheckGuard(tabPtr, cnoOfTablerec, tablerec);

unknown's avatar
unknown committed
133 134 135 136 137
  if (tabPtr.p->tableStatus != DEFINED )
  {
    ljam();
    error= CreateTrigRef::InvalidTable;
  }
138
  // Create trigger and associate it with the table
unknown's avatar
unknown committed
139 140
  else if (createTrigger(tabPtr.p, req))
  {
141 142 143 144 145 146 147 148 149 150 151 152
    ljam();
    // Send conf
    CreateTrigConf* const conf = (CreateTrigConf*)signal->getDataPtrSend();
    conf->setUserRef(reference());
    conf->setConnectionPtr(req->getConnectionPtr());
    conf->setRequestType(req->getRequestType());
    conf->setTableId(req->getTableId());
    conf->setIndexId(req->getIndexId());
    conf->setTriggerId(req->getTriggerId());
    conf->setTriggerInfo(req->getTriggerInfo());
    sendSignal(senderRef, GSN_CREATE_TRIG_CONF, 
               signal, CreateTrigConf::SignalLength, JBB);
unknown's avatar
unknown committed
153 154 155 156
    return;
  }
  else
  {
157
    ljam();
unknown's avatar
unknown committed
158
    error= CreateTrigRef::TooManyTriggers;
159
  }
unknown's avatar
unknown committed
160 161 162 163 164 165 166 167 168 169 170 171 172
  ndbassert(error != CreateTrigRef::NoError);
  // Send ref
  CreateTrigRef* const ref = (CreateTrigRef*)signal->getDataPtrSend();
  ref->setUserRef(reference());
  ref->setConnectionPtr(req->getConnectionPtr());
  ref->setRequestType(req->getRequestType());
  ref->setTableId(req->getTableId());
  ref->setIndexId(req->getIndexId());
  ref->setTriggerId(req->getTriggerId());
  ref->setTriggerInfo(req->getTriggerInfo());
  ref->setErrorCode(error);
  sendSignal(senderRef, GSN_CREATE_TRIG_REF, 
	     signal, CreateTrigRef::SignalLength, JBB);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}//Dbtup::execCREATE_TRIG_REQ()

void
Dbtup::execDROP_TRIG_REQ(Signal* signal)
{
  ljamEntry();
  BlockReference senderRef = signal->getSendersBlockRef();
  const DropTrigReq reqCopy = *(const DropTrigReq*)signal->getDataPtr();
  const DropTrigReq* const req = &reqCopy;

  // Find table
  TablerecPtr tabPtr;
  tabPtr.i = req->getTableId();
  ptrCheckGuard(tabPtr, cnoOfTablerec, tablerec);

  // Drop trigger
189
  Uint32 r = dropTrigger(tabPtr.p, req, refToBlock(senderRef));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  if (r == 0){
    // Send conf
    DropTrigConf* const conf = (DropTrigConf*)signal->getDataPtrSend();
    conf->setUserRef(senderRef);
    conf->setConnectionPtr(req->getConnectionPtr());
    conf->setRequestType(req->getRequestType());
    conf->setTableId(req->getTableId());
    conf->setIndexId(req->getIndexId());
    conf->setTriggerId(req->getTriggerId());
    sendSignal(senderRef, GSN_DROP_TRIG_CONF, 
	       signal, DropTrigConf::SignalLength, JBB);
  } else {
    // Send ref
    DropTrigRef* const ref = (DropTrigRef*)signal->getDataPtrSend();
    ref->setUserRef(senderRef);
    ref->setConnectionPtr(req->getConnectionPtr());
    ref->setRequestType(req->getRequestType());
    ref->setTableId(req->getTableId());
    ref->setIndexId(req->getIndexId());
    ref->setTriggerId(req->getTriggerId());
    ref->setErrorCode((DropTrigRef::ErrorCode)r);
    ref->setErrorLine(__LINE__);
    ref->setErrorNode(refToNode(reference()));
    sendSignal(senderRef, GSN_DROP_TRIG_REF, 
	       signal, DropTrigRef::SignalLength, JBB);
  }
}//Dbtup::DROP_TRIG_REQ()

/* ---------------------------------------------------------------- */
/* ------------------------- createTrigger ------------------------ */
/*                                                                  */
/* Creates a new trigger record by fetching one from the trigger    */
/* pool and associates it with the given table.                     */
/* Trigger type can be one of secondary_index, subscription,        */
/* constraint(NYI), foreign_key(NYI), schema_upgrade(NYI),          */
/* api_trigger(NYI) or sql_trigger(NYI).                            */   
/* Note that this method only checks for total number of allowed    */
/* triggers. Checking the number of allowed triggers per table is   */
/* done by TRIX.                                                    */
/*                                                                  */
/* ---------------------------------------------------------------- */
bool
Dbtup::createTrigger(Tablerec* table, const CreateTrigReq* req)
{
  if (ERROR_INSERTED(4003)) {
    CLEAR_ERROR_INSERT_VALUE;
    return false;
  }
  TriggerType::Value ttype = req->getTriggerType();
  TriggerActionTime::Value ttime = req->getTriggerActionTime();
  TriggerEvent::Value tevent = req->getTriggerEvent();

242
  DLList<TupTriggerData>* tlist = findTriggerList(table, ttype, ttime, tevent);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  ndbrequire(tlist != NULL);

  TriggerPtr tptr;
  if (!tlist->seize(tptr))
    return false;

  // Set trigger id
  tptr.p->triggerId = req->getTriggerId();

  //  ndbout_c("Create TupTrigger %u = %u %u %u %u", tptr.p->triggerId, table, ttype, ttime, tevent);

  // Set index id
  tptr.p->indexId = req->getIndexId();

  // Set trigger type etc
  tptr.p->triggerType = ttype;
  tptr.p->triggerActionTime = ttime;
  tptr.p->triggerEvent = tevent;

  tptr.p->sendBeforeValues = true;
  if ((tptr.p->triggerType == TriggerType::SUBSCRIPTION) &&
      ((tptr.p->triggerEvent == TriggerEvent::TE_UPDATE) ||
       (tptr.p->triggerEvent == TriggerEvent::TE_DELETE))) {
    ljam();
    tptr.p->sendBeforeValues = false;
  }
unknown's avatar
unknown committed
269
  /*
270 271 272 273 274 275 276
  tptr.p->sendOnlyChangedAttributes = false;
  if (((tptr.p->triggerType == TriggerType::SUBSCRIPTION) ||
      (tptr.p->triggerType == TriggerType::SUBSCRIPTION_BEFORE)) &&
      (tptr.p->triggerEvent == TriggerEvent::TE_UPDATE)) {
    ljam();
    tptr.p->sendOnlyChangedAttributes = true;
  }
unknown's avatar
unknown committed
277 278
  */
  tptr.p->sendOnlyChangedAttributes = !req->getReportAllMonitoredAttributes();
279 280 281 282 283 284 285 286
  // Set monitor all
  tptr.p->monitorAllAttributes = req->getMonitorAllAttributes();
  tptr.p->monitorReplicas = req->getMonitorReplicas();
  tptr.p->m_receiverBlock = refToBlock(req->getReceiverRef());

  tptr.p->attributeMask.clear();
  if (tptr.p->monitorAllAttributes) {
    ljam();
unknown's avatar
unknown committed
287
    for(Uint32 i = 0; i < table->m_no_of_attributes; i++) {
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
      if (!primaryKey(table, i)) {
        ljam();
        tptr.p->attributeMask.set(i);
      }
    }
  } else {
    // Set attribute mask
    ljam();
    tptr.p->attributeMask = req->getAttributeMask();
  }
  return true;
}//Dbtup::createTrigger()

bool
Dbtup::primaryKey(Tablerec* const regTabPtr, Uint32 attrId)
{
  Uint32 attrDescriptorStart = regTabPtr->tabDescriptor;
unknown's avatar
unknown committed
305 306
  Uint32 attrDescriptor = getTabDescrWord(attrDescriptorStart +
                                          (attrId * ZAD_SIZE));
307 308 309 310 311 312 313 314 315 316 317 318 319 320
  return (bool)AttributeDescriptor::getPrimaryKey(attrDescriptor);
}//Dbtup::primaryKey()

/* ---------------------------------------------------------------- */
/* -------------------------- dropTrigger ------------------------- */
/*                                                                  */
/* Deletes a trigger record by disassociating it with the given     */
/* table and returning it to the trigger pool.                      */
/* Trigger type can be one of secondary_index, subscription,        */
/* constraint(NYI), foreign_key(NYI), schema_upgrade(NYI),          */
/* api_trigger(NYI) or sql_trigger(NYI).                            */   
/*                                                                  */
/* ---------------------------------------------------------------- */
Uint32
321
Dbtup::dropTrigger(Tablerec* table, const DropTrigReq* req, BlockNumber sender)
322
{
323 324 325 326
  if (ERROR_INSERTED(4004)) {
    CLEAR_ERROR_INSERT_VALUE;
    return 9999;
  }
327 328 329 330 331 332
  Uint32 triggerId = req->getTriggerId();

  TriggerType::Value ttype = req->getTriggerType();
  TriggerActionTime::Value ttime = req->getTriggerActionTime();
  TriggerEvent::Value tevent = req->getTriggerEvent();

333
  //  ndbout_c("Drop TupTrigger %u = %u %u %u %u by %u", triggerId, table, ttype, ttime, tevent, sender);
334

335
  DLList<TupTriggerData>* tlist = findTriggerList(table, ttype, ttime, tevent);
336 337 338 339 340 341
  ndbrequire(tlist != NULL);

  Ptr<TupTriggerData> ptr;
  for (tlist->first(ptr); !ptr.isNull(); tlist->next(ptr)) {
    ljam();
    if (ptr.p->triggerId == triggerId) {
342 343 344 345 346 347 348 349 350 351 352 353 354
      if(ttype==TriggerType::SUBSCRIPTION && sender != ptr.p->m_receiverBlock)
      {
	/**
	 * You can only drop your own triggers for subscription triggers.
	 * Trigger IDs are private for each block.
	 *
	 * SUMA encodes information in the triggerId
	 *
	 * Backup doesn't really care about the Ids though.
	 */
	ljam();
	continue;
      }
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
      ljam();
      tlist->release(ptr.i);
      return 0;
    }
  }
  return DropTrigRef::TriggerNotFound;
}//Dbtup::dropTrigger()

/* ---------------------------------------------------------------- */
/* -------------- checkImmediateTriggersAfterOp ------------------ */
/*                                                                  */
/* Called after an insert, delete, or update operation takes        */
/* place. Fetches before tuple for deletes and updates and          */
/* after tuple for inserts and updates.                             */
/* Executes immediate triggers by sending FIRETRIGORD               */
/*                                                                  */
/* ---------------------------------------------------------------- */
unknown's avatar
unknown committed
372 373
void
Dbtup::checkImmediateTriggersAfterInsert(KeyReqStruct *req_struct,
unknown's avatar
unknown committed
374 375
                                         Operationrec *regOperPtr, 
                                         Tablerec *regTablePtr)
376
{
unknown's avatar
unknown committed
377
  if(refToBlock(req_struct->TC_ref) != DBTC) {
378 379 380
    return;
  }

unknown's avatar
unknown committed
381
  if ((regOperPtr->op_struct.primary_replica) &&
382 383
      (!(regTablePtr->afterInsertTriggers.isEmpty()))) {
    ljam();
unknown's avatar
unknown committed
384
    fireImmediateTriggers(req_struct,
385 386
                          regTablePtr->afterInsertTriggers,
                          regOperPtr);
unknown's avatar
unknown committed
387 388
  }
}
389

unknown's avatar
unknown committed
390 391
void
Dbtup::checkImmediateTriggersAfterUpdate(KeyReqStruct *req_struct,
unknown's avatar
unknown committed
392 393
                                         Operationrec* regOperPtr, 
                                         Tablerec* regTablePtr)
394
{
unknown's avatar
unknown committed
395
  if(refToBlock(req_struct->TC_ref) != DBTC) {
396 397 398
    return;
  }

unknown's avatar
unknown committed
399
  if ((regOperPtr->op_struct.primary_replica) &&
400 401
      (!(regTablePtr->afterUpdateTriggers.isEmpty()))) {
    ljam();
unknown's avatar
unknown committed
402
    fireImmediateTriggers(req_struct,
403 404
                          regTablePtr->afterUpdateTriggers,
                          regOperPtr);
unknown's avatar
unknown committed
405 406
  }
  if ((regOperPtr->op_struct.primary_replica) &&
407 408
      (!(regTablePtr->constraintUpdateTriggers.isEmpty()))) {
    ljam();
unknown's avatar
unknown committed
409
    fireImmediateTriggers(req_struct,
410 411
                          regTablePtr->constraintUpdateTriggers,
                          regOperPtr);
unknown's avatar
unknown committed
412 413
  }
}
414

unknown's avatar
unknown committed
415 416
void
Dbtup::checkImmediateTriggersAfterDelete(KeyReqStruct *req_struct,
unknown's avatar
unknown committed
417 418
                                         Operationrec* regOperPtr, 
                                         Tablerec* regTablePtr)
419
{
unknown's avatar
unknown committed
420
  if(refToBlock(req_struct->TC_ref) != DBTC) {
421 422 423
    return;
  }

unknown's avatar
unknown committed
424
  if ((regOperPtr->op_struct.primary_replica) &&
425 426
      (!(regTablePtr->afterDeleteTriggers.isEmpty()))) {
    ljam();
unknown's avatar
unknown committed
427
    executeTriggers(req_struct,
428 429
                    regTablePtr->afterDeleteTriggers,
                    regOperPtr);
unknown's avatar
unknown committed
430 431
  }
}
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

#if 0
/* ---------------------------------------------------------------- */
/* --------------------- checkDeferredTriggers -------------------- */
/*                                                                  */
/* Called before commit after an insert, delete, or update          */
/* operation. Fetches before tuple for deletes and updates and      */
/* after tuple for inserts and updates.                             */
/* Executes deferred triggers by sending FIRETRIGORD                */
/*                                                                  */
/* ---------------------------------------------------------------- */
void Dbtup::checkDeferredTriggers(Signal* signal, 
                                  Operationrec* const regOperPtr,
                                  Tablerec* const regTablePtr)
{
  ljam();
  // NYI
}//Dbtup::checkDeferredTriggers()
#endif

/* ---------------------------------------------------------------- */
/* --------------------- checkDetachedTriggers -------------------- */
/*                                                                  */
/* Called at commit after an insert, delete, or update operation.   */
/* Fetches before tuple for deletes and updates and                 */
/* after tuple for inserts and updates.                             */
/* Executes detached triggers by sending FIRETRIGORD                */
/*                                                                  */
/* ---------------------------------------------------------------- */
unknown's avatar
unknown committed
461
void Dbtup::checkDetachedTriggers(KeyReqStruct *req_struct,
unknown's avatar
unknown committed
462 463
                                  Operationrec* regOperPtr,
                                  Tablerec* regTablePtr)
464
{
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  Uint32 save_type = regOperPtr->op_struct.op_type;
  Tuple_header *save_ptr = req_struct->m_tuple_ptr;  

  switch (save_type) {
  case ZUPDATE:
  case ZINSERT:
    req_struct->m_tuple_ptr = (Tuple_header*)
      c_undo_buffer.get_ptr(&regOperPtr->m_copy_tuple_location);
    break;
  }

  /**
   * Set correct operation type and fix change mask
   * Note ALLOC is set in "orig" tuple
   */
  if (save_ptr->m_header_bits & Tuple_header::ALLOC) {
    if (save_type == ZDELETE) {
      // insert + delete = nothing
      ljam();
      return;
      goto end;
    }
    regOperPtr->op_struct.op_type = ZINSERT;
  }
  else if (save_type == ZINSERT) {
    /**
     * Tuple was not created but last op is INSERT.
     * This is possible only on DELETE + INSERT
     */
    regOperPtr->op_struct.op_type = ZUPDATE;
  }
unknown's avatar
unknown committed
496 497
  
  switch(regOperPtr->op_struct.op_type) {
498 499 500 501 502
  case(ZINSERT):
    ljam();
    if (regTablePtr->subscriptionInsertTriggers.isEmpty()) {
      // Table has no active triggers monitoring inserts at commit
      ljam();
503
      goto end;
unknown's avatar
unknown committed
504
    }
505 506

    // If any fired immediate insert trigger then fetch after tuple
unknown's avatar
unknown committed
507
    fireDetachedTriggers(req_struct,
508 509 510 511 512 513 514 515
                         regTablePtr->subscriptionInsertTriggers, 
                         regOperPtr);
    break;
  case(ZDELETE):
    ljam();
    if (regTablePtr->subscriptionDeleteTriggers.isEmpty()) {
      // Table has no active triggers monitoring deletes at commit
      ljam();
516
      goto end;
unknown's avatar
unknown committed
517
    }
518 519 520

    // Execute any after delete triggers by sending 
    // FIRETRIGORD with the before tuple
unknown's avatar
unknown committed
521 522 523
    fireDetachedTriggers(req_struct,
			 regTablePtr->subscriptionDeleteTriggers, 
			 regOperPtr);
524 525 526 527 528 529
    break;
  case(ZUPDATE):
    ljam();
    if (regTablePtr->subscriptionUpdateTriggers.isEmpty()) {
      // Table has no active triggers monitoring updates at commit
      ljam();
530
      goto end;
unknown's avatar
unknown committed
531
    }
532 533 534

    // If any fired immediate update trigger then fetch after tuple
    // and send two FIRETRIGORD one with before tuple and one with after tuple
unknown's avatar
unknown committed
535
    fireDetachedTriggers(req_struct,
536 537 538 539 540 541
                         regTablePtr->subscriptionUpdateTriggers, 
                         regOperPtr);
    break;
  default:
    ndbrequire(false);
    break;
unknown's avatar
unknown committed
542
  }
543 544 545 546

end:
  regOperPtr->op_struct.op_type = save_type;
  req_struct->m_tuple_ptr = save_ptr;
unknown's avatar
unknown committed
547
}
548 549

void 
unknown's avatar
unknown committed
550
Dbtup::fireImmediateTriggers(KeyReqStruct *req_struct,
551
                             DLList<TupTriggerData>& triggerList, 
552 553 554 555 556 557 558
                             Operationrec* const regOperPtr)
{
  TriggerPtr trigPtr;
  triggerList.first(trigPtr);
  while (trigPtr.i != RNIL) {
    ljam();
    if (trigPtr.p->monitorAllAttributes ||
unknown's avatar
unknown committed
559
        trigPtr.p->attributeMask.overlaps(req_struct->changeMask)) {
560
      ljam();
unknown's avatar
unknown committed
561
      executeTrigger(req_struct,
562 563 564 565 566 567 568 569 570
                     trigPtr.p,
                     regOperPtr);
    }//if
    triggerList.next(trigPtr);
  }//while
}//Dbtup::fireImmediateTriggers()

#if 0
void 
unknown's avatar
unknown committed
571 572
Dbtup::fireDeferredTriggers(Signal* signal,
                            KeyReqStruct *req_struct,
573
                            DLList<TupTriggerData>& triggerList, 
574 575 576 577 578 579 580
                            Operationrec* const regOperPtr)
{
  TriggerPtr trigPtr;
  triggerList.first(trigPtr);
  while (trigPtr.i != RNIL) {
    ljam();
    if (trigPtr.p->monitorAllAttributes ||
unknown's avatar
unknown committed
581
        trigPtr.p->attributeMask.overlaps(req_struct->changeMask)) {
582
      ljam();
unknown's avatar
unknown committed
583
      executeTrigger(req_struct,
584 585 586 587 588 589 590 591 592
                     trigPtr,
                     regOperPtr);
    }//if
    triggerList.next(trigPtr);
  }//while
}//Dbtup::fireDeferredTriggers()
#endif

void 
unknown's avatar
unknown committed
593
Dbtup::fireDetachedTriggers(KeyReqStruct *req_struct,
594
                            DLList<TupTriggerData>& triggerList, 
595 596
                            Operationrec* const regOperPtr)
{
unknown's avatar
unknown committed
597
  
598
  TriggerPtr trigPtr;  
unknown's avatar
unknown committed
599 600 601 602 603 604
  
  /**
   * Set disk page
   */
  req_struct->m_disk_page_ptr.i = m_pgman.m_ptr.i;
  
unknown's avatar
unknown committed
605
  ndbrequire(regOperPtr->is_first_operation());
606 607 608
  triggerList.first(trigPtr);
  while (trigPtr.i != RNIL) {
    ljam();
unknown's avatar
unknown committed
609 610
    if ((trigPtr.p->monitorReplicas ||
         regOperPtr->op_struct.primary_replica) &&
611
        (trigPtr.p->monitorAllAttributes ||
unknown's avatar
unknown committed
612
         trigPtr.p->attributeMask.overlaps(req_struct->changeMask))) {
613
      ljam();
unknown's avatar
unknown committed
614
      executeTrigger(req_struct,
615 616
                     trigPtr.p,
                     regOperPtr);
unknown's avatar
unknown committed
617
    }
618
    triggerList.next(trigPtr);
unknown's avatar
unknown committed
619 620 621 622
  }
}

void Dbtup::executeTriggers(KeyReqStruct *req_struct,
623
                            DLList<TupTriggerData>& triggerList, 
624 625 626 627 628 629
                            Operationrec* regOperPtr)
{
  TriggerPtr trigPtr;
  triggerList.first(trigPtr);
  while (trigPtr.i != RNIL) {
    ljam();
unknown's avatar
unknown committed
630
    executeTrigger(req_struct,
631 632 633 634
                   trigPtr.p,
                   regOperPtr);
    triggerList.next(trigPtr);

unknown's avatar
unknown committed
635 636
  }
}
637

unknown's avatar
unknown committed
638
void Dbtup::executeTrigger(KeyReqStruct *req_struct,
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
                           TupTriggerData* const trigPtr,
                           Operationrec* const regOperPtr)
{
  /**
   * The block below does not work together with GREP.
   * I have 2 db nodes (2 replicas) -> one node group.
   * I want to have FIRETRIG_ORD sent to all SumaParticipants,
   * from all nodes in the node group described above. However, 
   * only one of the nodes in the node group actually sends the
   *  FIRE_TRIG_ORD, and the other node enters this "hack" below.
   * I don't really know what the code snippet below does, but it
   * does not work with GREP the way Lars and I want it.
   * We need to have triggers fired from both the primary and the
   * backup replica, not only the primary as it is now.
   * 
   * Note: In Suma, I have changed triggers to be created with
   * setMonitorReplicas(true).
   * /Johan
   *
   * See RT 709
   */
  // XXX quick fix to NR, should fix in LQHKEYREQ instead
  /*  
unknown's avatar
unknown committed
662
      if (refToBlock(req_struct->TC_ref) == DBLQH) {
663 664 665 666
      jam();
      return;
      }
  */
unknown's avatar
unknown committed
667
  Signal* signal= req_struct->signal;
668 669
  BlockReference ref = trigPtr->m_receiverBlock;
  Uint32* const keyBuffer = &cinBuffer[0];
unknown's avatar
unknown committed
670 671 672 673 674 675 676
  Uint32* const afterBuffer = &coutBuffer[0];
  Uint32* const beforeBuffer = &clogMemBuffer[0];
  
  Uint32 noPrimKey, noAfterWords, noBeforeWords;
  FragrecordPtr regFragPtr;
  regFragPtr.i= regOperPtr->fragmentPtr;
  ptrCheckGuard(regFragPtr, cnoOfFragrec, fragrecord);
677 678 679 680 681 682 683 684 685 686 687 688 689

  if (ref == BACKUP) {
    ljam();
    /*
    In order for the implementation of BACKUP to work even when changing
    primaries in the middle of the backup we need to set the trigger on
    all replicas. This check checks whether this is the node where this
    trigger should be fired. The check should preferably have been put
    completely in the BACKUP block but it was about five times simpler
    to put it here and also much faster for the backup (small overhead
    for everybody else.
    */
    signal->theData[0] = trigPtr->triggerId;
unknown's avatar
unknown committed
690
    signal->theData[1] = regFragPtr.p->fragmentId;
691 692 693 694 695
    EXECUTE_DIRECT(BACKUP, GSN_BACKUP_TRIG_REQ, signal, 2);
    ljamEntry();
    if (signal->theData[0] == 0) {
      ljam();
      return;
unknown's avatar
unknown committed
696 697
    }
  }
698 699
  if (!readTriggerInfo(trigPtr,
                       regOperPtr,
unknown's avatar
unknown committed
700 701
                       req_struct,
                       regFragPtr.p,
702 703
                       keyBuffer,
                       noPrimKey,
unknown's avatar
unknown committed
704 705 706 707
                       afterBuffer,
                       noAfterWords,
                       beforeBuffer,
                       noBeforeWords)) {
708 709
    ljam();
    return;
unknown's avatar
unknown committed
710
  }
711 712 713 714 715 716 717 718
//--------------------------------------------------------------------
// Now all data for this trigger has been read. It is now time to send
// the trigger information consisting of two or three sets of TRIG_
// ATTRINFO signals and one FIRE_TRIG_ORD signal.
// We start by setting common header info for all TRIG_ATTRINFO signals.
//--------------------------------------------------------------------
  bool executeDirect;
  TrigAttrInfo* const trigAttrInfo = (TrigAttrInfo *)signal->getDataPtrSend();
unknown's avatar
unknown committed
719
  trigAttrInfo->setConnectionPtr(req_struct->TC_index);
720 721 722 723 724
  trigAttrInfo->setTriggerId(trigPtr->triggerId);

  switch(trigPtr->triggerType) {
  case (TriggerType::SECONDARY_INDEX):
    ljam();
unknown's avatar
unknown committed
725
    ref = req_struct->TC_ref;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    executeDirect = false;
    break;
  case (TriggerType::SUBSCRIPTION):
  case (TriggerType::SUBSCRIPTION_BEFORE):
    ljam();
    // Since only backup uses subscription triggers we send to backup directly for now
    ref = trigPtr->m_receiverBlock;
    executeDirect = true;
    break;
  case (TriggerType::READ_ONLY_CONSTRAINT):
    terrorCode = ZREAD_ONLY_CONSTRAINT_VIOLATION;
    // XXX should return status and abort the rest
    return;
  default:
    ndbrequire(false);
unknown's avatar
unknown committed
741
    executeDirect= false; // remove warning
742 743
  }//switch

unknown's avatar
unknown committed
744
  req_struct->no_fired_triggers++;
745 746 747 748

  trigAttrInfo->setAttrInfoType(TrigAttrInfo::PRIMARY_KEY);
  sendTrigAttrInfo(signal, keyBuffer, noPrimKey, executeDirect, ref);

unknown's avatar
unknown committed
749
  switch(regOperPtr->op_struct.op_type) {
750 751 752 753
  case(ZINSERT):
    ljam();
    // Send AttrInfo signals with new attribute values
    trigAttrInfo->setAttrInfoType(TrigAttrInfo::AFTER_VALUES);
unknown's avatar
unknown committed
754
    sendTrigAttrInfo(signal, afterBuffer, noAfterWords, executeDirect, ref);
755 756 757 758 759
    break;
  case(ZDELETE):
    if (trigPtr->sendBeforeValues) {
      ljam();
      trigAttrInfo->setAttrInfoType(TrigAttrInfo::BEFORE_VALUES);
unknown's avatar
unknown committed
760 761
      sendTrigAttrInfo(signal, beforeBuffer, noBeforeWords, executeDirect,ref);
    }
762 763 764 765 766 767
    break;
  case(ZUPDATE):
    ljam();
    if (trigPtr->sendBeforeValues) {
      ljam();
      trigAttrInfo->setAttrInfoType(TrigAttrInfo::BEFORE_VALUES);
unknown's avatar
unknown committed
768 769
      sendTrigAttrInfo(signal, beforeBuffer, noBeforeWords, executeDirect,ref);
    }
770
    trigAttrInfo->setAttrInfoType(TrigAttrInfo::AFTER_VALUES);
unknown's avatar
unknown committed
771
    sendTrigAttrInfo(signal, afterBuffer, noAfterWords, executeDirect, ref);
772 773 774
    break;
  default:
    ndbrequire(false);
unknown's avatar
unknown committed
775
  }
776
  sendFireTrigOrd(signal,
unknown's avatar
unknown committed
777
                  req_struct,
778 779
                  regOperPtr,
                  trigPtr,
unknown's avatar
unknown committed
780
		  regFragPtr.p->fragmentId,
781
                  noPrimKey,
unknown's avatar
unknown committed
782 783 784
                  noBeforeWords,
                  noAfterWords);
}
785 786

Uint32 Dbtup::setAttrIds(Bitmask<MAXNROFATTRIBUTESINWORDS>& attributeMask, 
unknown's avatar
unknown committed
787
                         Uint32 m_no_of_attributesibutes, 
788 789 790
                         Uint32* inBuffer)
{
  Uint32 bufIndx = 0;
unknown's avatar
unknown committed
791
  for (Uint32 i = 0; i < m_no_of_attributesibutes; i++) {
792 793 794 795
    ljam();
    if (attributeMask.get(i)) {
      ljam();
      AttributeHeader::init(&inBuffer[bufIndx++], i, 0);
unknown's avatar
unknown committed
796 797
    }
  }
798
  return bufIndx;
unknown's avatar
unknown committed
799
}
800 801

bool Dbtup::readTriggerInfo(TupTriggerData* const trigPtr,
unknown's avatar
unknown committed
802 803 804
                            Operationrec* const regOperPtr,
                            KeyReqStruct *req_struct,
                            Fragrecord* const regFragPtr,
805 806
                            Uint32* const keyBuffer,
                            Uint32& noPrimKey,
unknown's avatar
unknown committed
807 808 809 810
                            Uint32* const afterBuffer,
                            Uint32& noAfterWords,
                            Uint32* const beforeBuffer,
                            Uint32& noBeforeWords)
811
{
unknown's avatar
unknown committed
812 813 814
  //XXX this will not work with varsize attributes...
  noAfterWords = 0;
  noBeforeWords = 0;
815 816 817 818 819 820
  Uint32 readBuffer[MAX_ATTRIBUTES_IN_TABLE];

//---------------------------------------------------------------------------
// Set-up variables needed by readAttributes operPtr.p, tabptr.p
//---------------------------------------------------------------------------
  operPtr.p = regOperPtr;
unknown's avatar
unknown committed
821
  tabptr.i = regFragPtr->fragTableId;
822
  ptrCheckGuard(tabptr, cnoOfTablerec, tablerec);
unknown's avatar
unknown committed
823

824
  Tablerec* const regTabPtr = tabptr.p;
unknown's avatar
unknown committed
825 826 827 828 829 830 831
  Uint32 num_attr= regTabPtr->m_no_of_attributes;
  Uint32 descr_start= regTabPtr->tabDescriptor;
  ndbrequire(descr_start + (num_attr << ZAD_LOG_SIZE) <= cnoOfTabDescrRec);

  req_struct->check_offset[MM]= regTabPtr->get_check_offset(MM);
  req_struct->check_offset[DD]= regTabPtr->get_check_offset(DD);
  req_struct->attr_descr= &tableDescriptor[descr_start];
832 833 834 835

//--------------------------------------------------------------------
// Read Primary Key Values
//--------------------------------------------------------------------
unknown's avatar
unknown committed
836 837
  if (regTabPtr->need_expand()) 
    prepare_read(req_struct, regTabPtr, true);
unknown's avatar
unknown committed
838 839 840 841 842 843 844
  
  int ret = readAttributes(req_struct,
			   &tableDescriptor[regTabPtr->readKeyArray].tabDescr,
			   regTabPtr->noOfKeyAttr,
			   keyBuffer,
			   ZATTR_BUFFER_SIZE,
			   false);
unknown's avatar
unknown committed
845 846
  ndbrequire(ret != -1);
  noPrimKey= ret;
847
  Uint32 numAttrsToRead;
unknown's avatar
unknown committed
848
  if ((regOperPtr->op_struct.op_type == ZUPDATE) &&
849 850 851 852 853 854 855
      (trigPtr->sendOnlyChangedAttributes)) {
    ljam();
//--------------------------------------------------------------------
// Update that sends only changed information
//--------------------------------------------------------------------
    Bitmask<MAXNROFATTRIBUTESINWORDS> attributeMask;
    attributeMask = trigPtr->attributeMask;
unknown's avatar
unknown committed
856 857 858 859 860
    attributeMask.bitAND(req_struct->changeMask);
    numAttrsToRead = setAttrIds(attributeMask, regTabPtr->m_no_of_attributes, 
				&readBuffer[0]);
    
  } else if ((regOperPtr->op_struct.op_type == ZDELETE) &&
861 862 863 864 865 866 867 868 869
             (!trigPtr->sendBeforeValues)) {
    ljam();
//--------------------------------------------------------------------
// Delete without sending before values only read Primary Key
//--------------------------------------------------------------------
    return true;
  } else {
    ljam();
//--------------------------------------------------------------------
870 871 872
// All others send all attributes that are monitored, except:
// Omit unchanged blob inlines on update i.e.
// attributeMask & ~ (blobAttributeMask & ~ changeMask)
873
//--------------------------------------------------------------------
874 875 876 877 878 879 880 881 882
    Bitmask<MAXNROFATTRIBUTESINWORDS> attributeMask;
    attributeMask = trigPtr->attributeMask;
    if (regOperPtr->op_struct.op_type == ZUPDATE) {
      Bitmask<MAXNROFATTRIBUTESINWORDS> tmpMask = regTabPtr->blobAttributeMask;
      tmpMask.bitANDC(req_struct->changeMask);
      attributeMask.bitANDC(tmpMask);
    }
    numAttrsToRead = setAttrIds(attributeMask, regTabPtr->m_no_of_attributes,
                                &readBuffer[0]);
unknown's avatar
unknown committed
883
  }
884 885 886 887
  ndbrequire(numAttrsToRead < MAX_ATTRIBUTES_IN_TABLE);
//--------------------------------------------------------------------
// Read Main tuple values
//--------------------------------------------------------------------
unknown's avatar
unknown committed
888 889
  if (regOperPtr->op_struct.op_type != ZDELETE)
  {
890
    ljam();
unknown's avatar
unknown committed
891 892 893 894 895 896
    int ret = readAttributes(req_struct,
			     &readBuffer[0],
			     numAttrsToRead,
			     afterBuffer,
			     ZATTR_BUFFER_SIZE,
			     false);
unknown's avatar
unknown committed
897
    ndbrequire(ret != -1);
unknown's avatar
unknown committed
898
    noAfterWords= ret;
899 900
  } else {
    ljam();
unknown's avatar
unknown committed
901 902 903
    noAfterWords = 0;
  }

904 905 906 907 908
//--------------------------------------------------------------------
// Read Copy tuple values for UPDATE's
//--------------------------------------------------------------------
// Initialise pagep and tuple offset for read of copy tuple
//--------------------------------------------------------------------
unknown's avatar
unknown committed
909 910
  if ((regOperPtr->op_struct.op_type == ZUPDATE || 
       regOperPtr->op_struct.op_type == ZDELETE) &&
911 912
      (trigPtr->sendBeforeValues)) {
    ljam();
unknown's avatar
unknown committed
913 914 915 916 917 918 919 920 921 922 923 924
    
    Tuple_header *save= req_struct->m_tuple_ptr;
    PagePtr tmp;
    if(regOperPtr->is_first_operation())
    {
      Uint32 *ptr= get_ptr(&tmp, &regOperPtr->m_tuple_location, regTabPtr);
      req_struct->m_tuple_ptr= (Tuple_header*)ptr;
    }
    else
    {
      Uint32 *ptr= 
	c_undo_buffer.get_ptr(&req_struct->prevOpPtr.p->m_copy_tuple_location);
925

unknown's avatar
unknown committed
926 927
      req_struct->m_tuple_ptr= (Tuple_header*)ptr;
    }
928

unknown's avatar
unknown committed
929 930
    if (regTabPtr->need_expand()) // no disk 
      prepare_read(req_struct, regTabPtr, true);
unknown's avatar
unknown committed
931 932 933 934 935 936 937 938
    
    int ret = readAttributes(req_struct,
			     &readBuffer[0],
			     numAttrsToRead,
			     beforeBuffer,
			     ZATTR_BUFFER_SIZE,
			     false);
    req_struct->m_tuple_ptr= save;
unknown's avatar
unknown committed
939
    ndbrequire(ret != -1);
unknown's avatar
unknown committed
940
    noBeforeWords = ret;
941 942
    if (trigPtr->m_receiverBlock != SUMA &&
        (noAfterWords == noBeforeWords) &&
unknown's avatar
unknown committed
943
        (memcmp(afterBuffer, beforeBuffer, noAfterWords << 2) == 0)) {
944 945 946 947 948
//--------------------------------------------------------------------
// Although a trigger was fired it was not necessary since the old
// value and the new value was exactly the same
//--------------------------------------------------------------------
      ljam();
unknown's avatar
unknown committed
949
      //XXX does this work with collations?
950
      return false;
unknown's avatar
unknown committed
951 952
    }
  }
953
  return true;
unknown's avatar
unknown committed
954
}
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

void Dbtup::sendTrigAttrInfo(Signal* signal, 
                             Uint32* data, 
                             Uint32  dataLen,
                             bool    executeDirect,
                             BlockReference receiverReference)
{
  TrigAttrInfo* const trigAttrInfo = (TrigAttrInfo *)signal->getDataPtrSend();
  Uint32 sigLen;
  Uint32 dataIndex = 0;
  do {
    sigLen = dataLen - dataIndex;
    if (sigLen > TrigAttrInfo::DataLength) {
      ljam();
      sigLen = TrigAttrInfo::DataLength;
unknown's avatar
unknown committed
970
    }
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    MEMCOPY_NO_WORDS(trigAttrInfo->getData(), 
                     data + dataIndex,
                     sigLen);
    if (executeDirect) {
      ljam();
      EXECUTE_DIRECT(receiverReference, 
                     GSN_TRIG_ATTRINFO,
                     signal,
		     TrigAttrInfo::StaticLength + sigLen);
      ljamEntry();
    } else {
      ljam();
      sendSignal(receiverReference, 
                 GSN_TRIG_ATTRINFO, 
                 signal, 
                 TrigAttrInfo::StaticLength + sigLen,
                 JBB);
unknown's avatar
unknown committed
988
    }
989 990
    dataIndex += sigLen;
  } while (dataLen != dataIndex);
unknown's avatar
unknown committed
991
}
992

unknown's avatar
unknown committed
993 994
void Dbtup::sendFireTrigOrd(Signal* signal,
                            KeyReqStruct *req_struct,
995 996
                            Operationrec * const regOperPtr, 
                            TupTriggerData* const trigPtr, 
unknown's avatar
unknown committed
997
			    Uint32 fragmentId,
998 999 1000 1001 1002 1003
                            Uint32 noPrimKeyWords, 
                            Uint32 noBeforeValueWords, 
                            Uint32 noAfterValueWords)
{
  FireTrigOrd* const fireTrigOrd = (FireTrigOrd *)signal->getDataPtrSend();
  
unknown's avatar
unknown committed
1004
  fireTrigOrd->setConnectionPtr(req_struct->TC_index);
1005
  fireTrigOrd->setTriggerId(trigPtr->triggerId);
unknown's avatar
unknown committed
1006
  fireTrigOrd->fragId= fragmentId;
1007

unknown's avatar
unknown committed
1008
  switch(regOperPtr->op_struct.op_type) {
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  case(ZINSERT):
    ljam();
    fireTrigOrd->setTriggerEvent(TriggerEvent::TE_INSERT);
    break;
  case(ZDELETE):
    ljam();
    fireTrigOrd->setTriggerEvent(TriggerEvent::TE_DELETE);
    break;
  case(ZUPDATE):
    ljam();
    fireTrigOrd->setTriggerEvent(TriggerEvent::TE_UPDATE);
    break;
  default:
    ndbrequire(false);
    break;
unknown's avatar
unknown committed
1024
  }
1025 1026 1027 1028 1029 1030 1031 1032

  fireTrigOrd->setNoOfPrimaryKeyWords(noPrimKeyWords);
  fireTrigOrd->setNoOfBeforeValueWords(noBeforeValueWords);
  fireTrigOrd->setNoOfAfterValueWords(noAfterValueWords);

  switch(trigPtr->triggerType) {
  case (TriggerType::SECONDARY_INDEX):
    ljam();
unknown's avatar
unknown committed
1033
    sendSignal(req_struct->TC_ref, GSN_FIRE_TRIG_ORD, 
1034 1035 1036 1037 1038 1039
               signal, FireTrigOrd::SignalLength, JBB);
    break;
  case (TriggerType::SUBSCRIPTION_BEFORE): // Only Suma
    ljam();
    // Since only backup uses subscription triggers we 
    // send to backup directly for now
unknown's avatar
unknown committed
1040 1041
    fireTrigOrd->setGCI(req_struct->gci);
    fireTrigOrd->setHashValue(req_struct->hash_value);
1042 1043 1044 1045 1046 1047 1048 1049 1050
    EXECUTE_DIRECT(trigPtr->m_receiverBlock,
                   GSN_FIRE_TRIG_ORD,
                   signal,
		   FireTrigOrd::SignalWithHashValueLength);
    break;
  case (TriggerType::SUBSCRIPTION):
    ljam();
    // Since only backup uses subscription triggers we 
    // send to backup directly for now
unknown's avatar
unknown committed
1051
    fireTrigOrd->setGCI(req_struct->gci);
1052 1053 1054 1055 1056 1057 1058 1059
    EXECUTE_DIRECT(trigPtr->m_receiverBlock,
                   GSN_FIRE_TRIG_ORD,
                   signal,
		   FireTrigOrd::SignalWithGCILength);
    break;
  default:
    ndbrequire(false);
    break;
unknown's avatar
unknown committed
1060 1061
  }
}
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076

/*
 * Ordered index triggers.
 *
 * Insert: add entry to index
 * Update: add entry to index, de|ay remove until commit
 * Delete: do nothing, delay remove until commit
 * Commit: remove entry delayed from update and delete
 * Abort : remove entry added by insert and update
 *
 * See Notes.txt for the details.
 */

int
Dbtup::executeTuxInsertTriggers(Signal* signal,
unknown's avatar
unknown committed
1077 1078 1079
                                Operationrec* regOperPtr,
                                Fragrecord* regFragPtr,
                                Tablerec* regTabPtr)
1080 1081 1082
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
  // fill in constant part
unknown's avatar
unknown committed
1083 1084 1085 1086 1087
  req->tableId = regFragPtr->fragTableId;
  req->fragId = regFragPtr->fragmentId;
  req->pageId = regOperPtr->m_tuple_location.m_page_no;
  req->pageIndex = regOperPtr->m_tuple_location.m_page_idx;
  req->tupVersion = regOperPtr->tupVersion;
1088
  req->opInfo = TuxMaintReq::OpAdd;
unknown's avatar
unknown committed
1089
  return addTuxEntries(signal, regOperPtr, regTabPtr);
1090 1091 1092 1093
}

int
Dbtup::executeTuxUpdateTriggers(Signal* signal,
unknown's avatar
unknown committed
1094 1095 1096
                                Operationrec* regOperPtr,
                                Fragrecord* regFragPtr,
                                Tablerec* regTabPtr)
1097 1098 1099
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
  // fill in constant part
unknown's avatar
unknown committed
1100 1101 1102 1103 1104
  req->tableId = regFragPtr->fragTableId;
  req->fragId = regFragPtr->fragmentId;
  req->pageId = regOperPtr->m_tuple_location.m_page_no;
  req->pageIndex = regOperPtr->m_tuple_location.m_page_idx;
  req->tupVersion = regOperPtr->tupVersion;
1105
  req->opInfo = TuxMaintReq::OpAdd;
unknown's avatar
unknown committed
1106 1107 1108 1109 1110 1111 1112 1113 1114
  return addTuxEntries(signal, regOperPtr, regTabPtr);
}

int
Dbtup::addTuxEntries(Signal* signal,
                     Operationrec* regOperPtr,
                     Tablerec* regTabPtr)
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
1115
  const DLList<TupTriggerData>& triggerList = regTabPtr->tuxCustomTriggers;
1116
  TriggerPtr triggerPtr;
unknown's avatar
unknown committed
1117
  Uint32 failPtrI;
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  triggerList.first(triggerPtr);
  while (triggerPtr.i != RNIL) {
    ljam();
    req->indexId = triggerPtr.p->indexId;
    req->errorCode = RNIL;
    EXECUTE_DIRECT(DBTUX, GSN_TUX_MAINT_REQ,
        signal, TuxMaintReq::SignalLength);
    ljamEntry();
    if (req->errorCode != 0) {
      ljam();
      terrorCode = req->errorCode;
unknown's avatar
unknown committed
1129 1130
      failPtrI = triggerPtr.i;
      goto fail;
1131 1132 1133 1134
    }
    triggerList.next(triggerPtr);
  }
  return 0;
unknown's avatar
unknown committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
fail:
  req->opInfo = TuxMaintReq::OpRemove;
  triggerList.first(triggerPtr);
  while (triggerPtr.i != failPtrI) {
    ljam();
    req->indexId = triggerPtr.p->indexId;
    req->errorCode = RNIL;
    EXECUTE_DIRECT(DBTUX, GSN_TUX_MAINT_REQ,
        signal, TuxMaintReq::SignalLength);
    ljamEntry();
    ndbrequire(req->errorCode == 0);
    triggerList.next(triggerPtr);
  }
#ifdef VM_TRACE
  ndbout << "aborted partial tux update: op " << hex << regOperPtr << endl;
#endif
  return -1;
1152 1153 1154 1155 1156
}

int
Dbtup::executeTuxDeleteTriggers(Signal* signal,
                                Operationrec* const regOperPtr,
unknown's avatar
unknown committed
1157
                                Fragrecord* const regFragPtr,
1158 1159 1160 1161 1162 1163 1164 1165 1166
                                Tablerec* const regTabPtr)
{
  // do nothing
  return 0;
}

void
Dbtup::executeTuxCommitTriggers(Signal* signal,
                                Operationrec* regOperPtr,
unknown's avatar
unknown committed
1167 1168
                                Fragrecord* regFragPtr,
                                Tablerec* regTabPtr)
1169 1170 1171
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
  Uint32 tupVersion;
unknown's avatar
unknown committed
1172 1173
  if (regOperPtr->op_struct.op_type == ZINSERT) {
    if (! regOperPtr->op_struct.delete_insert_flag)
1174 1175
      return;
    ljam();
unknown's avatar
unknown committed
1176 1177
    tupVersion= decr_tup_version(regOperPtr->tupVersion);
  } else if (regOperPtr->op_struct.op_type == ZUPDATE) {
1178
    ljam();
unknown's avatar
unknown committed
1179 1180 1181
    tupVersion= decr_tup_version(regOperPtr->tupVersion);
  } else if (regOperPtr->op_struct.op_type == ZDELETE) {
    if (regOperPtr->op_struct.delete_insert_flag)
1182 1183
      return;
    ljam();
unknown's avatar
unknown committed
1184
    tupVersion= regOperPtr->tupVersion;
1185 1186
  } else {
    ndbrequire(false);
unknown's avatar
unknown committed
1187
    tupVersion= 0; // remove warning
1188 1189
  }
  // fill in constant part
unknown's avatar
unknown committed
1190 1191 1192 1193
  req->tableId = regFragPtr->fragTableId;
  req->fragId = regFragPtr->fragmentId;
  req->pageId = regOperPtr->m_tuple_location.m_page_no;
  req->pageIndex = regOperPtr->m_tuple_location.m_page_idx;
1194 1195
  req->tupVersion = tupVersion;
  req->opInfo = TuxMaintReq::OpRemove;
unknown's avatar
unknown committed
1196
  removeTuxEntries(signal, regTabPtr);
1197 1198 1199 1200 1201
}

void
Dbtup::executeTuxAbortTriggers(Signal* signal,
                               Operationrec* regOperPtr,
unknown's avatar
unknown committed
1202 1203
                               Fragrecord* regFragPtr,
                               Tablerec* regTabPtr)
1204 1205 1206 1207
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
  // get version
  Uint32 tupVersion;
unknown's avatar
unknown committed
1208
  if (regOperPtr->op_struct.op_type == ZINSERT) {
1209 1210
    ljam();
    tupVersion = regOperPtr->tupVersion;
unknown's avatar
unknown committed
1211
  } else if (regOperPtr->op_struct.op_type == ZUPDATE) {
1212 1213
    ljam();
    tupVersion = regOperPtr->tupVersion;
unknown's avatar
unknown committed
1214
  } else if (regOperPtr->op_struct.op_type == ZDELETE) {
1215 1216 1217 1218
    ljam();
    return;
  } else {
    ndbrequire(false);
unknown's avatar
unknown committed
1219
    tupVersion= 0; // remove warning
1220 1221
  }
  // fill in constant part
unknown's avatar
unknown committed
1222 1223 1224 1225
  req->tableId = regFragPtr->fragTableId;
  req->fragId = regFragPtr->fragmentId;
  req->pageId = regOperPtr->m_tuple_location.m_page_no;
  req->pageIndex = regOperPtr->m_tuple_location.m_page_idx;
1226 1227
  req->tupVersion = tupVersion;
  req->opInfo = TuxMaintReq::OpRemove;
unknown's avatar
unknown committed
1228
  removeTuxEntries(signal, regTabPtr);
unknown's avatar
unknown committed
1229 1230 1231 1232 1233 1234 1235
}

void
Dbtup::removeTuxEntries(Signal* signal,
                        Tablerec* regTabPtr)
{
  TuxMaintReq* const req = (TuxMaintReq*)signal->getDataPtrSend();
1236
  const DLList<TupTriggerData>& triggerList = regTabPtr->tuxCustomTriggers;
1237 1238 1239 1240 1241 1242 1243 1244 1245
  TriggerPtr triggerPtr;
  triggerList.first(triggerPtr);
  while (triggerPtr.i != RNIL) {
    ljam();
    req->indexId = triggerPtr.p->indexId;
    req->errorCode = RNIL,
    EXECUTE_DIRECT(DBTUX, GSN_TUX_MAINT_REQ,
        signal, TuxMaintReq::SignalLength);
    ljamEntry();
unknown's avatar
unknown committed
1246
    // must succeed
1247 1248 1249 1250
    ndbrequire(req->errorCode == 0);
    triggerList.next(triggerPtr);
  }
}