khenaidoo | bf6e7bb | 2018-08-14 22:27: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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "encoding/hex" |
| 21 | "encoding/json" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 22 | "github.com/google/uuid" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 24 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 25 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | "time" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 29 | // Root is used to provide an abstraction to the base root structure |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 30 | type Root interface { |
| 31 | Node |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 32 | Load(rootClass interface{}) *root |
| 33 | |
| 34 | ExecuteCallbacks() |
| 35 | AddCallback(callback CallbackFunction, args ...interface{}) |
| 36 | AddNotificationCallback(callback CallbackFunction, args ...interface{}) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 39 | // root points to the top of the data model tree or sub-tree identified by a proxy |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 40 | type root struct { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 41 | *node |
| 42 | |
| 43 | Callbacks []CallbackTuple |
| 44 | NotificationCallbacks []CallbackTuple |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 45 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 46 | DirtyNodes map[string][]*node |
| 47 | KvStore *Backend |
| 48 | Loading bool |
| 49 | RevisionClass interface{} |
| 50 | |
| 51 | mutex sync.RWMutex |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 54 | // NewRoot creates an new instance of a root object |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 55 | func NewRoot(initialData interface{}, kvStore *Backend) *root { |
| 56 | root := &root{} |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 57 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 58 | root.KvStore = kvStore |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 59 | root.DirtyNodes = make(map[string][]*node) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 60 | root.Loading = false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 61 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 62 | // If there is no storage in place just revert to |
| 63 | // a non persistent mechanism |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 64 | if kvStore != nil { |
| 65 | root.RevisionClass = reflect.TypeOf(PersistedRevision{}) |
| 66 | } else { |
| 67 | root.RevisionClass = reflect.TypeOf(NonPersistedRevision{}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 69 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 70 | root.Callbacks = []CallbackTuple{} |
| 71 | root.NotificationCallbacks = []CallbackTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 72 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 73 | root.node = NewNode(root, initialData, false, "") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 74 | |
| 75 | return root |
| 76 | } |
| 77 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 78 | // MakeTxBranch creates a new transaction branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 79 | func (r *root) MakeTxBranch() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 80 | txidBin, _ := uuid.New().MarshalBinary() |
| 81 | txid := hex.EncodeToString(txidBin)[:12] |
| 82 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 83 | r.DirtyNodes[txid] = []*node{r.node} |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 84 | r.node.MakeBranch(txid) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 85 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 86 | return txid |
| 87 | } |
| 88 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 89 | // DeleteTxBranch removes a transaction branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 90 | func (r *root) DeleteTxBranch(txid string) { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 91 | for _, dirtyNode := range r.DirtyNodes[txid] { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 92 | dirtyNode.DeleteBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 93 | } |
| 94 | delete(r.DirtyNodes, txid) |
| 95 | } |
| 96 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 97 | // FoldTxBranch will merge the contents of a transaction branch with the root object |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 98 | func (r *root) FoldTxBranch(txid string) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 99 | // Start by doing a dry run of the merge |
| 100 | // If that fails, it bails out and the branch is deleted |
| 101 | if _, err := r.node.MergeBranch(txid, true); err != nil { |
| 102 | // Merge operation fails |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 103 | r.DeleteTxBranch(txid) |
| 104 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 105 | r.node.MergeBranch(txid, false) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 106 | r.ExecuteCallbacks() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 107 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 110 | // ExecuteCallbacks will invoke all the callbacks linked to root object |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 111 | func (r *root) ExecuteCallbacks() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 112 | r.mutex.Lock() |
| 113 | log.Debugf("ExecuteCallbacks has the ROOT lock : %+v", r) |
| 114 | defer r.mutex.Unlock() |
| 115 | defer log.Debugf("ExecuteCallbacks released the ROOT lock : %+v", r) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 116 | for len(r.Callbacks) > 0 { |
| 117 | callback := r.Callbacks[0] |
| 118 | r.Callbacks = r.Callbacks[1:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 119 | callback.Execute(nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 120 | } |
| 121 | for len(r.NotificationCallbacks) > 0 { |
| 122 | callback := r.NotificationCallbacks[0] |
| 123 | r.NotificationCallbacks = r.NotificationCallbacks[1:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 124 | callback.Execute(nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 128 | func (r *root) hasCallbacks() bool { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 129 | return len(r.Callbacks) == 0 |
| 130 | } |
| 131 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 132 | // getCallbacks returns the available callbacks |
| 133 | func (r *root) GetCallbacks() []CallbackTuple { |
| 134 | r.mutex.Lock() |
| 135 | log.Debugf("getCallbacks has the ROOT lock : %+v", r) |
| 136 | defer r.mutex.Unlock() |
| 137 | defer log.Debugf("getCallbacks released the ROOT lock : %+v", r) |
| 138 | return r.Callbacks |
| 139 | } |
| 140 | |
| 141 | // getCallbacks returns the available notification callbacks |
| 142 | func (r *root) GetNotificationCallbacks() []CallbackTuple { |
| 143 | r.mutex.Lock() |
| 144 | log.Debugf("GetNotificationCallbacks has the ROOT lock : %+v", r) |
| 145 | defer r.mutex.Unlock() |
| 146 | defer log.Debugf("GetNotificationCallbacks released the ROOT lock : %+v", r) |
| 147 | return r.NotificationCallbacks |
| 148 | } |
| 149 | |
| 150 | // AddCallback inserts a new callback with its arguments |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 151 | func (r *root) AddCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 152 | r.mutex.Lock() |
| 153 | log.Debugf("AddCallback has the ROOT lock : %+v", r) |
| 154 | defer r.mutex.Unlock() |
| 155 | defer log.Debugf("AddCallback released the ROOT lock : %+v", r) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 156 | r.Callbacks = append(r.Callbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 157 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 158 | |
| 159 | // AddNotificationCallback inserts a new notification callback with its arguments |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 160 | func (r *root) AddNotificationCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 161 | r.mutex.Lock() |
| 162 | log.Debugf("AddNotificationCallback has the ROOT lock : %+v", r) |
| 163 | defer r.mutex.Unlock() |
| 164 | defer log.Debugf("AddNotificationCallback released the ROOT lock : %+v", r) |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 165 | r.NotificationCallbacks = append(r.NotificationCallbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 168 | // Update modifies the content of an object at a given path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 169 | func (r *root) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 170 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 171 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 172 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 173 | // TODO: raise error |
| 174 | } |
| 175 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 176 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 177 | // TODO: raise error |
| 178 | } |
| 179 | |
| 180 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 181 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 182 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 183 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 184 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 185 | result = r.node.Update(path, data, strict, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 186 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 187 | result = r.node.Update(path, data, strict, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 190 | r.node.GetRoot().ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 191 | |
| 192 | return result |
| 193 | } |
| 194 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 195 | // Add creates a new object at the given path with the provided data |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 196 | func (r *root) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 197 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 198 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 199 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 200 | // TODO: raise error |
| 201 | } |
| 202 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 203 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 204 | // TODO: raise error |
| 205 | } |
| 206 | |
| 207 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 208 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 209 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 210 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 211 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 212 | result = r.node.Add(path, data, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 213 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 214 | result = r.node.Add(path, data, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 217 | r.node.GetRoot().ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 218 | |
| 219 | return result |
| 220 | } |
| 221 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 222 | // Remove discards an object at a given path |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 223 | func (r *root) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 224 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 225 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 226 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 227 | // TODO: raise error |
| 228 | } |
| 229 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 230 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 231 | // TODO: raise error |
| 232 | } |
| 233 | |
| 234 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 235 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 236 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 237 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 238 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 239 | result = r.node.Remove(path, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 240 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 241 | result = r.node.Remove(path, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 244 | r.node.GetRoot().ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 245 | |
| 246 | return result |
| 247 | } |
| 248 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 249 | // Load retrieves data from a persistent storage |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 250 | func (r *root) Load(rootClass interface{}) *root { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 251 | //fakeKvStore := &Backend{} |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 252 | //root := NewRoot(rootClass, nil) |
| 253 | //root.KvStore = r.KvStore |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 254 | r.loadFromPersistence(rootClass) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 255 | return r |
| 256 | } |
| 257 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 258 | // MakeLatest updates a branch with the latest node revision |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 259 | func (r *root) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 260 | r.makeLatest(branch, revision, changeAnnouncement) |
| 261 | } |
| 262 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 263 | func (r *root) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 264 | if r.RevisionClass.(reflect.Type) == reflect.TypeOf(PersistedRevision{}) { |
| 265 | return NewPersistedRevision(branch, data, children) |
| 266 | } |
| 267 | |
| 268 | return NewNonPersistedRevision(r, branch, data, children) |
| 269 | } |
| 270 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 271 | func (r *root) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 272 | r.node.makeLatest(branch, revision, changeAnnouncement) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 273 | |
| 274 | if r.KvStore != nil && branch.Txid == "" { |
| 275 | tags := make(map[string]string) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 276 | for k, v := range r.node.Tags { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 277 | tags[k] = v.GetHash() |
| 278 | } |
| 279 | data := &rootData{ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 280 | Latest: branch.GetLatest().GetHash(), |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 281 | Tags: tags, |
| 282 | } |
| 283 | if blob, err := json.Marshal(data); err != nil { |
| 284 | // TODO report error |
| 285 | } else { |
| 286 | log.Debugf("Changing root to : %s", string(blob)) |
| 287 | if err := r.KvStore.Put("root", blob); err != nil { |
| 288 | log.Errorf("failed to properly put value in kvstore - err: %s", err.Error()) |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 294 | type rootData struct { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 295 | Latest string `json:latest` |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 296 | Tags map[string]string `json:tags` |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 299 | func (r *root) loadFromPersistence(rootClass interface{}) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 300 | var data rootData |
| 301 | |
| 302 | r.Loading = true |
| 303 | blob, _ := r.KvStore.Get("root") |
| 304 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 305 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 306 | if err := json.Unmarshal(blob.Value.([]byte), &data); err != nil { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 307 | log.Errorf("problem to unmarshal blob - error:%s\n", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 308 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 309 | stop := time.Now() |
| 310 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 311 | for tag, hash := range data.Tags { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 312 | r.node.LoadLatest(hash) |
| 313 | r.node.Tags[tag] = r.node.Latest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame^] | 316 | r.node.LoadLatest(data.Latest) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 317 | r.Loading = false |
| 318 | } |