blob: f390e0b0a8aa786899e72dbc77c27b5db4b7a3d2 [file] [log] [blame]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -04001package model
2
3import (
4 "bytes"
5 "compress/gzip"
6 "encoding/json"
7 "fmt"
8 "github.com/golang/protobuf/proto"
9 "io/ioutil"
10 "reflect"
11)
12
13type PersistedRevision struct {
14 *Revision
15 Compress bool
16 kvStore *Backend
17}
18
19func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]*Revision) *PersistedRevision {
20 pr := &PersistedRevision{}
21 pr.kvStore = branch.node.root.KvStore
22 pr.Revision = NewRevision(branch, data, children)
23 return pr
24}
25
26func (pr *PersistedRevision) finalize() {
27 pr.Revision.finalize()
28 pr.store()
29}
30
31type revData struct {
32 Children map[string][]string
33 Config string
34}
35
36func (pr *PersistedRevision) store() {
37 if ok, _ := pr.kvStore.Get(pr.Revision.Hash); ok != nil {
38 return
39 }
40
41 pr.storeConfig()
42
43 childrenHashes := make(map[string][]string)
44 for fieldName, children := range pr.Children {
45 hashes := []string{}
46 for _, rev := range children {
47 hashes = append(hashes, rev.Hash)
48 }
49 childrenHashes[fieldName] = hashes
50 }
51 data := &revData{
52 Children: childrenHashes,
53 Config: pr.Config.Hash,
54 }
55 if blob, err := json.Marshal(data); err != nil {
56 // TODO report error
57 } else {
58 if pr.Compress {
59 var b bytes.Buffer
60 w := gzip.NewWriter(&b)
61 w.Write(blob)
62 w.Close()
63 blob = b.Bytes()
64 }
65 pr.kvStore.Put(pr.Hash, blob)
66 }
67}
68
69func (pr *PersistedRevision) load(branch *Branch, kvStore *Backend, msgClass interface{}, hash string) *PersistedRevision {
70 blob, _ := kvStore.Get(hash)
71 output := blob.Value.([]byte)
72 var data revData
73 if pr.Compress {
74 b := bytes.NewBuffer(blob.Value.([]byte))
75 if r, err := gzip.NewReader(b); err != nil {
76 // TODO : report error
77 } else {
78 if output, err = ioutil.ReadAll(r); err != nil {
79 // TODO report error
80 }
81 }
82 }
83 if err := json.Unmarshal(output, &data); err != nil {
84 fmt.Errorf("problem to unmarshal data - %s", err.Error())
85 }
86
87 configHash := data.Config
88 configData := pr.loadConfig(kvStore, msgClass, configHash)
89 assembledChildren := make(map[string][]*Revision)
90
91 childrenHashes := data.Children
92 node := branch.node
93 for fieldName, child := range ChildrenFields(msgClass) {
94 var children []*Revision
95 for _, childHash := range childrenHashes[fieldName] {
96 //fmt.Printf("child class type: %+v", reflect.New(n).Elem().Interface())
97 childNode := node.makeNode(reflect.New(child.ClassType).Elem().Interface(), "")
98 childNode.LoadLatest(kvStore, childHash)
99 childRev := childNode.Latest()
100 children = append(children, childRev)
101 }
102 assembledChildren[fieldName] = children
103 }
104 rev := NewPersistedRevision(branch, configData, assembledChildren)
105 return rev
106}
107
108func (pr *PersistedRevision) storeConfig() {
109 if ok, _ := pr.kvStore.Get(pr.Config.Hash); ok != nil {
110 return
111 }
112 if blob, err := proto.Marshal(pr.Config.Data.(proto.Message)); err != nil {
113 // TODO report error
114 } else {
115 if pr.Compress {
116 var b bytes.Buffer
117 w := gzip.NewWriter(&b)
118 w.Write(blob)
119 w.Close()
120 blob = b.Bytes()
121 }
122 pr.kvStore.Put(pr.Config.Hash, blob)
123 }
124}
125
126func (pr *PersistedRevision) loadConfig(kvStore *Backend, msgClass interface{}, hash string) interface{} {
127 blob, _ := kvStore.Get(hash)
128 output := blob.Value.([]byte)
129
130 if pr.Compress {
131 b := bytes.NewBuffer(blob.Value.([]byte))
132 if r, err := gzip.NewReader(b); err != nil {
133 // TODO : report error
134 } else {
135 if output, err = ioutil.ReadAll(r); err != nil {
136 // TODO report error
137 }
138 }
139 }
140
141 var data reflect.Value
142 if msgClass != nil {
143 data = reflect.New(reflect.TypeOf(msgClass).Elem())
144 if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil {
145 // TODO report error
146 }
147 }
148
149 return data.Interface()
150}