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 ( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 23 | "context" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 24 | "fmt" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "reflect" |
| 26 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | "sync" |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 28 | "time" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 29 | |
| 30 | "github.com/golang/protobuf/proto" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 34 | // When a branch has no transaction id, everything gets stored in NONE |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 35 | const ( |
| 36 | NONE string = "none" |
| 37 | ) |
| 38 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | // Node interface is an abstraction of the node data structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 40 | type Node interface { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 41 | MakeLatest(ctx context.Context, branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 42 | |
| 43 | // CRUD functions |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 44 | Add(ctx context.Context, path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 45 | Get(ctx context.Context, path string, hash string, depth int, deep bool, txid string) (interface{}, error) |
| 46 | List(ctx context.Context, path string, hash string, depth int, deep bool, txid string) (interface{}, error) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 47 | Update(ctx context.Context, path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision |
| 48 | Remove(ctx context.Context, path string, txid string, makeBranch MakeBranchFunction) Revision |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 49 | CreateProxy(ctx context.Context, path string, exclusive bool) (*Proxy, error) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 50 | |
| 51 | GetProxy() *Proxy |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 52 | |
| 53 | MakeBranch(txid string) *Branch |
| 54 | DeleteBranch(txid string) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 55 | MergeBranch(ctx context.Context, txid string, dryRun bool) (Revision, error) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 56 | |
| 57 | MakeTxBranch() string |
| 58 | DeleteTxBranch(txid string) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 59 | FoldTxBranch(ctx context.Context, txid string) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type node struct { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 63 | mutex sync.RWMutex |
| 64 | Root *root |
| 65 | Type interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 66 | Branches map[string]*Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 67 | Tags map[string]Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | Proxy *Proxy |
| 69 | EventBus *EventBus |
| 70 | AutoPrune bool |
| 71 | } |
| 72 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 73 | // ChangeTuple holds details of modifications made to a revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 74 | type ChangeTuple struct { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 75 | Type CallbackType |
| 76 | PreviousData interface{} |
| 77 | LatestData interface{} |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 78 | } |
| 79 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 80 | // newNode creates a new instance of the node data structure |
| 81 | func newNode(root *root, initialData interface{}, autoPrune bool, txid string) *node { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 82 | n := &node{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 83 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 84 | n.Root = root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 85 | n.Branches = make(map[string]*Branch) |
| 86 | n.Tags = make(map[string]Revision) |
| 87 | n.Proxy = nil |
| 88 | n.EventBus = nil |
| 89 | n.AutoPrune = autoPrune |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | |
| 91 | if IsProtoMessage(initialData) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 92 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 93 | dataCopy := proto.Clone(initialData.(proto.Message)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 94 | n.initialize(dataCopy, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 95 | } else if reflect.ValueOf(initialData).IsValid() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 96 | // FIXME: this block does not reflect the original implementation |
| 97 | // it should be checking if the provided initial_data is already a type!??! |
| 98 | // it should be checked before IsProtoMessage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 99 | n.Type = reflect.ValueOf(initialData).Interface() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 100 | } else { |
| 101 | // not implemented error |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 102 | logger.Errorf("cannot process initial data - %+v", initialData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 105 | return n |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 108 | // MakeNode creates a new node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 109 | func (n *node) MakeNode(data interface{}, txid string) *node { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 110 | return newNode(n.Root, data, true, txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 113 | // MakeRevision create a new revision of the node in the tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 114 | 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] | 115 | return n.GetRoot().MakeRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 118 | // makeLatest will mark the revision of a node as being the latest |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 119 | func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 120 | // Keep a reference to the current revision |
| 121 | var previous string |
| 122 | if branch.GetLatest() != nil { |
| 123 | previous = branch.GetLatest().GetHash() |
| 124 | } |
| 125 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 126 | branch.AddRevision(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 128 | // If anything is new, then set the revision as the latest |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 129 | if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 130 | if revision.GetName() != "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 131 | logger.Debugw("saving-latest-data", log.Fields{"hash": revision.GetHash(), "data": revision.GetData()}) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 132 | // Tag a timestamp to that revision |
| 133 | revision.SetLastUpdate() |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 134 | getRevCache().Set(revision.GetName(), revision) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 135 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 136 | branch.SetLatest(revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 139 | // Delete the previous revision if anything has changed |
| 140 | if previous != "" && previous != branch.GetLatest().GetHash() { |
| 141 | branch.DeleteRevision(previous) |
| 142 | } |
| 143 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 144 | if changeAnnouncement != nil && branch.Txid == "" { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 145 | if n.Proxy != nil { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 146 | for _, change := range changeAnnouncement { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 147 | logger.Debugw("adding-callback", |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 148 | log.Fields{ |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 149 | "callbacks": n.GetProxy().getCallbacks(change.Type), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 150 | "type": change.Type, |
| 151 | "previousData": change.PreviousData, |
| 152 | "latestData": change.LatestData, |
| 153 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 154 | n.Root.AddCallback( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 155 | n.GetProxy().InvokeCallbacks, |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 156 | change.Type, |
| 157 | true, |
| 158 | change.PreviousData, |
| 159 | change.LatestData) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 160 | } |
| 161 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 165 | // 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] | 166 | func (n *node) Latest(txid ...string) Revision { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 167 | var branch *Branch |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 168 | |
| 169 | if len(txid) > 0 && txid[0] != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 170 | if branch = n.GetBranch(txid[0]); branch != nil { |
| 171 | return branch.GetLatest() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 172 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 173 | } else if branch = n.GetBranch(NONE); branch != nil { |
| 174 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 175 | } |
| 176 | return nil |
| 177 | } |
| 178 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 179 | // initialize prepares the content of a node along with its possible ramifications |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 180 | func (n *node) initialize(data interface{}, txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 181 | children := make(map[string][]Revision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 182 | for fieldName, field := range ChildrenFields(n.Type) { |
| 183 | _, fieldValue := GetAttributeValue(data, fieldName, 0) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 184 | |
| 185 | if fieldValue.IsValid() { |
| 186 | if field.IsContainer { |
| 187 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 188 | for i := 0; i < fieldValue.Len(); i++ { |
| 189 | v := fieldValue.Index(i) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 190 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil { |
| 192 | children[fieldName] = append(children[fieldName], rev) |
| 193 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 194 | |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 195 | // TODO: The following logic was ported from v1.0. Need to verify if it is required |
| 196 | //var keysSeen []string |
| 197 | //_, key := GetAttributeValue(v.Interface(), field.Key, 0) |
| 198 | //for _, k := range keysSeen { |
| 199 | // if k == key.String() { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 200 | // //logger.Errorf("duplicate key - %s", k) |
Stephane Barbarie | 1e28f3e | 2019-02-08 15:45:20 -0500 | [diff] [blame] | 201 | // } |
| 202 | //} |
| 203 | //keysSeen = append(keysSeen, key.String()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } else { |
| 207 | for i := 0; i < fieldValue.Len(); i++ { |
| 208 | v := fieldValue.Index(i) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 209 | if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil { |
| 210 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 211 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 215 | if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil { |
| 216 | children[fieldName] = append(children[fieldName], newNodeRev) |
| 217 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 218 | } |
| 219 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 220 | logger.Errorf("field is invalid - %+v", fieldValue) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 221 | } |
| 222 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 223 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 224 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 225 | rev := n.MakeRevision(branch, data, children) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 226 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 227 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 228 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 229 | n.SetBranch(NONE, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 230 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 231 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 232 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 235 | // findRevByKey retrieves a specific revision from a node tree |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 236 | func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) { |
| 237 | for i, rev := range revs { |
| 238 | dataValue := reflect.ValueOf(rev.GetData()) |
| 239 | dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0) |
| 240 | |
| 241 | fieldValue := dataValue.Elem().FieldByName(dataStruct.Name) |
| 242 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 243 | a := fmt.Sprintf("%s", fieldValue.Interface()) |
| 244 | b := fmt.Sprintf("%s", value) |
| 245 | if a == b { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 246 | return i, revs[i] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 250 | return -1, nil |
| 251 | } |
| 252 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 253 | // Get retrieves the data from a node tree that resides at the specified path |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 254 | func (n *node) List(ctx context.Context, path string, hash string, depth int, deep bool, txid string) (interface{}, error) { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 255 | n.mutex.Lock() |
| 256 | defer n.mutex.Unlock() |
| 257 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 258 | logger.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] | 259 | |
| 260 | for strings.HasPrefix(path, "/") { |
| 261 | path = path[1:] |
| 262 | } |
| 263 | |
| 264 | var branch *Branch |
| 265 | var rev Revision |
| 266 | |
| 267 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 268 | branch = n.GetBranch(NONE) |
| 269 | } |
| 270 | |
| 271 | if hash != "" { |
| 272 | rev = branch.GetRevision(hash) |
| 273 | } else { |
| 274 | rev = branch.GetLatest() |
| 275 | } |
| 276 | |
| 277 | var result interface{} |
| 278 | var prList []interface{} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 279 | |
| 280 | pr, err := rev.LoadFromPersistence(ctx, path, txid, nil) |
| 281 | if err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 282 | logger.Errorf("failed-to-load-from-persistence") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 283 | return nil, err |
| 284 | } |
| 285 | if pr != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 286 | for _, revEntry := range pr { |
| 287 | prList = append(prList, revEntry.GetData()) |
| 288 | } |
| 289 | result = prList |
| 290 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 291 | return result, nil |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Get retrieves the data from a node tree that resides at the specified path |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 295 | func (n *node) Get(ctx context.Context, path string, hash string, depth int, reconcile bool, txid string) (interface{}, error) { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 296 | n.mutex.Lock() |
| 297 | defer n.mutex.Unlock() |
| 298 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 299 | logger.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 300 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 301 | for strings.HasPrefix(path, "/") { |
| 302 | path = path[1:] |
| 303 | } |
| 304 | |
| 305 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 306 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 307 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 308 | if branch = n.GetBranch(txid); txid == "" || branch == nil { |
| 309 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if hash != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 313 | rev = branch.GetRevision(hash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 314 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 315 | rev = branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 318 | var result interface{} |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 319 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 320 | // If there is no request to reconcile, try to get it from memory |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 321 | if !reconcile { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 322 | // Try to find an entry matching the path value from one of these sources |
| 323 | // 1. Start with the cache which stores revisions by watch names |
| 324 | // 2. Then look in the revision tree, especially if it's a sub-path such as /devices/1234/flows |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 325 | // 3. Move on to the KV store if that path cannot be found or if the entry has expired |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 326 | if entry, exists := getRevCache().Get(path); exists && entry.(Revision) != nil { |
| 327 | entryAge := time.Since(entry.(Revision).GetLastUpdate()).Nanoseconds() / int64(time.Millisecond) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 328 | if entryAge < DataRefreshPeriod { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 329 | logger.Debugw("using-cache-entry", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 330 | "path": path, |
| 331 | "hash": hash, |
| 332 | "age": entryAge, |
| 333 | }) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 334 | return proto.Clone(entry.(Revision).GetData().(proto.Message)), nil |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 335 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 336 | logger.Debugw("cache-entry-expired", log.Fields{"path": path, "hash": hash, "age": entryAge}) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 337 | } else if result = n.getPath(ctx, rev.GetBranch().GetLatest(), path, depth); result != nil && reflect.ValueOf(result).IsValid() && !reflect.ValueOf(result).IsNil() { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 338 | logger.Debugw("using-rev-tree-entry", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 339 | return result, nil |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 340 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 341 | logger.Debugw("not-using-cache-entry", log.Fields{ |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 342 | "path": path, |
| 343 | "hash": hash, "depth": depth, |
| 344 | "reconcile": reconcile, |
| 345 | "txid": txid, |
| 346 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 347 | } |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 348 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 349 | logger.Debugw("reconcile-requested", log.Fields{ |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 350 | "path": path, |
| 351 | "hash": hash, |
| 352 | "reconcile": reconcile, |
| 353 | }) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 356 | // If we got to this point, we are either trying to reconcile with the db |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 357 | // or we simply failed at getting information from memory |
| 358 | if n.Root.KvStore != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 359 | if pr, err := rev.LoadFromPersistence(ctx, path, txid, nil); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 360 | logger.Errorf("failed-to-load-from-persistence") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 361 | return nil, err |
| 362 | } else if len(pr) > 0 { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 363 | // Did we receive a single or multiple revisions? |
| 364 | if len(pr) > 1 { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 365 | var revs []interface{} |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 366 | for _, revEntry := range pr { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 367 | revs = append(revs, revEntry.GetData()) |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 368 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 369 | result = revs |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 370 | } else { |
| 371 | result = pr[0].GetData() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 372 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 373 | } |
| 374 | } |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 375 | return result, nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 378 | //getPath traverses the specified path and retrieves the data associated to it |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 379 | func (n *node) getPath(ctx context.Context, rev Revision, path string, depth int) interface{} { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 380 | if path == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 381 | return n.getData(ctx, rev, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | partition := strings.SplitN(path, "/", 2) |
| 385 | name := partition[0] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 386 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 387 | if len(partition) < 2 { |
| 388 | path = "" |
| 389 | } else { |
| 390 | path = partition[1] |
| 391 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 392 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 393 | names := ChildrenFields(n.Type) |
| 394 | field := names[name] |
| 395 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 396 | if field != nil && field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 397 | children := make([]Revision, len(rev.GetChildren(name))) |
| 398 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 399 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 400 | if field.Key != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 401 | if path != "" { |
| 402 | partition = strings.SplitN(path, "/", 2) |
| 403 | key := partition[0] |
| 404 | path = "" |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 405 | keyValue := field.KeyFromStr(key) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 406 | _, childRev := n.findRevByKey(children, field.Key, keyValue) |
| 407 | if childRev == nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 408 | return nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 409 | } |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 410 | childNode := childRev.getNode() |
| 411 | return childNode.getPath(ctx, childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 412 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 413 | var response []interface{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 414 | for _, childRev := range children { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 415 | childNode := childRev.getNode() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 416 | value := childNode.getData(ctx, childRev, depth) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 417 | response = append(response, value) |
| 418 | } |
| 419 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 420 | } |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 421 | var response []interface{} |
| 422 | if path != "" { |
| 423 | // TODO: raise error |
| 424 | return response |
| 425 | } |
| 426 | for _, childRev := range children { |
| 427 | childNode := childRev.getNode() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 428 | value := childNode.getData(ctx, childRev, depth) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 429 | response = append(response, value) |
| 430 | } |
| 431 | return response |
| 432 | } else if children := rev.GetChildren(name); children != nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 433 | childRev := children[0] |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 434 | childNode := childRev.getNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 435 | return childNode.getPath(ctx, childRev, path, depth) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 436 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 437 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 438 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 441 | // getData retrieves the data from a node revision |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 442 | func (n *node) getData(ctx context.Context, rev Revision, depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 443 | msg := rev.GetBranch().GetLatest().Get(depth) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 444 | var modifiedMsg interface{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 445 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 446 | if n.GetProxy() != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 447 | logger.Debugw("invoking-get-callbacks", log.Fields{"data": msg}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 448 | if modifiedMsg = n.GetProxy().InvokeCallbacks(ctx, Get, false, msg); modifiedMsg != nil { |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 449 | msg = modifiedMsg |
| 450 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 451 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 452 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 453 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 454 | return msg |
| 455 | } |
| 456 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 457 | // Update changes the content of a node at the specified path with the provided data |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 458 | func (n *node) Update(ctx context.Context, path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 459 | n.mutex.Lock() |
| 460 | defer n.mutex.Unlock() |
| 461 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 462 | logger.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 463 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 464 | for strings.HasPrefix(path, "/") { |
| 465 | path = path[1:] |
| 466 | } |
| 467 | |
| 468 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 469 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 470 | branch = n.GetBranch(NONE) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 471 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 472 | branch = makeBranch(n) |
| 473 | } |
| 474 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 475 | if branch.GetLatest() != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 476 | logger.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 477 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 478 | if path == "" { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 479 | return n.doUpdate(ctx, branch, data, strict) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 480 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 481 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 482 | rev := branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 483 | |
| 484 | partition := strings.SplitN(path, "/", 2) |
| 485 | name := partition[0] |
| 486 | |
| 487 | if len(partition) < 2 { |
| 488 | path = "" |
| 489 | } else { |
| 490 | path = partition[1] |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 493 | field := ChildrenFields(n.Type)[name] |
| 494 | var children []Revision |
| 495 | |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 496 | if field == nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 497 | return n.doUpdate(ctx, branch, data, strict) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 498 | } |
| 499 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 500 | if field.IsContainer { |
| 501 | if path == "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 502 | logger.Errorf("cannot update a list") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 503 | } else if field.Key != "" { |
| 504 | partition := strings.SplitN(path, "/", 2) |
| 505 | key := partition[0] |
| 506 | if len(partition) < 2 { |
| 507 | path = "" |
| 508 | } else { |
| 509 | path = partition[1] |
| 510 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 511 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 512 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 513 | children = make([]Revision, len(rev.GetChildren(name))) |
| 514 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 515 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 516 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 517 | |
| 518 | if childRev == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 519 | logger.Debugw("child-revision-is-nil", log.Fields{"key": keyValue}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 520 | return branch.GetLatest() |
| 521 | } |
| 522 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 523 | childNode := childRev.getNode() |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 524 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 525 | // Save proxy in child node to ensure callbacks are called later on |
Stephane Barbarie | d62ac4e | 2019-02-05 14:08:38 -0500 | [diff] [blame] | 526 | // only assign in cases of non sub-folder proxies, i.e. "/" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 527 | if childNode.Proxy == nil && n.Proxy != nil && n.GetProxy().getFullPath() == "" { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 528 | childNode.Proxy = n.Proxy |
| 529 | } |
| 530 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 531 | newChildRev := childNode.Update(ctx, path, data, strict, txid, makeBranch) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 532 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 533 | if newChildRev.GetHash() == childRev.GetHash() { |
| 534 | if newChildRev != childRev { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 535 | logger.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 536 | newChildRev.ClearHash() |
| 537 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 538 | logger.Debugw("child-revisions-have-matching-hash", log.Fields{"hash": childRev.GetHash(), "key": keyValue}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 539 | return branch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 540 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 541 | |
| 542 | _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 543 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 544 | _newKeyType := newKey.String() |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 545 | _keyValueType := fmt.Sprintf("%s", keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 546 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 547 | if _newKeyType != _keyValueType { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 548 | logger.Errorf("cannot change key field") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 549 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 550 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 551 | // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 552 | newChildRev.SetName(name + "/" + _keyValueType) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 553 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 554 | branch.LatestLock.Lock() |
| 555 | defer branch.LatestLock.Unlock() |
| 556 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 557 | if idx >= 0 { |
| 558 | children[idx] = newChildRev |
| 559 | } else { |
| 560 | children = append(children, newChildRev) |
| 561 | } |
| 562 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 563 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 564 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 565 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 566 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 567 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 568 | return newChildRev |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 569 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 570 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 571 | logger.Errorf("cannot index into container with no keys") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 572 | } |
| 573 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 574 | childRev := rev.GetChildren(name)[0] |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 575 | childNode := childRev.getNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 576 | newChildRev := childNode.Update(ctx, path, data, strict, txid, makeBranch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 577 | |
| 578 | branch.LatestLock.Lock() |
| 579 | defer branch.LatestLock.Unlock() |
| 580 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 581 | updatedRev := rev.UpdateChildren(ctx, name, []Revision{newChildRev}, branch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 582 | n.makeLatest(branch, updatedRev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 583 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 584 | updatedRev.ChildDrop(name, childRev.GetHash()) |
| 585 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 586 | return newChildRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 587 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 588 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 589 | return nil |
| 590 | } |
| 591 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 592 | func (n *node) doUpdate(ctx context.Context, branch *Branch, data interface{}, strict bool) Revision { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 593 | logger.Debugw("comparing-types", log.Fields{"expected": reflect.ValueOf(n.Type).Type(), "actual": reflect.TypeOf(data)}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 594 | |
| 595 | if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() { |
| 596 | // TODO raise error |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 597 | logger.Errorw("types-do-not-match: %+v", log.Fields{"actual": reflect.TypeOf(data), "expected": n.Type}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 598 | return nil |
| 599 | } |
| 600 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 601 | if n.GetProxy() != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 602 | logger.Debug("invoking proxy PreUpdate Callbacks") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 603 | n.GetProxy().InvokeCallbacks(ctx, PreUpdate, false, branch.GetLatest(), data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 604 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 605 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 606 | if strict { |
| 607 | // TODO: checkAccessViolations(data, Branch.GetLatest.data) |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 608 | logger.Warn("access-violations-not-supported") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 609 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 610 | |
| 611 | // The way the model is used, this function is only invoked upon data change. Therefore, to also |
| 612 | // avoid a deep proto.message comparison (expensive), just create a new branch regardless |
| 613 | rev := branch.GetLatest().UpdateData(ctx, data, branch) |
| 614 | changes := []ChangeTuple{{PostUpdate, branch.GetLatest().GetData(), rev.GetData()}} |
| 615 | n.makeLatest(branch, rev, changes) |
| 616 | |
| 617 | return rev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 618 | } |
| 619 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 620 | // Add inserts a new node at the specified path with the provided data |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 621 | func (n *node) Add(ctx context.Context, path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 622 | n.mutex.Lock() |
| 623 | defer n.mutex.Unlock() |
| 624 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 625 | logger.Debugw("node-add-request", log.Fields{"path": path, "txid": txid}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 626 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 627 | for strings.HasPrefix(path, "/") { |
| 628 | path = path[1:] |
| 629 | } |
| 630 | if path == "" { |
| 631 | // TODO raise error |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 632 | logger.Errorf("cannot add for non-container mode") |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 633 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 637 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 638 | branch = n.GetBranch(NONE) |
| 639 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 640 | branch = makeBranch(n) |
| 641 | } |
| 642 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 643 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 644 | |
| 645 | partition := strings.SplitN(path, "/", 2) |
| 646 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 647 | |
| 648 | if len(partition) < 2 { |
| 649 | path = "" |
| 650 | } else { |
| 651 | path = partition[1] |
| 652 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 653 | |
| 654 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 655 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 656 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 657 | |
| 658 | if field.IsContainer { |
| 659 | if path == "" { |
| 660 | if field.Key != "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 661 | if n.GetProxy() != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 662 | logger.Debug("invoking proxy PreAdd Callbacks") |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 663 | n.GetProxy().InvokeCallbacks(ctx, PreAdd, false, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 664 | } |
| 665 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 666 | children = make([]Revision, len(rev.GetChildren(name))) |
| 667 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 668 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 669 | _, key := GetAttributeValue(data, field.Key, 0) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 670 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 671 | if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 672 | // TODO raise error |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 673 | logger.Warnw("duplicate-key-found", log.Fields{"key": key.String()}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 674 | return exists |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 675 | } |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 676 | childRev := n.MakeNode(data, "").Latest() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 677 | |
| 678 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 679 | childRev.SetName(name + "/" + key.String()) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 680 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 681 | branch.LatestLock.Lock() |
| 682 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 683 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 684 | children = append(children, childRev) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 685 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 686 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 687 | changes := []ChangeTuple{{PostAdd, nil, childRev.GetData()}} |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 688 | |
| 689 | n.makeLatest(branch, updatedRev, changes) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 690 | |
| 691 | return childRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 692 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 693 | logger.Errorf("cannot add to non-keyed container") |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 694 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 695 | } else if field.Key != "" { |
| 696 | partition := strings.SplitN(path, "/", 2) |
| 697 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 698 | if len(partition) < 2 { |
| 699 | path = "" |
| 700 | } else { |
| 701 | path = partition[1] |
| 702 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 703 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 704 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 705 | children = make([]Revision, len(rev.GetChildren(name))) |
| 706 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 707 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 708 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 709 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 710 | if childRev == nil { |
| 711 | return branch.GetLatest() |
| 712 | } |
| 713 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 714 | childNode := childRev.getNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 715 | newChildRev := childNode.Add(ctx, path, data, txid, makeBranch) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 716 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 717 | // Prefix the hash with the data type (e.g. devices, logical_devices, adapters) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 718 | newChildRev.SetName(name + "/" + keyValue.(string)) |
| 719 | |
| 720 | branch.LatestLock.Lock() |
| 721 | defer branch.LatestLock.Unlock() |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 722 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 723 | if idx >= 0 { |
| 724 | children[idx] = newChildRev |
| 725 | } else { |
| 726 | children = append(children, newChildRev) |
| 727 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 728 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 729 | updatedRev := rev.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 730 | n.makeLatest(branch, updatedRev, nil) |
| 731 | |
| 732 | updatedRev.ChildDrop(name, childRev.GetHash()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 733 | |
| 734 | return newChildRev |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 735 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 736 | logger.Errorf("cannot add to non-keyed container") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 737 | } |
| 738 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 739 | logger.Errorf("cannot add to non-container field") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 740 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 741 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 742 | return nil |
| 743 | } |
| 744 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 745 | // Remove eliminates a node at the specified path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 746 | func (n *node) Remove(ctx context.Context, path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 747 | n.mutex.Lock() |
| 748 | defer n.mutex.Unlock() |
| 749 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 750 | logger.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch}) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 751 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 752 | for strings.HasPrefix(path, "/") { |
| 753 | path = path[1:] |
| 754 | } |
| 755 | if path == "" { |
| 756 | // TODO raise error |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 757 | logger.Errorf("cannot remove for non-container mode") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 758 | } |
| 759 | var branch *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 760 | if txid == "" { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 761 | branch = n.GetBranch(NONE) |
| 762 | } else if branch = n.GetBranch(txid); branch == nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 763 | branch = makeBranch(n) |
| 764 | } |
| 765 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 766 | rev := branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 767 | |
| 768 | partition := strings.SplitN(path, "/", 2) |
| 769 | name := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 770 | if len(partition) < 2 { |
| 771 | path = "" |
| 772 | } else { |
| 773 | path = partition[1] |
| 774 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 775 | |
| 776 | field := ChildrenFields(n.Type)[name] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 777 | var children []Revision |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 778 | postAnnouncement := []ChangeTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 779 | |
| 780 | if field.IsContainer { |
| 781 | if path == "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 782 | logger.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 783 | } else if field.Key != "" { |
| 784 | partition := strings.SplitN(path, "/", 2) |
| 785 | key := partition[0] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 786 | if len(partition) < 2 { |
| 787 | path = "" |
| 788 | } else { |
| 789 | path = partition[1] |
| 790 | } |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 791 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 792 | keyValue := field.KeyFromStr(key) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 793 | children = make([]Revision, len(rev.GetChildren(name))) |
| 794 | copy(children, rev.GetChildren(name)) |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 795 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 796 | if path != "" { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 797 | if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 798 | childNode := childRev.getNode() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 799 | if childNode.Proxy == nil { |
| 800 | childNode.Proxy = n.Proxy |
| 801 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 802 | newChildRev := childNode.Remove(ctx, path, txid, makeBranch) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 803 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 804 | branch.LatestLock.Lock() |
| 805 | defer branch.LatestLock.Unlock() |
| 806 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 807 | if idx >= 0 { |
| 808 | children[idx] = newChildRev |
| 809 | } else { |
| 810 | children = append(children, newChildRev) |
| 811 | } |
| 812 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 813 | rev.SetChildren(name, children) |
| 814 | branch.GetLatest().Drop(txid, false) |
| 815 | n.makeLatest(branch, rev, nil) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 816 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 817 | return branch.GetLatest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 818 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 819 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 820 | idx, childRev := n.findRevByKey(children, field.Key, keyValue) |
| 821 | if childRev != nil && idx >= 0 { |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 822 | if n.GetProxy() != nil { |
| 823 | data := childRev.GetData() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 824 | n.GetProxy().InvokeCallbacks(ctx, PreRemove, false, data) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 825 | postAnnouncement = append(postAnnouncement, ChangeTuple{PostRemove, data, nil}) |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 826 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 827 | postAnnouncement = append(postAnnouncement, ChangeTuple{PostRemove, childRev.GetData(), nil}) |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 828 | } |
| 829 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 830 | childRev.StorageDrop(ctx, txid, true) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 831 | getRevCache().Delete(childRev.GetName()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 832 | |
| 833 | branch.LatestLock.Lock() |
| 834 | defer branch.LatestLock.Unlock() |
| 835 | |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 836 | children = append(children[:idx], children[idx+1:]...) |
| 837 | rev.SetChildren(name, children) |
| 838 | |
| 839 | branch.GetLatest().Drop(txid, false) |
| 840 | n.makeLatest(branch, rev, postAnnouncement) |
| 841 | |
| 842 | return rev |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 843 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 844 | logger.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 845 | } |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 846 | logger.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 847 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 848 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 849 | logger.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | return nil |
| 853 | } |
| 854 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 855 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 856 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 857 | // MakeBranchFunction is a type for function references intented to create a branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 858 | type MakeBranchFunction func(*node) *Branch |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 859 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 860 | // MakeBranch creates a new branch for the provided transaction id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 861 | func (n *node) MakeBranch(txid string) *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 862 | branchPoint := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 863 | branch := NewBranch(n, txid, branchPoint, true) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 864 | n.SetBranch(txid, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 865 | return branch |
| 866 | } |
| 867 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 868 | // DeleteBranch removes a branch with the specified id |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 869 | func (n *node) DeleteBranch(txid string) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 870 | delete(n.Branches, txid) |
| 871 | } |
| 872 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 873 | func (n *node) mergeChild(ctx context.Context, txid string, dryRun bool) func(Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 874 | f := func(rev Revision) Revision { |
| 875 | childBranch := rev.GetBranch() |
| 876 | |
| 877 | if childBranch.Txid == txid { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 878 | rev, _ = childBranch.Node.MergeBranch(ctx, txid, dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return rev |
| 882 | } |
| 883 | return f |
| 884 | } |
| 885 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 886 | // MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 887 | func (n *node) MergeBranch(ctx context.Context, txid string, dryRun bool) (Revision, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 888 | srcBranch := n.GetBranch(txid) |
| 889 | dstBranch := n.GetBranch(NONE) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 890 | |
| 891 | forkRev := srcBranch.Origin |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 892 | srcRev := srcBranch.GetLatest() |
| 893 | dstRev := dstBranch.GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 894 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 895 | rev, changes := Merge3Way(ctx, forkRev, srcRev, dstRev, n.mergeChild(ctx, txid, dryRun), dryRun) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 896 | |
| 897 | if !dryRun { |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 898 | if rev != nil { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 899 | rev.SetName(dstRev.GetName()) |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 900 | n.makeLatest(dstBranch, rev, changes) |
| 901 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 902 | n.DeleteBranch(txid) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 903 | } |
| 904 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 905 | // TODO: return proper error when one occurs |
| 906 | return rev, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 907 | } |
| 908 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 909 | // CreateProxy returns a reference to a sub-tree of the data model |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 910 | func (n *node) CreateProxy(ctx context.Context, path string, exclusive bool) (*Proxy, error) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 911 | return n.createProxy(ctx, path, path, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 912 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 913 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 914 | func (n *node) createProxy(ctx context.Context, path string, fullPath string, parentNode *node, exclusive bool) (*Proxy, error) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 915 | logger.Debugw("node-create-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 916 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 917 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 918 | "path": path, |
| 919 | "fullPath": fullPath, |
| 920 | }) |
| 921 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 922 | for strings.HasPrefix(path, "/") { |
| 923 | path = path[1:] |
| 924 | } |
| 925 | if path == "" { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 926 | return n.makeProxy(path, fullPath, parentNode, exclusive), nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 927 | } |
| 928 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 929 | rev := n.GetBranch(NONE).GetLatest() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 930 | partition := strings.SplitN(path, "/", 2) |
| 931 | name := partition[0] |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 932 | var nodeType interface{} |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 933 | if len(partition) < 2 { |
| 934 | path = "" |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 935 | nodeType = n.Type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 936 | } else { |
| 937 | path = partition[1] |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 938 | nodeType = parentNode.Type |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 939 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 940 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 941 | field := ChildrenFields(nodeType)[name] |
| 942 | |
| 943 | if field != nil { |
| 944 | if field.IsContainer { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 945 | logger.Debugw("container-field", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 946 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 947 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 948 | "path": path, |
| 949 | "name": name, |
| 950 | }) |
| 951 | if path == "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 952 | logger.Debugw("folder-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 953 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 954 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 955 | "fullPath": fullPath, |
| 956 | "name": name, |
| 957 | }) |
| 958 | newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 959 | return newNode.makeProxy(path, fullPath, parentNode, exclusive), nil |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 960 | } else if field.Key != "" { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 961 | logger.Debugw("key-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 962 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 963 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 964 | "fullPath": fullPath, |
| 965 | "name": name, |
| 966 | }) |
| 967 | partition := strings.SplitN(path, "/", 2) |
| 968 | key := partition[0] |
| 969 | if len(partition) < 2 { |
| 970 | path = "" |
| 971 | } else { |
| 972 | path = partition[1] |
| 973 | } |
| 974 | keyValue := field.KeyFromStr(key) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 975 | children := make([]Revision, len(rev.GetChildren(name))) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 976 | copy(children, rev.GetChildren(name)) |
| 977 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 978 | var childRev Revision |
| 979 | if _, childRev = n.findRevByKey(children, field.Key, keyValue); childRev != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 980 | logger.Debugw("found-revision-matching-key-in-memory", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 981 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 982 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 983 | "fullPath": fullPath, |
| 984 | "name": name, |
| 985 | }) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 986 | } else if revs, err := n.GetBranch(NONE).GetLatest().LoadFromPersistence(ctx, fullPath, "", nil); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 987 | logger.Errorf("failed-to-load-from-persistence") |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 988 | return nil, err |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 989 | } else if len(revs) > 0 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 990 | logger.Debugw("found-revision-matching-key-in-db", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 991 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 992 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 993 | "fullPath": fullPath, |
| 994 | "name": name, |
| 995 | }) |
| 996 | childRev = revs[0] |
| 997 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 998 | logger.Debugw("no-revision-matching-key", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 999 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1000 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1001 | "fullPath": fullPath, |
| 1002 | "name": name, |
| 1003 | }) |
| 1004 | } |
| 1005 | if childRev != nil { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 1006 | childNode := childRev.getNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1007 | return childNode.createProxy(ctx, path, fullPath, n, exclusive) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1008 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1009 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1010 | logger.Errorw("cannot-access-index-of-empty-container", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1011 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1012 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1013 | "path": path, |
| 1014 | "name": name, |
| 1015 | }) |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 1016 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1017 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1018 | logger.Debugw("non-container-field", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1019 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1020 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1021 | "path": path, |
| 1022 | "name": name, |
| 1023 | }) |
| 1024 | childRev := rev.GetChildren(name)[0] |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 1025 | childNode := childRev.getNode() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1026 | return childNode.createProxy(ctx, path, fullPath, n, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1027 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1028 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1029 | logger.Debugw("field-object-is-nil", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1030 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1031 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1032 | "fullPath": fullPath, |
| 1033 | "name": name, |
| 1034 | }) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1037 | logger.Warnw("cannot-create-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1038 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1039 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1040 | "path": path, |
| 1041 | "fullPath": fullPath, |
| 1042 | "latest-rev": rev.GetHash(), |
| 1043 | }) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 1044 | return nil, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1047 | func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1048 | logger.Debugw("node-make-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1049 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1050 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1051 | "path": path, |
| 1052 | "fullPath": fullPath, |
| 1053 | }) |
| 1054 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1055 | r := &root{ |
| 1056 | node: n, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1057 | Callbacks: n.Root.GetCallbacks(), |
| 1058 | NotificationCallbacks: n.Root.GetNotificationCallbacks(), |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 1059 | DirtyNodes: n.Root.DirtyNodes, |
| 1060 | KvStore: n.Root.KvStore, |
| 1061 | Loading: n.Root.Loading, |
| 1062 | RevisionClass: n.Root.RevisionClass, |
| 1063 | } |
| 1064 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1065 | if n.Proxy == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1066 | logger.Debugw("constructing-new-proxy", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1067 | "node-type": reflect.ValueOf(n.Type).Type(), |
| 1068 | "parent-node-type": reflect.ValueOf(parentNode.Type).Type(), |
| 1069 | "path": path, |
| 1070 | "fullPath": fullPath, |
| 1071 | }) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1072 | n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1073 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1074 | logger.Debugw("node-has-existing-proxy", log.Fields{ |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1075 | "node-type": reflect.ValueOf(n.GetProxy().Node.Type).Type(), |
| 1076 | "parent-node-type": reflect.ValueOf(n.GetProxy().ParentNode.Type).Type(), |
| 1077 | "path": n.GetProxy().Path, |
| 1078 | "fullPath": n.GetProxy().FullPath, |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 1079 | }) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1080 | if n.GetProxy().Exclusive { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 1081 | logger.Error("node is already owned exclusively") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1082 | } |
| 1083 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1084 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1085 | return n.Proxy |
| 1086 | } |
| 1087 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1088 | func (n *node) SetProxy(proxy *Proxy) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1089 | n.Proxy = proxy |
| 1090 | } |
| 1091 | |
| 1092 | func (n *node) GetProxy() *Proxy { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1093 | return n.Proxy |
| 1094 | } |
| 1095 | |
| 1096 | func (n *node) GetBranch(key string) *Branch { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 1097 | if n.Branches != nil { |
| 1098 | if branch, exists := n.Branches[key]; exists { |
| 1099 | return branch |
| 1100 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1101 | } |
| 1102 | return nil |
| 1103 | } |
| 1104 | |
| 1105 | func (n *node) SetBranch(key string, branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1106 | n.Branches[key] = branch |
| 1107 | } |
| 1108 | |
| 1109 | func (n *node) GetRoot() *root { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 1110 | return n.Root |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 1111 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 1112 | func (n *node) SetRoot(root *root) { |
| 1113 | n.Root = root |
| 1114 | } |