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