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