forked from sourcegraph/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
marshal.go
199 lines (173 loc) · 4.07 KB
/
marshal.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
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
package zoekt
import (
"bytes"
"encoding/binary"
"fmt"
"unsafe"
)
// Wire-format of map[uint32]MinimalRepoListEntry is pretty straightforward:
//
// byte(2) version
// uvarint(len(minimal))
// uvarint(sum(len(entry.Branches) for entry in minimal))
// for repoID, entry in minimal:
// uvarint(repoID)
// byte(entry.HasSymbols)
// uvarint(entry.IndexTimeUnix)
// uvarint(len(entry.Branches))
// for b in entry.Branches:
// str(b.Name)
// str(b.Version)
//
// Version 1 was the same, except it didn't have the IndexTimeUnix field.
// reposMapEncode implements an efficient encoder for ReposMap.
func reposMapEncode(minimal ReposMap) ([]byte, error) {
if minimal == nil {
return nil, nil
}
var b bytes.Buffer
var enc [binary.MaxVarintLen64]byte
varint := func(n int) {
m := binary.PutUvarint(enc[:], uint64(n))
b.Write(enc[:m])
}
str := func(s string) {
varint(len(s))
b.WriteString(s)
}
strSize := func(s string) int {
return binary.PutUvarint(enc[:], uint64(len(s))) + len(s)
}
// We calculate this up front so when decoding we only need to allocate the
// underlying array once.
allBranchesLen := 0
for _, entry := range minimal {
allBranchesLen += len(entry.Branches)
}
// Calculate size
size := 1 // version
size += binary.PutUvarint(enc[:], uint64(len(minimal)))
size += binary.PutUvarint(enc[:], uint64(allBranchesLen))
for repoID, entry := range minimal {
size += binary.PutUvarint(enc[:], uint64(repoID))
size += 1 // HasSymbols
size += binary.PutUvarint(enc[:], uint64(entry.IndexTimeUnix))
size += binary.PutUvarint(enc[:], uint64(len(entry.Branches)))
for _, b := range entry.Branches {
size += strSize(b.Name)
size += strSize(b.Version)
}
}
b.Grow(size)
// Version
b.WriteByte(2)
// Length
varint(len(minimal))
varint(allBranchesLen)
for repoID, entry := range minimal {
varint(int(repoID))
hasSymbols := byte(1)
if !entry.HasSymbols {
hasSymbols = 0
}
b.WriteByte(hasSymbols)
varint(int(entry.IndexTimeUnix))
varint(len(entry.Branches))
for _, b := range entry.Branches {
str(b.Name)
str(b.Version)
}
}
return b.Bytes(), nil
}
// reposMapDecode implements an efficient decoder for map[string]struct{}.
func reposMapDecode(b []byte) (ReposMap, error) {
// nil input
if len(b) == 0 {
return nil, nil
}
// binaryReader returns strings pointing into b to avoid allocations. We
// don't own b, so we create a copy of it.
r := binaryReader{
typ: "ReposMap",
b: append([]byte{}, b...),
}
// Version
var readIndexTime bool
v := r.byt()
switch v {
case 1:
readIndexTime = false
case 2:
readIndexTime = true
default:
return nil, fmt.Errorf("unsupported stringSet encoding version %d", v)
}
// Length
l := r.uvarint()
m := make(map[uint32]MinimalRepoListEntry, l)
// Pre-allocate slice for all branches
allBranchesLen := r.uvarint()
allBranches := make([]RepositoryBranch, 0, allBranchesLen)
for i := 0; i < l; i++ {
repoID := r.uvarint()
hasSymbols := r.byt() == 1
var indexTimeUnix int64
if readIndexTime {
indexTimeUnix = int64(r.uvarint())
}
lb := r.uvarint()
for i := 0; i < lb; i++ {
allBranches = append(allBranches, RepositoryBranch{
Name: r.str(),
Version: r.str(),
})
}
branches := allBranches[len(allBranches)-lb:]
m[uint32(repoID)] = MinimalRepoListEntry{
HasSymbols: hasSymbols,
Branches: branches,
IndexTimeUnix: indexTimeUnix,
}
}
return m, r.err
}
type binaryReader struct {
typ string
b []byte
err error
}
func (b *binaryReader) uvarint() int {
x, n := binary.Uvarint(b.b)
if n < 0 {
b.b = nil
b.err = fmt.Errorf("malformed %s", b.typ)
return 0
}
b.b = b.b[n:]
return int(x)
}
func (b *binaryReader) str() string {
l := b.uvarint()
if l > len(b.b) {
b.b = nil
b.err = fmt.Errorf("malformed %s", b.typ)
return ""
}
s := b2s(b.b[:l])
b.b = b.b[l:]
return s
}
func (b *binaryReader) byt() byte {
if len(b.b) < 1 {
b.b = nil
b.err = fmt.Errorf("malformed %s", b.typ)
return 0
}
x := b.b[0]
b.b = b.b[1:]
return x
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}