@@ -44,6 +44,7 @@ See the Mulan PSL v2 for more details. */
44
44
#include " storage/default/default_handler.h"
45
45
#include " storage/common/condition_filter.h"
46
46
#include " storage/trx/trx.h"
47
+ #include " storage/clog/clog.h"
47
48
48
49
using namespace common ;
49
50
@@ -175,18 +176,29 @@ void ExecuteStage::handle_request(common::StageEvent *event)
175
176
default_storage_stage_->handle_event (event);
176
177
} break ;
177
178
case SCF_SYNC: {
179
+ /*
178
180
RC rc = DefaultHandler::get_default().sync();
179
181
session_event->set_response(strrc(rc));
182
+ */
180
183
} break ;
181
184
case SCF_BEGIN: {
185
+ do_begin (sql_event);
186
+ /*
182
187
session_event->set_response("SUCCESS\n");
188
+ */
183
189
} break ;
184
190
case SCF_COMMIT: {
191
+ do_commit (sql_event);
192
+ /*
185
193
Trx *trx = session->current_trx();
186
194
RC rc = trx->commit();
187
195
session->set_trx_multi_operation_mode(false);
188
196
session_event->set_response(strrc(rc));
197
+ */
189
198
} break ;
199
+ case SCF_CLOG_SYNC: {
200
+ do_clog_sync (sql_event);
201
+ }
190
202
case SCF_ROLLBACK: {
191
203
Trx *trx = session_event->get_client ()->session ->current_trx ();
192
204
RC rc = trx->rollback ();
@@ -519,18 +531,40 @@ RC ExecuteStage::do_insert(SQLStageEvent *sql_event)
519
531
{
520
532
Stmt *stmt = sql_event->stmt ();
521
533
SessionEvent *session_event = sql_event->session_event ();
534
+ Session *session = session_event->session ();
535
+ Db *db = session->get_current_db ();
536
+ Trx *trx = session->current_trx ();
537
+ CLogManager *clog_manager = db->get_clog_manager ();
522
538
523
539
if (stmt == nullptr ) {
524
540
LOG_WARN (" cannot find statement" );
525
541
return RC::GENERIC_ERROR;
526
542
}
527
543
528
544
InsertStmt *insert_stmt = (InsertStmt *)stmt;
529
-
530
545
Table *table = insert_stmt->table ();
531
- RC rc = table->insert_record (nullptr , insert_stmt->value_amount (), insert_stmt->values ());
546
+
547
+ RC rc = table->insert_record (trx, insert_stmt->value_amount (), insert_stmt->values ());
532
548
if (rc == RC::SUCCESS) {
533
- session_event->set_response (" SUCCESS\n " );
549
+ if (!session->is_trx_multi_operation_mode ()) {
550
+ CLogRecord *clog_record = nullptr ;
551
+ rc = clog_manager->clog_gen_record (CLogType::REDO_MTR_COMMIT, trx->get_current_id (), clog_record);
552
+ if (rc != RC::SUCCESS || clog_record == nullptr ) {
553
+ session_event->set_response (" FAILURE\n " );
554
+ return rc;
555
+ }
556
+
557
+ rc = clog_manager->clog_append_record (clog_record);
558
+ if (rc != RC::SUCCESS) {
559
+ session_event->set_response (" FAILURE\n " );
560
+ return rc;
561
+ }
562
+
563
+ trx->next_current_id ();
564
+ session_event->set_response (" SUCCESS\n " );
565
+ } else {
566
+ session_event->set_response (" SUCCESS\n " );
567
+ }
534
568
} else {
535
569
session_event->set_response (" FAILURE\n " );
536
570
}
@@ -541,6 +575,10 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
541
575
{
542
576
Stmt *stmt = sql_event->stmt ();
543
577
SessionEvent *session_event = sql_event->session_event ();
578
+ Session *session = session_event->session ();
579
+ Db *db = session->get_current_db ();
580
+ Trx *trx = session->current_trx ();
581
+ CLogManager *clog_manager = db->get_clog_manager ();
544
582
545
583
if (stmt == nullptr ) {
546
584
LOG_WARN (" cannot find statement" );
@@ -551,14 +589,106 @@ RC ExecuteStage::do_delete(SQLStageEvent *sql_event)
551
589
TableScanOperator scan_oper (delete_stmt->table ());
552
590
PredicateOperator pred_oper (delete_stmt->filter_stmt ());
553
591
pred_oper.add_child (&scan_oper);
554
- DeleteOperator delete_oper (delete_stmt);
592
+ DeleteOperator delete_oper (delete_stmt, trx );
555
593
delete_oper.add_child (&pred_oper);
556
594
557
595
RC rc = delete_oper.open ();
596
+ if (rc != RC::SUCCESS) {
597
+ session_event->set_response (" FAILURE\n " );
598
+ } else {
599
+ session_event->set_response (" SUCCESS\n " );
600
+ if (!session->is_trx_multi_operation_mode ()) {
601
+ CLogRecord *clog_record = nullptr ;
602
+ rc = clog_manager->clog_gen_record (CLogType::REDO_MTR_COMMIT, trx->get_current_id (), clog_record);
603
+ if (rc != RC::SUCCESS || clog_record == nullptr ) {
604
+ session_event->set_response (" FAILURE\n " );
605
+ return rc;
606
+ }
607
+
608
+ rc = clog_manager->clog_append_record (clog_record);
609
+ if (rc != RC::SUCCESS) {
610
+ session_event->set_response (" FAILURE\n " );
611
+ return rc;
612
+ }
613
+
614
+ trx->next_current_id ();
615
+ session_event->set_response (" SUCCESS\n " );
616
+ }
617
+ }
618
+ return rc;
619
+ }
620
+
621
+ RC ExecuteStage::do_begin (SQLStageEvent *sql_event)
622
+ {
623
+ RC rc = RC::SUCCESS;
624
+ SessionEvent *session_event = sql_event->session_event ();
625
+ Session *session = session_event->session ();
626
+ Db *db = session->get_current_db ();
627
+ Trx *trx = session->current_trx ();
628
+ CLogManager *clog_manager = db->get_clog_manager ();
629
+
630
+ session->set_trx_multi_operation_mode (true );
631
+
632
+ CLogRecord *clog_record = nullptr ;
633
+ rc = clog_manager->clog_gen_record (CLogType::REDO_MTR_BEGIN, trx->get_current_id (), clog_record);
634
+ if (rc != RC::SUCCESS || clog_record == nullptr ) {
635
+ session_event->set_response (" FAILURE\n " );
636
+ return rc;
637
+ }
638
+
639
+ rc = clog_manager->clog_append_record (clog_record);
558
640
if (rc != RC::SUCCESS) {
559
641
session_event->set_response (" FAILURE\n " );
560
642
} else {
561
643
session_event->set_response (" SUCCESS\n " );
562
644
}
645
+
646
+ return rc;
647
+ }
648
+
649
+ RC ExecuteStage::do_commit (SQLStageEvent *sql_event)
650
+ {
651
+ RC rc = RC::SUCCESS;
652
+ SessionEvent *session_event = sql_event->session_event ();
653
+ Session *session = session_event->session ();
654
+ Db *db = session->get_current_db ();
655
+ Trx *trx = session->current_trx ();
656
+ CLogManager *clog_manager = db->get_clog_manager ();
657
+
658
+ session->set_trx_multi_operation_mode (false );
659
+
660
+ CLogRecord *clog_record = nullptr ;
661
+ rc = clog_manager->clog_gen_record (CLogType::REDO_MTR_COMMIT, trx->get_current_id (), clog_record);
662
+ if (rc != RC::SUCCESS || clog_record == nullptr ) {
663
+ session_event->set_response (" FAILURE\n " );
664
+ return rc;
665
+ }
666
+
667
+ rc = clog_manager->clog_append_record (clog_record);
668
+ if (rc != RC::SUCCESS) {
669
+ session_event->set_response (" FAILURE\n " );
670
+ } else {
671
+ session_event->set_response (" SUCCESS\n " );
672
+ }
673
+
674
+ trx->next_current_id ();
675
+
676
+ return rc;
677
+ }
678
+
679
+ RC ExecuteStage::do_clog_sync (SQLStageEvent *sql_event)
680
+ {
681
+ RC rc = RC::SUCCESS;
682
+ SessionEvent *session_event = sql_event->session_event ();
683
+ Db *db = session_event->session ()->get_current_db ();
684
+ CLogManager *clog_manager = db->get_clog_manager ();
685
+
686
+ rc = clog_manager->clog_sync ();
687
+ if (rc != RC::SUCCESS) {
688
+ session_event->set_response (" FAILURE\n " );
689
+ } else {
690
+ session_event->set_response (" SUCCESS\n " );
691
+ }
692
+
563
693
return rc;
564
694
}
0 commit comments