blob: 2ff199f5f02e1c94627df11ecd35120830a64087 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
Stephane Barbariee16186c2018-09-11 10:46:34 -040018// TODO: proper error handling
19// TODO: proper logging
20
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040021import (
22 "fmt"
23 "github.com/golang/protobuf/proto"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040024 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025 "reflect"
26 "strings"
27)
28
29const (
30 NONE string = "none"
31)
32
Stephane Barbarie06c4a742018-10-01 11:09:32 -040033type Node interface {
34 MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple)
35
36 // CRUD functions
37 Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision
38 Get(path string, hash string, depth int, deep bool, txid string) interface{}
39 Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision
40 Remove(path string, txid string, makeBranch MakeBranchFunction) Revision
41
42 MakeBranch(txid string) *Branch
43 DeleteBranch(txid string)
44 MergeBranch(txid string, dryRun bool) (Revision, error)
45
46 MakeTxBranch() string
47 DeleteTxBranch(txid string)
48 FoldTxBranch(txid string)
49
50 GetProxy(path string, exclusive bool) *Proxy
51}
52
53type node struct {
54 root *root
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040055 Type interface{}
56 Branches map[string]*Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -040057 Tags map[string]Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040058 Proxy *Proxy
59 EventBus *EventBus
60 AutoPrune bool
61}
62
Stephane Barbarie694e2b92018-09-07 12:17:36 -040063type ChangeTuple struct {
64 Type CallbackType
65 Data interface{}
66}
67
Stephane Barbarie06c4a742018-10-01 11:09:32 -040068func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node {
69 n := &node{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040070
Stephane Barbarieec0919b2018-09-05 14:14:29 -040071 n.root = root
72 n.Branches = make(map[string]*Branch)
73 n.Tags = make(map[string]Revision)
74 n.Proxy = nil
75 n.EventBus = nil
76 n.AutoPrune = autoPrune
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040077
78 if IsProtoMessage(initialData) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040079 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040080 dataCopy := proto.Clone(initialData.(proto.Message))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040081 n.initialize(dataCopy, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040082 } else if reflect.ValueOf(initialData).IsValid() {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040083 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040084 } else {
85 // not implemented error
86 fmt.Errorf("cannot process initial data - %+v", initialData)
87 }
88
Stephane Barbarieec0919b2018-09-05 14:14:29 -040089 return n
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040090}
91
Stephane Barbarie06c4a742018-10-01 11:09:32 -040092func (n *node) MakeNode(data interface{}, txid string) *node {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040093 return NewNode(n.root, data, true, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040094}
95
Stephane Barbarie06c4a742018-10-01 11:09:32 -040096func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040097 return n.root.MakeRevision(branch, data, children)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040098}
99
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400100func (n *node) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
101 n.makeLatest(branch, revision, changeAnnouncement)
102}
103func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400104 if _, ok := branch.Revisions[revision.GetHash()]; !ok {
105 branch.Revisions[revision.GetHash()] = revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400106 }
107
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400108 if branch.Latest == nil || revision.GetHash() != branch.Latest.GetHash() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400109 branch.Latest = revision
110 }
111
112 if changeAnnouncement != nil && branch.Txid == "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400113 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400114 for _, change := range changeAnnouncement {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400115 fmt.Printf("invoking callback - changeType: %+v, data:%+v\n", change.Type, change.Data)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400116 n.root.AddCallback(n.Proxy.InvokeCallbacks, change.Type, change.Data, true)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400117 }
118 }
119
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400120 for _, change := range changeAnnouncement {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400121 fmt.Printf("sending notification - changeType: %+v, data:%+v\n", change.Type, change.Data)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400122 n.root.AddNotificationCallback(n.makeEventBus().Advertise, change.Type, change.Data, revision.GetHash())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400123 }
124 }
125}
126
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400127func (n *node) Latest(txid ...string) Revision {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400128 var branch *Branch
129 var exists bool
130
131 if len(txid) > 0 && txid[0] != "" {
132 if branch, exists = n.Branches[txid[0]]; exists {
133 return branch.Latest
134 }
135 } else if branch, exists = n.Branches[NONE]; exists {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400136 return branch.Latest
137 }
138 return nil
139}
140
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400141func (n *node) GetHash(hash string) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400142 return n.Branches[NONE].Revisions[hash]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400143}
144
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400145func (n *node) initialize(data interface{}, txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400146 var children map[string][]Revision
147 children = make(map[string][]Revision)
148 for fieldName, field := range ChildrenFields(n.Type) {
149 _, fieldValue := GetAttributeValue(data, fieldName, 0)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400150
151 if fieldValue.IsValid() {
152 if field.IsContainer {
153 if field.Key != "" {
154 var keysSeen []string
155
156 for i := 0; i < fieldValue.Len(); i++ {
157 v := fieldValue.Index(i)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400158
159 rev := n.MakeNode(v.Interface(), txid).Latest(txid)
160
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400161 _, key := GetAttributeValue(v.Interface(), field.Key, 0)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400162 for _, k := range keysSeen {
163 if k == key.String() {
164 fmt.Errorf("duplicate key - %s", k)
165 }
166 }
167 children[fieldName] = append(children[fieldName], rev)
168 keysSeen = append(keysSeen, key.String())
169 }
170
171 } else {
172 for i := 0; i < fieldValue.Len(); i++ {
173 v := fieldValue.Index(i)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400174 children[fieldName] = append(children[fieldName], n.MakeNode(v.Interface(), txid).Latest())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400175 }
176 }
177 } else {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400178 children[fieldName] = append(children[fieldName], n.MakeNode(fieldValue.Interface(), txid).Latest())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400179 }
180 } else {
181 fmt.Errorf("field is invalid - %+v", fieldValue)
182 }
183 }
184 // FIXME: ClearField??? No such method in go protos. Reset?
185 //data.ClearField(field_name)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400186 branch := NewBranch(n, "", nil, n.AutoPrune)
187 rev := n.MakeRevision(branch, data, children)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400188 n.makeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400189
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400190 if txid == "" {
191 n.Branches[NONE] = branch
192 } else {
193 n.Branches[txid] = branch
194 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400195}
196
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400197func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) {
198 for i, rev := range revs {
199 dataValue := reflect.ValueOf(rev.GetData())
200 dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0)
201
202 fieldValue := dataValue.Elem().FieldByName(dataStruct.Name)
203
204 log.Debugf("fieldValue: %+v, type: %+v, value: %+v", fieldValue.Interface(), fieldValue.Type(), value)
205 a := fmt.Sprintf("%s", fieldValue.Interface())
206 b := fmt.Sprintf("%s", value)
207 if a == b {
208 return i, rev
209 }
210 }
211
212 fmt.Errorf("key %s=%s not found", keyName, value)
213
214 return -1, nil
215}
216
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400217//
218// Get operation
219//
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400220func (n *node) Get(path string, hash string, depth int, deep bool, txid string) interface{} {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400221 if deep {
222 depth = -1
223 }
224
225 for strings.HasPrefix(path, "/") {
226 path = path[1:]
227 }
228
229 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400230 var rev Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400231
232 // FIXME: should empty txid be cleaned up?
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400233 if branch = n.Branches[txid]; txid == "" || branch == nil {
234 branch = n.Branches[NONE]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400235 }
236
237 if hash != "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400238 rev = branch.Revisions[hash]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400239 } else {
240 rev = branch.Latest
241 }
242
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400243 return n.getPath(rev, path, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400244}
245
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400246func (n *node) getPath(rev Revision, path string, depth int) interface{} {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400247 if path == "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400248 return n.getData(rev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400249 }
250
251 partition := strings.SplitN(path, "/", 2)
252 name := partition[0]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400253
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400254 if len(partition) < 2 {
255 path = ""
256 } else {
257 path = partition[1]
258 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400259
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400260 names := ChildrenFields(n.Type)
261 field := names[name]
262
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400263 if field.IsContainer {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400264 if field.Key != "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400265 children := rev.GetChildren()[name]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400266 if path != "" {
267 partition = strings.SplitN(path, "/", 2)
268 key := partition[0]
269 path = ""
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400270 keyValue := field.KeyFromStr(key)
271 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400272 return nil
273 } else {
274 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400275 return childNode.getPath(childRev, path, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400276 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400277 } else {
278 var response []interface{}
279 for _, childRev := range children {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400280 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400281 value := childNode.getData(childRev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400282 response = append(response, value)
283 }
284 return response
285 }
286 } else {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400287 var response []interface{}
288 if path != "" {
289 // TODO: raise error
290 return response
291 }
292 for _, childRev := range rev.GetChildren()[name] {
293 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400294 value := childNode.getData(childRev, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400295 response = append(response, value)
296 }
297 return response
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400298 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400299 } else {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400300 childRev := rev.GetChildren()[name][0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400301 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400302 return childNode.getPath(childRev, path, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400303 }
304 return nil
305}
306
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400307func (n *node) getData(rev Revision, depth int) interface{} {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400308 msg := rev.Get(depth)
309
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400310 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400311 log.Debug("invoking proxy GET Callbacks")
312 msg = n.Proxy.InvokeCallbacks(GET, msg, false)
313
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400314 }
315 return msg
316}
317
318//
319// Update operation
320//
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400321func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400322 // FIXME: is this required ... a bit overkill to take out a "/"
323 for strings.HasPrefix(path, "/") {
324 path = path[1:]
325 }
326
327 var branch *Branch
328 var ok bool
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400329 if txid == "" {
330 branch = n.Branches[NONE]
331 } else if branch, ok = n.Branches[txid]; !ok {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400332 branch = makeBranch(n)
333 }
334
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400335 log.Debugf("Branch data : %+v, Passed data: %+v", branch.Latest.GetData(), data)
336
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400337 if path == "" {
338 return n.doUpdate(branch, data, strict)
339 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400340
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400341 // TODO missing some code here...
342 rev := branch.Latest
343
344 partition := strings.SplitN(path, "/", 2)
345 name := partition[0]
346
347 if len(partition) < 2 {
348 path = ""
349 } else {
350 path = partition[1]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400351 }
352
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400353 field := ChildrenFields(n.Type)[name]
354 var children []Revision
355
356 if field.IsContainer {
357 if path == "" {
358 fmt.Errorf("cannot update a list\n")
359 } else if field.Key != "" {
360 partition := strings.SplitN(path, "/", 2)
361 key := partition[0]
362 if len(partition) < 2 {
363 path = ""
364 } else {
365 path = partition[1]
366 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400367 keyValue := field.KeyFromStr(key)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400368 // TODO. Est-ce que le copy ne fonctionne pas? dois-je plutôt faire un clone de chaque item?
369 for _, v := range rev.GetChildren()[name] {
370 revCopy := reflect.ValueOf(v).Interface().(Revision)
371 children = append(children, revCopy)
372 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400373 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400374 childNode := childRev.GetNode()
375 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
376 if newChildRev.GetHash() == childRev.GetHash() {
377 if newChildRev != childRev {
378 log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev)
379 newChildRev.ClearHash()
380 }
381 return branch.Latest
382 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400383
384 _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0)
385 log.Debugf("newKey is %s", newKey.Interface())
386 _newKeyType := fmt.Sprintf("%s", newKey)
387 _keyValueType := fmt.Sprintf("%s", keyValue)
388 if _newKeyType != _keyValueType {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400389 fmt.Errorf("cannot change key field\n")
390 }
391 children[idx] = newChildRev
392 rev = rev.UpdateChildren(name, children, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400393 branch.Latest.Drop(txid, false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400394 n.root.MakeLatest(branch, rev, nil)
395 return rev
396 } else {
397 fmt.Errorf("cannot index into container with no keys\n")
398 }
399 } else {
400 childRev := rev.GetChildren()[name][0]
401 childNode := childRev.GetNode()
402 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
403 rev = rev.UpdateChildren(name, []Revision{newChildRev}, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400404 branch.Latest.Drop(txid, false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400405 n.root.MakeLatest(branch, rev, nil)
406 return rev
407 }
408 return nil
409}
410
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400411func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400412 log.Debugf("Comparing types - expected: %+v, actual: %+v", reflect.ValueOf(n.Type).Type(), reflect.TypeOf(data))
413
414 if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() {
415 // TODO raise error
416 fmt.Errorf("data does not match type: %+v", n.Type)
417 return nil
418 }
419
420 // TODO: validate that this actually works
421 //if n.hasChildren(data) {
422 // return nil
423 //}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400424
425 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400426 log.Debug("invoking proxy PRE_UPDATE Callbacks")
427 n.Proxy.InvokeCallbacks(PRE_UPDATE, data, false)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400428 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400429 if !reflect.DeepEqual(branch.Latest.GetData(), data) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400430 if strict {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400431 // TODO: checkAccessViolations(data, Branch.GetLatest.data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400432 fmt.Println("checking access violations")
433 }
434 rev := branch.Latest.UpdateData(data, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400435 branch.Latest.Drop(branch.Txid, true)
436 n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_UPDATE, rev.GetData()}})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400437 return rev
438 } else {
439 return branch.Latest
440 }
441}
442
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400443//
444// Add operation
445//
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400446func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400447 for strings.HasPrefix(path, "/") {
448 path = path[1:]
449 }
450 if path == "" {
451 // TODO raise error
452 fmt.Errorf("cannot add for non-container mode\n")
453 }
454
455 var branch *Branch
456 var ok bool
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400457 if txid == "" {
458 branch = n.Branches[NONE]
459 } else if branch, ok = n.Branches[txid]; !ok {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400460 branch = makeBranch(n)
461 }
462
463 rev := branch.Latest
464
465 partition := strings.SplitN(path, "/", 2)
466 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400467
468 if len(partition) < 2 {
469 path = ""
470 } else {
471 path = partition[1]
472 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400473
474 field := ChildrenFields(n.Type)[name]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400475 var children []Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400476
477 if field.IsContainer {
478 if path == "" {
479 if field.Key != "" {
480 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400481 log.Debug("invoking proxy PRE_ADD Callbacks")
482 n.Proxy.InvokeCallbacks(PRE_ADD, data, false)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400483 }
484
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400485 for _, v := range rev.GetChildren()[name] {
486 revCopy := reflect.ValueOf(v).Interface().(Revision)
487 children = append(children, revCopy)
488 }
489 _, key := GetAttributeValue(data, field.Key, 0)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400490 if _, rev := n.findRevByKey(children, field.Key, key.String()); rev != nil {
491 // TODO raise error
492 fmt.Errorf("duplicate key found: %s", key.String())
493 }
494
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400495 childRev := n.MakeNode(data, txid).Latest(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400496 children = append(children, childRev)
497 rev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400498 branch.Latest.Drop(txid, false)
499 n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_ADD, rev.GetData()}})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400500 return rev
501 } else {
502 fmt.Errorf("cannot add to non-keyed container\n")
503 }
504 } else if field.Key != "" {
505 partition := strings.SplitN(path, "/", 2)
506 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400507 if len(partition) < 2 {
508 path = ""
509 } else {
510 path = partition[1]
511 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400512 keyValue := field.KeyFromStr(key)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400513 copy(children, rev.GetChildren()[name])
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400514 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400515 childNode := childRev.GetNode()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400516 newChildRev := childNode.Add(path, data, txid, makeBranch)
517 children[idx] = newChildRev
518 rev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400519 branch.Latest.Drop(txid, false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400520 n.root.MakeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400521 return rev
522 } else {
523 fmt.Errorf("cannot add to non-keyed container\n")
524 }
525 } else {
526 fmt.Errorf("cannot add to non-container field\n")
527 }
528 return nil
529}
530
531//
532// Remove operation
533//
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400534func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400535 for strings.HasPrefix(path, "/") {
536 path = path[1:]
537 }
538 if path == "" {
539 // TODO raise error
540 fmt.Errorf("cannot remove for non-container mode\n")
541 }
542 var branch *Branch
543 var ok bool
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400544 if txid == "" {
545 branch = n.Branches[NONE]
546 } else if branch, ok = n.Branches[txid]; !ok {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400547 branch = makeBranch(n)
548 }
549
550 rev := branch.Latest
551
552 partition := strings.SplitN(path, "/", 2)
553 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400554 if len(partition) < 2 {
555 path = ""
556 } else {
557 path = partition[1]
558 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400559
560 field := ChildrenFields(n.Type)[name]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400561 var children []Revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400562 postAnnouncement := []ChangeTuple{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400563
564 if field.IsContainer {
565 if path == "" {
566 fmt.Errorf("cannot remove without a key\n")
567 } else if field.Key != "" {
568 partition := strings.SplitN(path, "/", 2)
569 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400570 if len(partition) < 2 {
571 path = ""
572 } else {
573 path = partition[1]
574 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400575 keyValue := field.KeyFromStr(key)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400576 if path != "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400577 for _, v := range rev.GetChildren()[name] {
578 newV := reflect.ValueOf(v).Interface().(Revision)
579 children = append(children, newV)
580 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400581 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400582 childNode := childRev.GetNode()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400583 newChildRev := childNode.Remove(path, txid, makeBranch)
584 children[idx] = newChildRev
585 rev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400586 branch.Latest.Drop(txid, false)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400587 n.root.MakeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400588 return rev
589 } else {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400590 for _, v := range rev.GetChildren()[name] {
591 newV := reflect.ValueOf(v).Interface().(Revision)
592 children = append(children, newV)
593 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400594 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400595 if n.Proxy != nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400596 data := childRev.GetData()
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400597 n.Proxy.InvokeCallbacks(PRE_REMOVE, data, false)
598 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400599 } else {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400600 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData()})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400601 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400602 childRev.Drop(txid, true)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400603 children = append(children[:idx], children[idx+1:]...)
604 rev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400605 branch.Latest.Drop(txid, false)
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400606 n.root.MakeLatest(branch, rev, postAnnouncement)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400607 return rev
608 }
609 } else {
610 fmt.Errorf("cannot add to non-keyed container\n")
611 }
612 } else {
613 fmt.Errorf("cannot add to non-container field\n")
614 }
615
616 return nil
617}
618
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400619// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
620
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400621type MakeBranchFunction func(*node) *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400622
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400623func (n *node) MakeBranch(txid string) *Branch {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400624 branchPoint := n.Branches[NONE].Latest
625 branch := NewBranch(n, txid, branchPoint, true)
626 n.Branches[txid] = branch
627 return branch
628}
629
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400630func (n *node) DeleteBranch(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400631 delete(n.Branches, txid)
632}
633
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400634func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400635 f := func(rev Revision) Revision {
636 childBranch := rev.GetBranch()
637
638 if childBranch.Txid == txid {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400639 rev, _ = childBranch.Node.MergeBranch(txid, dryRun)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400640 }
641
642 return rev
643 }
644 return f
645}
646
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400647func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400648 srcBranch := n.Branches[txid]
649 dstBranch := n.Branches[NONE]
650
651 forkRev := srcBranch.Origin
652 srcRev := srcBranch.Latest
653 dstRev := dstBranch.Latest
654
655 rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
656
657 if !dryRun {
658 n.root.MakeLatest(dstBranch, rev, changes)
659 delete(n.Branches, txid)
660 }
661
Stephane Barbariee16186c2018-09-11 10:46:34 -0400662 // TODO: return proper error when one occurs
663 return rev, nil
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400664}
665
666// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
667
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400668//func (n *node) diff(hash1, hash2, txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400669// branch := n.Branches[txid]
670// rev1 := branch.get(hash1)
671// rev2 := branch.get(hash2)
672//
673// if rev1.GetHash() == rev2.GetHash() {
674// // empty patch
675// } else {
676// // translate data to json and generate patch
677// patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData())
678// patch.
679// }
680//}
681
682// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
683
684// TODO: is tag mgmt used in the python implementation? Need to validate
685
686// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
687
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400688func (n *node) hasChildren(data interface{}) bool {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400689 for fieldName, field := range ChildrenFields(n.Type) {
690 _, fieldValue := GetAttributeValue(data, fieldName, 0)
691
692 if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() {
693 log.Error("cannot update external children")
694 return true
695 }
696 }
697
698 return false
699}
700
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400701// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400702
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400703func (n *node) GetProxy(path string, exclusive bool) *Proxy {
704 r := NewRoot(n.Type, n.root.KvStore)
705 r.node = *n
706 r.KvStore = n.root.KvStore
707
708 return n.getProxy(path, r, path, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400709}
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400710func (n *node) getProxy(path string, root Root, fullPath string, exclusive bool) *Proxy {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400711 for strings.HasPrefix(path, "/") {
712 path = path[1:]
713 }
714 if path == "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400715 return n.makeProxy(root, path, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400716 }
717
718 rev := n.Branches[NONE].Latest
719 partition := strings.SplitN(path, "/", 2)
720 name := partition[0]
721 path = partition[1]
722
723 field := ChildrenFields(n.Type)[name]
724 if field.IsContainer {
725 if path == "" {
726 log.Error("cannot proxy a container field")
727 }
728 if field.Key != "" {
729 partition := strings.SplitN(path, "/", 2)
730 key := partition[0]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400731 if len(partition) < 2 {
732 path = ""
733 } else {
734 path = partition[1]
735 }
736 keyValue := field.KeyFromStr(key)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400737 children := rev.GetChildren()[name]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400738 _, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400739 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400740
741 r := NewRoot(childNode.Type, n.root.KvStore)
742 r.node = *childNode
743 r.KvStore = childNode.root.KvStore
744
745 return childNode.getProxy(path, r, fullPath, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400746 }
747 log.Error("cannot index into container with no keys")
748 } else {
749 childRev := rev.GetChildren()[name][0]
750 childNode := childRev.GetNode()
751 return childNode.getProxy(path, root, fullPath, exclusive)
752 }
753
754 return nil
755}
756
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400757func (n *node) makeProxy(root Root, fullPath string, exclusive bool) *Proxy {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400758 if n.Proxy == nil {
759 n.Proxy = NewProxy(root, n, fullPath, exclusive)
760 } else {
761 if n.Proxy.Exclusive {
762 log.Error("node is already owned exclusively")
763 }
764 }
765 return n.Proxy
766}
767
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400768func (n *node) makeEventBus() *EventBus {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400769 if n.EventBus == nil {
770 n.EventBus = NewEventBus()
771 }
772 return n.EventBus
773}
774
775// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Persistence Loading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
776
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400777func (n *node) LoadLatest(kvStore *Backend, hash string) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400778 branch := NewBranch(n, "", nil, n.AutoPrune)
779 pr := &PersistedRevision{}
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400780 rev := pr.Load(branch, kvStore, n.Type, hash)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400781 n.makeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400782 n.Branches[NONE] = branch
783}
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400784
785func (n *node) MakeTxBranch() string {
786 return n.root.MakeTxBranch()
787}
788func (n *node) FoldTxBranch(txid string) {
789 n.root.FoldTxBranch(txid)
790}
791func (n *node) DeleteTxBranch(txid string) {
792 n.root.DeleteTxBranch(txid)
793}