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 | // Do not update the revision if data is the same |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 311 | 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] | 312 | 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] | 313 | return npr |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 316 | // Construct a new revision based on the current one |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 317 | newRev := NonPersistedRevision{} |
| 318 | newRev.Config = NewDataRevision(npr.Root, data) |
| 319 | newRev.Hash = npr.Hash |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 320 | newRev.Root = npr.Root |
| 321 | newRev.Name = npr.Name |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 322 | newRev.Branch = branch |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 323 | newRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 324 | |
| 325 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 326 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 327 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 328 | } |
| 329 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 330 | newRev.Finalize(ctx, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 331 | |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 332 | log.Debugw("update-data-complete", log.Fields{"updated": newRev.Config.Data, "provided": data}) |
| 333 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 334 | return &newRev |
| 335 | } |
| 336 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 337 | // UpdateChildren will refresh the list of children with the provided ones |
| 338 | // 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] | 339 | 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] | 340 | npr.mutex.Lock() |
| 341 | defer npr.mutex.Unlock() |
| 342 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 343 | // Construct a new revision based on the current one |
| 344 | updatedRev := &NonPersistedRevision{} |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 345 | updatedRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 346 | updatedRev.Hash = npr.Hash |
| 347 | updatedRev.Branch = branch |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 348 | updatedRev.Name = npr.Name |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 349 | updatedRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 350 | |
| 351 | updatedRev.Children = make(map[string][]Revision) |
| 352 | for entryName, childrenEntry := range branch.GetLatest().GetAllChildren() { |
| 353 | updatedRev.Children[entryName] = append(updatedRev.Children[entryName], childrenEntry...) |
| 354 | } |
| 355 | |
| 356 | var updatedChildren []Revision |
| 357 | |
| 358 | // Verify if the map contains already contains an entry matching the name value |
| 359 | // If so, we need to retain the contents of that entry and merge them with the provided children revision list |
| 360 | if existingChildren := branch.GetLatest().GetChildren(name); existingChildren != nil { |
| 361 | // Construct a map of unique child names with the respective index value |
| 362 | // for the children in the existing revision as well as the new ones |
| 363 | existingNames := make(map[string]int) |
| 364 | newNames := make(map[string]int) |
| 365 | |
| 366 | for i, newChild := range children { |
| 367 | newNames[newChild.GetName()] = i |
| 368 | } |
| 369 | |
| 370 | for i, existingChild := range existingChildren { |
| 371 | existingNames[existingChild.GetName()] = i |
| 372 | |
| 373 | // If an existing entry is not in the new list, add it to the updated list, so it is not forgotten |
| 374 | if _, exists := newNames[existingChild.GetName()]; !exists { |
| 375 | updatedChildren = append(updatedChildren, existingChild) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | log.Debugw("existing-children-names", log.Fields{"hash": npr.GetHash(), "names": existingNames}) |
| 380 | |
| 381 | // Merge existing and new children |
| 382 | for _, newChild := range children { |
| 383 | nameIndex, nameExists := existingNames[newChild.GetName()] |
| 384 | |
| 385 | // Does the existing list contain a child with that name? |
| 386 | if nameExists { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame^] | 387 | // This function is invoked only when the data has actually changed (current Core usage). Therefore, |
| 388 | // we need to avoid an expensive deep proto.message comparison and treat the data as an update |
| 389 | newChild.getNode().SetRoot(existingChildren[nameIndex].getNode().GetRoot()) |
| 390 | updatedChildren = append(updatedChildren, newChild) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 391 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 392 | log.Debugw("adding-unknown-child", log.Fields{ |
| 393 | "hash": newChild.GetHash(), |
| 394 | "data": newChild.GetData(), |
| 395 | }) |
| 396 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 397 | // new entry ... just add it |
| 398 | updatedChildren = append(updatedChildren, newChild) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Save children in new revision |
| 403 | updatedRev.SetChildren(name, updatedChildren) |
| 404 | |
| 405 | updatedNames := make(map[string]int) |
| 406 | for i, updatedChild := range updatedChildren { |
| 407 | updatedNames[updatedChild.GetName()] = i |
| 408 | } |
| 409 | |
| 410 | log.Debugw("updated-children-names", log.Fields{"hash": npr.GetHash(), "names": updatedNames}) |
| 411 | |
| 412 | } else { |
| 413 | // There are no children available, just save the provided ones |
| 414 | updatedRev.SetChildren(name, children) |
| 415 | } |
| 416 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 417 | updatedRev.Finalize(ctx, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 418 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 419 | return updatedRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 422 | // UpdateAllChildren will replace the current list of children with the provided ones |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 423 | 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] | 424 | npr.mutex.Lock() |
| 425 | defer npr.mutex.Unlock() |
| 426 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 427 | newRev := npr |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 428 | newRev.Config = npr.Config |
| 429 | newRev.Hash = npr.Hash |
| 430 | newRev.Branch = branch |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 431 | newRev.Name = npr.Name |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 432 | newRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 433 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 434 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 435 | for entryName, childrenEntry := range children { |
| 436 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 437 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 438 | newRev.Finalize(ctx, false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 439 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 440 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 441 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 442 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 443 | // Drop is used to indicate when a revision is no longer required |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 444 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 445 | log.Debugw("dropping-revision", log.Fields{"hash": npr.GetHash(), "name": npr.GetName()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 446 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 447 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 448 | // ChildDrop will remove a child entry matching the provided parameters from the current revision |
| 449 | func (npr *NonPersistedRevision) ChildDrop(childType string, childHash string) { |
| 450 | if childType != "" { |
| 451 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 452 | copy(children, npr.GetChildren(childType)) |
| 453 | for i, child := range children { |
| 454 | if child.GetHash() == childHash { |
| 455 | children = append(children[:i], children[i+1:]...) |
| 456 | npr.SetChildren(childType, children) |
| 457 | break |
| 458 | } |
| 459 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 460 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 461 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 462 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 463 | // ChildDropByName will remove a child entry matching the type and name |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 464 | func (npr *NonPersistedRevision) ChildDropByName(childName string) { |
| 465 | // Extract device type |
| 466 | parts := strings.SplitN(childName, "/", 2) |
| 467 | childType := parts[0] |
| 468 | |
| 469 | if childType != "" { |
| 470 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 471 | copy(children, npr.GetChildren(childType)) |
| 472 | for i, child := range children { |
| 473 | if child.GetName() == childName { |
| 474 | children = append(children[:i], children[i+1:]...) |
| 475 | npr.SetChildren(childType, children) |
| 476 | break |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 482 | // SetLastUpdate - |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 483 | func (npr *NonPersistedRevision) SetLastUpdate(ts ...time.Time) { |
| 484 | npr.mutex.Lock() |
| 485 | defer npr.mutex.Unlock() |
| 486 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 487 | if len(ts) > 0 { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 488 | npr.lastUpdate = ts[0] |
| 489 | } else { |
| 490 | npr.lastUpdate = time.Now() |
| 491 | } |
| 492 | } |
| 493 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 494 | // GetLastUpdate - |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 495 | func (npr *NonPersistedRevision) GetLastUpdate() time.Time { |
| 496 | npr.mutex.RLock() |
| 497 | defer npr.mutex.RUnlock() |
| 498 | |
| 499 | return npr.lastUpdate |
| 500 | } |
| 501 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 502 | 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] | 503 | // stub... required by interface |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 504 | return nil, nil |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 505 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 506 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 507 | // SetupWatch - |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 508 | func (npr *NonPersistedRevision) SetupWatch(ctx context.Context, key string) { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 509 | // stub ... required by interface |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 510 | } |
| 511 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 512 | // StorageDrop - |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 513 | func (npr *NonPersistedRevision) StorageDrop(ctx context.Context, txid string, includeConfig bool) { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 514 | // stub ... required by interface |
| 515 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 516 | |
| 517 | func (npr *NonPersistedRevision) getVersion() int64 { |
| 518 | return -1 |
| 519 | } |