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