-
Notifications
You must be signed in to change notification settings - Fork 5
/
record.go
173 lines (143 loc) · 2.9 KB
/
record.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package recording
import (
"encoding/json"
"runtime"
"sync"
"time"
)
type progressCallback func(done int)
type Record struct {
sync.Mutex
Head []byte `json:"data"`
Patches []patch `json:"states"`
current []byte
numPatches int
index int
frames []frame
progress progressCallback
}
func NewRecord(progress progressCallback) *Record {
return &Record{
Head: nil,
current: nil,
Patches: make([]patch, 0),
numPatches: 0,
index: 0,
frames: nil,
progress: progress,
}
}
func (e *Record) addPatch(p patch) {
e.Patches = append(e.Patches, p)
e.numPatches++
e.index = 0
}
func (e *Record) AddState(state []byte) error {
e.Lock()
defer e.Unlock()
// set reference state
if e.Head == nil {
e.Head = state
} else if err, p := doDiff(e.current, state); err != nil {
return err
} else {
e.addPatch(p)
}
e.current = state
return nil
}
func (e *Record) Reset() {
e.Lock()
defer e.Unlock()
e.current = e.Head
e.numPatches = len(e.Patches)
e.index = 0
}
func (e *Record) addFrameAt(idx int, f frame) {
e.current = f
e.frames[idx] = e.current
e.progress(1)
}
func (e *Record) Compile() error {
e.Lock()
defer e.Unlock()
defer func() {
// after generating the frames, free as much memory as possible
runtime.GC()
}()
// reset the state
e.current = e.Head
e.numPatches = len(e.Patches)
e.index = 0
e.frames = make([]frame, e.numPatches+1)
// first is the master frame
e.frames[0] = frame(e.Head)
// precompute frames so they can be accessed by index
for i := 0; i < e.numPatches; i++ {
if err, newFrame := doPatch(e.current, e.Patches[i]); err != nil {
return err
} else {
e.addFrameAt(i+1, newFrame)
}
}
e.progress(1)
return nil
}
func (e *Record) OnProgress(cb progressCallback) {
e.Lock()
defer e.Unlock()
e.numPatches = len(e.Patches)
e.progress = cb
}
func (e *Record) Frames() int {
e.Lock()
defer e.Unlock()
// master + sub states
return e.numPatches + 1
}
func (e *Record) Index() int {
e.Lock()
defer e.Unlock()
return e.index + 1
}
func (e *Record) SetFrom(from int) {
e.Lock()
defer e.Unlock()
e.index = from
}
func (e *Record) Over() bool {
e.Lock()
defer e.Unlock()
return e.index > e.numPatches
}
func (e *Record) Next() []byte {
e.Lock()
defer e.Unlock()
cur := e.index
e.index++
return e.frames[cur]
}
func (e *Record) TimeOf(idx int) time.Time {
e.Lock()
defer e.Unlock()
buf := e.frames[idx]
frame := make(map[string]interface{})
if err := json.Unmarshal(buf, &frame); err != nil {
panic(err)
return time.Time{}
} else if tm, err := time.Parse(time.RFC3339, frame["polled_at"].(string)); err != nil {
panic(err)
return time.Time{}
} else {
return tm
}
}
func (e *Record) StartedAt() time.Time {
return e.TimeOf(0)
}
func (e *Record) StoppedAt() time.Time {
return e.TimeOf(e.numPatches)
}
func (e *Record) Duration() time.Duration {
return e.StoppedAt().Sub(e.StartedAt())
}