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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 18 | // TODO: proper error handling |
| 19 | // TODO: proper logging |
| 20 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 21 | import ( |
| 22 | "fmt" |
| 23 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "reflect" |
| 26 | "strings" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | NONE string = "none" |
| 31 | ) |
| 32 | |
| 33 | type Node struct { |
| 34 | root *Root |
| 35 | Type interface{} |
| 36 | Branches map[string]*Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 37 | Tags map[string]Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 38 | Proxy *Proxy |
| 39 | EventBus *EventBus |
| 40 | AutoPrune bool |
| 41 | } |
| 42 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 43 | type ChangeTuple struct { |
| 44 | Type CallbackType |
| 45 | Data interface{} |
| 46 | } |
| 47 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 48 | func NewNode(root *Root, initialData interface{}, autoPrune bool, txid string) *Node { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 49 | n := &Node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 50 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 51 | n.root = root |
| 52 | n.Branches = make(map[string]*Branch) |
| 53 | n.Tags = make(map[string]Revision) |
| 54 | n.Proxy = nil |
| 55 | n.EventBus = nil |
| 56 | n.AutoPrune = autoPrune |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 57 | |
| 58 | if IsProtoMessage(initialData) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 59 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 60 | dataCopy := proto.Clone(initialData.(proto.Message)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | n.initialize(dataCopy, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 62 | } else if reflect.ValueOf(initialData).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 64 | } else { |
| 65 | // not implemented error |
| 66 | fmt.Errorf("cannot process initial data - %+v", initialData) |
| 67 | } |
| 68 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 69 | return n |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 72 | func (n *Node) MakeNode(data interface{}, txid string) *Node { |
| 73 | return NewNode(n.root, data, true, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 76 | func (n *Node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 77 | return n.root.MakeRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 80 | func (n *Node) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 81 | if _, ok := branch.Revisions[revision.GetHash()]; !ok { |
| 82 | branch.Revisions[revision.GetHash()] = revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 85 | if branch.Latest == nil || revision.GetHash() != branch.Latest.GetHash() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 86 | branch.Latest = revision |
| 87 | } |
| 88 | |
| 89 | if changeAnnouncement != nil && branch.Txid == "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 90 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 91 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 92 | fmt.Printf("invoking callback - changeType: %+v, data:%+v\n", change.Type, change.Data) |
| 93 | n.root.addCallback(n.Proxy.InvokeCallbacks, change.Type, change.Data, true) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 97 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 98 | fmt.Printf("sending notification - changeType: %+v, data:%+v\n", change.Type, change.Data) |
| 99 | n.root.addNotificationCallback(n.makeEventBus().Advertise, change.Type, change.Data, revision.GetHash()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 104 | func (n *Node) Latest(txid ...string) Revision { |
| 105 | var branch *Branch |
| 106 | var exists bool |
| 107 | |
| 108 | if len(txid) > 0 && txid[0] != "" { |
| 109 | if branch, exists = n.Branches[txid[0]]; exists { |
| 110 | return branch.Latest |
| 111 | } |
| 112 | } else if branch, exists = n.Branches[NONE]; exists { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 113 | return branch.Latest |
| 114 | } |
| 115 | return nil |
| 116 | } |
| 117 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 118 | func (n *Node) GetHash(hash string) Revision { |
| 119 | return n.Branches[NONE].Revisions[hash] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 122 | func (n *Node) initialize(data interface{}, txid string) { |
| 123 | var children map[string][]Revision |
| 124 | children = make(map[string][]Revision) |
| 125 | for fieldName, field := range ChildrenFields(n.Type) { |
| 126 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | |
| 128 | if fieldValue.IsValid() { |
| 129 | if field.IsContainer { |
| 130 | if field.Key != "" { |
| 131 | var keysSeen []string |
| 132 | |
| 133 | for i := 0; i < fieldValue.Len(); i++ { |
| 134 | v := fieldValue.Index(i) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 135 | |
| 136 | rev := n.MakeNode(v.Interface(), txid).Latest(txid) |
| 137 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 138 | _, key := GetAttributeValue(v.Interface(), field.Key, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 139 | for _, k := range keysSeen { |
| 140 | if k == key.String() { |
| 141 | fmt.Errorf("duplicate key - %s", k) |
| 142 | } |
| 143 | } |
| 144 | children[fieldName] = append(children[fieldName], rev) |
| 145 | keysSeen = append(keysSeen, key.String()) |
| 146 | } |
| 147 | |
| 148 | } else { |
| 149 | for i := 0; i < fieldValue.Len(); i++ { |
| 150 | v := fieldValue.Index(i) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 151 | children[fieldName] = append(children[fieldName], n.MakeNode(v.Interface(), txid).Latest()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 155 | children[fieldName] = append(children[fieldName], n.MakeNode(fieldValue.Interface(), txid).Latest()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 156 | } |
| 157 | } else { |
| 158 | fmt.Errorf("field is invalid - %+v", fieldValue) |
| 159 | } |
| 160 | } |
| 161 | // FIXME: ClearField??? No such method in go protos. Reset? |
| 162 | //data.ClearField(field_name) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 163 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 164 | rev := n.MakeRevision(branch, data, children) |
| 165 | n.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 166 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 167 | if txid == "" { |
| 168 | n.Branches[NONE] = branch |
| 169 | } else { |
| 170 | n.Branches[txid] = branch |
| 171 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 174 | // |
| 175 | // Get operation |
| 176 | // |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 177 | func (n *Node) Get(path string, hash string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 178 | if deep { |
| 179 | depth = -1 |
| 180 | } |
| 181 | |
| 182 | for strings.HasPrefix(path, "/") { |
| 183 | path = path[1:] |
| 184 | } |
| 185 | |
| 186 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 187 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 188 | |
| 189 | // FIXME: should empty txid be cleaned up? |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | if branch = n.Branches[txid]; txid == "" || branch == nil { |
| 191 | branch = n.Branches[NONE] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if hash != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 195 | rev = branch.Revisions[hash] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 196 | } else { |
| 197 | rev = branch.Latest |
| 198 | } |
| 199 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 200 | return n.get(rev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 203 | func (n *Node) findRevByKey(revs []Revision, keyName string, value string) (int, Revision) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 204 | for i, rev := range revs { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 205 | dataValue := reflect.ValueOf(rev.GetData()) |
| 206 | dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 207 | |
| 208 | fieldValue := dataValue.Elem().FieldByName(dataStruct.Name) |
| 209 | |
| 210 | if fieldValue.Interface().(string) == value { |
| 211 | return i, rev |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | fmt.Errorf("key %s=%s not found", keyName, value) |
| 216 | |
| 217 | return -1, nil |
| 218 | } |
| 219 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 220 | func (n *Node) get(rev Revision, path string, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 221 | if path == "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 222 | return n.doGet(rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | partition := strings.SplitN(path, "/", 2) |
| 226 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 227 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 228 | if len(partition) < 2 { |
| 229 | path = "" |
| 230 | } else { |
| 231 | path = partition[1] |
| 232 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 233 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 234 | names := ChildrenFields(n.Type) |
| 235 | field := names[name] |
| 236 | |
| 237 | if field != nil && field.IsContainer { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 238 | if field.Key != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 239 | children := rev.GetChildren()[name] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 240 | if path != "" { |
| 241 | partition = strings.SplitN(path, "/", 2) |
| 242 | key := partition[0] |
| 243 | path = "" |
| 244 | key = field.KeyFromStr(key).(string) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 245 | if _, childRev := n.findRevByKey(children, field.Key, key); childRev == nil { |
| 246 | return nil |
| 247 | } else { |
| 248 | childNode := childRev.GetNode() |
| 249 | return childNode.get(childRev, path, depth) |
| 250 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 251 | } else { |
| 252 | var response []interface{} |
| 253 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 254 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 255 | value := childNode.doGet(childRev, depth) |
| 256 | response = append(response, value) |
| 257 | } |
| 258 | return response |
| 259 | } |
| 260 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 261 | var response []interface{} |
| 262 | if path != "" { |
| 263 | // TODO: raise error |
| 264 | return response |
| 265 | } |
| 266 | for _, childRev := range rev.GetChildren()[name] { |
| 267 | childNode := childRev.GetNode() |
| 268 | value := childNode.doGet(childRev, depth) |
| 269 | response = append(response, value) |
| 270 | } |
| 271 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 272 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 273 | } else { |
| 274 | c1 := rev.GetChildren()[name] |
| 275 | childRev := c1[0] |
| 276 | childNode := childRev.GetNode() |
| 277 | return childNode.get(childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 278 | } |
| 279 | return nil |
| 280 | } |
| 281 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 282 | func (n *Node) doGet(rev Revision, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 283 | msg := rev.Get(depth) |
| 284 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 285 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 286 | log.Debug("invoking proxy GET Callbacks") |
| 287 | msg = n.Proxy.InvokeCallbacks(GET, msg, false) |
| 288 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 289 | } |
| 290 | return msg |
| 291 | } |
| 292 | |
| 293 | // |
| 294 | // Update operation |
| 295 | // |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 296 | func (n *Node) Update(path string, data interface{}, strict bool, txid string, makeBranch t_makeBranch) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 297 | // FIXME: is this required ... a bit overkill to take out a "/" |
| 298 | for strings.HasPrefix(path, "/") { |
| 299 | path = path[1:] |
| 300 | } |
| 301 | |
| 302 | var branch *Branch |
| 303 | var ok bool |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 304 | if txid == "" { |
| 305 | branch = n.Branches[NONE] |
| 306 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 307 | branch = makeBranch(n) |
| 308 | } |
| 309 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 310 | log.Debugf("Branch data : %+v, Passed data: %+v", branch.Latest.GetData(), data) |
| 311 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 312 | if path == "" { |
| 313 | return n.doUpdate(branch, data, strict) |
| 314 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 315 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 316 | // TODO missing some code here... |
| 317 | rev := branch.Latest |
| 318 | |
| 319 | partition := strings.SplitN(path, "/", 2) |
| 320 | name := partition[0] |
| 321 | |
| 322 | if len(partition) < 2 { |
| 323 | path = "" |
| 324 | } else { |
| 325 | path = partition[1] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 328 | field := ChildrenFields(n.Type)[name] |
| 329 | var children []Revision |
| 330 | |
| 331 | if field.IsContainer { |
| 332 | if path == "" { |
| 333 | fmt.Errorf("cannot update a list\n") |
| 334 | } else if field.Key != "" { |
| 335 | partition := strings.SplitN(path, "/", 2) |
| 336 | key := partition[0] |
| 337 | if len(partition) < 2 { |
| 338 | path = "" |
| 339 | } else { |
| 340 | path = partition[1] |
| 341 | } |
| 342 | key = field.KeyFromStr(key).(string) |
| 343 | // TODO. Est-ce que le copy ne fonctionne pas? dois-je plutôt faire un clone de chaque item? |
| 344 | for _, v := range rev.GetChildren()[name] { |
| 345 | revCopy := reflect.ValueOf(v).Interface().(Revision) |
| 346 | children = append(children, revCopy) |
| 347 | } |
| 348 | idx, childRev := n.findRevByKey(children, field.Key, key) |
| 349 | childNode := childRev.GetNode() |
| 350 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
| 351 | if newChildRev.GetHash() == childRev.GetHash() { |
| 352 | if newChildRev != childRev { |
| 353 | log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev) |
| 354 | newChildRev.ClearHash() |
| 355 | } |
| 356 | return branch.Latest |
| 357 | } |
| 358 | if _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0); newKey.Interface().(string) != key { |
| 359 | fmt.Errorf("cannot change key field\n") |
| 360 | } |
| 361 | children[idx] = newChildRev |
| 362 | rev = rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 363 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 364 | n.root.MakeLatest(branch, rev, nil) |
| 365 | return rev |
| 366 | } else { |
| 367 | fmt.Errorf("cannot index into container with no keys\n") |
| 368 | } |
| 369 | } else { |
| 370 | childRev := rev.GetChildren()[name][0] |
| 371 | childNode := childRev.GetNode() |
| 372 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
| 373 | rev = rev.UpdateChildren(name, []Revision{newChildRev}, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 374 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 375 | n.root.MakeLatest(branch, rev, nil) |
| 376 | return rev |
| 377 | } |
| 378 | return nil |
| 379 | } |
| 380 | |
| 381 | func (n *Node) doUpdate(branch *Branch, data interface{}, strict bool) Revision { |
| 382 | log.Debugf("Comparing types - expected: %+v, actual: %+v", reflect.ValueOf(n.Type).Type(), reflect.TypeOf(data)) |
| 383 | |
| 384 | if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() { |
| 385 | // TODO raise error |
| 386 | fmt.Errorf("data does not match type: %+v", n.Type) |
| 387 | return nil |
| 388 | } |
| 389 | |
| 390 | // TODO: validate that this actually works |
| 391 | //if n.hasChildren(data) { |
| 392 | // return nil |
| 393 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 394 | |
| 395 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 396 | log.Debug("invoking proxy PRE_UPDATE Callbacks") |
| 397 | n.Proxy.InvokeCallbacks(PRE_UPDATE, data, false) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 398 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 399 | if !reflect.DeepEqual(branch.Latest.GetData(), data) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 400 | if strict { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 401 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 402 | fmt.Println("checking access violations") |
| 403 | } |
| 404 | rev := branch.Latest.UpdateData(data, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 405 | branch.Latest.Drop(branch.Txid, true) |
| 406 | n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_UPDATE, rev.GetData()}}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 407 | return rev |
| 408 | } else { |
| 409 | return branch.Latest |
| 410 | } |
| 411 | } |
| 412 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 413 | // |
| 414 | // Add operation |
| 415 | // |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 416 | func (n *Node) Add(path string, data interface{}, txid string, makeBranch t_makeBranch) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 417 | for strings.HasPrefix(path, "/") { |
| 418 | path = path[1:] |
| 419 | } |
| 420 | if path == "" { |
| 421 | // TODO raise error |
| 422 | fmt.Errorf("cannot add for non-container mode\n") |
| 423 | } |
| 424 | |
| 425 | var branch *Branch |
| 426 | var ok bool |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 427 | if txid == "" { |
| 428 | branch = n.Branches[NONE] |
| 429 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 430 | branch = makeBranch(n) |
| 431 | } |
| 432 | |
| 433 | rev := branch.Latest |
| 434 | |
| 435 | partition := strings.SplitN(path, "/", 2) |
| 436 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 437 | |
| 438 | if len(partition) < 2 { |
| 439 | path = "" |
| 440 | } else { |
| 441 | path = partition[1] |
| 442 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 443 | |
| 444 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 445 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 446 | |
| 447 | if field.IsContainer { |
| 448 | if path == "" { |
| 449 | if field.Key != "" { |
| 450 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 451 | log.Debug("invoking proxy PRE_ADD Callbacks") |
| 452 | n.Proxy.InvokeCallbacks(PRE_ADD, data, false) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 453 | } |
| 454 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 455 | for _, v := range rev.GetChildren()[name] { |
| 456 | revCopy := reflect.ValueOf(v).Interface().(Revision) |
| 457 | children = append(children, revCopy) |
| 458 | } |
| 459 | _, key := GetAttributeValue(data, field.Key, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 460 | if _, rev := n.findRevByKey(children, field.Key, key.String()); rev != nil { |
| 461 | // TODO raise error |
| 462 | fmt.Errorf("duplicate key found: %s", key.String()) |
| 463 | } |
| 464 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 465 | childRev := n.MakeNode(data, txid).Latest(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 466 | children = append(children, childRev) |
| 467 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 468 | branch.Latest.Drop(txid, false) |
| 469 | n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_ADD, rev.GetData()}}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 470 | return rev |
| 471 | } else { |
| 472 | fmt.Errorf("cannot add to non-keyed container\n") |
| 473 | } |
| 474 | } else if field.Key != "" { |
| 475 | partition := strings.SplitN(path, "/", 2) |
| 476 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 477 | if len(partition) < 2 { |
| 478 | path = "" |
| 479 | } else { |
| 480 | path = partition[1] |
| 481 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 482 | key = field.KeyFromStr(key).(string) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 483 | copy(children, rev.GetChildren()[name]) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 484 | idx, childRev := n.findRevByKey(children, field.Key, key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 485 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 486 | newChildRev := childNode.Add(path, data, txid, makeBranch) |
| 487 | children[idx] = newChildRev |
| 488 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 489 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 490 | n.root.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 491 | return rev |
| 492 | } else { |
| 493 | fmt.Errorf("cannot add to non-keyed container\n") |
| 494 | } |
| 495 | } else { |
| 496 | fmt.Errorf("cannot add to non-container field\n") |
| 497 | } |
| 498 | return nil |
| 499 | } |
| 500 | |
| 501 | // |
| 502 | // Remove operation |
| 503 | // |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 504 | func (n *Node) Remove(path string, txid string, makeBranch t_makeBranch) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 505 | for strings.HasPrefix(path, "/") { |
| 506 | path = path[1:] |
| 507 | } |
| 508 | if path == "" { |
| 509 | // TODO raise error |
| 510 | fmt.Errorf("cannot remove for non-container mode\n") |
| 511 | } |
| 512 | var branch *Branch |
| 513 | var ok bool |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 514 | if txid == "" { |
| 515 | branch = n.Branches[NONE] |
| 516 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 517 | branch = makeBranch(n) |
| 518 | } |
| 519 | |
| 520 | rev := branch.Latest |
| 521 | |
| 522 | partition := strings.SplitN(path, "/", 2) |
| 523 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 524 | if len(partition) < 2 { |
| 525 | path = "" |
| 526 | } else { |
| 527 | path = partition[1] |
| 528 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 529 | |
| 530 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 531 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 532 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 533 | |
| 534 | if field.IsContainer { |
| 535 | if path == "" { |
| 536 | fmt.Errorf("cannot remove without a key\n") |
| 537 | } else if field.Key != "" { |
| 538 | partition := strings.SplitN(path, "/", 2) |
| 539 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 540 | if len(partition) < 2 { |
| 541 | path = "" |
| 542 | } else { |
| 543 | path = partition[1] |
| 544 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 545 | key = field.KeyFromStr(key).(string) |
| 546 | if path != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 547 | for _, v := range rev.GetChildren()[name] { |
| 548 | newV := reflect.ValueOf(v).Interface().(Revision) |
| 549 | children = append(children, newV) |
| 550 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 551 | idx, childRev := n.findRevByKey(children, field.Key, key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 552 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 553 | newChildRev := childNode.Remove(path, txid, makeBranch) |
| 554 | children[idx] = newChildRev |
| 555 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 556 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 557 | n.root.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 558 | return rev |
| 559 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 560 | for _, v := range rev.GetChildren()[name] { |
| 561 | newV := reflect.ValueOf(v).Interface().(Revision) |
| 562 | children = append(children, newV) |
| 563 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 564 | idx, childRev := n.findRevByKey(children, field.Key, key) |
| 565 | if n.Proxy != nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 566 | data := childRev.GetData() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 567 | n.Proxy.InvokeCallbacks(PRE_REMOVE, data, false) |
| 568 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 569 | } else { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 570 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 571 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 572 | childRev.Drop(txid, true) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 573 | children = append(children[:idx], children[idx+1:]...) |
| 574 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame^] | 575 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 576 | n.root.MakeLatest(branch, rev, postAnnouncement) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 577 | return rev |
| 578 | } |
| 579 | } else { |
| 580 | fmt.Errorf("cannot add to non-keyed container\n") |
| 581 | } |
| 582 | } else { |
| 583 | fmt.Errorf("cannot add to non-container field\n") |
| 584 | } |
| 585 | |
| 586 | return nil |
| 587 | } |
| 588 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 589 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 590 | |
| 591 | type t_makeBranch func(*Node) *Branch |
| 592 | |
| 593 | func (n *Node) makeTxBranch(txid string) *Branch { |
| 594 | branchPoint := n.Branches[NONE].Latest |
| 595 | branch := NewBranch(n, txid, branchPoint, true) |
| 596 | n.Branches[txid] = branch |
| 597 | return branch |
| 598 | } |
| 599 | |
| 600 | func (n *Node) deleteTxBranch(txid string) { |
| 601 | delete(n.Branches, txid) |
| 602 | } |
| 603 | |
| 604 | func (n *Node) mergeChild(txid string, dryRun bool) func(Revision) Revision { |
| 605 | f := func(rev Revision) Revision { |
| 606 | childBranch := rev.GetBranch() |
| 607 | |
| 608 | if childBranch.Txid == txid { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 609 | rev, _ = childBranch.Node.mergeTxBranch(txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return rev |
| 613 | } |
| 614 | return f |
| 615 | } |
| 616 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 617 | func (n *Node) mergeTxBranch(txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 618 | srcBranch := n.Branches[txid] |
| 619 | dstBranch := n.Branches[NONE] |
| 620 | |
| 621 | forkRev := srcBranch.Origin |
| 622 | srcRev := srcBranch.Latest |
| 623 | dstRev := dstBranch.Latest |
| 624 | |
| 625 | rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun) |
| 626 | |
| 627 | if !dryRun { |
| 628 | n.root.MakeLatest(dstBranch, rev, changes) |
| 629 | delete(n.Branches, txid) |
| 630 | } |
| 631 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 632 | // TODO: return proper error when one occurs |
| 633 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 637 | |
| 638 | //func (n *Node) diff(hash1, hash2, txid string) { |
| 639 | // branch := n.Branches[txid] |
| 640 | // rev1 := branch.get(hash1) |
| 641 | // rev2 := branch.get(hash2) |
| 642 | // |
| 643 | // if rev1.GetHash() == rev2.GetHash() { |
| 644 | // // empty patch |
| 645 | // } else { |
| 646 | // // translate data to json and generate patch |
| 647 | // patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData()) |
| 648 | // patch. |
| 649 | // } |
| 650 | //} |
| 651 | |
| 652 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 653 | |
| 654 | // TODO: is tag mgmt used in the python implementation? Need to validate |
| 655 | |
| 656 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 657 | |
| 658 | func (n *Node) hasChildren(data interface{}) bool { |
| 659 | for fieldName, field := range ChildrenFields(n.Type) { |
| 660 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
| 661 | |
| 662 | if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() { |
| 663 | log.Error("cannot update external children") |
| 664 | return true |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return false |
| 669 | } |
| 670 | |
| 671 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 672 | |
| 673 | func (n *Node) GetProxy(path string, exclusive bool) *Proxy { |
| 674 | return n.getProxy(path, n.root, path, exclusive) |
| 675 | } |
| 676 | func (n *Node) getProxy(path string, root *Root, fullPath string, exclusive bool) *Proxy { |
| 677 | for strings.HasPrefix(path, "/") { |
| 678 | path = path[1:] |
| 679 | } |
| 680 | if path == "" { |
| 681 | return n.makeProxy(n.root, path, exclusive) |
| 682 | } |
| 683 | |
| 684 | rev := n.Branches[NONE].Latest |
| 685 | partition := strings.SplitN(path, "/", 2) |
| 686 | name := partition[0] |
| 687 | path = partition[1] |
| 688 | |
| 689 | field := ChildrenFields(n.Type)[name] |
| 690 | if field.IsContainer { |
| 691 | if path == "" { |
| 692 | log.Error("cannot proxy a container field") |
| 693 | } |
| 694 | if field.Key != "" { |
| 695 | partition := strings.SplitN(path, "/", 2) |
| 696 | key := partition[0] |
| 697 | path = partition[1] |
| 698 | key = field.KeyFromStr(key).(string) |
| 699 | children := rev.GetChildren()[name] |
| 700 | _, childRev := n.findRevByKey(children, field.Key, key) |
| 701 | childNode := childRev.GetNode() |
| 702 | return childNode.getProxy(path, root, fullPath, exclusive) |
| 703 | } |
| 704 | log.Error("cannot index into container with no keys") |
| 705 | } else { |
| 706 | childRev := rev.GetChildren()[name][0] |
| 707 | childNode := childRev.GetNode() |
| 708 | return childNode.getProxy(path, root, fullPath, exclusive) |
| 709 | } |
| 710 | |
| 711 | return nil |
| 712 | } |
| 713 | |
| 714 | func (n *Node) makeProxy(root *Root, fullPath string, exclusive bool) *Proxy { |
| 715 | if n.Proxy == nil { |
| 716 | n.Proxy = NewProxy(root, n, fullPath, exclusive) |
| 717 | } else { |
| 718 | if n.Proxy.Exclusive { |
| 719 | log.Error("node is already owned exclusively") |
| 720 | } |
| 721 | } |
| 722 | return n.Proxy |
| 723 | } |
| 724 | |
| 725 | func (n *Node) makeEventBus() *EventBus { |
| 726 | if n.EventBus == nil { |
| 727 | n.EventBus = NewEventBus() |
| 728 | } |
| 729 | return n.EventBus |
| 730 | } |
| 731 | |
| 732 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Persistence Loading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 733 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 734 | func (n *Node) LoadLatest(kvStore *Backend, hash string) { |
| 735 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 736 | pr := &PersistedRevision{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 737 | rev := pr.Load(branch, kvStore, n.Type, hash) |
| 738 | n.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 739 | n.Branches[NONE] = branch |
| 740 | } |