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 | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "github.com/google/uuid" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 26 | "sync" |
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 | |
| 33 | ExecuteCallbacks() |
| 34 | AddCallback(callback CallbackFunction, args ...interface{}) |
| 35 | AddNotificationCallback(callback CallbackFunction, args ...interface{}) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 38 | // 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] | 39 | type root struct { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 40 | *node |
| 41 | |
| 42 | Callbacks []CallbackTuple |
| 43 | NotificationCallbacks []CallbackTuple |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 44 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 45 | DirtyNodes map[string][]*node |
| 46 | KvStore *Backend |
| 47 | Loading bool |
| 48 | RevisionClass interface{} |
| 49 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 50 | mutex sync.RWMutex |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | // NewRoot creates an new instance of a root object |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 54 | func NewRoot(initialData interface{}, kvStore *Backend) *root { |
| 55 | root := &root{} |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 56 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 57 | root.KvStore = kvStore |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 58 | root.DirtyNodes = make(map[string][]*node) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 59 | root.Loading = false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 60 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 61 | // If there is no storage in place just revert to |
| 62 | // a non persistent mechanism |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 63 | if kvStore != nil { |
| 64 | root.RevisionClass = reflect.TypeOf(PersistedRevision{}) |
| 65 | } else { |
| 66 | root.RevisionClass = reflect.TypeOf(NonPersistedRevision{}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 68 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 69 | root.Callbacks = []CallbackTuple{} |
| 70 | root.NotificationCallbacks = []CallbackTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 71 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 72 | root.node = NewNode(root, initialData, false, "") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 73 | |
| 74 | return root |
| 75 | } |
| 76 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 77 | // MakeTxBranch creates a new transaction branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 78 | func (r *root) MakeTxBranch() string { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 79 | txidBin, _ := uuid.New().MarshalBinary() |
| 80 | txid := hex.EncodeToString(txidBin)[:12] |
| 81 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 82 | r.DirtyNodes[txid] = []*node{r.node} |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 83 | r.node.MakeBranch(txid) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 85 | return txid |
| 86 | } |
| 87 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | // DeleteTxBranch removes a transaction branch |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 89 | func (r *root) DeleteTxBranch(txid string) { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 90 | for _, dirtyNode := range r.DirtyNodes[txid] { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 91 | dirtyNode.DeleteBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 92 | } |
| 93 | delete(r.DirtyNodes, txid) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 94 | r.node.DeleteBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 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 | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 107 | r.DeleteTxBranch(txid) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 108 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | // ExecuteCallbacks will invoke all the callbacks linked to root object |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 112 | func (r *root) ExecuteCallbacks() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 113 | r.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | defer r.mutex.Unlock() |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 115 | |
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:] |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 119 | go callback.Execute(nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 120 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 121 | //for len(r.NotificationCallbacks) > 0 { |
| 122 | // callback := r.NotificationCallbacks[0] |
| 123 | // r.NotificationCallbacks = r.NotificationCallbacks[1:] |
| 124 | // go callback.Execute(nil) |
| 125 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 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() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 135 | defer r.mutex.Unlock() |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 136 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 137 | return r.Callbacks |
| 138 | } |
| 139 | |
| 140 | // getCallbacks returns the available notification callbacks |
| 141 | func (r *root) GetNotificationCallbacks() []CallbackTuple { |
| 142 | r.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 143 | defer r.mutex.Unlock() |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 144 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 145 | return r.NotificationCallbacks |
| 146 | } |
| 147 | |
| 148 | // AddCallback inserts a new callback with its arguments |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 149 | func (r *root) AddCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 150 | r.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 151 | defer r.mutex.Unlock() |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 152 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 153 | r.Callbacks = append(r.Callbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 154 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 155 | |
| 156 | // AddNotificationCallback inserts a new notification callback with its arguments |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 157 | func (r *root) AddNotificationCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 158 | r.mutex.Lock() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 159 | defer r.mutex.Unlock() |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 160 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 161 | r.NotificationCallbacks = append(r.NotificationCallbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 164 | func (r *root) syncParent(childRev Revision, txid string) { |
| 165 | data := proto.Clone(r.Proxy.ParentNode.Latest().GetData().(proto.Message)) |
| 166 | |
| 167 | for fieldName, _ := range ChildrenFields(data) { |
| 168 | childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0) |
| 169 | if reflect.TypeOf(childRev.GetData()) == reflect.TypeOf(childDataHolder.Interface()) { |
| 170 | childDataHolder = reflect.ValueOf(childRev.GetData()) |
| 171 | reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | r.Proxy.ParentNode.Latest().SetConfig(NewDataRevision(r.Proxy.ParentNode.Root, data)) |
| 176 | r.Proxy.ParentNode.Latest(txid).Finalize(false) |
| 177 | } |
| 178 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 179 | // 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] | 180 | 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] | 181 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 182 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 183 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 184 | // TODO: raise error |
| 185 | } |
| 186 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 187 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 188 | // TODO: raise error |
| 189 | } |
| 190 | |
| 191 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 192 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 193 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 194 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 195 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 196 | result = r.node.Update(path, data, strict, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 197 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 198 | result = r.node.Update(path, data, strict, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 201 | if result != nil { |
| 202 | if r.Proxy.FullPath != r.Proxy.Path { |
| 203 | r.syncParent(result, txid) |
| 204 | } else { |
| 205 | result.Finalize(false) |
| 206 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 209 | r.node.GetRoot().ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 210 | |
| 211 | return result |
| 212 | } |
| 213 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 214 | // 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] | 215 | 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] | 216 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 217 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 218 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 219 | // TODO: raise error |
| 220 | } |
| 221 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 222 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 223 | // TODO: raise error |
| 224 | } |
| 225 | |
| 226 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 227 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 228 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 229 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 230 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 231 | result = r.node.Add(path, data, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 232 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 233 | result = r.node.Add(path, data, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 236 | if result != nil { |
| 237 | result.Finalize(true) |
| 238 | r.node.GetRoot().ExecuteCallbacks() |
| 239 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 240 | return result |
| 241 | } |
| 242 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 243 | // Remove discards an object at a given path |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 244 | func (r *root) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 245 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 246 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 247 | if makeBranch != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 248 | // TODO: raise error |
| 249 | } |
| 250 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 251 | if r.hasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 252 | // TODO: raise error |
| 253 | } |
| 254 | |
| 255 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 256 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 257 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 258 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 259 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 260 | result = r.node.Remove(path, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 261 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 262 | result = r.node.Remove(path, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 265 | r.node.GetRoot().ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 266 | |
| 267 | return result |
| 268 | } |
| 269 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 270 | // MakeLatest updates a branch with the latest node revision |
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.makeLatest(branch, revision, changeAnnouncement) |
| 273 | } |
| 274 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 275 | func (r *root) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
| 276 | if r.RevisionClass.(reflect.Type) == reflect.TypeOf(PersistedRevision{}) { |
| 277 | return NewPersistedRevision(branch, data, children) |
| 278 | } |
| 279 | |
| 280 | return NewNonPersistedRevision(r, branch, data, children) |
| 281 | } |
| 282 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 283 | func (r *root) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 284 | r.node.makeLatest(branch, revision, changeAnnouncement) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 285 | |
| 286 | if r.KvStore != nil && branch.Txid == "" { |
| 287 | tags := make(map[string]string) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 288 | for k, v := range r.node.Tags { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 289 | tags[k] = v.GetHash() |
| 290 | } |
| 291 | data := &rootData{ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 292 | Latest: branch.GetLatest().GetHash(), |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 293 | Tags: tags, |
| 294 | } |
| 295 | if blob, err := json.Marshal(data); err != nil { |
| 296 | // TODO report error |
| 297 | } else { |
| 298 | log.Debugf("Changing root to : %s", string(blob)) |
| 299 | if err := r.KvStore.Put("root", blob); err != nil { |
| 300 | log.Errorf("failed to properly put value in kvstore - err: %s", err.Error()) |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 306 | type rootData struct { |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 307 | Latest string `json:"latest"` |
| 308 | Tags map[string]string `json:"tags"` |
| 309 | } |