Stephane Barbarie | ec0919b | 2018-09-05 14:14: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 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 20 | "context" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 21 | "crypto/md5" |
| 22 | "fmt" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
| 25 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | "reflect" |
| 27 | "sort" |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 28 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 29 | "sync" |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 30 | "time" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 31 | ) |
| 32 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 33 | // TODO: Cache logic will have to be revisited to cleanup unused entries in memory (disabled for now) |
| 34 | // |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 35 | type revCacheSingleton struct { |
| 36 | sync.RWMutex |
| 37 | Cache sync.Map |
| 38 | } |
| 39 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 40 | func (s *revCacheSingleton) Get(path string) (interface{}, bool) { |
| 41 | return s.Cache.Load(path) |
| 42 | } |
| 43 | func (s *revCacheSingleton) Set(path string, value interface{}) { |
| 44 | s.Cache.Store(path, value) |
| 45 | } |
| 46 | func (s *revCacheSingleton) Delete(path string) { |
| 47 | s.Cache.Delete(path) |
| 48 | } |
| 49 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 50 | var revCacheInstance *revCacheSingleton |
| 51 | var revCacheOnce sync.Once |
| 52 | |
| 53 | func GetRevCache() *revCacheSingleton { |
| 54 | revCacheOnce.Do(func() { |
| 55 | revCacheInstance = &revCacheSingleton{Cache: sync.Map{}} |
| 56 | }) |
| 57 | return revCacheInstance |
| 58 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 59 | |
| 60 | type NonPersistedRevision struct { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 61 | mutex sync.RWMutex |
| 62 | Root *root |
| 63 | Config *DataRevision |
| 64 | childrenLock sync.RWMutex |
| 65 | Children map[string][]Revision |
| 66 | Hash string |
| 67 | Branch *Branch |
| 68 | WeakRef string |
| 69 | Name string |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 70 | lastUpdate time.Time |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 73 | func NewNonPersistedRevision(root *root, branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 74 | r := &NonPersistedRevision{} |
| 75 | r.Root = root |
| 76 | r.Branch = branch |
| 77 | r.Config = NewDataRevision(root, data) |
| 78 | r.Children = children |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 79 | r.Hash = r.hashContent() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 80 | return r |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func (npr *NonPersistedRevision) SetConfig(config *DataRevision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | npr.mutex.Lock() |
| 85 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 86 | npr.Config = config |
| 87 | } |
| 88 | |
| 89 | func (npr *NonPersistedRevision) GetConfig() *DataRevision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 90 | npr.mutex.Lock() |
| 91 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 92 | return npr.Config |
| 93 | } |
| 94 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 95 | func (npr *NonPersistedRevision) SetAllChildren(children map[string][]Revision) { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 96 | npr.childrenLock.Lock() |
| 97 | defer npr.childrenLock.Unlock() |
| 98 | npr.Children = make(map[string][]Revision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 99 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 100 | for key, value := range children { |
| 101 | npr.Children[key] = make([]Revision, len(value)) |
| 102 | copy(npr.Children[key], value) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 106 | func (npr *NonPersistedRevision) SetChildren(name string, children []Revision) { |
| 107 | npr.childrenLock.Lock() |
| 108 | defer npr.childrenLock.Unlock() |
| 109 | |
| 110 | npr.Children[name] = make([]Revision, len(children)) |
| 111 | copy(npr.Children[name], children) |
| 112 | } |
| 113 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 114 | func (npr *NonPersistedRevision) GetAllChildren() map[string][]Revision { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 115 | npr.childrenLock.Lock() |
| 116 | defer npr.childrenLock.Unlock() |
| 117 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 118 | return npr.Children |
| 119 | } |
| 120 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 121 | func (npr *NonPersistedRevision) GetChildren(name string) []Revision { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 122 | npr.childrenLock.Lock() |
| 123 | defer npr.childrenLock.Unlock() |
| 124 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 125 | if _, exists := npr.Children[name]; exists { |
| 126 | return npr.Children[name] |
| 127 | } |
| 128 | return nil |
| 129 | } |
| 130 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 131 | func (npr *NonPersistedRevision) SetHash(hash string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 132 | npr.mutex.Lock() |
| 133 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 134 | npr.Hash = hash |
| 135 | } |
| 136 | |
| 137 | func (npr *NonPersistedRevision) GetHash() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 138 | //npr.mutex.Lock() |
| 139 | //defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 140 | return npr.Hash |
| 141 | } |
| 142 | |
| 143 | func (npr *NonPersistedRevision) ClearHash() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 144 | npr.mutex.Lock() |
| 145 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 146 | npr.Hash = "" |
| 147 | } |
| 148 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 149 | func (npr *NonPersistedRevision) GetName() string { |
| 150 | //npr.mutex.Lock() |
| 151 | //defer npr.mutex.Unlock() |
| 152 | return npr.Name |
| 153 | } |
| 154 | |
| 155 | func (npr *NonPersistedRevision) SetName(name string) { |
| 156 | //npr.mutex.Lock() |
| 157 | //defer npr.mutex.Unlock() |
| 158 | npr.Name = name |
| 159 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 160 | func (npr *NonPersistedRevision) SetBranch(branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 161 | npr.mutex.Lock() |
| 162 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 163 | npr.Branch = branch |
| 164 | } |
| 165 | |
| 166 | func (npr *NonPersistedRevision) GetBranch() *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 167 | npr.mutex.Lock() |
| 168 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 169 | return npr.Branch |
| 170 | } |
| 171 | |
| 172 | func (npr *NonPersistedRevision) GetData() interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 173 | npr.mutex.Lock() |
| 174 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 175 | if npr.Config == nil { |
| 176 | return nil |
| 177 | } |
| 178 | return npr.Config.Data |
| 179 | } |
| 180 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 181 | func (npr *NonPersistedRevision) GetNode() *node { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 182 | npr.mutex.Lock() |
| 183 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 184 | return npr.Branch.Node |
| 185 | } |
| 186 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 187 | func (npr *NonPersistedRevision) Finalize(skipOnExist bool) { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 188 | npr.Hash = npr.hashContent() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 191 | // hashContent generates a hash string based on the contents of the revision. |
| 192 | // The string should be unique to avoid conflicts with other revisions |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 193 | func (npr *NonPersistedRevision) hashContent() string { |
| 194 | var buffer bytes.Buffer |
| 195 | var childrenKeys []string |
| 196 | |
| 197 | if npr.Config != nil { |
| 198 | buffer.WriteString(npr.Config.Hash) |
| 199 | } |
| 200 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 201 | if npr.Name != "" { |
| 202 | buffer.WriteString(npr.Name) |
| 203 | } |
| 204 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 205 | for key := range npr.Children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 206 | childrenKeys = append(childrenKeys, key) |
| 207 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 208 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 209 | sort.Strings(childrenKeys) |
| 210 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 211 | if len(npr.Children) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 212 | // Loop through sorted Children keys |
| 213 | for _, key := range childrenKeys { |
| 214 | for _, child := range npr.Children[key] { |
| 215 | if child != nil && child.GetHash() != "" { |
| 216 | buffer.WriteString(child.GetHash()) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 223 | } |
| 224 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 225 | // Get will retrieve the data for the current revision |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 226 | func (npr *NonPersistedRevision) Get(depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 227 | // 1. Clone the data to avoid any concurrent access issues |
| 228 | // 2. The current rev might still be pointing to an old config |
| 229 | // thus, force the revision to get its latest value |
| 230 | latestRev := npr.GetBranch().GetLatest() |
| 231 | originalData := proto.Clone(latestRev.GetData().(proto.Message)) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 232 | data := originalData |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 233 | |
| 234 | if depth != 0 { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 235 | // FIXME: Traversing the struct through reflection sometimes corrupts the data. |
| 236 | // Unlike the original python implementation, golang structs are not lazy loaded. |
| 237 | // Keeping this non-critical logic for now, but Get operations should be forced to |
| 238 | // depth=0 to avoid going through the following loop. |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 239 | for fieldName, field := range ChildrenFields(latestRev.GetData()) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 240 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 241 | if field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 242 | for _, rev := range latestRev.GetChildren(fieldName) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 243 | childData := rev.Get(depth - 1) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 244 | foundEntry := false |
| 245 | for i := 0; i < childDataHolder.Len(); i++ { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 246 | cdh_if := childDataHolder.Index(i).Interface() |
| 247 | if cdh_if.(proto.Message).String() == childData.(proto.Message).String() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 248 | foundEntry = true |
| 249 | break |
| 250 | } |
| 251 | } |
| 252 | if !foundEntry { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 253 | // avoid duplicates by adding it only if the child was not found in the holder |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 254 | childDataHolder = reflect.Append(childDataHolder, reflect.ValueOf(childData)) |
| 255 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 256 | } |
| 257 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 258 | if revs := npr.GetBranch().GetLatest().GetChildren(fieldName); revs != nil && len(revs) > 0 { |
| 259 | rev := revs[0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 260 | if rev != nil { |
| 261 | childData := rev.Get(depth - 1) |
| 262 | if reflect.TypeOf(childData) == reflect.TypeOf(childDataHolder.Interface()) { |
| 263 | childDataHolder = reflect.ValueOf(childData) |
| 264 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 265 | } |
| 266 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 267 | } |
| 268 | // Merge child data with cloned object |
| 269 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 270 | } |
| 271 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 272 | |
| 273 | result := data |
| 274 | |
| 275 | if result != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 276 | // We need to send back a copy of the retrieved object |
| 277 | result = proto.Clone(data.(proto.Message)) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return result |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 283 | // UpdateData will refresh the data content of the revision |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 284 | func (npr *NonPersistedRevision) UpdateData(ctx context.Context, data interface{}, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 285 | npr.mutex.Lock() |
| 286 | defer npr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 287 | |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 288 | log.Debugw("update-data", log.Fields{"hash": npr.GetHash(), "current": npr.Config.Data, "provided": data}) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 289 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 290 | // Do not update the revision if data is the same |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 291 | if npr.Config.Data != nil && npr.Config.hashData(npr.Root, data) == npr.Config.Hash { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 292 | log.Debugw("stored-data-matches-latest", log.Fields{"stored": npr.Config.Data, "provided": data}) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 293 | return npr |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 296 | // Construct a new revision based on the current one |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 297 | newRev := NonPersistedRevision{} |
| 298 | newRev.Config = NewDataRevision(npr.Root, data) |
| 299 | newRev.Hash = npr.Hash |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 300 | newRev.Root = npr.Root |
| 301 | newRev.Name = npr.Name |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 302 | newRev.Branch = branch |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 303 | newRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 304 | |
| 305 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 306 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 307 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 308 | } |
| 309 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 310 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 311 | |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 312 | log.Debugw("update-data-complete", log.Fields{"updated": newRev.Config.Data, "provided": data}) |
| 313 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 314 | return &newRev |
| 315 | } |
| 316 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 317 | // UpdateChildren will refresh the list of children with the provided ones |
| 318 | // It will carefully go through the list and ensure that no child is lost |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 319 | func (npr *NonPersistedRevision) UpdateChildren(ctx context.Context, name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 320 | npr.mutex.Lock() |
| 321 | defer npr.mutex.Unlock() |
| 322 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 323 | // Construct a new revision based on the current one |
| 324 | updatedRev := &NonPersistedRevision{} |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 325 | updatedRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 326 | updatedRev.Hash = npr.Hash |
| 327 | updatedRev.Branch = branch |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 328 | updatedRev.Name = npr.Name |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 329 | updatedRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 330 | |
| 331 | updatedRev.Children = make(map[string][]Revision) |
| 332 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
| 333 | updatedRev.Children[entryName] = append(updatedRev.Children[entryName], childrenEntry...) |
| 334 | } |
| 335 | |
| 336 | var updatedChildren []Revision |
| 337 | |
| 338 | // Verify if the map contains already contains an entry matching the name value |
| 339 | // If so, we need to retain the contents of that entry and merge them with the provided children revision list |
| 340 | if existingChildren := branch.GetLatest().GetChildren(name); existingChildren != nil { |
| 341 | // Construct a map of unique child names with the respective index value |
| 342 | // for the children in the existing revision as well as the new ones |
| 343 | existingNames := make(map[string]int) |
| 344 | newNames := make(map[string]int) |
| 345 | |
| 346 | for i, newChild := range children { |
| 347 | newNames[newChild.GetName()] = i |
| 348 | } |
| 349 | |
| 350 | for i, existingChild := range existingChildren { |
| 351 | existingNames[existingChild.GetName()] = i |
| 352 | |
| 353 | // If an existing entry is not in the new list, add it to the updated list, so it is not forgotten |
| 354 | if _, exists := newNames[existingChild.GetName()]; !exists { |
| 355 | updatedChildren = append(updatedChildren, existingChild) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | log.Debugw("existing-children-names", log.Fields{"hash": npr.GetHash(), "names": existingNames}) |
| 360 | |
| 361 | // Merge existing and new children |
| 362 | for _, newChild := range children { |
| 363 | nameIndex, nameExists := existingNames[newChild.GetName()] |
| 364 | |
| 365 | // Does the existing list contain a child with that name? |
| 366 | if nameExists { |
| 367 | // Check if the data has changed or not |
| 368 | if existingChildren[nameIndex].GetData().(proto.Message).String() != newChild.GetData().(proto.Message).String() { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 369 | log.Debugw("replacing-existing-child", log.Fields{ |
| 370 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 371 | "old-data": existingChildren[nameIndex].GetData(), |
| 372 | "new-hash": newChild.GetHash(), |
| 373 | "new-data": newChild.GetData(), |
| 374 | }) |
| 375 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 376 | // replace entry |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 377 | newChild.GetNode().SetRoot(existingChildren[nameIndex].GetNode().GetRoot()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 378 | updatedChildren = append(updatedChildren, newChild) |
| 379 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 380 | log.Debugw("keeping-existing-child", log.Fields{ |
| 381 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 382 | "old-data": existingChildren[nameIndex].GetData(), |
| 383 | "new-hash": newChild.GetHash(), |
| 384 | "new-data": newChild.GetData(), |
| 385 | }) |
| 386 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 387 | // keep existing entry |
| 388 | updatedChildren = append(updatedChildren, existingChildren[nameIndex]) |
| 389 | } |
| 390 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 391 | log.Debugw("adding-unknown-child", log.Fields{ |
| 392 | "hash": newChild.GetHash(), |
| 393 | "data": newChild.GetData(), |
| 394 | }) |
| 395 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 396 | // new entry ... just add it |
| 397 | updatedChildren = append(updatedChildren, newChild) |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Save children in new revision |
| 402 | updatedRev.SetChildren(name, updatedChildren) |
| 403 | |
| 404 | updatedNames := make(map[string]int) |
| 405 | for i, updatedChild := range updatedChildren { |
| 406 | updatedNames[updatedChild.GetName()] = i |
| 407 | } |
| 408 | |
| 409 | log.Debugw("updated-children-names", log.Fields{"hash": npr.GetHash(), "names": updatedNames}) |
| 410 | |
| 411 | } else { |
| 412 | // There are no children available, just save the provided ones |
| 413 | updatedRev.SetChildren(name, children) |
| 414 | } |
| 415 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 416 | updatedRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 417 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 418 | return updatedRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 421 | // UpdateAllChildren will replace the current list of children with the provided ones |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 422 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 423 | npr.mutex.Lock() |
| 424 | defer npr.mutex.Unlock() |
| 425 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 426 | newRev := npr |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 427 | newRev.Config = npr.Config |
| 428 | newRev.Hash = npr.Hash |
| 429 | newRev.Branch = branch |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 430 | newRev.Name = npr.Name |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 431 | newRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 432 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 433 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 434 | for entryName, childrenEntry := range children { |
| 435 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 436 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 437 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 438 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 439 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 440 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 441 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 442 | // Drop is used to indicate when a revision is no longer required |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 443 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 444 | log.Debugw("dropping-revision", log.Fields{"hash": npr.GetHash(), "name": npr.GetName()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 445 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 446 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 447 | // ChildDrop will remove a child entry matching the provided parameters from the current revision |
| 448 | func (npr *NonPersistedRevision) ChildDrop(childType string, childHash string) { |
| 449 | if childType != "" { |
| 450 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 451 | copy(children, npr.GetChildren(childType)) |
| 452 | for i, child := range children { |
| 453 | if child.GetHash() == childHash { |
| 454 | children = append(children[:i], children[i+1:]...) |
| 455 | npr.SetChildren(childType, children) |
| 456 | break |
| 457 | } |
| 458 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 459 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 460 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 461 | |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 462 | /// ChildDropByName will remove a child entry matching the type and name |
| 463 | func (npr *NonPersistedRevision) ChildDropByName(childName string) { |
| 464 | // Extract device type |
| 465 | parts := strings.SplitN(childName, "/", 2) |
| 466 | childType := parts[0] |
| 467 | |
| 468 | if childType != "" { |
| 469 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 470 | copy(children, npr.GetChildren(childType)) |
| 471 | for i, child := range children { |
| 472 | if child.GetName() == childName { |
| 473 | children = append(children[:i], children[i+1:]...) |
| 474 | npr.SetChildren(childType, children) |
| 475 | break |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 481 | func (npr *NonPersistedRevision) SetLastUpdate(ts ...time.Time) { |
| 482 | npr.mutex.Lock() |
| 483 | defer npr.mutex.Unlock() |
| 484 | |
| 485 | if ts != nil && len(ts) > 0 { |
| 486 | npr.lastUpdate = ts[0] |
| 487 | } else { |
| 488 | npr.lastUpdate = time.Now() |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | func (npr *NonPersistedRevision) GetLastUpdate() time.Time { |
| 493 | npr.mutex.RLock() |
| 494 | defer npr.mutex.RUnlock() |
| 495 | |
| 496 | return npr.lastUpdate |
| 497 | } |
| 498 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 499 | func (npr *NonPersistedRevision) LoadFromPersistence(ctx context.Context, path string, txid string, blobs map[string]*kvstore.KVPair) []Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 500 | // stub... required by interface |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 501 | return nil |
| 502 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 503 | |
| 504 | func (npr *NonPersistedRevision) SetupWatch(key string) { |
| 505 | // stub ... required by interface |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 508 | func (npr *NonPersistedRevision) StorageDrop(txid string, includeConfig bool) { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 509 | // stub ... required by interface |
| 510 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 511 | |
| 512 | func (npr *NonPersistedRevision) getVersion() int64 { |
| 513 | return -1 |
| 514 | } |