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