Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "crypto/md5" |
| 21 | "fmt" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 24 | "reflect" |
| 25 | "sort" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 26 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 29 | type revCacheSingleton struct { |
| 30 | sync.RWMutex |
| 31 | Cache map[string]interface{} |
| 32 | } |
| 33 | |
| 34 | var revCacheInstance *revCacheSingleton |
| 35 | var revCacheOnce sync.Once |
| 36 | |
| 37 | func GetRevCache() *revCacheSingleton { |
| 38 | revCacheOnce.Do(func() { |
| 39 | revCacheInstance = &revCacheSingleton{Cache: make(map[string]interface{})} |
| 40 | }) |
| 41 | return revCacheInstance |
| 42 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 43 | |
| 44 | type NonPersistedRevision struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 45 | mutex sync.RWMutex |
| 46 | Root *root |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 47 | Config *DataRevision |
| 48 | Children map[string][]Revision |
| 49 | Hash string |
| 50 | Branch *Branch |
| 51 | WeakRef string |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 52 | Name string |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 55 | func NewNonPersistedRevision(root *root, branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 56 | r := &NonPersistedRevision{} |
| 57 | r.Root = root |
| 58 | r.Branch = branch |
| 59 | r.Config = NewDataRevision(root, data) |
| 60 | r.Children = children |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 61 | r.Hash = r.hashContent() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 62 | return r |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func (npr *NonPersistedRevision) SetConfig(config *DataRevision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 66 | npr.mutex.Lock() |
| 67 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 68 | npr.Config = config |
| 69 | } |
| 70 | |
| 71 | func (npr *NonPersistedRevision) GetConfig() *DataRevision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 72 | npr.mutex.Lock() |
| 73 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 74 | return npr.Config |
| 75 | } |
| 76 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 77 | func (npr *NonPersistedRevision) SetAllChildren(children map[string][]Revision) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 78 | npr.mutex.Lock() |
| 79 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | npr.Children = children |
| 81 | } |
| 82 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 83 | func (npr *NonPersistedRevision) SetChildren(name string, children []Revision) { |
| 84 | npr.mutex.Lock() |
| 85 | defer npr.mutex.Unlock() |
| 86 | if _, exists := npr.Children[name]; exists { |
| 87 | npr.Children[name] = children |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (npr *NonPersistedRevision) GetAllChildren() map[string][]Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | npr.mutex.Lock() |
| 93 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 94 | return npr.Children |
| 95 | } |
| 96 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 97 | func (npr *NonPersistedRevision) GetChildren(name string) []Revision { |
| 98 | npr.mutex.Lock() |
| 99 | defer npr.mutex.Unlock() |
| 100 | if _, exists := npr.Children[name]; exists { |
| 101 | return npr.Children[name] |
| 102 | } |
| 103 | return nil |
| 104 | } |
| 105 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 106 | func (npr *NonPersistedRevision) SetHash(hash string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 107 | npr.mutex.Lock() |
| 108 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 109 | npr.Hash = hash |
| 110 | } |
| 111 | |
| 112 | func (npr *NonPersistedRevision) GetHash() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 113 | //npr.mutex.Lock() |
| 114 | //defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 115 | return npr.Hash |
| 116 | } |
| 117 | |
| 118 | func (npr *NonPersistedRevision) ClearHash() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 119 | npr.mutex.Lock() |
| 120 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 121 | npr.Hash = "" |
| 122 | } |
| 123 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 124 | func (npr *NonPersistedRevision) GetName() string { |
| 125 | //npr.mutex.Lock() |
| 126 | //defer npr.mutex.Unlock() |
| 127 | return npr.Name |
| 128 | } |
| 129 | |
| 130 | func (npr *NonPersistedRevision) SetName(name string) { |
| 131 | //npr.mutex.Lock() |
| 132 | //defer npr.mutex.Unlock() |
| 133 | npr.Name = name |
| 134 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 135 | func (npr *NonPersistedRevision) SetBranch(branch *Branch) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 136 | npr.mutex.Lock() |
| 137 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 138 | npr.Branch = branch |
| 139 | } |
| 140 | |
| 141 | func (npr *NonPersistedRevision) GetBranch() *Branch { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 142 | npr.mutex.Lock() |
| 143 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 144 | return npr.Branch |
| 145 | } |
| 146 | |
| 147 | func (npr *NonPersistedRevision) GetData() interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 148 | npr.mutex.Lock() |
| 149 | defer npr.mutex.Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 150 | if npr.Config == nil { |
| 151 | return nil |
| 152 | } |
| 153 | return npr.Config.Data |
| 154 | } |
| 155 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 156 | func (npr *NonPersistedRevision) GetNode() *node { |
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 | return npr.Branch.Node |
| 160 | } |
| 161 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 162 | func (npr *NonPersistedRevision) Finalize(skipOnExist bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 163 | GetRevCache().Lock() |
| 164 | defer GetRevCache().Unlock() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 165 | |
Stephane Barbarie | 1039ec4 | 2019-02-04 10:43:16 -0500 | [diff] [blame] | 166 | if !skipOnExist { |
| 167 | npr.Hash = npr.hashContent() |
| 168 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 169 | if _, exists := GetRevCache().Cache[npr.Hash]; !exists { |
| 170 | GetRevCache().Cache[npr.Hash] = npr |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 171 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 172 | if _, exists := GetRevCache().Cache[npr.Config.Hash]; !exists { |
| 173 | GetRevCache().Cache[npr.Config.Hash] = npr.Config |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 174 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 175 | npr.Config = GetRevCache().Cache[npr.Config.Hash].(*DataRevision) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | func (npr *NonPersistedRevision) hashContent() string { |
| 180 | var buffer bytes.Buffer |
| 181 | var childrenKeys []string |
| 182 | |
| 183 | if npr.Config != nil { |
| 184 | buffer.WriteString(npr.Config.Hash) |
| 185 | } |
| 186 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | for key := range npr.Children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 188 | childrenKeys = append(childrenKeys, key) |
| 189 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 190 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 191 | sort.Strings(childrenKeys) |
| 192 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 193 | if len(npr.Children) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 194 | // Loop through sorted Children keys |
| 195 | for _, key := range childrenKeys { |
| 196 | for _, child := range npr.Children[key] { |
| 197 | if child != nil && child.GetHash() != "" { |
| 198 | buffer.WriteString(child.GetHash()) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return fmt.Sprintf("%x", md5.Sum(buffer.Bytes()))[:12] |
| 205 | } |
| 206 | |
| 207 | func (npr *NonPersistedRevision) Get(depth int) interface{} { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 208 | // 1. Clone the data to avoid any concurrent access issues |
| 209 | // 2. The current rev might still be pointing to an old config |
| 210 | // thus, force the revision to get its latest value |
| 211 | latestRev := npr.GetBranch().GetLatest() |
| 212 | originalData := proto.Clone(latestRev.GetData().(proto.Message)) |
| 213 | |
| 214 | data := originalData |
| 215 | // Get back to the interface type |
| 216 | //data := reflect.ValueOf(originalData).Interface() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 217 | |
| 218 | if depth != 0 { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 219 | for fieldName, field := range ChildrenFields(latestRev.GetData()) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 220 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 221 | if field.IsContainer { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 222 | for _, rev := range latestRev.GetChildren(fieldName) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 223 | childData := rev.Get(depth - 1) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 224 | foundEntry := false |
| 225 | for i := 0; i < childDataHolder.Len(); i++ { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 226 | cdh_if := childDataHolder.Index(i).Interface() |
| 227 | if cdh_if.(proto.Message).String() == childData.(proto.Message).String() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 228 | foundEntry = true |
| 229 | break |
| 230 | } |
| 231 | } |
| 232 | if !foundEntry { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 233 | // 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] | 234 | childDataHolder = reflect.Append(childDataHolder, reflect.ValueOf(childData)) |
| 235 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 236 | } |
| 237 | } else { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 238 | if revs := latestRev.GetChildren(fieldName); revs != nil && len(revs) > 0 { |
| 239 | rev := latestRev.GetChildren(fieldName)[0] |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 240 | if rev != nil { |
| 241 | childData := rev.Get(depth - 1) |
| 242 | if reflect.TypeOf(childData) == reflect.TypeOf(childDataHolder.Interface()) { |
| 243 | childDataHolder = reflect.ValueOf(childData) |
| 244 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 247 | } |
| 248 | // Merge child data with cloned object |
| 249 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 250 | } |
| 251 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 252 | |
| 253 | result := data |
| 254 | |
| 255 | if result != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 256 | // We need to send back a copy of the retrieved object |
| 257 | result = proto.Clone(data.(proto.Message)) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return result |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | func (npr *NonPersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 264 | npr.mutex.Lock() |
| 265 | defer npr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 266 | |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 267 | 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] | 268 | 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] | 269 | return npr |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 272 | newRev := NonPersistedRevision{} |
| 273 | newRev.Config = NewDataRevision(npr.Root, data) |
| 274 | newRev.Hash = npr.Hash |
| 275 | newRev.Branch = branch |
| 276 | |
| 277 | newRev.Children = make(map[string][]Revision) |
| 278 | for entryName, childrenEntry := range npr.Children { |
| 279 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 280 | } |
| 281 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 282 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 283 | |
| 284 | return &newRev |
| 285 | } |
| 286 | |
| 287 | func (npr *NonPersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 288 | npr.mutex.Lock() |
| 289 | defer npr.mutex.Unlock() |
| 290 | |
| 291 | updatedRev := npr |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 292 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 293 | // Verify if the map contains already contains an entry matching the name value |
| 294 | // If so, we need to retain the contents of that entry and merge them with the provided children revision list |
| 295 | if _, exists := updatedRev.Children[name]; exists { |
| 296 | // Go through all child hashes and save their index within the map |
| 297 | existChildMap := make(map[string]int) |
| 298 | for i, child := range updatedRev.Children[name] { |
| 299 | existChildMap[child.GetHash()] = i |
| 300 | } |
| 301 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 302 | for _, newChild := range children { |
| 303 | if _, childExists := existChildMap[newChild.GetHash()]; !childExists { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 304 | // revision is not present in the existing list... add it |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 305 | updatedRev.Children[name] = append(updatedRev.Children[name], newChild) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 306 | } else { |
| 307 | // replace |
| 308 | updatedRev.Children[name][existChildMap[newChild.GetHash()]] = newChild |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } else { |
| 312 | // Map entry does not exist, thus just create a new entry and assign the provided revisions |
| 313 | updatedRev.Children[name] = make([]Revision, len(children)) |
| 314 | copy(updatedRev.Children[name], children) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Stephane Barbarie | 933b09b | 2019-01-09 11:12:09 -0500 | [diff] [blame] | 317 | updatedRev.Config = NewDataRevision(npr.Root, npr.Config.Data) |
| 318 | updatedRev.Hash = npr.Hash |
| 319 | updatedRev.Branch = branch |
| 320 | updatedRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 321 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 322 | return updatedRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | func (npr *NonPersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 326 | npr.mutex.Lock() |
| 327 | defer npr.mutex.Unlock() |
| 328 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 329 | newRev := npr |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 330 | newRev.Config = npr.Config |
| 331 | newRev.Hash = npr.Hash |
| 332 | newRev.Branch = branch |
| 333 | newRev.Children = make(map[string][]Revision) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 334 | for entryName, childrenEntry := range children { |
| 335 | newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 336 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 337 | newRev.Finalize(false) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 338 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 339 | return newRev |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 340 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 341 | |
| 342 | func (npr *NonPersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 343 | GetRevCache().Lock() |
| 344 | defer GetRevCache().Unlock() |
| 345 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 346 | if includeConfig { |
| 347 | delete(GetRevCache().Cache, npr.Config.Hash) |
| 348 | } |
| 349 | delete(GetRevCache().Cache, npr.Hash) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 350 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 351 | |
| 352 | func (npr *NonPersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 353 | // stub... required by interface |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 354 | return nil |
| 355 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 356 | |
| 357 | func (npr *NonPersistedRevision) SetupWatch(key string) { |
| 358 | // stub ... required by interface |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | func (pr *NonPersistedRevision) StorageDrop(txid string, includeConfig bool) { |
| 362 | // stub ... required by interface |
| 363 | } |