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