blob: 207df0973129e504a6629f84a9a289ac247572a9 [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -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 */
16
17package model
18
19// TODO: proper error handling
20// TODO: proper logging
21
22import (
23 "fmt"
24 "github.com/golang/protobuf/proto"
25 "github.com/opencord/voltha-go/common/log"
26 "reflect"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040027 "strings"
28 "sync"
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040029 "time"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040030)
31
32// When a branch has no transaction id, everything gets stored in NONE
33const (
34 NONE string = "none"
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040035
36 // period to determine when data requires a refresh (in seconds)
37 // TODO: make this configurable?
38 DATA_REFRESH_PERIOD int64 = 5000
Matt Jeanneretcab955f2019-04-10 15:45:57 -040039)
40
41// Node interface is an abstraction of the node data structure
42type Node interface {
43 MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple)
44
45 // CRUD functions
46 Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision
47 Get(path string, hash string, depth int, deep bool, txid string) interface{}
48 Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision
49 Remove(path string, txid string, makeBranch MakeBranchFunction) Revision
50
51 MakeBranch(txid string) *Branch
52 DeleteBranch(txid string)
53 MergeBranch(txid string, dryRun bool) (Revision, error)
54
55 MakeTxBranch() string
56 DeleteTxBranch(txid string)
57 FoldTxBranch(txid string)
58
59 CreateProxy(path string, exclusive bool) *Proxy
60 GetProxy() *Proxy
61}
62
63type node struct {
Mahir Gunyele77977b2019-06-27 05:36:22 -070064 mutex sync.RWMutex
65 Root *root
66 Type interface{}
67
Matt Jeanneretcab955f2019-04-10 15:45:57 -040068 Branches map[string]*Branch
69 Tags map[string]Revision
70 Proxy *Proxy
71 EventBus *EventBus
72 AutoPrune bool
73}
74
75// ChangeTuple holds details of modifications made to a revision
76type ChangeTuple struct {
77 Type CallbackType
78 PreviousData interface{}
79 LatestData interface{}
80}
81
82// NewNode creates a new instance of the node data structure
83func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node {
84 n := &node{}
85
86 n.Root = root
87 n.Branches = make(map[string]*Branch)
88 n.Tags = make(map[string]Revision)
89 n.Proxy = nil
90 n.EventBus = nil
91 n.AutoPrune = autoPrune
92
93 if IsProtoMessage(initialData) {
94 n.Type = reflect.ValueOf(initialData).Interface()
95 dataCopy := proto.Clone(initialData.(proto.Message))
96 n.initialize(dataCopy, txid)
97 } else if reflect.ValueOf(initialData).IsValid() {
98 // FIXME: this block does not reflect the original implementation
99 // it should be checking if the provided initial_data is already a type!??!
100 // it should be checked before IsProtoMessage
101 n.Type = reflect.ValueOf(initialData).Interface()
102 } else {
103 // not implemented error
104 log.Errorf("cannot process initial data - %+v", initialData)
105 }
106
107 return n
108}
109
110// MakeNode creates a new node in the tree
111func (n *node) MakeNode(data interface{}, txid string) *node {
112 return NewNode(n.Root, data, true, txid)
113}
114
115// MakeRevision create a new revision of the node in the tree
116func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
117 return n.GetRoot().MakeRevision(branch, data, children)
118}
119
120// makeLatest will mark the revision of a node as being the latest
121func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400122 // Keep a reference to the current revision
123 var previous string
124 if branch.GetLatest() != nil {
125 previous = branch.GetLatest().GetHash()
126 }
127
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400128 branch.AddRevision(revision)
129
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400130 // If anything is new, then set the revision as the latest
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400131 if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400132 if revision.GetName() != "" {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400133 log.Debugw("saving-latest-data", log.Fields{"hash": revision.GetHash(), "data": revision.GetData()})
134 // Tag a timestamp to that revision
135 revision.SetLastUpdate()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400136 GetRevCache().Cache.Store(revision.GetName(), revision)
137 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400138 branch.SetLatest(revision)
139 }
140
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400141 // Delete the previous revision if anything has changed
142 if previous != "" && previous != branch.GetLatest().GetHash() {
143 branch.DeleteRevision(previous)
144 }
145
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400146 if changeAnnouncement != nil && branch.Txid == "" {
147 if n.Proxy != nil {
148 for _, change := range changeAnnouncement {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400149 log.Debugw("adding-callback",
150 log.Fields{
151 "callbacks": n.Proxy.getCallbacks(change.Type),
152 "type": change.Type,
153 "previousData": change.PreviousData,
154 "latestData": change.LatestData,
155 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400156 n.Root.AddCallback(
157 n.Proxy.InvokeCallbacks,
158 change.Type,
159 true,
160 change.PreviousData,
161 change.LatestData)
162 }
163 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400164 }
165}
166
167// Latest returns the latest revision of node with or without the transaction id
168func (n *node) Latest(txid ...string) Revision {
169 var branch *Branch
170
171 if len(txid) > 0 && txid[0] != "" {
172 if branch = n.GetBranch(txid[0]); branch != nil {
173 return branch.GetLatest()
174 }
175 } else if branch = n.GetBranch(NONE); branch != nil {
176 return branch.GetLatest()
177 }
178 return nil
179}
180
181// initialize prepares the content of a node along with its possible ramifications
182func (n *node) initialize(data interface{}, txid string) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400183 children := make(map[string][]Revision)
184 for fieldName, field := range ChildrenFields(n.Type) {
185 _, fieldValue := GetAttributeValue(data, fieldName, 0)
186
187 if fieldValue.IsValid() {
188 if field.IsContainer {
189 if field.Key != "" {
190 for i := 0; i < fieldValue.Len(); i++ {
191 v := fieldValue.Index(i)
192
193 if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil {
194 children[fieldName] = append(children[fieldName], rev)
195 }
196
197 // TODO: The following logic was ported from v1.0. Need to verify if it is required
198 //var keysSeen []string
199 //_, key := GetAttributeValue(v.Interface(), field.Key, 0)
200 //for _, k := range keysSeen {
201 // if k == key.String() {
202 // //log.Errorf("duplicate key - %s", k)
203 // }
204 //}
205 //keysSeen = append(keysSeen, key.String())
206 }
207
208 } else {
209 for i := 0; i < fieldValue.Len(); i++ {
210 v := fieldValue.Index(i)
211 if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil {
212 children[fieldName] = append(children[fieldName], newNodeRev)
213 }
214 }
215 }
216 } else {
217 if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil {
218 children[fieldName] = append(children[fieldName], newNodeRev)
219 }
220 }
221 } else {
222 log.Errorf("field is invalid - %+v", fieldValue)
223 }
224 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400225
226 branch := NewBranch(n, "", nil, n.AutoPrune)
227 rev := n.MakeRevision(branch, data, children)
228 n.makeLatest(branch, rev, nil)
229
230 if txid == "" {
231 n.SetBranch(NONE, branch)
232 } else {
233 n.SetBranch(txid, branch)
234 }
235}
236
237// findRevByKey retrieves a specific revision from a node tree
238func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400239 for i, rev := range revs {
240 dataValue := reflect.ValueOf(rev.GetData())
241 dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0)
242
243 fieldValue := dataValue.Elem().FieldByName(dataStruct.Name)
244
245 a := fmt.Sprintf("%s", fieldValue.Interface())
246 b := fmt.Sprintf("%s", value)
247 if a == b {
248 return i, revs[i]
249 }
250 }
251
252 return -1, nil
253}
254
255// Get retrieves the data from a node tree that resides at the specified path
256func (n *node) List(path string, hash string, depth int, deep bool, txid string) interface{} {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400257 n.mutex.Lock()
258 defer n.mutex.Unlock()
259
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400260 log.Debugw("node-list-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid})
261 if deep {
262 depth = -1
263 }
264
265 for strings.HasPrefix(path, "/") {
266 path = path[1:]
267 }
268
269 var branch *Branch
270 var rev Revision
271
272 if branch = n.GetBranch(txid); txid == "" || branch == nil {
273 branch = n.GetBranch(NONE)
274 }
275
276 if hash != "" {
277 rev = branch.GetRevision(hash)
278 } else {
279 rev = branch.GetLatest()
280 }
281
282 var result interface{}
283 var prList []interface{}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400284 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400285 for _, revEntry := range pr {
286 prList = append(prList, revEntry.GetData())
287 }
288 result = prList
289 }
290
291 return result
292}
293
294// Get retrieves the data from a node tree that resides at the specified path
295func (n *node) Get(path string, hash string, depth int, reconcile bool, txid string) interface{} {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400296 n.mutex.Lock()
297 defer n.mutex.Unlock()
298
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400299 log.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400300
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400301 for strings.HasPrefix(path, "/") {
302 path = path[1:]
303 }
304
305 var branch *Branch
306 var rev Revision
307
308 if branch = n.GetBranch(txid); txid == "" || branch == nil {
309 branch = n.GetBranch(NONE)
310 }
311
312 if hash != "" {
313 rev = branch.GetRevision(hash)
314 } else {
315 rev = branch.GetLatest()
316 }
317
318 var result interface{}
319
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400320 // If there is no request to reconcile, try to get it from memory
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400321 if !reconcile {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400322 // Try to find an entry matching the path value from one of these sources
323 // 1. Start with the cache which stores revisions by watch names
324 // 2. Then look in the revision tree, especially if it's a sub-path such as /devices/1234/flows
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400325 // 3. Move on to the KV store if that path cannot be found or if the entry has expired
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400326 if entry, exists := GetRevCache().Cache.Load(path); exists && entry.(Revision) != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400327 entryAge := time.Now().Sub(entry.(Revision).GetLastUpdate()).Nanoseconds() / int64(time.Millisecond)
328 if entryAge < DATA_REFRESH_PERIOD {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700329 log.Debugw("using-cache-entry", log.Fields{
330 "path": path,
331 "hash": hash,
332 "age": entryAge,
333 })
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400334 return proto.Clone(entry.(Revision).GetData().(proto.Message))
335 } else {
336 log.Debugw("cache-entry-expired", log.Fields{"path": path, "hash": hash, "age": entryAge})
337 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400338 } else if result = n.getPath(rev.GetBranch().GetLatest(), path, depth); result != nil && reflect.ValueOf(result).IsValid() && !reflect.ValueOf(result).IsNil() {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400339 log.Debugw("using-rev-tree-entry", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400340 return result
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400341 } else {
342 log.Debugw("not-using-cache-entry", log.Fields{
343 "path": path,
344 "hash": hash, "depth": depth,
345 "reconcile": reconcile,
346 "txid": txid,
347 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400348 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400349 } else {
350 log.Debugw("reconcile-requested", log.Fields{
351 "path": path,
352 "hash": hash,
353 "reconcile": reconcile,
354 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400355 }
356
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400357 // If we got to this point, we are either trying to reconcile with the db
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400358 // or we simply failed at getting information from memory
359 if n.Root.KvStore != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400360 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil && len(pr) > 0 {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400361 // Did we receive a single or multiple revisions?
362 if len(pr) > 1 {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400363 var revs []interface{}
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400364 for _, revEntry := range pr {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400365 revs = append(revs, revEntry.GetData())
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400366 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400367 result = revs
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400368 } else {
369 result = pr[0].GetData()
370 }
371 }
372 }
373
374 return result
375}
376
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400377//getPath traverses the specified path and retrieves the data associated to it
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400378func (n *node) getPath(rev Revision, path string, depth int) interface{} {
379 if path == "" {
380 return n.getData(rev, depth)
381 }
382
383 partition := strings.SplitN(path, "/", 2)
384 name := partition[0]
385
386 if len(partition) < 2 {
387 path = ""
388 } else {
389 path = partition[1]
390 }
391
392 names := ChildrenFields(n.Type)
393 field := names[name]
394
395 if field != nil && field.IsContainer {
396 children := make([]Revision, len(rev.GetChildren(name)))
397 copy(children, rev.GetChildren(name))
398
399 if field.Key != "" {
400 if path != "" {
401 partition = strings.SplitN(path, "/", 2)
402 key := partition[0]
403 path = ""
404 keyValue := field.KeyFromStr(key)
405 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil {
406 return nil
407 } else {
408 childNode := childRev.GetNode()
409 return childNode.getPath(childRev, path, depth)
410 }
411 } else {
412 var response []interface{}
413 for _, childRev := range children {
414 childNode := childRev.GetNode()
415 value := childNode.getData(childRev, depth)
416 response = append(response, value)
417 }
418 return response
419 }
420 } else {
421 var response []interface{}
422 if path != "" {
423 // TODO: raise error
424 return response
425 }
426 for _, childRev := range children {
427 childNode := childRev.GetNode()
428 value := childNode.getData(childRev, depth)
429 response = append(response, value)
430 }
431 return response
432 }
433 }
434
435 childRev := rev.GetChildren(name)[0]
436 childNode := childRev.GetNode()
437 return childNode.getPath(childRev, path, depth)
438}
439
440// getData retrieves the data from a node revision
441func (n *node) getData(rev Revision, depth int) interface{} {
442 msg := rev.GetBranch().GetLatest().Get(depth)
443 var modifiedMsg interface{}
444
445 if n.GetProxy() != nil {
446 log.Debugw("invoking-get-callbacks", log.Fields{"data": msg})
447 if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil {
448 msg = modifiedMsg
449 }
450
451 }
452
453 return msg
454}
455
456// Update changes the content of a node at the specified path with the provided data
457func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400458 n.mutex.Lock()
459 defer n.mutex.Unlock()
460
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400461 log.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid, "makeBranch": makeBranch})
462
463 for strings.HasPrefix(path, "/") {
464 path = path[1:]
465 }
466
467 var branch *Branch
468 if txid == "" {
469 branch = n.GetBranch(NONE)
470 } else if branch = n.GetBranch(txid); branch == nil {
471 branch = makeBranch(n)
472 }
473
474 if branch.GetLatest() != nil {
475 log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data)
476 }
477 if path == "" {
478 return n.doUpdate(branch, data, strict)
479 }
480
481 rev := branch.GetLatest()
482
483 partition := strings.SplitN(path, "/", 2)
484 name := partition[0]
485
486 if len(partition) < 2 {
487 path = ""
488 } else {
489 path = partition[1]
490 }
491
492 field := ChildrenFields(n.Type)[name]
493 var children []Revision
494
495 if field == nil {
496 return n.doUpdate(branch, data, strict)
497 }
498
499 if field.IsContainer {
500 if path == "" {
501 log.Errorf("cannot update a list")
502 } else if field.Key != "" {
503 partition := strings.SplitN(path, "/", 2)
504 key := partition[0]
505 if len(partition) < 2 {
506 path = ""
507 } else {
508 path = partition[1]
509 }
510 keyValue := field.KeyFromStr(key)
511
512 children = make([]Revision, len(rev.GetChildren(name)))
513 copy(children, rev.GetChildren(name))
514
515 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400516
517 if childRev == nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400518 log.Debugw("child-revision-is-nil", log.Fields{"key": keyValue})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400519 return branch.GetLatest()
520 }
521
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400522 childNode := childRev.GetNode()
523
524 // Save proxy in child node to ensure callbacks are called later on
525 // only assign in cases of non sub-folder proxies, i.e. "/"
526 if childNode.Proxy == nil && n.Proxy != nil && n.Proxy.getFullPath() == "" {
527 childNode.Proxy = n.Proxy
528 }
529
530 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
531
532 if newChildRev.GetHash() == childRev.GetHash() {
533 if newChildRev != childRev {
534 log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev)
535 newChildRev.ClearHash()
536 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400537 log.Debugw("child-revisions-have-matching-hash", log.Fields{"hash": childRev.GetHash(), "key": keyValue})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400538 return branch.GetLatest()
539 }
540
541 _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0)
542
543 _newKeyType := fmt.Sprintf("%s", newKey)
544 _keyValueType := fmt.Sprintf("%s", keyValue)
545
546 if _newKeyType != _keyValueType {
547 log.Errorf("cannot change key field")
548 }
549
550 // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters)
551 newChildRev.SetName(name + "/" + _keyValueType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400552
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400553 branch.LatestLock.Lock()
554 defer branch.LatestLock.Unlock()
555
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400556 if idx >= 0 {
557 children[idx] = newChildRev
558 } else {
559 children = append(children, newChildRev)
560 }
561
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400562 updatedRev := rev.UpdateChildren(name, children, branch)
563
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400564 n.makeLatest(branch, updatedRev, nil)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400565 updatedRev.ChildDrop(name, childRev.GetHash())
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400566
567 return newChildRev
568
569 } else {
570 log.Errorf("cannot index into container with no keys")
571 }
572 } else {
573 childRev := rev.GetChildren(name)[0]
574 childNode := childRev.GetNode()
575 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400576
577 branch.LatestLock.Lock()
578 defer branch.LatestLock.Unlock()
579
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400580 updatedRev := rev.UpdateChildren(name, []Revision{newChildRev}, branch)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400581 n.makeLatest(branch, updatedRev, nil)
582
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400583 updatedRev.ChildDrop(name, childRev.GetHash())
584
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400585 return newChildRev
586 }
587
588 return nil
589}
590
591func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400592 log.Debugw("comparing-types", log.Fields{"expected": reflect.ValueOf(n.Type).Type(), "actual": reflect.TypeOf(data)})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400593
594 if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() {
595 // TODO raise error
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400596 log.Errorw("types-do-not-match: %+v", log.Fields{"actual": reflect.TypeOf(data), "expected": n.Type})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400597 return nil
598 }
599
600 // TODO: validate that this actually works
601 //if n.hasChildren(data) {
602 // return nil
603 //}
604
605 if n.GetProxy() != nil {
606 log.Debug("invoking proxy PRE_UPDATE Callbacks")
607 n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data)
608 }
609
610 if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() {
611 if strict {
612 // TODO: checkAccessViolations(data, Branch.GetLatest.data)
613 log.Debugf("checking access violations")
614 }
615
616 rev := branch.GetLatest().UpdateData(data, branch)
617 changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}}
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400618 n.makeLatest(branch, rev, changes)
619
620 return rev
621 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400622 return branch.GetLatest()
623}
624
625// Add inserts a new node at the specified path with the provided data
626func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400627 n.mutex.Lock()
628 defer n.mutex.Unlock()
629
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400630 log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
631
632 for strings.HasPrefix(path, "/") {
633 path = path[1:]
634 }
635 if path == "" {
636 // TODO raise error
637 log.Errorf("cannot add for non-container mode")
638 return nil
639 }
640
641 var branch *Branch
642 if txid == "" {
643 branch = n.GetBranch(NONE)
644 } else if branch = n.GetBranch(txid); branch == nil {
645 branch = makeBranch(n)
646 }
647
648 rev := branch.GetLatest()
649
650 partition := strings.SplitN(path, "/", 2)
651 name := partition[0]
652
653 if len(partition) < 2 {
654 path = ""
655 } else {
656 path = partition[1]
657 }
658
659 field := ChildrenFields(n.Type)[name]
660
661 var children []Revision
662
663 if field.IsContainer {
664 if path == "" {
665 if field.Key != "" {
666 if n.GetProxy() != nil {
667 log.Debug("invoking proxy PRE_ADD Callbacks")
668 n.GetProxy().InvokeCallbacks(PRE_ADD, false, data)
669 }
670
671 children = make([]Revision, len(rev.GetChildren(name)))
672 copy(children, rev.GetChildren(name))
673
674 _, key := GetAttributeValue(data, field.Key, 0)
675
676 if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil {
677 // TODO raise error
678 log.Warnw("duplicate-key-found", log.Fields{"key": key.String()})
679 return exists
680 }
681 childRev := n.MakeNode(data, "").Latest()
682
683 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
684 childRev.SetName(name + "/" + key.String())
685
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400686 branch.LatestLock.Lock()
687 defer branch.LatestLock.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400688
689 children = append(children, childRev)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400690
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400691 updatedRev := rev.UpdateChildren(name, children, branch)
692 changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}}
693 childRev.SetupWatch(childRev.GetName())
694
695 n.makeLatest(branch, updatedRev, changes)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400696
697 return childRev
698 }
699 log.Errorf("cannot add to non-keyed container")
700
701 } else if field.Key != "" {
702 partition := strings.SplitN(path, "/", 2)
703 key := partition[0]
704 if len(partition) < 2 {
705 path = ""
706 } else {
707 path = partition[1]
708 }
709 keyValue := field.KeyFromStr(key)
710
711 children = make([]Revision, len(rev.GetChildren(name)))
712 copy(children, rev.GetChildren(name))
713
714 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
715
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400716 if childRev == nil {
717 return branch.GetLatest()
718 }
719
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400720 childNode := childRev.GetNode()
721 newChildRev := childNode.Add(path, data, txid, makeBranch)
722
723 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400724 newChildRev.SetName(name + "/" + keyValue.(string))
725
726 branch.LatestLock.Lock()
727 defer branch.LatestLock.Unlock()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400728
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400729 if idx >= 0 {
730 children[idx] = newChildRev
731 } else {
732 children = append(children, newChildRev)
733 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400734
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400735 updatedRev := rev.UpdateChildren(name, children, branch)
736 n.makeLatest(branch, updatedRev, nil)
737
738 updatedRev.ChildDrop(name, childRev.GetHash())
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400739
740 return newChildRev
741 } else {
742 log.Errorf("cannot add to non-keyed container")
743 }
744 } else {
745 log.Errorf("cannot add to non-container field")
746 }
747
748 return nil
749}
750
751// Remove eliminates a node at the specified path
752func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400753 n.mutex.Lock()
754 defer n.mutex.Unlock()
755
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400756 log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
757
758 for strings.HasPrefix(path, "/") {
759 path = path[1:]
760 }
761 if path == "" {
762 // TODO raise error
763 log.Errorf("cannot remove for non-container mode")
764 }
765 var branch *Branch
766 if txid == "" {
767 branch = n.GetBranch(NONE)
768 } else if branch = n.GetBranch(txid); branch == nil {
769 branch = makeBranch(n)
770 }
771
772 rev := branch.GetLatest()
773
774 partition := strings.SplitN(path, "/", 2)
775 name := partition[0]
776 if len(partition) < 2 {
777 path = ""
778 } else {
779 path = partition[1]
780 }
781
782 field := ChildrenFields(n.Type)[name]
783 var children []Revision
784 postAnnouncement := []ChangeTuple{}
785
786 if field.IsContainer {
787 if path == "" {
788 log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path})
789 } else if field.Key != "" {
790 partition := strings.SplitN(path, "/", 2)
791 key := partition[0]
792 if len(partition) < 2 {
793 path = ""
794 } else {
795 path = partition[1]
796 }
797
798 keyValue := field.KeyFromStr(key)
799 children = make([]Revision, len(rev.GetChildren(name)))
800 copy(children, rev.GetChildren(name))
801
802 if path != "" {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400803 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
804 childNode := childRev.GetNode()
805 if childNode.Proxy == nil {
806 childNode.Proxy = n.Proxy
807 }
808 newChildRev := childNode.Remove(path, txid, makeBranch)
809
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400810 branch.LatestLock.Lock()
811 defer branch.LatestLock.Unlock()
812
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400813 if idx >= 0 {
814 children[idx] = newChildRev
815 } else {
816 children = append(children, newChildRev)
817 }
818
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400819 rev.SetChildren(name, children)
820 branch.GetLatest().Drop(txid, false)
821 n.makeLatest(branch, rev, nil)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400822 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400823 return branch.GetLatest()
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400824 }
825
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400826 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil && idx >= 0 {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400827 if n.GetProxy() != nil {
828 data := childRev.GetData()
829 n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
830 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
831 } else {
832 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
833 }
834
835 childRev.StorageDrop(txid, true)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400836 GetRevCache().Cache.Delete(childRev.GetName())
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400837
838 branch.LatestLock.Lock()
839 defer branch.LatestLock.Unlock()
840
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400841 children = append(children[:idx], children[idx+1:]...)
842 rev.SetChildren(name, children)
843
844 branch.GetLatest().Drop(txid, false)
845 n.makeLatest(branch, rev, postAnnouncement)
846
847 return rev
848 } else {
849 log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)})
850 }
851 }
852 log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key})
853
854 } else {
855 log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path})
856 }
857
858 return nil
859}
860
861// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
862
863// MakeBranchFunction is a type for function references intented to create a branch
864type MakeBranchFunction func(*node) *Branch
865
866// MakeBranch creates a new branch for the provided transaction id
867func (n *node) MakeBranch(txid string) *Branch {
868 branchPoint := n.GetBranch(NONE).GetLatest()
869 branch := NewBranch(n, txid, branchPoint, true)
870 n.SetBranch(txid, branch)
871 return branch
872}
873
874// DeleteBranch removes a branch with the specified id
875func (n *node) DeleteBranch(txid string) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400876 delete(n.Branches, txid)
877}
878
879func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision {
880 f := func(rev Revision) Revision {
881 childBranch := rev.GetBranch()
882
883 if childBranch.Txid == txid {
884 rev, _ = childBranch.Node.MergeBranch(txid, dryRun)
885 }
886
887 return rev
888 }
889 return f
890}
891
892// MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node
893func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) {
894 srcBranch := n.GetBranch(txid)
895 dstBranch := n.GetBranch(NONE)
896
897 forkRev := srcBranch.Origin
898 srcRev := srcBranch.GetLatest()
899 dstRev := dstBranch.GetLatest()
900
901 rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
902
903 if !dryRun {
904 if rev != nil {
905 rev.SetName(dstRev.GetName())
906 n.makeLatest(dstBranch, rev, changes)
907 }
908 n.DeleteBranch(txid)
909 }
910
911 // TODO: return proper error when one occurs
912 return rev, nil
913}
914
915// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
916
917//func (n *node) diff(hash1, hash2, txid string) {
918// branch := n.Branches[txid]
919// rev1 := branch.GetHash(hash1)
920// rev2 := branch.GetHash(hash2)
921//
922// if rev1.GetHash() == rev2.GetHash() {
923// // empty patch
924// } else {
925// // translate data to json and generate patch
926// patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData())
927// patch.
928// }
929//}
930
931// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
932
933// TODO: is tag mgmt used in the python implementation? Need to validate
934
935// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
936
937func (n *node) hasChildren(data interface{}) bool {
938 for fieldName, field := range ChildrenFields(n.Type) {
939 _, fieldValue := GetAttributeValue(data, fieldName, 0)
940
941 if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() {
942 log.Error("cannot update external children")
943 return true
944 }
945 }
946
947 return false
948}
949
950// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
951
952// CreateProxy returns a reference to a sub-tree of the data model
953func (n *node) CreateProxy(path string, exclusive bool) *Proxy {
954 return n.createProxy(path, path, n, exclusive)
955}
956
957func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700958 log.Debugw("node-create-proxy", log.Fields{
959 "node-type": reflect.ValueOf(n.Type).Type(),
960 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
961 "path": path,
962 "fullPath": fullPath,
963 })
964
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400965 for strings.HasPrefix(path, "/") {
966 path = path[1:]
967 }
968 if path == "" {
969 return n.makeProxy(path, fullPath, parentNode, exclusive)
970 }
971
972 rev := n.GetBranch(NONE).GetLatest()
973 partition := strings.SplitN(path, "/", 2)
974 name := partition[0]
Mahir Gunyele77977b2019-06-27 05:36:22 -0700975 var nodeType interface{}
976 // Node type is chosen depending on if we have reached the end of path or not
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400977 if len(partition) < 2 {
978 path = ""
Mahir Gunyele77977b2019-06-27 05:36:22 -0700979 nodeType = n.Type
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400980 } else {
981 path = partition[1]
Mahir Gunyele77977b2019-06-27 05:36:22 -0700982 nodeType = parentNode.Type
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400983 }
984
Mahir Gunyele77977b2019-06-27 05:36:22 -0700985 field := ChildrenFields(nodeType)[name]
986
987 if field != nil {
988 if field.IsContainer {
989 log.Debugw("container-field", log.Fields{
990 "node-type": reflect.ValueOf(n.Type).Type(),
991 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
992 "path": path,
993 "name": name,
994 })
995 if path == "" {
996 log.Debugw("folder-proxy", log.Fields{
997 "node-type": reflect.ValueOf(n.Type).Type(),
998 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
999 "fullPath": fullPath,
1000 "name": name,
1001 })
1002 newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "")
1003 return newNode.makeProxy(path, fullPath, parentNode, exclusive)
1004 } else if field.Key != "" {
1005 log.Debugw("key-proxy", log.Fields{
1006 "node-type": reflect.ValueOf(n.Type).Type(),
1007 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1008 "fullPath": fullPath,
1009 "name": name,
1010 })
1011 partition := strings.SplitN(path, "/", 2)
1012 key := partition[0]
1013 if len(partition) < 2 {
1014 path = ""
1015 } else {
1016 path = partition[1]
1017 }
1018 keyValue := field.KeyFromStr(key)
1019 var children []Revision
1020 children = make([]Revision, len(rev.GetChildren(name)))
1021 copy(children, rev.GetChildren(name))
1022
1023 // Try to find a matching revision in memory
1024 // If not found try the db
1025 var childRev Revision
1026 if _, childRev = n.findRevByKey(children, field.Key, keyValue); childRev != nil {
1027 log.Debugw("found-revision-matching-key-in-memory", log.Fields{
1028 "node-type": reflect.ValueOf(n.Type).Type(),
1029 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1030 "fullPath": fullPath,
1031 "name": name,
1032 })
1033 } else if revs := n.GetBranch(NONE).GetLatest().LoadFromPersistence(fullPath, "", nil); revs != nil && len(revs) > 0 {
1034 log.Debugw("found-revision-matching-key-in-db", log.Fields{
1035 "node-type": reflect.ValueOf(n.Type).Type(),
1036 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1037 "fullPath": fullPath,
1038 "name": name,
1039 })
1040 childRev = revs[0]
1041 } else {
1042 log.Debugw("no-revision-matching-key", log.Fields{
1043 "node-type": reflect.ValueOf(n.Type).Type(),
1044 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1045 "fullPath": fullPath,
1046 "name": name,
1047 })
1048 }
1049 if childRev != nil {
1050 childNode := childRev.GetNode()
1051 return childNode.createProxy(path, fullPath, n, exclusive)
1052 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001053 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001054 log.Errorw("cannot-access-index-of-empty-container", log.Fields{
1055 "node-type": reflect.ValueOf(n.Type).Type(),
1056 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1057 "path": path,
1058 "name": name,
1059 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001060 }
1061 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001062 log.Debugw("non-container-field", log.Fields{
1063 "node-type": reflect.ValueOf(n.Type).Type(),
1064 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1065 "path": path,
1066 "name": name,
1067 })
1068 childRev := rev.GetChildren(name)[0]
1069 childNode := childRev.GetNode()
1070 return childNode.createProxy(path, fullPath, n, exclusive)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001071 }
1072 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001073 log.Debugw("field-object-is-nil", log.Fields{
1074 "node-type": reflect.ValueOf(n.Type).Type(),
1075 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1076 "fullPath": fullPath,
1077 "name": name,
1078 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001079 }
1080
Mahir Gunyele77977b2019-06-27 05:36:22 -07001081 log.Warnw("cannot-create-proxy", log.Fields{
1082 "node-type": reflect.ValueOf(n.Type).Type(),
1083 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1084 "path": path,
1085 "fullPath": fullPath,
1086 "latest-rev": rev.GetHash(),
1087 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001088 return nil
1089}
1090
1091func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001092 log.Debugw("node-make-proxy", log.Fields{
1093 "node-type": reflect.ValueOf(n.Type).Type(),
1094 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1095 "path": path,
1096 "fullPath": fullPath,
1097 })
1098
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001099 r := &root{
1100 node: n,
1101 Callbacks: n.Root.GetCallbacks(),
1102 NotificationCallbacks: n.Root.GetNotificationCallbacks(),
1103 DirtyNodes: n.Root.DirtyNodes,
1104 KvStore: n.Root.KvStore,
1105 Loading: n.Root.Loading,
1106 RevisionClass: n.Root.RevisionClass,
1107 }
1108
1109 if n.Proxy == nil {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001110 log.Debugw("constructing-new-proxy", log.Fields{
1111 "node-type": reflect.ValueOf(n.Type).Type(),
1112 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1113 "path": path,
1114 "fullPath": fullPath,
1115 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001116 n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive)
1117 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -07001118 log.Debugw("node-has-existing-proxy", log.Fields{
1119 "node-type": reflect.ValueOf(n.Proxy.Node.Type).Type(),
1120 "parent-node-type": reflect.ValueOf(n.Proxy.ParentNode.Type).Type(),
1121 "path": n.Proxy.Path,
1122 "fullPath": n.Proxy.FullPath,
1123 })
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001124 if n.Proxy.Exclusive {
1125 log.Error("node is already owned exclusively")
1126 }
1127 }
1128
1129 return n.Proxy
1130}
1131
1132func (n *node) makeEventBus() *EventBus {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001133 if n.EventBus == nil {
1134 n.EventBus = NewEventBus()
1135 }
1136 return n.EventBus
1137}
1138
1139func (n *node) SetProxy(proxy *Proxy) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001140 n.Proxy = proxy
1141}
1142
1143func (n *node) GetProxy() *Proxy {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001144 return n.Proxy
1145}
1146
1147func (n *node) GetBranch(key string) *Branch {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001148 if n.Branches != nil {
1149 if branch, exists := n.Branches[key]; exists {
1150 return branch
1151 }
1152 }
1153 return nil
1154}
1155
1156func (n *node) SetBranch(key string, branch *Branch) {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001157 n.Branches[key] = branch
1158}
1159
1160func (n *node) GetRoot() *root {
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001161 return n.Root
1162}