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