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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 19 | // TODO: proper error handling |
| 20 | // TODO: proper logging |
| 21 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 22 | import ( |
| 23 | "fmt" |
| 24 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 26 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | "runtime/debug" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 28 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 29 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 32 | // When a branch has no transaction id, everything gets stored in NONE |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 33 | const ( |
| 34 | NONE string = "none" |
| 35 | ) |
| 36 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 37 | // Node interface is an abstraction of the node data structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 38 | type Node interface { |
| 39 | MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) |
| 40 | |
| 41 | // CRUD functions |
| 42 | Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision |
| 43 | Get(path string, hash string, depth int, deep bool, txid string) interface{} |
| 44 | Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision |
| 45 | Remove(path string, txid string, makeBranch MakeBranchFunction) Revision |
| 46 | |
| 47 | MakeBranch(txid string) *Branch |
| 48 | DeleteBranch(txid string) |
| 49 | MergeBranch(txid string, dryRun bool) (Revision, error) |
| 50 | |
| 51 | MakeTxBranch() string |
| 52 | DeleteTxBranch(txid string) |
| 53 | FoldTxBranch(txid string) |
| 54 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 55 | CreateProxy(path string, exclusive bool) *Proxy |
| 56 | GetProxy() *Proxy |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type node struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | sync.RWMutex |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 61 | Root *root |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 62 | Type interface{} |
| 63 | Branches map[string]*Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | Tags map[string]Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | Proxy *Proxy |
| 66 | EventBus *EventBus |
| 67 | AutoPrune bool |
| 68 | } |
| 69 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 70 | // ChangeTuple holds details of modifications made to a revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 71 | type ChangeTuple struct { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 72 | Type CallbackType |
| 73 | PreviousData interface{} |
| 74 | LatestData interface{} |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 77 | // NewNode creates a new instance of the node data structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 78 | func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node { |
| 79 | n := &node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 80 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 81 | n.Root = root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 82 | n.Branches = make(map[string]*Branch) |
| 83 | n.Tags = make(map[string]Revision) |
| 84 | n.Proxy = nil |
| 85 | n.EventBus = nil |
| 86 | n.AutoPrune = autoPrune |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 87 | |
| 88 | if IsProtoMessage(initialData) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 89 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | dataCopy := proto.Clone(initialData.(proto.Message)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 91 | n.initialize(dataCopy, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 92 | } else if reflect.ValueOf(initialData).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 93 | // FIXME: this block does not reflect the original implementation |
| 94 | // it should be checking if the provided initial_data is already a type!??! |
| 95 | // it should be checked before IsProtoMessage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 96 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 97 | } else { |
| 98 | // not implemented error |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 99 | log.Errorf("cannot process initial data - %+v", initialData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 102 | return n |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 105 | // MakeNode creates a new node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 106 | func (n *node) MakeNode(data interface{}, txid string) *node { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 107 | return NewNode(n.Root, data, true, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 110 | // MakeRevision create a new revision of the node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 111 | func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 112 | return n.GetRoot().MakeRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 115 | // makeLatest will mark the revision of a node as being the latest |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 116 | func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 117 | branch.AddRevision(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 118 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 119 | if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() { |
| 120 | branch.SetLatest(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if changeAnnouncement != nil && branch.Txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 124 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 125 | for _, change := range changeAnnouncement { |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 126 | log.Debugw("invoking callback", |
| 127 | log.Fields{ |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 128 | "callbacks": n.GetProxy().getCallbacks(change.Type), |
| 129 | "type": change.Type, |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 130 | "previousData": change.PreviousData, |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 131 | "latestData": change.LatestData, |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 132 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 133 | n.GetRoot().AddCallback( |
| 134 | n.GetProxy().InvokeCallbacks, |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 135 | change.Type, |
| 136 | true, |
| 137 | change.PreviousData, |
| 138 | change.LatestData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 142 | for _, change := range changeAnnouncement { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 143 | log.Debugf("sending notification - changeType: %+v, previous:%+v, latest: %+v", |
| 144 | change.Type, |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 145 | change.PreviousData, |
| 146 | change.LatestData) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 147 | n.GetRoot().AddNotificationCallback( |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 148 | n.makeEventBus().Advertise, |
| 149 | change.Type, |
| 150 | revision.GetHash(), |
| 151 | change.PreviousData, |
| 152 | change.LatestData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 157 | // Latest returns the latest revision of node with or without the transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 158 | func (n *node) Latest(txid ...string) Revision { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 159 | var branch *Branch |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 160 | |
| 161 | if len(txid) > 0 && txid[0] != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 162 | if branch = n.GetBranch(txid[0]); branch != nil { |
| 163 | return branch.GetLatest() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 164 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 165 | } else if branch = n.GetBranch(NONE); branch != nil { |
| 166 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 171 | // initialize prepares the content of a node along with its possible ramifications |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 172 | func (n *node) initialize(data interface{}, txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 173 | n.Lock() |
| 174 | children := make(map[string][]Revision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 175 | for fieldName, field := range ChildrenFields(n.Type) { |
| 176 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 177 | |
| 178 | if fieldValue.IsValid() { |
| 179 | if field.IsContainer { |
| 180 | if field.Key != "" { |
| 181 | var keysSeen []string |
| 182 | |
| 183 | for i := 0; i < fieldValue.Len(); i++ { |
| 184 | v := fieldValue.Index(i) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 185 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 186 | if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil { |
| 187 | children[fieldName] = append(children[fieldName], rev) |
| 188 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 189 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | _, key := GetAttributeValue(v.Interface(), field.Key, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 191 | for _, k := range keysSeen { |
| 192 | if k == key.String() { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 193 | log.Errorf("duplicate key - %s", k) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 194 | } |
| 195 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 196 | keysSeen = append(keysSeen, key.String()) |
| 197 | } |
| 198 | |
| 199 | } else { |
| 200 | for i := 0; i < fieldValue.Len(); i++ { |
| 201 | v := fieldValue.Index(i) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil { |
| 203 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 204 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 208 | if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil { |
| 209 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 210 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 211 | } |
| 212 | } else { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 213 | log.Errorf("field is invalid - %+v", fieldValue) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 216 | n.Unlock() |
| 217 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 218 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 219 | rev := n.MakeRevision(branch, data, children) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 220 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 221 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 222 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 223 | n.SetBranch(NONE, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 224 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 225 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 226 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 229 | // findRevByKey retrieves a specific revision from a node tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 230 | func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 231 | n.Lock() |
| 232 | defer n.Unlock() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 233 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 234 | for i, rev := range revs { |
| 235 | dataValue := reflect.ValueOf(rev.GetData()) |
| 236 | dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0) |
| 237 | |
| 238 | fieldValue := dataValue.Elem().FieldByName(dataStruct.Name) |
| 239 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 240 | a := fmt.Sprintf("%s", fieldValue.Interface()) |
| 241 | b := fmt.Sprintf("%s", value) |
| 242 | if a == b { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 243 | return i, revs[i] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 247 | return -1, nil |
| 248 | } |
| 249 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 250 | // Get retrieves the data from a node tree that resides at the specified path |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 251 | func (n *node) List(path string, hash string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 252 | log.Debugw("node-list-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 253 | if deep { |
| 254 | depth = -1 |
| 255 | } |
| 256 | |
| 257 | for strings.HasPrefix(path, "/") { |
| 258 | path = path[1:] |
| 259 | } |
| 260 | |
| 261 | var branch *Branch |
| 262 | var rev Revision |
| 263 | |
| 264 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 265 | branch = n.GetBranch(NONE) |
| 266 | } |
| 267 | |
| 268 | if hash != "" { |
| 269 | rev = branch.GetRevision(hash) |
| 270 | } else { |
| 271 | rev = branch.GetLatest() |
| 272 | } |
| 273 | |
| 274 | var result interface{} |
| 275 | var prList []interface{} |
| 276 | if pr := rev.LoadFromPersistence(path, txid); pr != nil { |
| 277 | for _, revEntry := range pr { |
| 278 | prList = append(prList, revEntry.GetData()) |
| 279 | } |
| 280 | result = prList |
| 281 | } |
| 282 | |
| 283 | return result |
| 284 | } |
| 285 | |
| 286 | // Get retrieves the data from a node tree that resides at the specified path |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 287 | func (n *node) Get(path string, hash string, depth int, deep bool, txid string) interface{} { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 288 | log.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 289 | if deep { |
| 290 | depth = -1 |
| 291 | } |
| 292 | |
| 293 | for strings.HasPrefix(path, "/") { |
| 294 | path = path[1:] |
| 295 | } |
| 296 | |
| 297 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 298 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 299 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 300 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 301 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if hash != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 305 | rev = branch.GetRevision(hash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 306 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 307 | rev = branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 310 | var result interface{} |
| 311 | if result = n.getPath(rev.GetBranch().GetLatest(), path, depth); |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 312 | (result == nil || reflect.ValueOf(result).IsValid() && reflect.ValueOf(result).IsNil()) && n.Root.KvStore != nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 313 | // We got nothing from memory, try to pull it from persistence |
| 314 | var prList []interface{} |
| 315 | if pr := rev.LoadFromPersistence(path, txid); pr != nil { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 316 | // Did we receive a single or multiple revisions? |
| 317 | if len(pr) > 1 { |
| 318 | for _, revEntry := range pr { |
| 319 | prList = append(prList, revEntry.GetData()) |
| 320 | } |
| 321 | result = prList |
| 322 | } else { |
| 323 | result = pr[0].GetData() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 324 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | return result |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 331 | // getPath traverses the specified path and retrieves the data associated to it |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 332 | func (n *node) getPath(rev Revision, path string, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 333 | if path == "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 334 | return n.getData(rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | partition := strings.SplitN(path, "/", 2) |
| 338 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 339 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 340 | if len(partition) < 2 { |
| 341 | path = "" |
| 342 | } else { |
| 343 | path = partition[1] |
| 344 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 345 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 346 | names := ChildrenFields(n.Type) |
| 347 | field := names[name] |
| 348 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 349 | if field != nil && field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 350 | children := make([]Revision, len(rev.GetChildren(name))) |
| 351 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 352 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 353 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 354 | if path != "" { |
| 355 | partition = strings.SplitN(path, "/", 2) |
| 356 | key := partition[0] |
| 357 | path = "" |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 358 | keyValue := field.KeyFromStr(key) |
| 359 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 360 | return nil |
| 361 | } else { |
| 362 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 363 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 364 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 365 | } else { |
| 366 | var response []interface{} |
| 367 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 368 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 369 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 370 | response = append(response, value) |
| 371 | } |
| 372 | return response |
| 373 | } |
| 374 | } else { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 375 | var response []interface{} |
| 376 | if path != "" { |
| 377 | // TODO: raise error |
| 378 | return response |
| 379 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 380 | for _, childRev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 381 | childNode := childRev.GetNode() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 382 | value := childNode.getData(childRev, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 383 | response = append(response, value) |
| 384 | } |
| 385 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 386 | } |
| 387 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 388 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 389 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 390 | childNode := childRev.GetNode() |
| 391 | return childNode.getPath(childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 394 | // getData retrieves the data from a node revision |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 395 | func (n *node) getData(rev Revision, depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 396 | msg := rev.GetBranch().GetLatest().Get(depth) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 397 | var modifiedMsg interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 398 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 399 | if n.GetProxy() != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 400 | log.Debugw("invoking-get-callbacks", log.Fields{"data": msg}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 401 | if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 402 | msg = modifiedMsg |
| 403 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 404 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 405 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 406 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 407 | return msg |
| 408 | } |
| 409 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 410 | // Update changes the content of a node at the specified path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 411 | func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 412 | log.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 413 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 414 | for strings.HasPrefix(path, "/") { |
| 415 | path = path[1:] |
| 416 | } |
| 417 | |
| 418 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 419 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 420 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 421 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 422 | branch = makeBranch(n) |
| 423 | } |
| 424 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 425 | if branch.GetLatest() != nil { |
| 426 | log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data) |
| 427 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 428 | if path == "" { |
| 429 | return n.doUpdate(branch, data, strict) |
| 430 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 431 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 432 | rev := branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 433 | |
| 434 | partition := strings.SplitN(path, "/", 2) |
| 435 | name := partition[0] |
| 436 | |
| 437 | if len(partition) < 2 { |
| 438 | path = "" |
| 439 | } else { |
| 440 | path = partition[1] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 443 | field := ChildrenFields(n.Type)[name] |
| 444 | var children []Revision |
| 445 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 446 | if field == nil { |
| 447 | return n.doUpdate(branch, data, strict) |
| 448 | } |
| 449 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 450 | if field.IsContainer { |
| 451 | if path == "" { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 452 | log.Errorf("cannot update a list") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 453 | } else if field.Key != "" { |
| 454 | partition := strings.SplitN(path, "/", 2) |
| 455 | key := partition[0] |
| 456 | if len(partition) < 2 { |
| 457 | path = "" |
| 458 | } else { |
| 459 | path = partition[1] |
| 460 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 461 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 462 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 463 | children = make([]Revision, len(rev.GetChildren(name))) |
| 464 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 465 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 466 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 467 | childNode := childRev.GetNode() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 468 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 469 | // Save proxy in child node to ensure callbacks are called later on |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 470 | // only assign in cases of non sub-folder proxies, i.e. "/" |
| 471 | if childNode.Proxy == nil && n.Proxy != nil && n.Proxy.getFullPath() == "" { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 472 | childNode.Proxy = n.Proxy |
| 473 | } |
| 474 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 475 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 476 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 477 | if newChildRev.GetHash() == childRev.GetHash() { |
| 478 | if newChildRev != childRev { |
| 479 | log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev) |
| 480 | newChildRev.ClearHash() |
| 481 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 482 | return branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 483 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 484 | |
| 485 | _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 486 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 487 | _newKeyType := fmt.Sprintf("%s", newKey) |
| 488 | _keyValueType := fmt.Sprintf("%s", keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 489 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 490 | if _newKeyType != _keyValueType { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 491 | log.Errorf("cannot change key field") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 492 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 493 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 494 | // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters) |
| 495 | newChildRev.SetHash(name + "/" + _keyValueType) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 496 | children[idx] = newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 497 | |
| 498 | updatedRev := rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 499 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 500 | branch.GetLatest().Drop(txid, false) |
| 501 | n.makeLatest(branch, updatedRev, nil) |
| 502 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 503 | return newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 504 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 505 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 506 | log.Errorf("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 507 | } |
| 508 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 509 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 510 | childNode := childRev.GetNode() |
| 511 | newChildRev := childNode.Update(path, data, strict, txid, makeBranch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 512 | updatedRev := rev.UpdateChildren(name, []Revision{newChildRev}, branch) |
| 513 | rev.Drop(txid, false) |
| 514 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 515 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 516 | return newChildRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 517 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 518 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 519 | return nil |
| 520 | } |
| 521 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 522 | func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 523 | log.Debugf("Comparing types - expected: %+v, actual: %+v &&&&&& %s", reflect.ValueOf(n.Type).Type(), |
| 524 | reflect.TypeOf(data), |
| 525 | string(debug.Stack())) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 526 | |
| 527 | if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() { |
| 528 | // TODO raise error |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 529 | log.Errorf("data does not match type: %+v", n.Type) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 530 | return nil |
| 531 | } |
| 532 | |
| 533 | // TODO: validate that this actually works |
| 534 | //if n.hasChildren(data) { |
| 535 | // return nil |
| 536 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 537 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 538 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 539 | log.Debug("invoking proxy PRE_UPDATE Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 540 | n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 541 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 542 | |
| 543 | if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 544 | if strict { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 545 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 546 | log.Debugf("checking access violations") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 547 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 548 | |
| 549 | rev := branch.GetLatest().UpdateData(data, branch) |
| 550 | changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}} |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 551 | |
| 552 | // FIXME VOL-1293: the following statement corrupts the kv when using a subproxy (commenting for now) |
| 553 | // FIXME VOL-1293 cont'd: need to figure out the required conditions otherwise we are not cleaning up entries |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 554 | //branch.GetLatest().Drop(branch.Txid, true) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 555 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 556 | n.makeLatest(branch, rev, changes) |
| 557 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 558 | return rev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 559 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 560 | |
| 561 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 564 | // Add inserts a new node at the specified path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 565 | func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 566 | log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 567 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 568 | for strings.HasPrefix(path, "/") { |
| 569 | path = path[1:] |
| 570 | } |
| 571 | if path == "" { |
| 572 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 573 | log.Errorf("cannot add for non-container mode") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 574 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 578 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 579 | branch = n.GetBranch(NONE) |
| 580 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 581 | branch = makeBranch(n) |
| 582 | } |
| 583 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 584 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 585 | |
| 586 | partition := strings.SplitN(path, "/", 2) |
| 587 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 588 | |
| 589 | if len(partition) < 2 { |
| 590 | path = "" |
| 591 | } else { |
| 592 | path = partition[1] |
| 593 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 594 | |
| 595 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 596 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 597 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 598 | |
| 599 | if field.IsContainer { |
| 600 | if path == "" { |
| 601 | if field.Key != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 602 | if n.GetProxy() != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 603 | log.Debug("invoking proxy PRE_ADD Callbacks") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 604 | n.GetProxy().InvokeCallbacks(PRE_ADD, false, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 607 | children = make([]Revision, len(rev.GetChildren(name))) |
| 608 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 609 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 610 | _, key := GetAttributeValue(data, field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 611 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 612 | if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 613 | // TODO raise error |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 614 | log.Errorf("duplicate key found: %s", key.String()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 615 | return exists |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 616 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 617 | childRev := n.MakeNode(data, "").Latest() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 618 | |
| 619 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
| 620 | childRev.SetHash(name + "/" + key.String()) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 621 | |
| 622 | // Create watch for <component>/<key> |
| 623 | childRev.SetupWatch(childRev.GetHash()) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 624 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 625 | children = append(children, childRev) |
| 626 | rev = rev.UpdateChildren(name, children, branch) |
| 627 | changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 628 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 629 | rev.Drop(txid, false) |
| 630 | n.makeLatest(branch, rev, changes) |
| 631 | |
| 632 | return childRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 633 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 634 | log.Errorf("cannot add to non-keyed container") |
| 635 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 636 | } else if field.Key != "" { |
| 637 | partition := strings.SplitN(path, "/", 2) |
| 638 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 639 | if len(partition) < 2 { |
| 640 | path = "" |
| 641 | } else { |
| 642 | path = partition[1] |
| 643 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 644 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 645 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 646 | children = make([]Revision, len(rev.GetChildren(name))) |
| 647 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 648 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 649 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 650 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 651 | childNode := childRev.GetNode() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 652 | newChildRev := childNode.Add(path, data, txid, makeBranch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 653 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 654 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
| 655 | childRev.SetHash(name + "/" + keyValue.(string)) |
| 656 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 657 | children[idx] = newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 658 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 659 | rev = rev.UpdateChildren(name, children, branch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 660 | rev.Drop(txid, false) |
| 661 | n.makeLatest(branch, rev.GetBranch().GetLatest(), nil) |
| 662 | |
| 663 | return newChildRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 664 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 665 | log.Errorf("cannot add to non-keyed container") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 666 | } |
| 667 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 668 | log.Errorf("cannot add to non-container field") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 669 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 670 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 671 | return nil |
| 672 | } |
| 673 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 674 | // Remove eliminates a node at the specified path |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 675 | func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame^] | 676 | log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 677 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 678 | for strings.HasPrefix(path, "/") { |
| 679 | path = path[1:] |
| 680 | } |
| 681 | if path == "" { |
| 682 | // TODO raise error |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 683 | log.Errorf("cannot remove for non-container mode") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 684 | } |
| 685 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 686 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 687 | branch = n.GetBranch(NONE) |
| 688 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 689 | branch = makeBranch(n) |
| 690 | } |
| 691 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 692 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 693 | |
| 694 | partition := strings.SplitN(path, "/", 2) |
| 695 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 696 | if len(partition) < 2 { |
| 697 | path = "" |
| 698 | } else { |
| 699 | path = partition[1] |
| 700 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 701 | |
| 702 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 703 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 704 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 705 | |
| 706 | if field.IsContainer { |
| 707 | if path == "" { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 708 | log.Errorf("cannot remove without a key") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 709 | } else if field.Key != "" { |
| 710 | partition := strings.SplitN(path, "/", 2) |
| 711 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 712 | if len(partition) < 2 { |
| 713 | path = "" |
| 714 | } else { |
| 715 | path = partition[1] |
| 716 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 717 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 718 | children = make([]Revision, len(rev.GetChildren(name))) |
| 719 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 720 | if path != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 721 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 722 | childNode := childRev.GetNode() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 723 | if childNode.Proxy == nil { |
| 724 | childNode.Proxy = n.Proxy |
| 725 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 726 | newChildRev := childNode.Remove(path, txid, makeBranch) |
| 727 | children[idx] = newChildRev |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 728 | rev.SetChildren(name, children) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 729 | branch.GetLatest().Drop(txid, false) |
| 730 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 731 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 732 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 733 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
| 734 | if n.GetProxy() != nil { |
| 735 | data := childRev.GetData() |
| 736 | n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data) |
| 737 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil}) |
| 738 | } else { |
| 739 | postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil}) |
| 740 | } |
| 741 | childRev.Drop(txid, true) |
| 742 | children = append(children[:idx], children[idx+1:]...) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 743 | rev.SetChildren(name, children) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 744 | branch.GetLatest().Drop(txid, false) |
| 745 | n.makeLatest(branch, rev, postAnnouncement) |
| 746 | return rev |
| 747 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 748 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 749 | log.Errorf("cannot add to non-keyed container") |
| 750 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 751 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 752 | log.Errorf("cannot add to non-container field") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | return nil |
| 756 | } |
| 757 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 758 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 759 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 760 | // MakeBranchFunction is a type for function references intented to create a branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 761 | type MakeBranchFunction func(*node) *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 762 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 763 | // MakeBranch creates a new branch for the provided transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 764 | func (n *node) MakeBranch(txid string) *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 765 | branchPoint := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 766 | branch := NewBranch(n, txid, branchPoint, true) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 767 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 768 | return branch |
| 769 | } |
| 770 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 771 | // DeleteBranch removes a branch with the specified id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 772 | func (n *node) DeleteBranch(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 773 | n.Lock() |
| 774 | defer n.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 775 | delete(n.Branches, txid) |
| 776 | } |
| 777 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 778 | func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 779 | f := func(rev Revision) Revision { |
| 780 | childBranch := rev.GetBranch() |
| 781 | |
| 782 | if childBranch.Txid == txid { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 783 | rev, _ = childBranch.Node.MergeBranch(txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | return rev |
| 787 | } |
| 788 | return f |
| 789 | } |
| 790 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 791 | // MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 792 | func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 793 | srcBranch := n.GetBranch(txid) |
| 794 | dstBranch := n.GetBranch(NONE) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 795 | |
| 796 | forkRev := srcBranch.Origin |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 797 | srcRev := srcBranch.GetLatest() |
| 798 | dstRev := dstBranch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 799 | |
| 800 | rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun) |
| 801 | |
| 802 | if !dryRun { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 803 | if rev != nil { |
| 804 | n.makeLatest(dstBranch, rev, changes) |
| 805 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 806 | n.DeleteBranch(txid) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 809 | // TODO: return proper error when one occurs |
| 810 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 814 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 815 | //func (n *node) diff(hash1, hash2, txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 816 | // branch := n.Branches[txid] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 817 | // rev1 := branch.GetHash(hash1) |
| 818 | // rev2 := branch.GetHash(hash2) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 819 | // |
| 820 | // if rev1.GetHash() == rev2.GetHash() { |
| 821 | // // empty patch |
| 822 | // } else { |
| 823 | // // translate data to json and generate patch |
| 824 | // patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData()) |
| 825 | // patch. |
| 826 | // } |
| 827 | //} |
| 828 | |
| 829 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 830 | |
| 831 | // TODO: is tag mgmt used in the python implementation? Need to validate |
| 832 | |
| 833 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 834 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 835 | func (n *node) hasChildren(data interface{}) bool { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 836 | for fieldName, field := range ChildrenFields(n.Type) { |
| 837 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
| 838 | |
| 839 | if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() { |
| 840 | log.Error("cannot update external children") |
| 841 | return true |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | return false |
| 846 | } |
| 847 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 848 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 849 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 850 | // CreateProxy returns a reference to a sub-tree of the data model |
| 851 | func (n *node) CreateProxy(path string, exclusive bool) *Proxy { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 852 | return n.createProxy(path, path, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 853 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 854 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 855 | func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 856 | for strings.HasPrefix(path, "/") { |
| 857 | path = path[1:] |
| 858 | } |
| 859 | if path == "" { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 860 | return n.makeProxy(path, fullPath, parentNode, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 863 | rev := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 864 | partition := strings.SplitN(path, "/", 2) |
| 865 | name := partition[0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 866 | if len(partition) < 2 { |
| 867 | path = "" |
| 868 | } else { |
| 869 | path = partition[1] |
| 870 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 871 | |
| 872 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 873 | if field.IsContainer { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 874 | if path == "" { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 875 | //log.Error("cannot proxy a container field") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 876 | newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "") |
| 877 | return newNode.makeProxy(path, fullPath, parentNode, exclusive) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 878 | } else if field.Key != "" { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 879 | partition := strings.SplitN(path, "/", 2) |
| 880 | key := partition[0] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 881 | if len(partition) < 2 { |
| 882 | path = "" |
| 883 | } else { |
| 884 | path = partition[1] |
| 885 | } |
| 886 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 887 | var children []Revision |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 888 | children = make([]Revision, len(rev.GetChildren(name))) |
| 889 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 890 | if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 891 | childNode := childRev.GetNode() |
| 892 | return childNode.createProxy(path, fullPath, n, exclusive) |
| 893 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 894 | } else { |
| 895 | log.Error("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 896 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 897 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 898 | childRev := rev.GetChildren(name)[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 899 | childNode := childRev.GetNode() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 900 | return childNode.createProxy(path, fullPath, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 901 | } |
| 902 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 903 | log.Warnf("Cannot create proxy - latest rev:%s, all revs:%+v", rev.GetHash(), n.GetBranch(NONE).Revisions) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 904 | return nil |
| 905 | } |
| 906 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 907 | func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 908 | n.Lock() |
| 909 | defer n.Unlock() |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 910 | r := &root{ |
| 911 | node: n, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 912 | Callbacks: n.Root.GetCallbacks(), |
| 913 | NotificationCallbacks: n.Root.GetNotificationCallbacks(), |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 914 | DirtyNodes: n.Root.DirtyNodes, |
| 915 | KvStore: n.Root.KvStore, |
| 916 | Loading: n.Root.Loading, |
| 917 | RevisionClass: n.Root.RevisionClass, |
| 918 | } |
| 919 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 920 | if n.Proxy == nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 921 | n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 922 | } else { |
| 923 | if n.Proxy.Exclusive { |
| 924 | log.Error("node is already owned exclusively") |
| 925 | } |
| 926 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 927 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 928 | return n.Proxy |
| 929 | } |
| 930 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 931 | func (n *node) makeEventBus() *EventBus { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 932 | n.Lock() |
| 933 | defer n.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 934 | if n.EventBus == nil { |
| 935 | n.EventBus = NewEventBus() |
| 936 | } |
| 937 | return n.EventBus |
| 938 | } |
| 939 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 940 | func (n *node) SetProxy(proxy *Proxy) { |
| 941 | n.Lock() |
| 942 | defer n.Unlock() |
| 943 | n.Proxy = proxy |
| 944 | } |
| 945 | |
| 946 | func (n *node) GetProxy() *Proxy { |
| 947 | n.Lock() |
| 948 | defer n.Unlock() |
| 949 | return n.Proxy |
| 950 | } |
| 951 | |
| 952 | func (n *node) GetBranch(key string) *Branch { |
| 953 | n.Lock() |
| 954 | defer n.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 955 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 956 | if n.Branches != nil { |
| 957 | if branch, exists := n.Branches[key]; exists { |
| 958 | return branch |
| 959 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 960 | } |
| 961 | return nil |
| 962 | } |
| 963 | |
| 964 | func (n *node) SetBranch(key string, branch *Branch) { |
| 965 | n.Lock() |
| 966 | defer n.Unlock() |
| 967 | n.Branches[key] = branch |
| 968 | } |
| 969 | |
| 970 | func (n *node) GetRoot() *root { |
| 971 | n.Lock() |
| 972 | defer n.Unlock() |
| 973 | return n.Root |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 974 | } |