go.ligato.io/vpp-agent/v3@v3.5.0/client/txn.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 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 client 16 17 import ( 18 "context" 19 20 "google.golang.org/protobuf/proto" 21 22 "go.ligato.io/vpp-agent/v3/pkg/models" 23 ) 24 25 // Txn is the 26 type Txn struct { 27 items map[string]proto.Message 28 } 29 30 // NewTxn 31 func NewTxn(commitFunc func() error) *Txn { 32 return &Txn{ 33 items: make(map[string]proto.Message), 34 } 35 } 36 37 // Update updates 38 func (t *Txn) Update(item proto.Message) { 39 t.items[models.Key(item)] = item 40 } 41 42 func (t *Txn) Delete(item proto.Message) { 43 t.items[models.Key(item)] = nil 44 } 45 46 func (t *Txn) Commit(ctx context.Context) { 47 48 } 49 50 // FindItem returns item with given ID from the request items. 51 // If the found is true the model with such ID is found 52 // and if the model is nil the item represents delete. 53 func (t *Txn) FindItem(id string) (model proto.Message, found bool) { 54 item, ok := t.items[id] 55 return item, ok 56 } 57 58 // ListItems returns map of items defined for the request, 59 // where key represents model ID and nil value represents delete. 60 // NOTE: Do not alter the returned map directly. 61 func (t *Txn) ListItems() map[string]proto.Message { 62 return t.items 63 } 64 65 // RemoveItem removes an item from the transaction. 66 // This will revert any Update or Delete done for the item. 67 func (t *Txn) RemoveItem(model proto.Message) { 68 delete(t.items, models.Key(model)) 69 }