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