-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
366 lines (336 loc) · 9.02 KB
/
set.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package pin
import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"hash/fnv"
"io"
"sort"
"unsafe"
"gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
"github.com/ipfs/go-ipfs/blocks/key"
"github.com/ipfs/go-ipfs/merkledag"
"github.com/ipfs/go-ipfs/pin/internal/pb"
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)
const (
defaultFanout = 256
maxItems = 8192
)
func randomSeed() (uint32, error) {
var buf [4]byte
if _, err := rand.Read(buf[:]); err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(buf[:]), nil
}
func hash(seed uint32, k key.Key) uint32 {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], seed)
h := fnv.New32a()
_, _ = h.Write(buf[:])
_, _ = io.WriteString(h, string(k))
return h.Sum32()
}
type itemIterator func() (k key.Key, data []byte, ok bool)
type keyObserver func(key.Key)
// refcount is the marshaled format of refcounts. It may change
// between versions; this is valid for version 1. Changing it may
// become desirable if there are many links with refcount > 255.
//
// There are two guarantees that need to be preserved, if this is
// changed:
//
// - the marshaled format is of fixed size, matching
// unsafe.Sizeof(refcount(0))
// - methods of refcount handle endianness, and may
// in later versions need encoding/binary.
type refcount uint8
func (r refcount) Bytes() []byte {
return []byte{byte(r)}
}
// readRefcount returns the idx'th refcount in []byte, which is
// assumed to be a sequence of refcount.Bytes results.
func (r *refcount) ReadFromIdx(buf []byte, idx int) {
*r = refcount(buf[idx])
}
type sortByHash struct {
links []*merkledag.Link
data []byte
}
func (s sortByHash) Len() int {
return len(s.links)
}
func (s sortByHash) Less(a, b int) bool {
return bytes.Compare(s.links[a].Hash, s.links[b].Hash) == -1
}
func (s sortByHash) Swap(a, b int) {
s.links[a], s.links[b] = s.links[b], s.links[a]
if len(s.data) != 0 {
const n = int(unsafe.Sizeof(refcount(0)))
tmp := make([]byte, n)
copy(tmp, s.data[a*n:a*n+n])
copy(s.data[a*n:a*n+n], s.data[b*n:b*n+n])
copy(s.data[b*n:b*n+n], tmp)
}
}
func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint64, iter itemIterator, internalKeys keyObserver) (*merkledag.Node, error) {
seed, err := randomSeed()
if err != nil {
return nil, err
}
n := &merkledag.Node{
Links: make([]*merkledag.Link, 0, defaultFanout+maxItems),
}
for i := 0; i < defaultFanout; i++ {
n.Links = append(n.Links, &merkledag.Link{Hash: emptyKey.ToMultihash()})
}
internalKeys(emptyKey)
hdr := &pb.Set{
Version: proto.Uint32(1),
Fanout: proto.Uint32(defaultFanout),
Seed: proto.Uint32(seed),
}
if err := writeHdr(n, hdr); err != nil {
return nil, err
}
hdrLen := len(n.Data)
if estimatedLen < maxItems {
// it'll probably fit
for i := 0; i < maxItems; i++ {
k, data, ok := iter()
if !ok {
// all done
break
}
n.Links = append(n.Links, &merkledag.Link{Hash: k.ToMultihash()})
n.Data = append(n.Data, data...)
}
// sort by hash, also swap item Data
s := sortByHash{
links: n.Links[defaultFanout:],
data: n.Data[hdrLen:],
}
sort.Stable(s)
}
// wasteful but simple
type item struct {
k key.Key
data []byte
}
hashed := make(map[uint32][]item)
for {
k, data, ok := iter()
if !ok {
break
}
h := hash(seed, k)
hashed[h] = append(hashed[h], item{k, data})
}
for h, items := range hashed {
childIter := func() (k key.Key, data []byte, ok bool) {
if len(items) == 0 {
return "", nil, false
}
first := items[0]
items = items[1:]
return first.k, first.data, true
}
child, err := storeItems(ctx, dag, uint64(len(items)), childIter, internalKeys)
if err != nil {
return nil, err
}
size, err := child.Size()
if err != nil {
return nil, err
}
childKey, err := dag.Add(child)
if err != nil {
return nil, err
}
internalKeys(childKey)
l := &merkledag.Link{
Name: "",
Hash: childKey.ToMultihash(),
Size: size,
Node: child,
}
n.Links[int(h%defaultFanout)] = l
}
return n, nil
}
func readHdr(n *merkledag.Node) (*pb.Set, []byte, error) {
hdrLenRaw, consumed := binary.Uvarint(n.Data)
if consumed <= 0 {
return nil, nil, errors.New("invalid Set header length")
}
buf := n.Data[consumed:]
if hdrLenRaw > uint64(len(buf)) {
return nil, nil, errors.New("impossibly large Set header length")
}
// as hdrLenRaw was <= an int, we now know it fits in an int
hdrLen := int(hdrLenRaw)
var hdr pb.Set
if err := proto.Unmarshal(buf[:hdrLen], &hdr); err != nil {
return nil, nil, err
}
buf = buf[hdrLen:]
if v := hdr.GetVersion(); v != 1 {
return nil, nil, fmt.Errorf("unsupported Set version: %d", v)
}
if uint64(hdr.GetFanout()) > uint64(len(n.Links)) {
return nil, nil, errors.New("impossibly large Fanout")
}
return &hdr, buf, nil
}
func writeHdr(n *merkledag.Node, hdr *pb.Set) error {
hdrData, err := proto.Marshal(hdr)
if err != nil {
return err
}
n.Data = make([]byte, binary.MaxVarintLen64, binary.MaxVarintLen64+len(hdrData))
written := binary.PutUvarint(n.Data, uint64(len(hdrData)))
n.Data = n.Data[:written]
n.Data = append(n.Data, hdrData...)
return nil
}
type walkerFunc func(buf []byte, idx int, link *merkledag.Link) error
func walkItems(ctx context.Context, dag merkledag.DAGService, n *merkledag.Node, fn walkerFunc, children keyObserver) error {
hdr, buf, err := readHdr(n)
if err != nil {
return err
}
// readHdr guarantees fanout is a safe value
fanout := hdr.GetFanout()
for i, l := range n.Links[fanout:] {
if err := fn(buf, i, l); err != nil {
return err
}
}
for _, l := range n.Links[:fanout] {
children(key.Key(l.Hash))
if key.Key(l.Hash) == emptyKey {
continue
}
subtree, err := l.GetNode(ctx, dag)
if err != nil {
return err
}
if err := walkItems(ctx, dag, subtree, fn, children); err != nil {
return err
}
}
return nil
}
func loadSet(ctx context.Context, dag merkledag.DAGService, root *merkledag.Node, name string, internalKeys keyObserver) ([]key.Key, error) {
l, err := root.GetNodeLink(name)
if err != nil {
return nil, err
}
internalKeys(key.Key(l.Hash))
n, err := l.GetNode(ctx, dag)
if err != nil {
return nil, err
}
var res []key.Key
walk := func(buf []byte, idx int, link *merkledag.Link) error {
res = append(res, key.Key(link.Hash))
return nil
}
if err := walkItems(ctx, dag, n, walk, internalKeys); err != nil {
return nil, err
}
return res, nil
}
func loadMultiset(ctx context.Context, dag merkledag.DAGService, root *merkledag.Node, name string, internalKeys keyObserver) (map[key.Key]uint64, error) {
l, err := root.GetNodeLink(name)
if err != nil {
return nil, err
}
internalKeys(key.Key(l.Hash))
n, err := l.GetNode(ctx, dag)
if err != nil {
return nil, err
}
refcounts := make(map[key.Key]uint64)
walk := func(buf []byte, idx int, link *merkledag.Link) error {
var r refcount
r.ReadFromIdx(buf, idx)
refcounts[key.Key(link.Hash)] += uint64(r)
return nil
}
if err := walkItems(ctx, dag, n, walk, internalKeys); err != nil {
return nil, err
}
return refcounts, nil
}
func storeSet(ctx context.Context, dag merkledag.DAGService, keys []key.Key, internalKeys keyObserver) (*merkledag.Node, error) {
iter := func() (k key.Key, data []byte, ok bool) {
if len(keys) == 0 {
return "", nil, false
}
first := keys[0]
keys = keys[1:]
return first, nil, true
}
n, err := storeItems(ctx, dag, uint64(len(keys)), iter, internalKeys)
if err != nil {
return nil, err
}
k, err := dag.Add(n)
if err != nil {
return nil, err
}
internalKeys(k)
return n, nil
}
func copyRefcounts(orig map[key.Key]uint64) map[key.Key]uint64 {
r := make(map[key.Key]uint64, len(orig))
for k, v := range orig {
r[k] = v
}
return r
}
func storeMultiset(ctx context.Context, dag merkledag.DAGService, refcounts map[key.Key]uint64, internalKeys keyObserver) (*merkledag.Node, error) {
// make a working copy of the refcounts
refcounts = copyRefcounts(refcounts)
iter := func() (k key.Key, data []byte, ok bool) {
// Every call of this function returns the next refcount item.
//
// This function splits out the uint64 reference counts as
// smaller increments, as fits in type refcount. Most of the
// time the refcount will fit inside just one, so this saves
// space.
//
// We use range here to pick an arbitrary item in the map, but
// not really iterate the map.
for k, refs := range refcounts {
// Max value a single multiset item can store
num := ^refcount(0)
if refs <= uint64(num) {
// Remaining count fits in a single item; remove the
// key from the map.
num = refcount(refs)
delete(refcounts, k)
} else {
// Count is too large to fit in one item, the key will
// repeat in some later call.
refcounts[k] -= uint64(num)
}
return k, num.Bytes(), true
}
return "", nil, false
}
n, err := storeItems(ctx, dag, uint64(len(refcounts)), iter, internalKeys)
if err != nil {
return nil, err
}
k, err := dag.Add(n)
if err != nil {
return nil, err
}
internalKeys(k)
return n, nil
}