github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/txn_wrapper.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 moengine 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/pb/txn" 22 "github.com/matrixorigin/matrixone/pkg/txn/client" 23 "github.com/matrixorigin/matrixone/pkg/txn/rpc" 24 ) 25 26 type wrappedEngine struct { 27 engine TxnEngine 28 } 29 30 func EngineToTxnClient(engine TxnEngine) client.TxnClient { 31 return &wrappedEngine{ 32 engine: engine, 33 } 34 } 35 36 var _ client.TxnClient = new(wrappedEngine) 37 38 func (w *wrappedEngine) New(options ...client.TxnOption) (client.TxnOperator, error) { 39 tx, err := w.engine.StartTxn(nil) 40 if err != nil { 41 panic(err) 42 } 43 return &wrappedTx{ 44 tx: tx, 45 }, nil 46 } 47 48 func (w *wrappedEngine) NewWithSnapshot(snapshot []byte) (client.TxnOperator, error) { 49 txn := w.engine.(*txnEngine).impl.TxnMgr.GetTxnByCtx(snapshot) 50 if txn == nil { 51 return nil, moerr.NewMissingTxnNoCtx() 52 } 53 return TxnToTxnOperator(txn), nil 54 } 55 56 func (w *wrappedEngine) Close() error { 57 return w.engine.Close() 58 } 59 60 type wrappedTx struct { 61 tx Txn 62 } 63 64 func TxnToTxnOperator(tx Txn) client.TxnOperator { 65 return &wrappedTx{ 66 tx: tx, 67 } 68 } 69 70 var _ client.TxnOperator = new(wrappedTx) 71 72 func (w *wrappedTx) ApplySnapshot(data []byte) error { 73 panic("should not call") 74 } 75 76 func (w *wrappedTx) Commit(ctx context.Context) error { 77 return w.tx.Commit() 78 } 79 80 func (*wrappedTx) Read(ctx context.Context, ops []txn.TxnRequest) (*rpc.SendResult, error) { 81 panic("should not call") 82 } 83 84 func (w *wrappedTx) Rollback(ctx context.Context) error { 85 return w.tx.Rollback() 86 } 87 88 func (w *wrappedTx) Snapshot() ([]byte, error) { 89 return w.tx.GetCtx(), nil 90 } 91 92 func (*wrappedTx) Write(ctx context.Context, ops []txn.TxnRequest) (*rpc.SendResult, error) { 93 panic("should not call") 94 } 95 96 func (*wrappedTx) WriteAndCommit(ctx context.Context, ops []txn.TxnRequest) (*rpc.SendResult, error) { 97 panic("should not call") 98 } 99 100 func (w *wrappedTx) Txn() txn.TxnMeta { 101 return txn.TxnMeta{ 102 ID: w.tx.GetCtx(), 103 } 104 }