github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/txn/txnbase/state.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package txnbase
    16  
    17  import (
    18  	"sync/atomic"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  )
    22  
    23  var (
    24  	ErrTransferTransactionState = moerr.NewInternalErrorNoCtx("tae: transfer transaction state error")
    25  )
    26  
    27  const (
    28  	TSUncommitted int32 = iota
    29  	TSCommitting
    30  	TSCommitted
    31  	TSRollbacking
    32  	TSRollbacked
    33  )
    34  
    35  type TxnState struct {
    36  	state int32
    37  }
    38  
    39  func (ts *TxnState) ToCommitting() error {
    40  	if atomic.CompareAndSwapInt32(&ts.state, TSUncommitted, TSCommitting) {
    41  		return nil
    42  	}
    43  	return ErrTransferTransactionState
    44  }
    45  
    46  func (ts *TxnState) ToCommitted() error {
    47  	if atomic.CompareAndSwapInt32(&ts.state, TSCommitting, TSCommitted) {
    48  		return nil
    49  	}
    50  	return ErrTransferTransactionState
    51  }
    52  
    53  func (ts *TxnState) ToRollbacking() error {
    54  	if atomic.CompareAndSwapInt32(&ts.state, TSUncommitted, TSRollbacking) {
    55  		return nil
    56  	}
    57  	return ErrTransferTransactionState
    58  }
    59  
    60  func (ts *TxnState) ToRollbacked() error {
    61  	if atomic.CompareAndSwapInt32(&ts.state, TSRollbacking, TSRollbacked) {
    62  		return nil
    63  	}
    64  	return ErrTransferTransactionState
    65  }
    66  
    67  func (ts *TxnState) IsUncommitted() bool {
    68  	return atomic.LoadInt32(&ts.state) == TSUncommitted
    69  }
    70  
    71  func (ts *TxnState) IsCommitted() bool {
    72  	return atomic.LoadInt32(&ts.state) == TSCommitted
    73  }
    74  
    75  func (ts *TxnState) IsRollbacked() bool {
    76  	return atomic.LoadInt32(&ts.state) == TSRollbacked
    77  }