blob: 3408f18c8c6bfa8b6b5a415f17fcf51bdec41df3 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
Stephane Barbariedc5022d2018-11-19 15:21:44 -050019import "sync"
20
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040021// TODO: implement weak references or something equivalent
22// TODO: missing proper logging
23
Stephane Barbariedc5022d2018-11-19 15:21:44 -050024// Branch structure is used to classify a collection of transaction based revisions
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025type Branch struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026 sync.RWMutex
Stephane Barbarie06c4a742018-10-01 11:09:32 -040027 Node *node
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028 Txid string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040029 Origin Revision
30 Revisions map[string]Revision
31 Latest Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040032}
33
Stephane Barbariedc5022d2018-11-19 15:21:44 -050034// NewBranch creates a new instance of the Branch structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040035func NewBranch(node *node, txid string, origin Revision, autoPrune bool) *Branch {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050036 b := &Branch{}
37 b.Node = node
38 b.Txid = txid
39 b.Origin = origin
40 b.Revisions = make(map[string]Revision)
41 b.Latest = origin
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040042
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043 return b
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040044}
45
Stephane Barbariedc5022d2018-11-19 15:21:44 -050046// SetLatest assigns the latest revision for this branch
47func (b *Branch) SetLatest(latest Revision) {
48 b.Lock()
49 defer b.Unlock()
50
51 b.Latest = latest
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040052}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050053
54// GetLatest retrieves the latest revision of the branch
55func (b *Branch) GetLatest() Revision {
56 b.Lock()
57 defer b.Unlock()
58
59 return b.Latest
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040060}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050061
62// GetOrigin retrieves the original revision of the branch
63func (b *Branch) GetOrigin() Revision {
64 b.Lock()
65 defer b.Unlock()
66
67 return b.Origin
68}
69
70// AddRevision inserts a new revision to the branch
71func (b *Branch) AddRevision(revision Revision) {
72 if revision != nil && b.GetRevision(revision.GetHash()) == nil {
73 b.SetRevision(revision.GetHash(), revision)
74 }
75}
76
77// GetRevision pulls a revision entry at the specified hash
78func (b *Branch) GetRevision(hash string) Revision {
79 b.Lock()
80 defer b.Unlock()
81
82 if revision, ok := b.Revisions[hash]; !ok {
83 return revision
84 }
85
86 return nil
87}
88
89// SetRevision updates a revision entry at the specified hash
90func (b *Branch) SetRevision(hash string, revision Revision) {
91 b.Lock()
92 defer b.Unlock()
93
94 b.Revisions[hash] = revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040095}