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" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/common/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 |
| 44 | Get(ctx context.Context, path string, hash string, depth int, deep bool, txid string) interface{} |
| 45 | List(ctx context.Context, path string, hash string, depth int, deep bool, txid string) interface{} |
| 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 |
| 48 | CreateProxy(ctx context.Context, path string, exclusive bool) *Proxy |
| 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 |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 253 | func (n *node) List(ctx context.Context, path string, hash string, depth int, deep bool, txid string) interface{} { |
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{} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 281 | if pr := rev.LoadFromPersistence(ctx, path, txid, nil); pr != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 282 | for _, revEntry := range pr { |
| 283 | prList = append(prList, revEntry.GetData()) |
| 284 | } |
| 285 | result = prList |
| 286 | } |
| 287 | |
| 288 | return result |
| 289 | } |
| 290 | |
| 291 | // Get retrieves the data from a node tree that resides at the specified path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 292 | func (n *node) Get(ctx context.Context, path string, hash string, depth int, reconcile bool, txid string) interface{} { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 293 | n.mutex.Lock() |
| 294 | defer n.mutex.Unlock() |
| 295 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 296 | 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] | 297 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 298 | for strings.HasPrefix(path, "/") { |
| 299 | path = path[1:] |
| 300 | } |
| 301 | |
| 302 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 303 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 304 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 305 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 306 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if hash != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 310 | rev = branch.GetRevision(hash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 311 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 312 | rev = branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 315 | var result interface{} |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 316 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 317 | // 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] | 318 | if !reconcile { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 319 | // Try to find an entry matching the path value from one of these sources |
| 320 | // 1. Start with the cache which stores revisions by watch names |
| 321 | // 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] | 322 | // 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] | 323 | if entry, exists := GetRevCache().Get(path); exists && entry.(Revision) != nil { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 324 | entryAge := time.Now().Sub(entry.(Revision).GetLastUpdate()).Nanoseconds() / int64(time.Millisecond) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 325 | if entryAge < DataRefreshPeriod { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 326 | log.Debugw("using-cache-entry", log.Fields{ |
| 327 | "path": path, |
| 328 | "hash": hash, |
| 329 | "age": entryAge, |
| 330 | }) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 331 | return proto.Clone(entry.(Revision).GetData().(proto.Message)) |
| 332 | } else { |
| 333 | log.Debugw("cache-entry-expired", log.Fields{"path": path, "hash": hash, "age": entryAge}) |
| 334 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 335 | } 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] | 336 | log.Debugw("using-rev-tree-entry", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid}) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 337 | return result |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 338 | } else { |
| 339 | log.Debugw("not-using-cache-entry", log.Fields{ |
| 340 | "path": path, |
| 341 | "hash": hash, "depth": depth, |
| 342 | "reconcile": reconcile, |
| 343 | "txid": txid, |
| 344 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 345 | } |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 346 | } else { |
| 347 | log.Debugw("reconcile-requested", log.Fields{ |
| 348 | "path": path, |
| 349 | "hash": hash, |
| 350 | "reconcile": reconcile, |
| 351 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 354 | // 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] | 355 | // or we simply failed at getting information from memory |
| 356 | if n.Root.KvStore != nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 357 | if pr := rev.LoadFromPersistence(ctx, path, txid, nil); pr != nil && len(pr) > 0 { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 358 | // Did we receive a single or multiple revisions? |
| 359 | if len(pr) > 1 { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 360 | var revs []interface{} |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 361 | for _, revEntry := range pr { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 362 | revs = append(revs, revEntry.GetData()) |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 363 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 364 | result = revs |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 365 | } else { |
| 366 | result = pr[0].GetData() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 367 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 374 | //getPath traverses the specified path and retrieves the data associated to it |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 375 | 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] | 376 | if path == "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 377 | return n.getData(rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | partition := strings.SplitN(path, "/", 2) |
| 381 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 382 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 383 | if len(partition) < 2 { |
| 384 | path = "" |
| 385 | } else { |
| 386 | path = partition[1] |
| 387 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 388 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 389 | names := ChildrenFields(n.Type) |
| 390 | field := names[name] |
| 391 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 392 | if field != nil && field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 393 | children := make([]Revision, len(rev.GetChildren(name))) |
| 394 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 395 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 396 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 397 | if path != "" { |
| 398 | partition = strings.SplitN(path, "/", 2) |
| 399 | key := partition[0] |
| 400 | path = "" |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 401 | keyValue := field.KeyFromStr(key) |
| 402 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 403 | return nil |
| 404 | } else { |
| 405 | childNode := childRev.GetNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 406 | return childNode.getPath(ctx, childRev, path, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 407 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 408 | } else { |
| 409 | var response []interface{} |
| 410 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 411 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 412 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 413 | response = append(response, value) |
| 414 | } |
| 415 | return response |
| 416 | } |
| 417 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 418 | var response []interface{} |
| 419 | if path != "" { |
| 420 | // TODO: raise error |
| 421 | return response |
| 422 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 423 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 424 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 425 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 426 | response = append(response, value) |
| 427 | } |
| 428 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 429 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 430 | } else if children := rev.GetChildren(name); children != nil && len(children) > 0 { |
| 431 | childRev := children[0] |
| 432 | childNode := childRev.GetNode() |
| 433 | return childNode.getPath(ctx, childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 434 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 435 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 436 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 439 | // getData retrieves the data from a node revision |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 440 | func (n *node) getData(rev Revision, depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 441 | msg := rev.GetBranch().GetLatest().Get(depth) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 442 | var modifiedMsg interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 443 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 444 | if n.GetProxy() != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 445 | log.Debugw("invoking-get-callbacks", log.Fields{"data": msg}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 446 | if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 447 | msg = modifiedMsg |
| 448 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 449 | |
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 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 452 | return msg |
| 453 | } |
| 454 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 455 | // 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] | 456 | 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] | 457 | n.mutex.Lock() |
| 458 | defer n.mutex.Unlock() |
| 459 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 460 | 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] | 461 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 462 | for strings.HasPrefix(path, "/") { |
| 463 | path = path[1:] |
| 464 | } |
| 465 | |
| 466 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 467 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 468 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 469 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 470 | branch = makeBranch(n) |
| 471 | } |
| 472 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 473 | if branch.GetLatest() != nil { |
| 474 | log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data) |
| 475 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 476 | if path == "" { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 477 | return n.doUpdate(ctx, branch, data, strict) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 478 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 479 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 480 | rev := branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 481 | |
| 482 | partition := strings.SplitN(path, "/", 2) |
| 483 | name := partition[0] |
| 484 | |
| 485 | if len(partition) < 2 { |
| 486 | path = "" |
| 487 | } else { |
| 488 | path = partition[1] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 491 | field := ChildrenFields(n.Type)[name] |
| 492 | var children []Revision |
| 493 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 494 | if field == nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 495 | return n.doUpdate(ctx, branch, data, strict) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 496 | } |
| 497 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 498 | if field.IsContainer { |
| 499 | if path == "" { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 500 | log.Errorf("cannot update a list") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 501 | } else if field.Key != "" { |
| 502 | partition := strings.SplitN(path, "/", 2) |
| 503 | key := partition[0] |
| 504 | if len(partition) < 2 { |
| 505 | path = "" |
| 506 | } else { |
| 507 | path = partition[1] |
| 508 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 509 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 510 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 511 | children = make([]Revision, len(rev.GetChildren(name))) |
| 512 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 513 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 514 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 515 | |
| 516 | if childRev == nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 517 | log.Debugw("child-revision-is-nil", log.Fields{"key": keyValue}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 518 | return branch.GetLatest() |
| 519 | } |
| 520 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 521 | childNode := childRev.GetNode() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 522 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 523 | // Save proxy in child node to ensure callbacks are called later on |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 524 | // only assign in cases of non sub-folder proxies, i.e. "/" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 525 | if childNode.Proxy == nil && n.Proxy != nil && n.GetProxy().getFullPath() == "" { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 526 | childNode.Proxy = n.Proxy |
| 527 | } |
| 528 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 529 | newChildRev := childNode.Update(ctx, path, data, strict, txid, makeBranch) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 530 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 531 | if newChildRev.GetHash() == childRev.GetHash() { |
| 532 | if newChildRev != childRev { |
| 533 | log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev) |
| 534 | newChildRev.ClearHash() |
| 535 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 536 | 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] | 537 | return branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 538 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 539 | |
| 540 | _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 541 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 542 | _newKeyType := fmt.Sprintf("%s", newKey) |
| 543 | _keyValueType := fmt.Sprintf("%s", keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 544 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 545 | if _newKeyType != _keyValueType { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 546 | log.Errorf("cannot change key field") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 547 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 548 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 549 | // 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] | 550 | newChildRev.SetName(name + "/" + _keyValueType) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 551 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 552 | branch.LatestLock.Lock() |
| 553 | defer branch.LatestLock.Unlock() |
| 554 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 555 | if idx >= 0 { |
| 556 | children[idx] = newChildRev |
| 557 | } else { |
| 558 | children = append(children, newChildRev) |
| 559 | } |
| 560 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 561 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 562 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 563 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 564 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 565 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 566 | return newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 567 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 568 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 569 | log.Errorf("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 570 | } |
| 571 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 572 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 573 | childNode := childRev.GetNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 574 | newChildRev := childNode.Update(ctx, path, data, strict, txid, makeBranch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 575 | |
| 576 | branch.LatestLock.Lock() |
| 577 | defer branch.LatestLock.Unlock() |
| 578 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 579 | updatedRev := rev.UpdateChildren(ctx, name, []Revision{newChildRev}, branch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 580 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 581 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 582 | updatedRev.ChildDrop(name, childRev.GetHash()) |
| 583 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 584 | return newChildRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 585 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 586 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 587 | return nil |
| 588 | } |
| 589 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 590 | 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] | 591 | 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] | 592 | |
| 593 | if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() { |
| 594 | // TODO raise error |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 595 | 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] | 596 | return nil |
| 597 | } |
| 598 | |
| 599 | // TODO: validate that this actually works |
| 600 | //if n.hasChildren(data) { |
| 601 | // return nil |
| 602 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 603 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 604 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 605 | log.Debug("invoking proxy PRE_UPDATE Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 606 | n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 607 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 608 | |
| 609 | if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 610 | if strict { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 611 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 612 | log.Debugf("checking access violations") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 613 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 614 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 615 | rev := branch.GetLatest().UpdateData(ctx, data, branch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 616 | changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 617 | n.makeLatest(branch, rev, changes) |
| 618 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 619 | return rev |
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 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 624 | // 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] | 625 | 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] | 626 | n.mutex.Lock() |
| 627 | defer n.mutex.Unlock() |
| 628 | |
khenaidoo | b324421 | 2019-08-27 14:32:27 -0400 | [diff] [blame] | 629 | log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 630 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 631 | for strings.HasPrefix(path, "/") { |
| 632 | path = path[1:] |
| 633 | } |
| 634 | if path == "" { |
| 635 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 636 | log.Errorf("cannot add for non-container mode") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 637 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 641 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 642 | branch = n.GetBranch(NONE) |
| 643 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 644 | branch = makeBranch(n) |
| 645 | } |
| 646 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 647 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 648 | |
| 649 | partition := strings.SplitN(path, "/", 2) |
| 650 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 651 | |
| 652 | if len(partition) < 2 { |
| 653 | path = "" |
| 654 | } else { |
| 655 | path = partition[1] |
| 656 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 657 | |
| 658 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 659 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 660 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 661 | |
| 662 | if field.IsContainer { |
| 663 | if path == "" { |
| 664 | if field.Key != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 665 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 666 | log.Debug("invoking proxy PRE_ADD Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 667 | n.GetProxy().InvokeCallbacks(PRE_ADD, false, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 670 | children = make([]Revision, len(rev.GetChildren(name))) |
| 671 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 672 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 673 | _, key := GetAttributeValue(data, field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 674 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 675 | if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 676 | // TODO raise error |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 677 | log.Warnw("duplicate-key-found", log.Fields{"key": key.String()}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 678 | return exists |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 679 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 680 | childRev := n.MakeNode(data, "").Latest() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 681 | |
| 682 | // 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] | 683 | childRev.SetName(name + "/" + key.String()) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 684 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 685 | branch.LatestLock.Lock() |
| 686 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 687 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 688 | children = append(children, childRev) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 689 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 690 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 691 | changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}} |
| 692 | childRev.SetupWatch(childRev.GetName()) |
| 693 | |
| 694 | n.makeLatest(branch, updatedRev, changes) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 695 | |
| 696 | return childRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 697 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 698 | log.Errorf("cannot add to non-keyed container") |
| 699 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 700 | } else if field.Key != "" { |
| 701 | partition := strings.SplitN(path, "/", 2) |
| 702 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 703 | if len(partition) < 2 { |
| 704 | path = "" |
| 705 | } else { |
| 706 | path = partition[1] |
| 707 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 708 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 709 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 710 | children = make([]Revision, len(rev.GetChildren(name))) |
| 711 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 712 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 713 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 714 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 715 | if childRev == nil { |
| 716 | return branch.GetLatest() |
| 717 | } |
| 718 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 719 | childNode := childRev.GetNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 720 | newChildRev := childNode.Add(ctx, path, data, txid, makeBranch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 721 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 722 | // 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] | 723 | newChildRev.SetName(name + "/" + keyValue.(string)) |
| 724 | |
| 725 | branch.LatestLock.Lock() |
| 726 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 727 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 728 | if idx >= 0 { |
| 729 | children[idx] = newChildRev |
| 730 | } else { |
| 731 | children = append(children, newChildRev) |
| 732 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 733 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 734 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 735 | n.makeLatest(branch, updatedRev, nil) |
| 736 | |
| 737 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 738 | |
| 739 | return newChildRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 740 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 741 | log.Errorf("cannot add to non-keyed container") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 742 | } |
| 743 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 744 | log.Errorf("cannot add to non-container field") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 745 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 746 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 747 | return nil |
| 748 | } |
| 749 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 750 | // Remove eliminates a node at the specified path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 751 | 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] | 752 | n.mutex.Lock() |
| 753 | defer n.mutex.Unlock() |
| 754 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 755 | 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] | 756 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 757 | for strings.HasPrefix(path, "/") { |
| 758 | path = path[1:] |
| 759 | } |
| 760 | if path == "" { |
| 761 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 762 | log.Errorf("cannot remove for non-container mode") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 763 | } |
| 764 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 765 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 766 | branch = n.GetBranch(NONE) |
| 767 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 768 | branch = makeBranch(n) |
| 769 | } |
| 770 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 771 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 772 | |
| 773 | partition := strings.SplitN(path, "/", 2) |
| 774 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 775 | if len(partition) < 2 { |
| 776 | path = "" |
| 777 | } else { |
| 778 | path = partition[1] |
| 779 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 780 | |
| 781 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 782 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 783 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 784 | |
| 785 | if field.IsContainer { |
| 786 | if path == "" { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 787 | log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 788 | } else if field.Key != "" { |
| 789 | partition := strings.SplitN(path, "/", 2) |
| 790 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 791 | if len(partition) < 2 { |
| 792 | path = "" |
| 793 | } else { |
| 794 | path = partition[1] |
| 795 | } |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 796 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 797 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 798 | children = make([]Revision, len(rev.GetChildren(name))) |
| 799 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 800 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 801 | if path != "" { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 802 | if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 803 | childNode := childRev.GetNode() |
| 804 | if childNode.Proxy == nil { |
| 805 | childNode.Proxy = n.Proxy |
| 806 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 807 | newChildRev := childNode.Remove(ctx, path, txid, makeBranch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 808 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 809 | branch.LatestLock.Lock() |
| 810 | defer branch.LatestLock.Unlock() |
| 811 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 812 | if idx >= 0 { |
| 813 | children[idx] = newChildRev |
| 814 | } else { |
| 815 | children = append(children, newChildRev) |
| 816 | } |
| 817 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 818 | rev.SetChildren(name, children) |
| 819 | branch.GetLatest().Drop(txid, false) |
| 820 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 821 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 822 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 823 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 824 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 825 | 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] | 826 | if n.GetProxy() != nil { |
| 827 | data := childRev.GetData() |
| 828 | n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data) |
| 829 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil}) |
| 830 | } else { |
| 831 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil}) |
| 832 | } |
| 833 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 834 | childRev.StorageDrop(txid, true) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 835 | GetRevCache().Delete(childRev.GetName()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 836 | |
| 837 | branch.LatestLock.Lock() |
| 838 | defer branch.LatestLock.Unlock() |
| 839 | |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 840 | children = append(children[:idx], children[idx+1:]...) |
| 841 | rev.SetChildren(name, children) |
| 842 | |
| 843 | branch.GetLatest().Drop(txid, false) |
| 844 | n.makeLatest(branch, rev, postAnnouncement) |
| 845 | |
| 846 | return rev |
| 847 | } else { |
| 848 | log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)}) |
| 849 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 850 | } |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 851 | 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] | 852 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 853 | } else { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 854 | 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] | 855 | } |
| 856 | |
| 857 | return nil |
| 858 | } |
| 859 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 860 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 861 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 862 | // MakeBranchFunction is a type for function references intented to create a branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 863 | type MakeBranchFunction func(*node) *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 864 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 865 | // MakeBranch creates a new branch for the provided transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 866 | func (n *node) MakeBranch(txid string) *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 867 | branchPoint := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 868 | branch := NewBranch(n, txid, branchPoint, true) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 869 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 870 | return branch |
| 871 | } |
| 872 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 873 | // DeleteBranch removes a branch with the specified id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 874 | func (n *node) DeleteBranch(txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 875 | delete(n.Branches, txid) |
| 876 | } |
| 877 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 878 | func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 879 | f := func(rev Revision) Revision { |
| 880 | childBranch := rev.GetBranch() |
| 881 | |
| 882 | if childBranch.Txid == txid { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 883 | rev, _ = childBranch.Node.MergeBranch(txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | return rev |
| 887 | } |
| 888 | return f |
| 889 | } |
| 890 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 891 | // 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] | 892 | func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 893 | srcBranch := n.GetBranch(txid) |
| 894 | dstBranch := n.GetBranch(NONE) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 895 | |
| 896 | forkRev := srcBranch.Origin |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 897 | srcRev := srcBranch.GetLatest() |
| 898 | dstRev := dstBranch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 899 | |
| 900 | rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun) |
| 901 | |
| 902 | if !dryRun { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 903 | if rev != nil { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 904 | rev.SetName(dstRev.GetName()) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 905 | n.makeLatest(dstBranch, rev, changes) |
| 906 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 907 | n.DeleteBranch(txid) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 908 | } |
| 909 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 910 | // TODO: return proper error when one occurs |
| 911 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 915 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 916 | //func (n *node) diff(hash1, hash2, txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 917 | // branch := n.Branches[txid] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 918 | // rev1 := branch.GetHash(hash1) |
| 919 | // rev2 := branch.GetHash(hash2) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 920 | // |
| 921 | // if rev1.GetHash() == rev2.GetHash() { |
| 922 | // // empty patch |
| 923 | // } else { |
| 924 | // // translate data to json and generate patch |
| 925 | // patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData()) |
| 926 | // patch. |
| 927 | // } |
| 928 | //} |
| 929 | |
| 930 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 931 | |
| 932 | // TODO: is tag mgmt used in the python implementation? Need to validate |
| 933 | |
| 934 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 935 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 936 | func (n *node) hasChildren(data interface{}) bool { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 937 | for fieldName, field := range ChildrenFields(n.Type) { |
| 938 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
| 939 | |
| 940 | if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() { |
| 941 | log.Error("cannot update external children") |
| 942 | return true |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return false |
| 947 | } |
| 948 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 949 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 950 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 951 | // CreateProxy returns a reference to a sub-tree of the data model |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 952 | func (n *node) CreateProxy(ctx context.Context, path string, exclusive bool) *Proxy { |
| 953 | return n.createProxy(ctx, path, path, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 954 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 955 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 956 | func (n *node) createProxy(ctx context.Context, path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 957 | log.Debugw("node-create-proxy", log.Fields{ |
| 958 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 959 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 960 | "path": path, |
| 961 | "fullPath": fullPath, |
| 962 | }) |
| 963 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 964 | for strings.HasPrefix(path, "/") { |
| 965 | path = path[1:] |
| 966 | } |
| 967 | if path == "" { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 968 | return n.makeProxy(path, fullPath, parentNode, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 969 | } |
| 970 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 971 | rev := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 972 | partition := strings.SplitN(path, "/", 2) |
| 973 | name := partition[0] |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 974 | var nodeType interface{} |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 975 | if len(partition) < 2 { |
| 976 | path = "" |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 977 | nodeType = n.Type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 978 | } else { |
| 979 | path = partition[1] |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 980 | nodeType = parentNode.Type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 981 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 982 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 983 | field := ChildrenFields(nodeType)[name] |
| 984 | |
| 985 | if field != nil { |
| 986 | if field.IsContainer { |
| 987 | log.Debugw("container-field", log.Fields{ |
| 988 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 989 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 990 | "path": path, |
| 991 | "name": name, |
| 992 | }) |
| 993 | if path == "" { |
| 994 | log.Debugw("folder-proxy", log.Fields{ |
| 995 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 996 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 997 | "fullPath": fullPath, |
| 998 | "name": name, |
| 999 | }) |
| 1000 | newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "") |
| 1001 | return newNode.makeProxy(path, fullPath, parentNode, exclusive) |
| 1002 | } else if field.Key != "" { |
| 1003 | log.Debugw("key-proxy", log.Fields{ |
| 1004 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1005 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1006 | "fullPath": fullPath, |
| 1007 | "name": name, |
| 1008 | }) |
| 1009 | partition := strings.SplitN(path, "/", 2) |
| 1010 | key := partition[0] |
| 1011 | if len(partition) < 2 { |
| 1012 | path = "" |
| 1013 | } else { |
| 1014 | path = partition[1] |
| 1015 | } |
| 1016 | keyValue := field.KeyFromStr(key) |
| 1017 | var children []Revision |
| 1018 | children = make([]Revision, len(rev.GetChildren(name))) |
| 1019 | copy(children, rev.GetChildren(name)) |
| 1020 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1021 | var childRev Revision |
| 1022 | if _, childRev = n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 1023 | log.Debugw("found-revision-matching-key-in-memory", log.Fields{ |
| 1024 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1025 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1026 | "fullPath": fullPath, |
| 1027 | "name": name, |
| 1028 | }) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1029 | } else if revs := n.GetBranch(NONE).GetLatest().LoadFromPersistence(ctx, fullPath, "", nil); revs != nil && len(revs) > 0 { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1030 | log.Debugw("found-revision-matching-key-in-db", 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 | }) |
| 1036 | childRev = revs[0] |
| 1037 | } else { |
| 1038 | log.Debugw("no-revision-matching-key", log.Fields{ |
| 1039 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1040 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1041 | "fullPath": fullPath, |
| 1042 | "name": name, |
| 1043 | }) |
| 1044 | } |
| 1045 | if childRev != nil { |
| 1046 | childNode := childRev.GetNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1047 | return childNode.createProxy(ctx, path, fullPath, n, exclusive) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1048 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1049 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1050 | log.Errorw("cannot-access-index-of-empty-container", log.Fields{ |
| 1051 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1052 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1053 | "path": path, |
| 1054 | "name": name, |
| 1055 | }) |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 1056 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1057 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1058 | log.Debugw("non-container-field", log.Fields{ |
| 1059 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1060 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1061 | "path": path, |
| 1062 | "name": name, |
| 1063 | }) |
| 1064 | childRev := rev.GetChildren(name)[0] |
| 1065 | childNode := childRev.GetNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1066 | return childNode.createProxy(ctx, path, fullPath, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1067 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1068 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1069 | log.Debugw("field-object-is-nil", log.Fields{ |
| 1070 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1071 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1072 | "fullPath": fullPath, |
| 1073 | "name": name, |
| 1074 | }) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1077 | log.Warnw("cannot-create-proxy", log.Fields{ |
| 1078 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1079 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1080 | "path": path, |
| 1081 | "fullPath": fullPath, |
| 1082 | "latest-rev": rev.GetHash(), |
| 1083 | }) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1084 | return nil |
| 1085 | } |
| 1086 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1087 | 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] | 1088 | log.Debugw("node-make-proxy", log.Fields{ |
| 1089 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1090 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1091 | "path": path, |
| 1092 | "fullPath": fullPath, |
| 1093 | }) |
| 1094 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1095 | r := &root{ |
| 1096 | node: n, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1097 | Callbacks: n.Root.GetCallbacks(), |
| 1098 | NotificationCallbacks: n.Root.GetNotificationCallbacks(), |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1099 | DirtyNodes: n.Root.DirtyNodes, |
| 1100 | KvStore: n.Root.KvStore, |
| 1101 | Loading: n.Root.Loading, |
| 1102 | RevisionClass: n.Root.RevisionClass, |
| 1103 | } |
| 1104 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1105 | if n.Proxy == nil { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1106 | log.Debugw("constructing-new-proxy", log.Fields{ |
| 1107 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1108 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1109 | "path": path, |
| 1110 | "fullPath": fullPath, |
| 1111 | }) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1112 | n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1113 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1114 | log.Debugw("node-has-existing-proxy", log.Fields{ |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1115 | "node-type": reflect.ValueOf(n.GetProxy().Node.Type).Type(), |
| 1116 | "parent-node-type": reflect.ValueOf(n.GetProxy().ParentNode.Type).Type(), |
| 1117 | "path": n.GetProxy().Path, |
| 1118 | "fullPath": n.GetProxy().FullPath, |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1119 | }) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1120 | if n.GetProxy().Exclusive { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1121 | log.Error("node is already owned exclusively") |
| 1122 | } |
| 1123 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1124 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1125 | return n.Proxy |
| 1126 | } |
| 1127 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1128 | func (n *node) makeEventBus() *EventBus { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1129 | if n.EventBus == nil { |
| 1130 | n.EventBus = NewEventBus() |
| 1131 | } |
| 1132 | return n.EventBus |
| 1133 | } |
| 1134 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1135 | func (n *node) SetProxy(proxy *Proxy) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1136 | n.Proxy = proxy |
| 1137 | } |
| 1138 | |
| 1139 | func (n *node) GetProxy() *Proxy { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1140 | return n.Proxy |
| 1141 | } |
| 1142 | |
| 1143 | func (n *node) GetBranch(key string) *Branch { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1144 | if n.Branches != nil { |
| 1145 | if branch, exists := n.Branches[key]; exists { |
| 1146 | return branch |
| 1147 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1148 | } |
| 1149 | return nil |
| 1150 | } |
| 1151 | |
| 1152 | func (n *node) SetBranch(key string, branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1153 | n.Branches[key] = branch |
| 1154 | } |
| 1155 | |
| 1156 | func (n *node) GetRoot() *root { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1157 | return n.Root |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1158 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1159 | func (n *node) SetRoot(root *root) { |
| 1160 | n.Root = root |
| 1161 | } |