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 | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 19 | // TODO: proper error handling |
| 20 | // TODO: proper logging |
| 21 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 22 | import ( |
| 23 | "fmt" |
| 24 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 26 | "reflect" |
| 27 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 28 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 31 | // When a branch has no transaction id, everything gets stored in NONE |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 32 | const ( |
| 33 | NONE string = "none" |
| 34 | ) |
| 35 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 36 | // Node interface is an abstraction of the node data structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 37 | type Node interface { |
| 38 | MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) |
| 39 | |
| 40 | // CRUD functions |
| 41 | Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision |
| 42 | Get(path string, hash string, depth int, deep bool, txid string) interface{} |
| 43 | Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision |
| 44 | Remove(path string, txid string, makeBranch MakeBranchFunction) Revision |
| 45 | |
| 46 | MakeBranch(txid string) *Branch |
| 47 | DeleteBranch(txid string) |
| 48 | MergeBranch(txid string, dryRun bool) (Revision, error) |
| 49 | |
| 50 | MakeTxBranch() string |
| 51 | DeleteTxBranch(txid string) |
| 52 | FoldTxBranch(txid string) |
| 53 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 54 | CreateProxy(path string, exclusive bool) *Proxy |
| 55 | GetProxy() *Proxy |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | type node struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 59 | sync.RWMutex |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 60 | Root *root |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | Type interface{} |
| 62 | Branches map[string]*Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | Tags map[string]Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 64 | Proxy *Proxy |
| 65 | EventBus *EventBus |
| 66 | AutoPrune bool |
| 67 | } |
| 68 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 69 | // ChangeTuple holds details of modifications made to a revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 70 | type ChangeTuple struct { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 71 | Type CallbackType |
| 72 | PreviousData interface{} |
| 73 | LatestData interface{} |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 76 | // NewNode creates a new instance of the node data structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 77 | func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node { |
| 78 | n := &node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 79 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 80 | n.Root = root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 81 | n.Branches = make(map[string]*Branch) |
| 82 | n.Tags = make(map[string]Revision) |
| 83 | n.Proxy = nil |
| 84 | n.EventBus = nil |
| 85 | n.AutoPrune = autoPrune |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 86 | |
| 87 | if IsProtoMessage(initialData) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 88 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 89 | dataCopy := proto.Clone(initialData.(proto.Message)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 90 | n.initialize(dataCopy, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 91 | } else if reflect.ValueOf(initialData).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | // FIXME: this block does not reflect the original implementation |
| 93 | // it should be checking if the provided initial_data is already a type!??! |
| 94 | // it should be checked before IsProtoMessage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 95 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 96 | } else { |
| 97 | // not implemented error |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 98 | log.Errorf("cannot process initial data - %+v", initialData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 101 | return n |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 104 | // MakeNode creates a new node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 105 | func (n *node) MakeNode(data interface{}, txid string) *node { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 106 | return NewNode(n.Root, data, true, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 109 | // MakeRevision create a new revision of the node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 110 | func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | return n.GetRoot().MakeRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | // makeLatest will mark the revision of a node as being the latest |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 115 | func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 116 | n.Lock() |
| 117 | defer n.Unlock() |
| 118 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 119 | // Keep a reference to the current revision |
| 120 | var previous string |
| 121 | if branch.GetLatest() != nil { |
| 122 | previous = branch.GetLatest().GetHash() |
| 123 | } |
| 124 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 125 | branch.AddRevision(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 126 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 127 | // If anything is new, then set the revision as the latest |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 128 | if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 129 | if revision.GetName() != "" { |
| 130 | GetRevCache().Cache.Store(revision.GetName(), revision) |
| 131 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 132 | branch.SetLatest(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 135 | // Delete the previous revision if anything has changed |
| 136 | if previous != "" && previous != branch.GetLatest().GetHash() { |
| 137 | branch.DeleteRevision(previous) |
| 138 | } |
| 139 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 140 | if changeAnnouncement != nil && branch.Txid == "" { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 141 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 142 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 143 | log.Debugw("adding-callback", |
| 144 | log.Fields{ |
| 145 | "callbacks": n.Proxy.getCallbacks(change.Type), |
| 146 | "type": change.Type, |
| 147 | "previousData": change.PreviousData, |
| 148 | "latestData": change.LatestData, |
| 149 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 150 | n.Root.AddCallback( |
| 151 | n.Proxy.InvokeCallbacks, |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 152 | change.Type, |
| 153 | true, |
| 154 | change.PreviousData, |
| 155 | change.LatestData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 161 | // Latest returns the latest revision of node with or without the transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 162 | func (n *node) Latest(txid ...string) Revision { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 163 | var branch *Branch |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 164 | |
| 165 | if len(txid) > 0 && txid[0] != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 166 | if branch = n.GetBranch(txid[0]); branch != nil { |
| 167 | return branch.GetLatest() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 168 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 169 | } else if branch = n.GetBranch(NONE); branch != nil { |
| 170 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 171 | } |
| 172 | return nil |
| 173 | } |
| 174 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 175 | // initialize prepares the content of a node along with its possible ramifications |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 176 | func (n *node) initialize(data interface{}, txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 177 | n.Lock() |
| 178 | children := make(map[string][]Revision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 179 | for fieldName, field := range ChildrenFields(n.Type) { |
| 180 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 181 | |
| 182 | if fieldValue.IsValid() { |
| 183 | if field.IsContainer { |
| 184 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 185 | for i := 0; i < fieldValue.Len(); i++ { |
| 186 | v := fieldValue.Index(i) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 187 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 188 | if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil { |
| 189 | children[fieldName] = append(children[fieldName], rev) |
| 190 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 191 | |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 192 | // TODO: The following logic was ported from v1.0. Need to verify if it is required |
| 193 | //var keysSeen []string |
| 194 | //_, key := GetAttributeValue(v.Interface(), field.Key, 0) |
| 195 | //for _, k := range keysSeen { |
| 196 | // if k == key.String() { |
| 197 | // //log.Errorf("duplicate key - %s", k) |
| 198 | // } |
| 199 | //} |
| 200 | //keysSeen = append(keysSeen, key.String()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | } else { |
| 204 | for i := 0; i < fieldValue.Len(); i++ { |
| 205 | v := fieldValue.Index(i) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 206 | if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil { |
| 207 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 208 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 212 | if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil { |
| 213 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 214 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 215 | } |
| 216 | } else { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 217 | log.Errorf("field is invalid - %+v", fieldValue) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 218 | } |
| 219 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 220 | n.Unlock() |
| 221 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 222 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 223 | rev := n.MakeRevision(branch, data, children) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 224 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 225 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 226 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 227 | n.SetBranch(NONE, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 228 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 229 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 230 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 233 | // findRevByKey retrieves a specific revision from a node tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 234 | func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 235 | n.Lock() |
| 236 | defer n.Unlock() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 237 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 238 | for i, rev := range revs { |
| 239 | dataValue := reflect.ValueOf(rev.GetData()) |
| 240 | dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0) |
| 241 | |
| 242 | fieldValue := dataValue.Elem().FieldByName(dataStruct.Name) |
| 243 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 244 | a := fmt.Sprintf("%s", fieldValue.Interface()) |
| 245 | b := fmt.Sprintf("%s", value) |
| 246 | if a == b { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 247 | return i, revs[i] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 251 | return -1, nil |
| 252 | } |
| 253 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 254 | // Get retrieves the data from a node tree that resides at the specified path |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 255 | func (n *node) List(path string, hash string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 256 | log.Debugw("node-list-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 257 | if deep { |
| 258 | depth = -1 |
| 259 | } |
| 260 | |
| 261 | for strings.HasPrefix(path, "/") { |
| 262 | path = path[1:] |
| 263 | } |
| 264 | |
| 265 | var branch *Branch |
| 266 | var rev Revision |
| 267 | |
| 268 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 269 | branch = n.GetBranch(NONE) |
| 270 | } |
| 271 | |
| 272 | if hash != "" { |
| 273 | rev = branch.GetRevision(hash) |
| 274 | } else { |
| 275 | rev = branch.GetLatest() |
| 276 | } |
| 277 | |
| 278 | var result interface{} |
| 279 | var prList []interface{} |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 280 | if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 281 | for _, revEntry := range pr { |
| 282 | prList = append(prList, revEntry.GetData()) |
| 283 | } |
| 284 | result = prList |
| 285 | } |
| 286 | |
| 287 | return result |
| 288 | } |
| 289 | |
| 290 | // Get retrieves the data from a node tree that resides at the specified path |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 291 | func (n *node) Get(path string, hash string, depth int, reconcile bool, txid string) interface{} { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 292 | log.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 293 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 294 | for strings.HasPrefix(path, "/") { |
| 295 | path = path[1:] |
| 296 | } |
| 297 | |
| 298 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 299 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 300 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 301 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 302 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if hash != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 306 | rev = branch.GetRevision(hash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 307 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 308 | rev = branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 311 | var result interface{} |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 312 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 313 | // If there is no request to reconcile, try to get it from memory |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 314 | if !reconcile { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 315 | // Try to find an entry matching the path value from one of these sources |
| 316 | // 1. Start with the cache which stores revisions by watch names |
| 317 | // 2. Then look in the revision tree, especially if it's a sub-path such as /devices/1234/flows |
| 318 | // 3. As a last effort, move on to the KV store |
| 319 | if entry, exists := GetRevCache().Cache.Load(path); exists && entry.(Revision) != nil { |
| 320 | return proto.Clone(entry.(Revision).GetData().(proto.Message)) |
| 321 | } else if result = n.getPath(rev.GetBranch().GetLatest(), path, depth); result != nil && reflect.ValueOf(result).IsValid() && !reflect.ValueOf(result).IsNil() { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 322 | return result |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // If we got to this point, we are either trying to reconcile with the db or |
| 327 | // or we simply failed at getting information from memory |
| 328 | if n.Root.KvStore != nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 329 | if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil && len(pr) > 0 { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 330 | // Did we receive a single or multiple revisions? |
| 331 | if len(pr) > 1 { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 332 | var revs []interface{} |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 333 | for _, revEntry := range pr { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 334 | revs = append(revs, revEntry.GetData()) |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 335 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 336 | result = revs |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 337 | } else { |
| 338 | result = pr[0].GetData() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 339 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 346 | //getPath traverses the specified path and retrieves the data associated to it |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 347 | func (n *node) getPath(rev Revision, path string, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 348 | if path == "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 349 | return n.getData(rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | partition := strings.SplitN(path, "/", 2) |
| 353 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 354 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 355 | if len(partition) < 2 { |
| 356 | path = "" |
| 357 | } else { |
| 358 | path = partition[1] |
| 359 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 360 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 361 | names := ChildrenFields(n.Type) |
| 362 | field := names[name] |
| 363 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 364 | if field != nil && field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 365 | children := make([]Revision, len(rev.GetChildren(name))) |
| 366 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 367 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 368 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 369 | if path != "" { |
| 370 | partition = strings.SplitN(path, "/", 2) |
| 371 | key := partition[0] |
| 372 | path = "" |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 373 | keyValue := field.KeyFromStr(key) |
| 374 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 375 | return nil |
| 376 | } else { |
| 377 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 378 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 379 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 380 | } else { |
| 381 | var response []interface{} |
| 382 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 383 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 384 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 385 | response = append(response, value) |
| 386 | } |
| 387 | return response |
| 388 | } |
| 389 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 390 | var response []interface{} |
| 391 | if path != "" { |
| 392 | // TODO: raise error |
| 393 | return response |
| 394 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 395 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 396 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 397 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 398 | response = append(response, value) |
| 399 | } |
| 400 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 403 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 404 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 405 | childNode := childRev.GetNode() |
| 406 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 407 | } |
| 408 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 409 | // getData retrieves the data from a node revision |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 410 | func (n *node) getData(rev Revision, depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 411 | msg := rev.GetBranch().GetLatest().Get(depth) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 412 | var modifiedMsg interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 413 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 414 | if n.GetProxy() != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 415 | log.Debugw("invoking-get-callbacks", log.Fields{"data": msg}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 416 | if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 417 | msg = modifiedMsg |
| 418 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 419 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 420 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 421 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 422 | return msg |
| 423 | } |
| 424 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 425 | // Update changes the content of a node at the specified path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 426 | func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 427 | log.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 428 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 429 | for strings.HasPrefix(path, "/") { |
| 430 | path = path[1:] |
| 431 | } |
| 432 | |
| 433 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 434 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 435 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 436 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 437 | branch = makeBranch(n) |
| 438 | } |
| 439 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 440 | if branch.GetLatest() != nil { |
| 441 | log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data) |
| 442 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 443 | if path == "" { |
| 444 | return n.doUpdate(branch, data, strict) |
| 445 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 446 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 447 | rev := branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 448 | |
| 449 | partition := strings.SplitN(path, "/", 2) |
| 450 | name := partition[0] |
| 451 | |
| 452 | if len(partition) < 2 { |
| 453 | path = "" |
| 454 | } else { |
| 455 | path = partition[1] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 458 | field := ChildrenFields(n.Type)[name] |
| 459 | var children []Revision |
| 460 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 461 | if field == nil { |
| 462 | return n.doUpdate(branch, data, strict) |
| 463 | } |
| 464 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 465 | if field.IsContainer { |
| 466 | if path == "" { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 467 | log.Errorf("cannot update a list") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 468 | } else if field.Key != "" { |
| 469 | partition := strings.SplitN(path, "/", 2) |
| 470 | key := partition[0] |
| 471 | if len(partition) < 2 { |
| 472 | path = "" |
| 473 | } else { |
| 474 | path = partition[1] |
| 475 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 476 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 477 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 478 | children = make([]Revision, len(rev.GetChildren(name))) |
| 479 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 480 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 481 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 482 | |
| 483 | if childRev == nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 484 | log.Debugw("child-revision-is-nil", log.Fields{"key": keyValue}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 485 | return branch.GetLatest() |
| 486 | } |
| 487 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 488 | childNode := childRev.GetNode() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 489 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 490 | // Save proxy in child node to ensure callbacks are called later on |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 491 | // only assign in cases of non sub-folder proxies, i.e. "/" |
| 492 | if childNode.Proxy == nil && n.Proxy != nil && n.Proxy.getFullPath() == "" { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 493 | childNode.Proxy = n.Proxy |
| 494 | } |
| 495 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 496 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 497 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 498 | if newChildRev.GetHash() == childRev.GetHash() { |
| 499 | if newChildRev != childRev { |
| 500 | log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev) |
| 501 | newChildRev.ClearHash() |
| 502 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 503 | log.Debugw("child-revisions-have-matching-hash", log.Fields{"hash": childRev.GetHash(), "key": keyValue}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 504 | return branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 505 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 506 | |
| 507 | _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 508 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 509 | _newKeyType := fmt.Sprintf("%s", newKey) |
| 510 | _keyValueType := fmt.Sprintf("%s", keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 511 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 512 | if _newKeyType != _keyValueType { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 513 | log.Errorf("cannot change key field") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 514 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 515 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 516 | // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 517 | newChildRev.SetName(name + "/" + _keyValueType) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 518 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 519 | branch.LatestLock.Lock() |
| 520 | defer branch.LatestLock.Unlock() |
| 521 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 522 | if idx >= 0 { |
| 523 | children[idx] = newChildRev |
| 524 | } else { |
| 525 | children = append(children, newChildRev) |
| 526 | } |
| 527 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 528 | updatedRev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 529 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 530 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 531 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 532 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 533 | return newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 534 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 535 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 536 | log.Errorf("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 537 | } |
| 538 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 539 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 540 | childNode := childRev.GetNode() |
| 541 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 542 | |
| 543 | branch.LatestLock.Lock() |
| 544 | defer branch.LatestLock.Unlock() |
| 545 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 546 | updatedRev := rev.UpdateChildren(name, []Revision{newChildRev}, branch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 547 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 548 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 549 | updatedRev.ChildDrop(name, childRev.GetHash()) |
| 550 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 551 | return newChildRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 552 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 553 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 554 | return nil |
| 555 | } |
| 556 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 557 | func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 558 | log.Debugw("comparing-types", log.Fields{"expected": reflect.ValueOf(n.Type).Type(), "actual": reflect.TypeOf(data)}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 559 | |
| 560 | if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() { |
| 561 | // TODO raise error |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 562 | log.Errorw("types-do-not-match: %+v", log.Fields{"actual": reflect.TypeOf(data), "expected": n.Type}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 563 | return nil |
| 564 | } |
| 565 | |
| 566 | // TODO: validate that this actually works |
| 567 | //if n.hasChildren(data) { |
| 568 | // return nil |
| 569 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 570 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 571 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 572 | log.Debug("invoking proxy PRE_UPDATE Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 573 | n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 574 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 575 | |
| 576 | if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 577 | if strict { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 578 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 579 | log.Debugf("checking access violations") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 580 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 581 | |
| 582 | rev := branch.GetLatest().UpdateData(data, branch) |
| 583 | changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 584 | n.makeLatest(branch, rev, changes) |
| 585 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 586 | return rev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 587 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 588 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 591 | // Add inserts a new node at the specified path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 592 | func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 593 | log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 594 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 595 | for strings.HasPrefix(path, "/") { |
| 596 | path = path[1:] |
| 597 | } |
| 598 | if path == "" { |
| 599 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 600 | log.Errorf("cannot add for non-container mode") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 601 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 605 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 606 | branch = n.GetBranch(NONE) |
| 607 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 608 | branch = makeBranch(n) |
| 609 | } |
| 610 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 611 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 612 | |
| 613 | partition := strings.SplitN(path, "/", 2) |
| 614 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 615 | |
| 616 | if len(partition) < 2 { |
| 617 | path = "" |
| 618 | } else { |
| 619 | path = partition[1] |
| 620 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 621 | |
| 622 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 623 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 624 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 625 | |
| 626 | if field.IsContainer { |
| 627 | if path == "" { |
| 628 | if field.Key != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 629 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 630 | log.Debug("invoking proxy PRE_ADD Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 631 | n.GetProxy().InvokeCallbacks(PRE_ADD, false, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 634 | children = make([]Revision, len(rev.GetChildren(name))) |
| 635 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 636 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 637 | _, key := GetAttributeValue(data, field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 638 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 639 | if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 640 | // TODO raise error |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 641 | log.Warnw("duplicate-key-found", log.Fields{"key": key.String()}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 642 | return exists |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 643 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 644 | childRev := n.MakeNode(data, "").Latest() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 645 | |
| 646 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 647 | childRev.SetName(name + "/" + key.String()) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 648 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 649 | branch.LatestLock.Lock() |
| 650 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 651 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 652 | children = append(children, childRev) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 653 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 654 | updatedRev := rev.UpdateChildren(name, children, branch) |
| 655 | changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}} |
| 656 | childRev.SetupWatch(childRev.GetName()) |
| 657 | |
| 658 | n.makeLatest(branch, updatedRev, changes) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 659 | |
| 660 | return childRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 661 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 662 | log.Errorf("cannot add to non-keyed container") |
| 663 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 664 | } else if field.Key != "" { |
| 665 | partition := strings.SplitN(path, "/", 2) |
| 666 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 667 | if len(partition) < 2 { |
| 668 | path = "" |
| 669 | } else { |
| 670 | path = partition[1] |
| 671 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 672 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 673 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 674 | children = make([]Revision, len(rev.GetChildren(name))) |
| 675 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 676 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 677 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 678 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 679 | if childRev == nil { |
| 680 | return branch.GetLatest() |
| 681 | } |
| 682 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 683 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 684 | newChildRev := childNode.Add(path, data, txid, makeBranch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 685 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 686 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 687 | newChildRev.SetName(name + "/" + keyValue.(string)) |
| 688 | |
| 689 | branch.LatestLock.Lock() |
| 690 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 691 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 692 | if idx >= 0 { |
| 693 | children[idx] = newChildRev |
| 694 | } else { |
| 695 | children = append(children, newChildRev) |
| 696 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 697 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 698 | updatedRev := rev.UpdateChildren(name, children, branch) |
| 699 | n.makeLatest(branch, updatedRev, nil) |
| 700 | |
| 701 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 702 | |
| 703 | return newChildRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 704 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 705 | log.Errorf("cannot add to non-keyed container") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 706 | } |
| 707 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 708 | log.Errorf("cannot add to non-container field") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 709 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 710 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 711 | return nil |
| 712 | } |
| 713 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 714 | // Remove eliminates a node at the specified path |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 715 | func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 716 | log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 717 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 718 | for strings.HasPrefix(path, "/") { |
| 719 | path = path[1:] |
| 720 | } |
| 721 | if path == "" { |
| 722 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 723 | log.Errorf("cannot remove for non-container mode") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 724 | } |
| 725 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 726 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 727 | branch = n.GetBranch(NONE) |
| 728 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 729 | branch = makeBranch(n) |
| 730 | } |
| 731 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 732 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 733 | |
| 734 | partition := strings.SplitN(path, "/", 2) |
| 735 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 736 | if len(partition) < 2 { |
| 737 | path = "" |
| 738 | } else { |
| 739 | path = partition[1] |
| 740 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 741 | |
| 742 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 743 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 744 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 745 | |
| 746 | if field.IsContainer { |
| 747 | if path == "" { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 748 | log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 749 | } else if field.Key != "" { |
| 750 | partition := strings.SplitN(path, "/", 2) |
| 751 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 752 | if len(partition) < 2 { |
| 753 | path = "" |
| 754 | } else { |
| 755 | path = partition[1] |
| 756 | } |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 757 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 758 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 759 | children = make([]Revision, len(rev.GetChildren(name))) |
| 760 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 761 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 762 | if path != "" { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 763 | if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 764 | childNode := childRev.GetNode() |
| 765 | if childNode.Proxy == nil { |
| 766 | childNode.Proxy = n.Proxy |
| 767 | } |
| 768 | newChildRev := childNode.Remove(path, txid, makeBranch) |
| 769 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 770 | branch.LatestLock.Lock() |
| 771 | defer branch.LatestLock.Unlock() |
| 772 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 773 | if idx >= 0 { |
| 774 | children[idx] = newChildRev |
| 775 | } else { |
| 776 | children = append(children, newChildRev) |
| 777 | } |
| 778 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 779 | rev.SetChildren(name, children) |
| 780 | branch.GetLatest().Drop(txid, false) |
| 781 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 782 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 783 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 784 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 785 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 786 | if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil && idx >= 0 { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 787 | if n.GetProxy() != nil { |
| 788 | data := childRev.GetData() |
| 789 | n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data) |
| 790 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil}) |
| 791 | } else { |
| 792 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil}) |
| 793 | } |
| 794 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 795 | childRev.StorageDrop(txid, true) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 796 | GetRevCache().Cache.Delete(childRev.GetName()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 797 | |
| 798 | branch.LatestLock.Lock() |
| 799 | defer branch.LatestLock.Unlock() |
| 800 | |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 801 | children = append(children[:idx], children[idx+1:]...) |
| 802 | rev.SetChildren(name, children) |
| 803 | |
| 804 | branch.GetLatest().Drop(txid, false) |
| 805 | n.makeLatest(branch, rev, postAnnouncement) |
| 806 | |
| 807 | return rev |
| 808 | } else { |
| 809 | log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)}) |
| 810 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 811 | } |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 812 | log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 813 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 814 | } else { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 815 | log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | return nil |
| 819 | } |
| 820 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 821 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 822 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 823 | // MakeBranchFunction is a type for function references intented to create a branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 824 | type MakeBranchFunction func(*node) *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 825 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 826 | // MakeBranch creates a new branch for the provided transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 827 | func (n *node) MakeBranch(txid string) *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 828 | branchPoint := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 829 | branch := NewBranch(n, txid, branchPoint, true) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 830 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 831 | return branch |
| 832 | } |
| 833 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 834 | // DeleteBranch removes a branch with the specified id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 835 | func (n *node) DeleteBranch(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 836 | n.Lock() |
| 837 | defer n.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 838 | delete(n.Branches, txid) |
| 839 | } |
| 840 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 841 | func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 842 | f := func(rev Revision) Revision { |
| 843 | childBranch := rev.GetBranch() |
| 844 | |
| 845 | if childBranch.Txid == txid { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 846 | rev, _ = childBranch.Node.MergeBranch(txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return rev |
| 850 | } |
| 851 | return f |
| 852 | } |
| 853 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 854 | // MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 855 | func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 856 | srcBranch := n.GetBranch(txid) |
| 857 | dstBranch := n.GetBranch(NONE) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 858 | |
| 859 | forkRev := srcBranch.Origin |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 860 | srcRev := srcBranch.GetLatest() |
| 861 | dstRev := dstBranch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 862 | |
| 863 | rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun) |
| 864 | |
| 865 | if !dryRun { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 866 | if rev != nil { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 867 | rev.SetName(dstRev.GetName()) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 868 | n.makeLatest(dstBranch, rev, changes) |
| 869 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 870 | n.DeleteBranch(txid) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 871 | } |
| 872 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 873 | // TODO: return proper error when one occurs |
| 874 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 878 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 879 | //func (n *node) diff(hash1, hash2, txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 880 | // branch := n.Branches[txid] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 881 | // rev1 := branch.GetHash(hash1) |
| 882 | // rev2 := branch.GetHash(hash2) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 883 | // |
| 884 | // if rev1.GetHash() == rev2.GetHash() { |
| 885 | // // empty patch |
| 886 | // } else { |
| 887 | // // translate data to json and generate patch |
| 888 | // patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData()) |
| 889 | // patch. |
| 890 | // } |
| 891 | //} |
| 892 | |
| 893 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 894 | |
| 895 | // TODO: is tag mgmt used in the python implementation? Need to validate |
| 896 | |
| 897 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 898 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 899 | func (n *node) hasChildren(data interface{}) bool { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 900 | for fieldName, field := range ChildrenFields(n.Type) { |
| 901 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
| 902 | |
| 903 | if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() { |
| 904 | log.Error("cannot update external children") |
| 905 | return true |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return false |
| 910 | } |
| 911 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 912 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 913 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 914 | // CreateProxy returns a reference to a sub-tree of the data model |
| 915 | func (n *node) CreateProxy(path string, exclusive bool) *Proxy { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 916 | return n.createProxy(path, path, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 917 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 918 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 919 | func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 920 | for strings.HasPrefix(path, "/") { |
| 921 | path = path[1:] |
| 922 | } |
| 923 | if path == "" { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 924 | return n.makeProxy(path, fullPath, parentNode, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 927 | rev := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 928 | partition := strings.SplitN(path, "/", 2) |
| 929 | name := partition[0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 930 | if len(partition) < 2 { |
| 931 | path = "" |
| 932 | } else { |
| 933 | path = partition[1] |
| 934 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 935 | |
| 936 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 937 | if field.IsContainer { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 938 | if path == "" { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 939 | //log.Error("cannot proxy a container field") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 940 | newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "") |
| 941 | return newNode.makeProxy(path, fullPath, parentNode, exclusive) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 942 | } else if field.Key != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 943 | partition := strings.SplitN(path, "/", 2) |
| 944 | key := partition[0] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 945 | if len(partition) < 2 { |
| 946 | path = "" |
| 947 | } else { |
| 948 | path = partition[1] |
| 949 | } |
| 950 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 951 | var children []Revision |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 952 | children = make([]Revision, len(rev.GetChildren(name))) |
| 953 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 954 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 955 | childNode := childRev.GetNode() |
| 956 | return childNode.createProxy(path, fullPath, n, exclusive) |
| 957 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 958 | } else { |
| 959 | log.Error("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 960 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 961 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 962 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 963 | childNode := childRev.GetNode() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 964 | return childNode.createProxy(path, fullPath, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 965 | } |
| 966 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 967 | log.Warnf("Cannot create proxy - latest rev:%s, all revs:%+v", rev.GetHash(), n.GetBranch(NONE).Revisions) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 968 | return nil |
| 969 | } |
| 970 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 971 | func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 972 | n.Lock() |
| 973 | defer n.Unlock() |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 974 | r := &root{ |
| 975 | node: n, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 976 | Callbacks: n.Root.GetCallbacks(), |
| 977 | NotificationCallbacks: n.Root.GetNotificationCallbacks(), |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 978 | DirtyNodes: n.Root.DirtyNodes, |
| 979 | KvStore: n.Root.KvStore, |
| 980 | Loading: n.Root.Loading, |
| 981 | RevisionClass: n.Root.RevisionClass, |
| 982 | } |
| 983 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 984 | if n.Proxy == nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 985 | n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 986 | } else { |
| 987 | if n.Proxy.Exclusive { |
| 988 | log.Error("node is already owned exclusively") |
| 989 | } |
| 990 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 991 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 992 | return n.Proxy |
| 993 | } |
| 994 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 995 | func (n *node) makeEventBus() *EventBus { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 996 | n.Lock() |
| 997 | defer n.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 998 | if n.EventBus == nil { |
| 999 | n.EventBus = NewEventBus() |
| 1000 | } |
| 1001 | return n.EventBus |
| 1002 | } |
| 1003 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1004 | func (n *node) SetProxy(proxy *Proxy) { |
| 1005 | n.Lock() |
| 1006 | defer n.Unlock() |
| 1007 | n.Proxy = proxy |
| 1008 | } |
| 1009 | |
| 1010 | func (n *node) GetProxy() *Proxy { |
| 1011 | n.Lock() |
| 1012 | defer n.Unlock() |
| 1013 | return n.Proxy |
| 1014 | } |
| 1015 | |
| 1016 | func (n *node) GetBranch(key string) *Branch { |
| 1017 | n.Lock() |
| 1018 | defer n.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 1019 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1020 | if n.Branches != nil { |
| 1021 | if branch, exists := n.Branches[key]; exists { |
| 1022 | return branch |
| 1023 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1024 | } |
| 1025 | return nil |
| 1026 | } |
| 1027 | |
| 1028 | func (n *node) SetBranch(key string, branch *Branch) { |
| 1029 | n.Lock() |
| 1030 | defer n.Unlock() |
| 1031 | n.Branches[key] = branch |
| 1032 | } |
| 1033 | |
| 1034 | func (n *node) GetRoot() *root { |
| 1035 | n.Lock() |
| 1036 | defer n.Unlock() |
| 1037 | return n.Root |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1038 | } |