1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package storage 18 19 // transaction represents something that may need to be finalized on success or 20 // failure of the larger transaction. 21 type transaction interface { 22 // Commit tells the transaction to finalize any changes it may have 23 // pending. This cannot fail, so errors must be handled internally. 24 Commit() 25 26 // Revert tells the transaction to abandon or undo any changes it may have 27 // pending. This cannot fail, so errors must be handled internally. 28 Revert() 29 } 30 31 // metaTransaction is a collection of transactions. 32 type metaTransaction []transaction 33 34 func (mt metaTransaction) Commit() { 35 for _, t := range mt { 36 t.Commit() 37 } 38 } 39 40 func (mt metaTransaction) Revert() { 41 for _, t := range mt { 42 t.Revert() 43 } 44 } 45 46 // callbackTransaction is a transaction which calls arbitrary functions. 47 type callbackTransaction struct { 48 commit func() 49 revert func() 50 } 51 52 func (cb callbackTransaction) Commit() { 53 if cb.commit != nil { 54 cb.commit() 55 } 56 } 57 58 func (cb callbackTransaction) Revert() { 59 if cb.revert != nil { 60 cb.revert() 61 } 62 } 63