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