-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.go
145 lines (125 loc) · 2.87 KB
/
serialization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package segment
import (
"bufio"
"bytes"
"io"
"time"
"github.com/taoey/pyroscope/pkg/util/serialization"
"github.com/taoey/pyroscope/pkg/util/varint"
)
// serialization format version. it's not very useful right now, but it will be in the future
const currentVersion = 1
func (s *Segment) populateFromMetadata(metadata map[string]interface{}) {
if v, ok := metadata["sampleRate"]; ok {
s.sampleRate = int(v.(float64))
}
if v, ok := metadata["spyName"]; ok {
s.spyName = v.(string)
}
}
func (s *Segment) generateMetadata() map[string]interface{} {
return map[string]interface{}{
"sampleRate": s.sampleRate,
"spyName": s.spyName,
}
}
func (s *Segment) Serialize(w io.Writer) error {
s.m.RLock()
defer s.m.RUnlock()
varint.Write(w, currentVersion)
serialization.WriteMetadata(w, s.generateMetadata())
nodes := []*streeNode{s.root}
for len(nodes) > 0 {
n := nodes[0]
varint.Write(w, uint64(n.depth))
varint.Write(w, uint64(n.time.Unix()))
varint.Write(w, n.samples)
p := uint64(0)
if n.present {
p = 1
}
varint.Write(w, p)
nodes = nodes[1:]
// depth
// time
// keyInChunks
// children
l := 0
r := []*streeNode{}
for _, v := range n.children {
if v != nil {
l++
r = append(r, v)
}
}
varint.Write(w, uint64(l))
nodes = append(r, nodes...)
}
return nil
}
func Deserialize(resolution time.Duration, multiplier int, r io.Reader) (*Segment, error) {
s := New(resolution, multiplier)
br := bufio.NewReader(r) // TODO if it's already a bytereader skip
// reads serialization format version, see comment at the top
_, err := varint.Read(br)
if err != nil {
return nil, err
}
metadata, err := serialization.ReadMetadata(br)
if err != nil {
return nil, err
}
s.populateFromMetadata(metadata)
parents := []*streeNode{nil}
for len(parents) > 0 {
depth, err := varint.Read(br)
if err != nil {
return nil, err
}
timeVal, err := varint.Read(br)
if err != nil {
return nil, err
}
samplesVal, err := varint.Read(br)
if err != nil {
return nil, err
}
presentVal, err := varint.Read(br)
if err != nil {
return nil, err
}
node := newNode(time.Unix(int64(timeVal), 0), int(depth), s.multiplier)
if presentVal == 1 {
node.present = true
}
node.samples = samplesVal
if s.root == nil {
s.root = node
}
parent := parents[0]
parents = parents[1:]
if parent != nil {
parent.replace(node)
}
childrenLen, err := varint.Read(br)
if err != nil {
return nil, err
}
r := []*streeNode{}
for i := 0; i < int(childrenLen); i++ {
r = append(r, node)
}
parents = append(r, parents...)
}
return s, nil
}
func (t *Segment) Bytes() []byte {
b := bytes.Buffer{}
t.Serialize(&b)
return b.Bytes()
}
func FromBytes(resolution time.Duration, multiplier int, p []byte) *Segment {
// TODO: handle error
t, _ := Deserialize(resolution, multiplier, bytes.NewReader(p))
return t
}