Matt Jeanneret | cab955f | 2019-04-10 15:45:57 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 model |
| 18 | |
| 19 | import ( |
| 20 | "github.com/opencord/voltha-go/common/log" |
| 21 | "sync" |
| 22 | ) |
| 23 | |
| 24 | // TODO: implement weak references or something equivalent |
| 25 | // TODO: missing proper logging |
| 26 | |
| 27 | // Branch structure is used to classify a collection of transaction based revisions |
| 28 | type Branch struct { |
| 29 | sync.RWMutex |
| 30 | Node *node |
| 31 | Txid string |
| 32 | Origin Revision |
| 33 | Revisions map[string]Revision |
| 34 | Latest Revision |
| 35 | } |
| 36 | |
| 37 | // NewBranch creates a new instance of the Branch structure |
| 38 | func NewBranch(node *node, txid string, origin Revision, autoPrune bool) *Branch { |
| 39 | b := &Branch{} |
| 40 | b.Node = node |
| 41 | b.Txid = txid |
| 42 | b.Origin = origin |
| 43 | b.Revisions = make(map[string]Revision) |
| 44 | b.Latest = origin |
| 45 | |
| 46 | return b |
| 47 | } |
| 48 | |
| 49 | // SetLatest assigns the latest revision for this branch |
| 50 | func (b *Branch) SetLatest(latest Revision) { |
| 51 | b.Lock() |
| 52 | defer b.Unlock() |
| 53 | |
| 54 | if b.Latest != nil { |
| 55 | log.Debugf("Switching latest from <%s> to <%s>", b.Latest.GetHash(), latest.GetHash()) |
| 56 | } else { |
| 57 | log.Debugf("Switching latest from <NIL> to <%s>", latest.GetHash()) |
| 58 | } |
| 59 | |
| 60 | |
| 61 | b.Latest = latest |
| 62 | } |
| 63 | |
| 64 | // GetLatest retrieves the latest revision of the branch |
| 65 | func (b *Branch) GetLatest() Revision { |
| 66 | b.Lock() |
| 67 | defer b.Unlock() |
| 68 | |
| 69 | return b.Latest |
| 70 | } |
| 71 | |
| 72 | // GetOrigin retrieves the original revision of the branch |
| 73 | func (b *Branch) GetOrigin() Revision { |
| 74 | b.Lock() |
| 75 | defer b.Unlock() |
| 76 | |
| 77 | return b.Origin |
| 78 | } |
| 79 | |
| 80 | // AddRevision inserts a new revision to the branch |
| 81 | func (b *Branch) AddRevision(revision Revision) { |
| 82 | if revision != nil && b.GetRevision(revision.GetHash()) == nil { |
| 83 | b.SetRevision(revision.GetHash(), revision) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // GetRevision pulls a revision entry at the specified hash |
| 88 | func (b *Branch) GetRevision(hash string) Revision { |
| 89 | b.Lock() |
| 90 | defer b.Unlock() |
| 91 | |
| 92 | if revision, ok := b.Revisions[hash]; ok { |
| 93 | return revision |
| 94 | } |
| 95 | |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // SetRevision updates a revision entry at the specified hash |
| 100 | func (b *Branch) SetRevision(hash string, revision Revision) { |
| 101 | b.Lock() |
| 102 | defer b.Unlock() |
| 103 | |
| 104 | b.Revisions[hash] = revision |
| 105 | } |