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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "compress/gzip" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "github.com/golang/protobuf/proto" |
| 24 | "io/ioutil" |
| 25 | "reflect" |
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 | |
| 29 | type PersistedRevision struct { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 30 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | Compress bool |
| 32 | kvStore *Backend |
| 33 | } |
| 34 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | pr := &PersistedRevision{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 37 | pr.kvStore = branch.Node.root.KvStore |
| 38 | pr.Revision = NewNonPersistedRevision(branch, data, children) |
| 39 | pr.Finalize() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 40 | return pr |
| 41 | } |
| 42 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 43 | func (pr *PersistedRevision) Finalize() { |
| 44 | //pr.Revision.Finalize() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 45 | pr.store() |
| 46 | } |
| 47 | |
| 48 | type revData struct { |
| 49 | Children map[string][]string |
| 50 | Config string |
| 51 | } |
| 52 | |
| 53 | func (pr *PersistedRevision) store() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 54 | if ok, _ := pr.kvStore.Get(pr.Revision.GetHash()); ok != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | return |
| 56 | } |
| 57 | |
| 58 | pr.storeConfig() |
| 59 | |
| 60 | childrenHashes := make(map[string][]string) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | for fieldName, children := range pr.GetChildren() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 62 | hashes := []string{} |
| 63 | for _, rev := range children { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | hashes = append(hashes, rev.GetHash()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | } |
| 66 | childrenHashes[fieldName] = hashes |
| 67 | } |
| 68 | data := &revData{ |
| 69 | Children: childrenHashes, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 70 | Config: pr.GetConfig().Hash, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 71 | } |
| 72 | if blob, err := json.Marshal(data); err != nil { |
| 73 | // TODO report error |
| 74 | } else { |
| 75 | if pr.Compress { |
| 76 | var b bytes.Buffer |
| 77 | w := gzip.NewWriter(&b) |
| 78 | w.Write(blob) |
| 79 | w.Close() |
| 80 | blob = b.Bytes() |
| 81 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 82 | pr.kvStore.Put(pr.GetHash(), blob) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 86 | func (pr *PersistedRevision) Load(branch *Branch, kvStore *Backend, msgClass interface{}, hash string) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 87 | blob, _ := kvStore.Get(hash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 88 | |
| 89 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | output := blob.Value.([]byte) |
| 91 | var data revData |
| 92 | if pr.Compress { |
| 93 | b := bytes.NewBuffer(blob.Value.([]byte)) |
| 94 | if r, err := gzip.NewReader(b); err != nil { |
| 95 | // TODO : report error |
| 96 | } else { |
| 97 | if output, err = ioutil.ReadAll(r); err != nil { |
| 98 | // TODO report error |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | if err := json.Unmarshal(output, &data); err != nil { |
| 103 | fmt.Errorf("problem to unmarshal data - %s", err.Error()) |
| 104 | } |
| 105 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 106 | stop := time.Now() |
| 107 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 108 | configHash := data.Config |
| 109 | configData := pr.loadConfig(kvStore, msgClass, configHash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 110 | |
| 111 | assembledChildren := make(map[string][]Revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 112 | |
| 113 | childrenHashes := data.Children |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 114 | node := branch.Node |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 115 | for fieldName, child := range ChildrenFields(msgClass) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 116 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 117 | for _, childHash := range childrenHashes[fieldName] { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 118 | childNode := node.MakeNode(reflect.New(child.ClassType).Elem().Interface(), "") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 119 | childNode.LoadLatest(kvStore, childHash) |
| 120 | childRev := childNode.Latest() |
| 121 | children = append(children, childRev) |
| 122 | } |
| 123 | assembledChildren[fieldName] = children |
| 124 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 125 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 126 | rev := NewPersistedRevision(branch, configData, assembledChildren) |
| 127 | return rev |
| 128 | } |
| 129 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 130 | func (pr *PersistedRevision) assignValue(a, b Revision) Revision { |
| 131 | a = b |
| 132 | return a |
| 133 | } |
| 134 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 135 | func (pr *PersistedRevision) storeConfig() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 136 | if ok, _ := pr.kvStore.Get(pr.GetConfig().Hash); ok != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 137 | return |
| 138 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 139 | if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 140 | // TODO report error |
| 141 | } else { |
| 142 | if pr.Compress { |
| 143 | var b bytes.Buffer |
| 144 | w := gzip.NewWriter(&b) |
| 145 | w.Write(blob) |
| 146 | w.Close() |
| 147 | blob = b.Bytes() |
| 148 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 149 | pr.kvStore.Put(pr.GetConfig().Hash, blob) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | func (pr *PersistedRevision) loadConfig(kvStore *Backend, msgClass interface{}, hash string) interface{} { |
| 154 | blob, _ := kvStore.Get(hash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 155 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 156 | output := blob.Value.([]byte) |
| 157 | |
| 158 | if pr.Compress { |
| 159 | b := bytes.NewBuffer(blob.Value.([]byte)) |
| 160 | if r, err := gzip.NewReader(b); err != nil { |
| 161 | // TODO : report error |
| 162 | } else { |
| 163 | if output, err = ioutil.ReadAll(r); err != nil { |
| 164 | // TODO report error |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | var data reflect.Value |
| 170 | if msgClass != nil { |
| 171 | data = reflect.New(reflect.TypeOf(msgClass).Elem()) |
| 172 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
| 173 | // TODO report error |
| 174 | } |
| 175 | } |
| 176 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 177 | stop := time.Now() |
| 178 | |
| 179 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 180 | return data.Interface() |
| 181 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 182 | |
| 183 | func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
| 184 | newNPR := pr.Revision.UpdateData(data, branch) |
| 185 | |
| 186 | newPR := &PersistedRevision{ |
| 187 | Revision: newNPR, |
| 188 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame^] | 189 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | newPR.Finalize() |
| 193 | |
| 194 | return newPR |
| 195 | } |
| 196 | |
| 197 | func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
| 198 | newNPR := pr.Revision.UpdateChildren(name, children, branch) |
| 199 | |
| 200 | newPR := &PersistedRevision{ |
| 201 | Revision: newNPR, |
| 202 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame^] | 203 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | newPR.Finalize() |
| 207 | |
| 208 | return newPR |
| 209 | } |
| 210 | |
| 211 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
| 212 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 213 | |
| 214 | newPR := &PersistedRevision{ |
| 215 | Revision: newNPR, |
| 216 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame^] | 217 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | newPR.Finalize() |
| 221 | |
| 222 | return newPR |
| 223 | } |