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