txn.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2016 The etcd Authors
  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. package clientv3
  15. import (
  16. "context"
  17. "sync"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. )
  20. // Txn is the interface that wraps mini-transactions.
  21. //
  22. // Txn(context.TODO()).If(
  23. // Compare(Value(k1), ">", v1),
  24. // Compare(Version(k1), "=", 2)
  25. // ).Then(
  26. // OpPut(k2,v2), OpPut(k3,v3)
  27. // ).Else(
  28. // OpPut(k4,v4), OpPut(k5,v5)
  29. // ).Commit()
  30. //
  31. type Txn interface {
  32. // If takes a list of comparison. If all comparisons passed in succeed,
  33. // the operations passed into Then() will be executed. Or the operations
  34. // passed into Else() will be executed.
  35. If(cs ...Cmp) Txn
  36. // Then takes a list of operations. The Ops list will be executed, if the
  37. // comparisons passed in If() succeed.
  38. Then(ops ...Op) Txn
  39. // Else takes a list of operations. The Ops list will be executed, if the
  40. // comparisons passed in If() fail.
  41. Else(ops ...Op) Txn
  42. // Commit tries to commit the transaction.
  43. Commit() (*TxnResponse, error)
  44. }
  45. type txn struct {
  46. kv *kv
  47. ctx context.Context
  48. mu sync.Mutex
  49. cif bool
  50. cthen bool
  51. celse bool
  52. isWrite bool
  53. cmps []*pb.Compare
  54. sus []*pb.RequestOp
  55. fas []*pb.RequestOp
  56. }
  57. func (txn *txn) If(cs ...Cmp) Txn {
  58. txn.mu.Lock()
  59. defer txn.mu.Unlock()
  60. if txn.cif {
  61. panic("cannot call If twice!")
  62. }
  63. if txn.cthen {
  64. panic("cannot call If after Then!")
  65. }
  66. if txn.celse {
  67. panic("cannot call If after Else!")
  68. }
  69. txn.cif = true
  70. for i := range cs {
  71. txn.cmps = append(txn.cmps, (*pb.Compare)(&cs[i]))
  72. }
  73. return txn
  74. }
  75. func (txn *txn) Then(ops ...Op) Txn {
  76. txn.mu.Lock()
  77. defer txn.mu.Unlock()
  78. if txn.cthen {
  79. panic("cannot call Then twice!")
  80. }
  81. if txn.celse {
  82. panic("cannot call Then after Else!")
  83. }
  84. txn.cthen = true
  85. for _, op := range ops {
  86. txn.isWrite = txn.isWrite || op.isWrite()
  87. txn.sus = append(txn.sus, op.toRequestOp())
  88. }
  89. return txn
  90. }
  91. func (txn *txn) Else(ops ...Op) Txn {
  92. txn.mu.Lock()
  93. defer txn.mu.Unlock()
  94. if txn.celse {
  95. panic("cannot call Else twice!")
  96. }
  97. txn.celse = true
  98. for _, op := range ops {
  99. txn.isWrite = txn.isWrite || op.isWrite()
  100. txn.fas = append(txn.fas, op.toRequestOp())
  101. }
  102. return txn
  103. }
  104. func (txn *txn) Commit() (*TxnResponse, error) {
  105. txn.mu.Lock()
  106. defer txn.mu.Unlock()
  107. r := &pb.TxnRequest{Compare: txn.cmps, Success: txn.sus, Failure: txn.fas}
  108. var resp *pb.TxnResponse
  109. var err error
  110. resp, err = txn.kv.remote.Txn(txn.ctx, r)
  111. if err != nil {
  112. return nil, toErr(txn.ctx, err)
  113. }
  114. return (*TxnResponse)(resp), nil
  115. }