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 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 33 | type 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 | |
| 53 | type node struct { |
| 54 | root *root |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | Type interface{} |
| 56 | Branches map[string]*Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 57 | Tags map[string]Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 58 | Proxy *Proxy |
| 59 | EventBus *EventBus |
| 60 | AutoPrune bool |
| 61 | } |
| 62 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 63 | type ChangeTuple struct { |
| 64 | Type CallbackType |
| 65 | Data interface{} |
| 66 | } |
| 67 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 68 | func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node { |
| 69 | n := &node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 77 | |
| 78 | if IsProtoMessage(initialData) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 79 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 80 | dataCopy := proto.Clone(initialData.(proto.Message)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 81 | n.initialize(dataCopy, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 82 | } else if reflect.ValueOf(initialData).IsValid() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 83 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 84 | } else { |
| 85 | // not implemented error |
| 86 | fmt.Errorf("cannot process initial data - %+v", initialData) |
| 87 | } |
| 88 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 89 | return n |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 92 | func (n *node) MakeNode(data interface{}, txid string) *node { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 93 | return NewNode(n.root, data, true, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 96 | func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 97 | return n.root.MakeRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 100 | func (n *node) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 101 | n.makeLatest(branch, revision, changeAnnouncement) |
| 102 | } |
| 103 | func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 104 | if _, ok := branch.Revisions[revision.GetHash()]; !ok { |
| 105 | branch.Revisions[revision.GetHash()] = revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 108 | if branch.Latest == nil || revision.GetHash() != branch.Latest.GetHash() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 109 | branch.Latest = revision |
| 110 | } |
| 111 | |
| 112 | if changeAnnouncement != nil && branch.Txid == "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 113 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 114 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 115 | fmt.Printf("invoking callback - changeType: %+v, data:%+v\n", change.Type, change.Data) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 116 | n.root.AddCallback(n.Proxy.InvokeCallbacks, change.Type, change.Data, true) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 120 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 121 | fmt.Printf("sending notification - changeType: %+v, data:%+v\n", change.Type, change.Data) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 122 | n.root.AddNotificationCallback(n.makeEventBus().Advertise, change.Type, change.Data, revision.GetHash()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 127 | func (n *node) Latest(txid ...string) Revision { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 128 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 136 | return branch.Latest |
| 137 | } |
| 138 | return nil |
| 139 | } |
| 140 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 141 | func (n *node) GetHash(hash string) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 142 | return n.Branches[NONE].Revisions[hash] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 145 | func (n *node) initialize(data interface{}, txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 146 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 150 | |
| 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 Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 158 | |
| 159 | rev := n.MakeNode(v.Interface(), txid).Latest(txid) |
| 160 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 161 | _, key := GetAttributeValue(v.Interface(), field.Key, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 174 | children[fieldName] = append(children[fieldName], n.MakeNode(v.Interface(), txid).Latest()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 178 | children[fieldName] = append(children[fieldName], n.MakeNode(fieldValue.Interface(), txid).Latest()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 179 | } |
| 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 186 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 187 | rev := n.MakeRevision(branch, data, children) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 188 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 189 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | if txid == "" { |
| 191 | n.Branches[NONE] = branch |
| 192 | } else { |
| 193 | n.Branches[txid] = branch |
| 194 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 197 | func (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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 217 | // |
| 218 | // Get operation |
| 219 | // |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 220 | 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] | 221 | if deep { |
| 222 | depth = -1 |
| 223 | } |
| 224 | |
| 225 | for strings.HasPrefix(path, "/") { |
| 226 | path = path[1:] |
| 227 | } |
| 228 | |
| 229 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 230 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 231 | |
| 232 | // FIXME: should empty txid be cleaned up? |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 233 | if branch = n.Branches[txid]; txid == "" || branch == nil { |
| 234 | branch = n.Branches[NONE] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if hash != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 238 | rev = branch.Revisions[hash] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 239 | } else { |
| 240 | rev = branch.Latest |
| 241 | } |
| 242 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 243 | return n.getPath(rev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 246 | func (n *node) getPath(rev Revision, path string, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 247 | if path == "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 248 | return n.getData(rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | partition := strings.SplitN(path, "/", 2) |
| 252 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 253 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 254 | if len(partition) < 2 { |
| 255 | path = "" |
| 256 | } else { |
| 257 | path = partition[1] |
| 258 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 259 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 260 | names := ChildrenFields(n.Type) |
| 261 | field := names[name] |
| 262 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 263 | if field.IsContainer { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 264 | if field.Key != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 265 | children := rev.GetChildren()[name] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 266 | if path != "" { |
| 267 | partition = strings.SplitN(path, "/", 2) |
| 268 | key := partition[0] |
| 269 | path = "" |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 270 | keyValue := field.KeyFromStr(key) |
| 271 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 272 | return nil |
| 273 | } else { |
| 274 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 275 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 276 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 277 | } else { |
| 278 | var response []interface{} |
| 279 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 280 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 281 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 282 | response = append(response, value) |
| 283 | } |
| 284 | return response |
| 285 | } |
| 286 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 287 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 294 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 295 | response = append(response, value) |
| 296 | } |
| 297 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 298 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 299 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 300 | childRev := rev.GetChildren()[name][0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 301 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 302 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 303 | } |
| 304 | return nil |
| 305 | } |
| 306 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 307 | func (n *node) getData(rev Revision, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 308 | msg := rev.Get(depth) |
| 309 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 310 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 311 | log.Debug("invoking proxy GET Callbacks") |
| 312 | msg = n.Proxy.InvokeCallbacks(GET, msg, false) |
| 313 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 314 | } |
| 315 | return msg |
| 316 | } |
| 317 | |
| 318 | // |
| 319 | // Update operation |
| 320 | // |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 321 | func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 322 | // 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 329 | if txid == "" { |
| 330 | branch = n.Branches[NONE] |
| 331 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 332 | branch = makeBranch(n) |
| 333 | } |
| 334 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 335 | log.Debugf("Branch data : %+v, Passed data: %+v", branch.Latest.GetData(), data) |
| 336 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 337 | if path == "" { |
| 338 | return n.doUpdate(branch, data, strict) |
| 339 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 340 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 341 | // 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 353 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 367 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 368 | // 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 373 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 374 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 383 | |
| 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 389 | fmt.Errorf("cannot change key field\n") |
| 390 | } |
| 391 | children[idx] = newChildRev |
| 392 | rev = rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 393 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 394 | 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 Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 404 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 405 | n.root.MakeLatest(branch, rev, nil) |
| 406 | return rev |
| 407 | } |
| 408 | return nil |
| 409 | } |
| 410 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 411 | func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 412 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 424 | |
| 425 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 426 | log.Debug("invoking proxy PRE_UPDATE Callbacks") |
| 427 | n.Proxy.InvokeCallbacks(PRE_UPDATE, data, false) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 428 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 429 | if !reflect.DeepEqual(branch.Latest.GetData(), data) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 430 | if strict { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 431 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 432 | fmt.Println("checking access violations") |
| 433 | } |
| 434 | rev := branch.Latest.UpdateData(data, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 435 | branch.Latest.Drop(branch.Txid, true) |
| 436 | n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_UPDATE, rev.GetData()}}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 437 | return rev |
| 438 | } else { |
| 439 | return branch.Latest |
| 440 | } |
| 441 | } |
| 442 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 443 | // |
| 444 | // Add operation |
| 445 | // |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 446 | func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 447 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 457 | if txid == "" { |
| 458 | branch = n.Branches[NONE] |
| 459 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 460 | branch = makeBranch(n) |
| 461 | } |
| 462 | |
| 463 | rev := branch.Latest |
| 464 | |
| 465 | partition := strings.SplitN(path, "/", 2) |
| 466 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 467 | |
| 468 | if len(partition) < 2 { |
| 469 | path = "" |
| 470 | } else { |
| 471 | path = partition[1] |
| 472 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 473 | |
| 474 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 475 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 476 | |
| 477 | if field.IsContainer { |
| 478 | if path == "" { |
| 479 | if field.Key != "" { |
| 480 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 481 | log.Debug("invoking proxy PRE_ADD Callbacks") |
| 482 | n.Proxy.InvokeCallbacks(PRE_ADD, data, false) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 483 | } |
| 484 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 485 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 490 | 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 Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 495 | childRev := n.MakeNode(data, txid).Latest(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 496 | children = append(children, childRev) |
| 497 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 498 | branch.Latest.Drop(txid, false) |
| 499 | n.root.MakeLatest(branch, rev, []ChangeTuple{{POST_ADD, rev.GetData()}}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 500 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 507 | if len(partition) < 2 { |
| 508 | path = "" |
| 509 | } else { |
| 510 | path = partition[1] |
| 511 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 512 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 513 | copy(children, rev.GetChildren()[name]) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 514 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 515 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 516 | newChildRev := childNode.Add(path, data, txid, makeBranch) |
| 517 | children[idx] = newChildRev |
| 518 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 519 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 520 | n.root.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 521 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 534 | func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 535 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 544 | if txid == "" { |
| 545 | branch = n.Branches[NONE] |
| 546 | } else if branch, ok = n.Branches[txid]; !ok { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 547 | branch = makeBranch(n) |
| 548 | } |
| 549 | |
| 550 | rev := branch.Latest |
| 551 | |
| 552 | partition := strings.SplitN(path, "/", 2) |
| 553 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 554 | if len(partition) < 2 { |
| 555 | path = "" |
| 556 | } else { |
| 557 | path = partition[1] |
| 558 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 559 | |
| 560 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 561 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 562 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 563 | |
| 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 570 | if len(partition) < 2 { |
| 571 | path = "" |
| 572 | } else { |
| 573 | path = partition[1] |
| 574 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 575 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 576 | if path != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 577 | for _, v := range rev.GetChildren()[name] { |
| 578 | newV := reflect.ValueOf(v).Interface().(Revision) |
| 579 | children = append(children, newV) |
| 580 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 581 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 582 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 583 | newChildRev := childNode.Remove(path, txid, makeBranch) |
| 584 | children[idx] = newChildRev |
| 585 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 586 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 587 | n.root.MakeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 588 | return rev |
| 589 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 590 | for _, v := range rev.GetChildren()[name] { |
| 591 | newV := reflect.ValueOf(v).Interface().(Revision) |
| 592 | children = append(children, newV) |
| 593 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 594 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 595 | if n.Proxy != nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 596 | data := childRev.GetData() |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 597 | n.Proxy.InvokeCallbacks(PRE_REMOVE, data, false) |
| 598 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 599 | } else { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 600 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 601 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 602 | childRev.Drop(txid, true) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 603 | children = append(children[:idx], children[idx+1:]...) |
| 604 | rev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 605 | branch.Latest.Drop(txid, false) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 606 | n.root.MakeLatest(branch, rev, postAnnouncement) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 607 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 619 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 620 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 621 | type MakeBranchFunction func(*node) *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 622 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 623 | func (n *node) MakeBranch(txid string) *Branch { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 624 | branchPoint := n.Branches[NONE].Latest |
| 625 | branch := NewBranch(n, txid, branchPoint, true) |
| 626 | n.Branches[txid] = branch |
| 627 | return branch |
| 628 | } |
| 629 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 630 | func (n *node) DeleteBranch(txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 631 | delete(n.Branches, txid) |
| 632 | } |
| 633 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 634 | func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 635 | f := func(rev Revision) Revision { |
| 636 | childBranch := rev.GetBranch() |
| 637 | |
| 638 | if childBranch.Txid == txid { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 639 | rev, _ = childBranch.Node.MergeBranch(txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return rev |
| 643 | } |
| 644 | return f |
| 645 | } |
| 646 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 647 | func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 648 | 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 Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 662 | // TODO: return proper error when one occurs |
| 663 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 667 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 668 | //func (n *node) diff(hash1, hash2, txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 669 | // 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 688 | func (n *node) hasChildren(data interface{}) bool { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 689 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 701 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 702 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 703 | func (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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 709 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 710 | func (n *node) getProxy(path string, root Root, fullPath string, exclusive bool) *Proxy { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 711 | for strings.HasPrefix(path, "/") { |
| 712 | path = path[1:] |
| 713 | } |
| 714 | if path == "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 715 | return n.makeProxy(root, path, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 716 | } |
| 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 731 | if len(partition) < 2 { |
| 732 | path = "" |
| 733 | } else { |
| 734 | path = partition[1] |
| 735 | } |
| 736 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 737 | children := rev.GetChildren()[name] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 738 | _, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 739 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 740 | |
| 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 746 | } |
| 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 757 | func (n *node) makeProxy(root Root, fullPath string, exclusive bool) *Proxy { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 758 | 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 Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 768 | func (n *node) makeEventBus() *EventBus { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 769 | if n.EventBus == nil { |
| 770 | n.EventBus = NewEventBus() |
| 771 | } |
| 772 | return n.EventBus |
| 773 | } |
| 774 | |
| 775 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Persistence Loading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 776 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 777 | func (n *node) LoadLatest(kvStore *Backend, hash string) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 778 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 779 | pr := &PersistedRevision{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 780 | rev := pr.Load(branch, kvStore, n.Type, hash) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 781 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 782 | n.Branches[NONE] = branch |
| 783 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame^] | 784 | |
| 785 | func (n *node) MakeTxBranch() string { |
| 786 | return n.root.MakeTxBranch() |
| 787 | } |
| 788 | func (n *node) FoldTxBranch(txid string) { |
| 789 | n.root.FoldTxBranch(txid) |
| 790 | } |
| 791 | func (n *node) DeleteTxBranch(txid string) { |
| 792 | n.root.DeleteTxBranch(txid) |
| 793 | } |