Repository navigation
Expand file tree
/
Copy pathcompact_diff_binary.go
More file actions
260 lines (246 loc) · 8.12 KB
/
Copy pathcompact_diff_binary.go
File metadata and controls
260 lines (246 loc) · 8.12 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"bufio"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
)
const (
compactDiffBinaryMetadataLimit = 16 << 20
compactDiffBinaryMagic = "GDD\x02"
)
// WriteBinary serializes the compact report as GDD v2. The gzip container and
// HTML envelope are supplied by the report renderer. The stream is:
// magic, metadata JSON length+bytes, string-table count and length-prefixed
// UTF-8 strings, preorder node count and 8-uvarint base records, then overlay
// count and 7-uvarint sparse before/status records. String references are
// one-based (zero means absent); overlay indices are delta-coded.
func (r *compactDiffReport) WriteBinary(w io.Writer) error {
return writeCompactDiffBinary(w, r)
}
type compactDiffBinaryStrings struct {
values []string
index map[string]uint64
}
func (s *compactDiffBinaryStrings) intern(value string) uint64 {
if value == "" {
return 0
}
if ref, ok := s.index[value]; ok {
return ref
}
ref := uint64(len(s.values) + 1)
s.index[value] = ref
s.values = append(s.values, value)
return ref
}
type compactDiffBinaryFrame struct {
id, next int32
depth int
parent string
}
func writeCompactDiffBinary(w io.Writer, r *compactDiffReport) error {
if r == nil || r.root < 0 || r.root >= len(r.rows) {
return errors.New("compact diff contains an invalid root")
}
if r.Count < 1 || r.Count > maxReportNodes || len(r.rows) > maxReportNodes {
return fmt.Errorf("compact diff node count exceeds supported limit (%d)", maxReportNodes)
}
metadata := struct {
Before DiffSnapshot `json:"before"`
After DiffSnapshot `json:"after"`
Complete bool `json:"complete"`
Increased int64 `json:"increased"`
Decreased int64 `json:"decreased"`
StatusCounts map[string]int `json:"statusCounts"`
Rankings DiffRankings `json:"rankings"`
}{r.Before, r.After, r.Complete, r.Increased, r.Decreased, r.StatusCounts, r.Rankings}
metadataBytes, err := json.Marshal(metadata)
if err != nil {
return err
}
if len(metadataBytes) > compactDiffBinaryMetadataLimit {
return fmt.Errorf("compact diff metadata exceeds %d bytes", compactDiffBinaryMetadataLimit)
}
strings := compactDiffBinaryStrings{index: make(map[string]uint64)}
order := make([]int32, 0, r.Count)
stack := []compactDiffBinaryFrame{{id: int32(r.root), next: -2}}
for len(stack) > 0 {
frame := &stack[len(stack)-1]
if frame.next == -2 {
if frame.depth > compactMaxDepth {
return errors.New("compact diff output exceeds maximum depth")
}
if int(frame.id) < 0 || int(frame.id) >= len(r.rows) {
return errors.New("compact diff contains an invalid child reference")
}
row := r.rows[frame.id]
if row.before < 0 || row.after < 0 {
return errors.New("compact diff contains a negative size")
}
if row.beforeType > compactTypeDir || row.afterType > compactTypeDir {
return errors.New("compact diff contains an invalid node type")
}
if len(order) >= maxReportNodes {
return fmt.Errorf("compact diff node count exceeds supported limit (%d)", maxReportNodes)
}
order = append(order, frame.id)
strings.intern(row.name)
if makePath(frame.parent, row.name) != row.fullPath {
strings.intern(row.fullPath)
}
strings.intern(row.afterMTime)
frame.next = row.firstChild
}
for frame.next >= 0 {
if int(frame.next) >= len(r.rows) {
return errors.New("compact diff contains an invalid child reference")
}
if emitCompactDiffRow(r, frame.next) {
break
}
frame.next = r.rows[frame.next].nextSibling
}
if frame.next >= 0 {
child := frame.next
if int(child) >= len(r.rows) {
return errors.New("compact diff contains an invalid child reference")
}
frame.next = r.rows[child].nextSibling
stack = append(stack, compactDiffBinaryFrame{id: child, next: -2, depth: frame.depth + 1, parent: r.rows[frame.id].fullPath})
continue
}
stack = stack[:len(stack)-1]
}
if len(order) != r.Count {
return fmt.Errorf("compact diff emitted %d nodes, report count is %d", len(order), r.Count)
}
var overlayCount uint64
for _, id := range order {
row := r.rows[id]
if compactDiffNeedsOverlay(row) {
overlayCount++
strings.intern(row.beforeMTime)
}
}
if uint64(len(strings.values)) > uint64(maxReportNodes)*4 {
return fmt.Errorf("compact diff string table exceeds supported limit")
}
bw := bufio.NewWriterSize(w, compactDiffChunkSize)
if err := writeAll(bw, []byte(compactDiffBinaryMagic)); err != nil {
return err
}
if err := writeCompactDiffUvarint(bw, uint64(len(metadataBytes))); err != nil {
return err
}
if err := writeAll(bw, metadataBytes); err != nil {
return err
}
if err := writeCompactDiffUvarint(bw, uint64(len(strings.values))); err != nil {
return err
}
for _, value := range strings.values {
if err := writeCompactDiffUvarint(bw, uint64(len(value))); err != nil {
return err
}
if err := writeAll(bw, []byte(value)); err != nil {
return err
}
}
if err := writeCompactDiffUvarint(bw, uint64(len(order))); err != nil {
return err
}
baseStack := []compactDiffBinaryFrame{{id: int32(r.root), next: -2}}
for len(baseStack) > 0 {
frame := &baseStack[len(baseStack)-1]
if frame.next == -2 {
row := r.rows[frame.id]
pathOverride := uint64(0)
if makePath(frame.parent, row.name) != row.fullPath {
pathOverride = strings.index[row.fullPath]
}
if err := writeCompactDiffBaseRecord(bw, r, row, strings.index, pathOverride); err != nil {
return err
}
frame.next = row.firstChild
}
for frame.next >= 0 {
if int(frame.next) >= len(r.rows) {
return errors.New("compact diff contains an invalid child reference")
}
if emitCompactDiffRow(r, frame.next) {
break
}
frame.next = r.rows[frame.next].nextSibling
}
if frame.next >= 0 {
child := frame.next
frame.next = r.rows[child].nextSibling
baseStack = append(baseStack, compactDiffBinaryFrame{id: child, next: -2, parent: r.rows[frame.id].fullPath})
continue
}
baseStack = baseStack[:len(baseStack)-1]
}
if err := writeCompactDiffUvarint(bw, overlayCount); err != nil {
return err
}
var previous uint64
firstOverlay := true
for i, id := range order {
row := r.rows[id]
if !compactDiffNeedsOverlay(row) {
continue
}
index := uint64(i)
delta := index
if !firstOverlay {
delta = index - previous
}
if err := writeCompactDiffUvarint(bw, delta); err != nil {
return err
}
previous = index
firstOverlay = false
for _, value := range [...]uint64{uint64(row.beforeType), uint64(row.before), uint64(row.beforeFiles), uint64(row.beforeItems), strings.index[row.beforeMTime], uint64(row.status)} {
if err := writeCompactDiffUvarint(bw, value); err != nil {
return err
}
}
}
return bw.Flush()
}
func emitCompactDiffRow(r *compactDiffReport, id int32) bool {
return r.Complete || r.rows[id].keep
}
func compactDiffNeedsOverlay(row compactDiffRow) bool {
return row.beforeType != row.afterType || row.before != row.after || row.beforeFiles != row.afterFiles || row.beforeItems != row.afterItems || row.beforeMTime != row.afterMTime || row.status != compactStatusUnchanged
}
func writeCompactDiffBaseRecord(w io.Writer, r *compactDiffReport, row compactDiffRow, refs map[string]uint64, pathOverride uint64) error {
afterType := row.afterType
afterSize, afterFiles, afterItems, afterMTime := row.after, row.afterFiles, row.afterItems, row.afterMTime
if afterType == compactTypeUnknown {
afterSize, afterFiles, afterItems, afterMTime = 0, 0, 0, ""
}
for _, value := range [...]uint64{refs[row.name], pathOverride, uint64(afterType), uint64(afterSize), uint64(afterFiles), uint64(afterItems), refs[afterMTime], uint64(compactDiffEmittedChildren(r, row))} {
if err := writeCompactDiffUvarint(w, value); err != nil {
return err
}
}
return nil
}
func compactDiffEmittedChildren(r *compactDiffReport, row compactDiffRow) uint64 {
var count uint64
for child := row.firstChild; child >= 0; child = r.rows[child].nextSibling {
if emitCompactDiffRow(r, child) {
count++
}
}
return count
}
func writeCompactDiffUvarint(w io.Writer, value uint64) error {
var buf [binary.MaxVarintLen64]byte
n := binary.PutUvarint(buf[:], value)
return writeAll(w, buf[:n])
}