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