You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

undo.go 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package undo
  18. import (
  19. "context"
  20. "database/sql"
  21. "database/sql/driver"
  22. "errors"
  23. "sync"
  24. "github.com/seata/seata-go/pkg/datasource/sql/types"
  25. )
  26. var (
  27. solts = map[types.DBType]*undoLogMgrHolder{}
  28. builders = map[types.ExecutorType]func() UndoLogBuilder{}
  29. )
  30. type undoLogMgrHolder struct {
  31. once sync.Once
  32. mgr UndoLogManager
  33. }
  34. func RegisterUndoLogManager(m UndoLogManager) error {
  35. if _, exist := solts[m.DBType()]; exist {
  36. return nil
  37. }
  38. solts[m.DBType()] = &undoLogMgrHolder{
  39. mgr: m,
  40. once: sync.Once{},
  41. }
  42. return nil
  43. }
  44. func RegisterUndoLogBuilder(executorType types.ExecutorType, fun func() UndoLogBuilder) {
  45. if _, ok := builders[executorType]; !ok {
  46. builders[executorType] = fun
  47. }
  48. }
  49. func GetUndologBuilder(sqlType types.ExecutorType) UndoLogBuilder {
  50. if f, ok := builders[sqlType]; ok {
  51. return f()
  52. }
  53. return nil
  54. }
  55. // UndoLogManager
  56. type UndoLogManager interface {
  57. Init()
  58. // InsertUndoLog
  59. InsertUndoLog(l []BranchUndoLog, tx driver.Conn) error
  60. // DeleteUndoLog
  61. DeleteUndoLog(ctx context.Context, xid string, branchID int64, conn *sql.Conn) error
  62. // BatchDeleteUndoLog
  63. BatchDeleteUndoLog(xid []string, branchID []int64, conn *sql.Conn) error
  64. // FlushUndoLog
  65. FlushUndoLog(txCtx *types.TransactionContext, tx driver.Conn) error
  66. // RunUndo
  67. RunUndo(xid string, branchID int64, conn *sql.Conn) error
  68. // DBType
  69. DBType() types.DBType
  70. // HasUndoLogTable
  71. HasUndoLogTable(ctx context.Context, conn *sql.Conn) (bool, error)
  72. }
  73. // GetUndoLogManager
  74. func GetUndoLogManager(d types.DBType) (UndoLogManager, error) {
  75. v, ok := solts[d]
  76. if !ok {
  77. return nil, errors.New("not found UndoLogManager")
  78. }
  79. v.once.Do(func() {
  80. v.mgr.Init()
  81. })
  82. return v.mgr, nil
  83. }
  84. // BranchUndoLog
  85. type BranchUndoLog struct {
  86. // Xid
  87. Xid string `json:"xid"`
  88. // BranchID
  89. BranchID string `json:"branchId"`
  90. // Logs
  91. Logs []SQLUndoLog `json:"sqlUndoLogs"`
  92. }
  93. // Marshal
  94. func (b *BranchUndoLog) Marshal() []byte {
  95. return nil
  96. }
  97. // SQLUndoLog
  98. type SQLUndoLog struct {
  99. SQLType types.SQLType `json:"sqlType"`
  100. TableName string `json:"tableName"`
  101. Images types.RoundRecordImage `json:"images"`
  102. }
  103. // UndoLogParser
  104. type UndoLogParser interface {
  105. // GetName
  106. GetName() string
  107. // GetDefaultContent
  108. GetDefaultContent() []byte
  109. // Encode
  110. Encode(l BranchUndoLog) []byte
  111. // Decode
  112. Decode(b []byte) BranchUndoLog
  113. }
  114. type UndoLogBuilder interface {
  115. BeforeImage(ctx context.Context, execCtx *types.ExecContext) ([]*types.RecordImage, error)
  116. AfterImage(ctx context.Context, execCtx *types.ExecContext, beforImages []*types.RecordImage) ([]*types.RecordImage, error)
  117. GetExecutorType() types.ExecutorType
  118. }