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" |
| 31 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
| 32 | "github.com/opencord/voltha-lib-go/v2/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 - |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 207 | func (npr *NonPersistedRevision) Finalize(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 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 330 | newRev.Finalize(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 { |
| 387 | // Check if the data has changed or not |
| 388 | if existingChildren[nameIndex].GetData().(proto.Message).String() != newChild.GetData().(proto.Message).String() { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 389 | log.Debugw("replacing-existing-child", log.Fields{ |
| 390 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 391 | "old-data": existingChildren[nameIndex].GetData(), |
| 392 | "new-hash": newChild.GetHash(), |
| 393 | "new-data": newChild.GetData(), |
| 394 | }) |
| 395 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 396 | // replace entry |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 397 | newChild.getNode().SetRoot(existingChildren[nameIndex].getNode().GetRoot()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 398 | updatedChildren = append(updatedChildren, newChild) |
| 399 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 400 | log.Debugw("keeping-existing-child", log.Fields{ |
| 401 | "old-hash": existingChildren[nameIndex].GetHash(), |
| 402 | "old-data": existingChildren[nameIndex].GetData(), |
| 403 | "new-hash": newChild.GetHash(), |
| 404 | "new-data": newChild.GetData(), |
| 405 | }) |
| 406 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 407 | // keep existing entry |
| 408 | updatedChildren = append(updatedChildren, existingChildren[nameIndex]) |
| 409 | } |
| 410 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 411 | log.Debugw("adding-unknown-child", log.Fields{ |
| 412 | "hash": newChild.GetHash(), |
| 413 | "data": newChild.GetData(), |
| 414 | }) |
| 415 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 416 | // new entry ... just add it |
| 417 | updatedChildren = append(updatedChildren, newChild) |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Save children in new revision |
| 422 | updatedRev.SetChildren(name, updatedChildren) |
| 423 | |
| 424 | updatedNames := make(map[string]int) |
| 425 | for i, updatedChild := range updatedChildren { |
| 426 | updatedNames[updatedChild.GetName()] = i |
| 427 | } |
| 428 | |
| 429 | log.Debugw("updated-children-names", log.Fields{"hash": npr.GetHash(), "names": updatedNames}) |
| 430 | |
| 431 | } else { |
| 432 | // There are no children available, just save the provided ones |
| 433 | updatedRev.SetChildren(name, children) |
| 434 | } |
| 435 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 436 | updatedRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 437 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 438 | return updatedRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 441 | // UpdateAllChildren will replace the current list of children with the provided ones |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 442 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 443 | npr.mutex.Lock() |
| 444 | defer npr.mutex.Unlock() |
| 445 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 446 | newRev := npr |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 447 | newRev.Config = npr.Config |
| 448 | newRev.Hash = npr.Hash |
| 449 | newRev.Branch = branch |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 450 | newRev.Name = npr.Name |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 451 | newRev.lastUpdate = npr.lastUpdate |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 452 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 453 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 454 | for entryName, childrenEntry := range children { |
| 455 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 456 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 457 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 458 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 459 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 460 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 461 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 462 | // Drop is used to indicate when a revision is no longer required |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 463 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 464 | log.Debugw("dropping-revision", log.Fields{"hash": npr.GetHash(), "name": npr.GetName()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 465 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 466 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 467 | // ChildDrop will remove a child entry matching the provided parameters from the current revision |
| 468 | func (npr *NonPersistedRevision) ChildDrop(childType string, childHash string) { |
| 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.GetHash() == childHash { |
| 474 | children = append(children[:i], children[i+1:]...) |
| 475 | npr.SetChildren(childType, children) |
| 476 | break |
| 477 | } |
| 478 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 479 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 480 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 481 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 482 | // ChildDropByName will remove a child entry matching the type and name |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 483 | func (npr *NonPersistedRevision) ChildDropByName(childName string) { |
| 484 | // Extract device type |
| 485 | parts := strings.SplitN(childName, "/", 2) |
| 486 | childType := parts[0] |
| 487 | |
| 488 | if childType != "" { |
| 489 | children := make([]Revision, len(npr.GetChildren(childType))) |
| 490 | copy(children, npr.GetChildren(childType)) |
| 491 | for i, child := range children { |
| 492 | if child.GetName() == childName { |
| 493 | children = append(children[:i], children[i+1:]...) |
| 494 | npr.SetChildren(childType, children) |
| 495 | break |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 501 | // SetLastUpdate - |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 502 | func (npr *NonPersistedRevision) SetLastUpdate(ts ...time.Time) { |
| 503 | npr.mutex.Lock() |
| 504 | defer npr.mutex.Unlock() |
| 505 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 506 | if len(ts) > 0 { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 507 | npr.lastUpdate = ts[0] |
| 508 | } else { |
| 509 | npr.lastUpdate = time.Now() |
| 510 | } |
| 511 | } |
| 512 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 513 | // GetLastUpdate - |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 514 | func (npr *NonPersistedRevision) GetLastUpdate() time.Time { |
| 515 | npr.mutex.RLock() |
| 516 | defer npr.mutex.RUnlock() |
| 517 | |
| 518 | return npr.lastUpdate |
| 519 | } |
| 520 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 521 | 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] | 522 | // stub... required by interface |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 523 | return nil, nil |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 524 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 525 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 526 | // SetupWatch - |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 527 | func (npr *NonPersistedRevision) SetupWatch(key string) { |
| 528 | // stub ... required by interface |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 529 | } |
| 530 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 531 | // StorageDrop - |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 532 | func (npr *NonPersistedRevision) StorageDrop(txid string, includeConfig bool) { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 533 | // stub ... required by interface |
| 534 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 535 | |
| 536 | func (npr *NonPersistedRevision) getVersion() int64 { |
| 537 | return -1 |
| 538 | } |