-
Notifications
You must be signed in to change notification settings - Fork 44
/
selectivecar_test.go
228 lines (189 loc) · 6.68 KB
/
selectivecar_test.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
package car_test
import (
"bytes"
"context"
"testing"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
dstest "github.com/ipfs/go-merkledag/test"
car "github.com/ipld/go-car"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/stretchr/testify/require"
)
func TestRoundtripSelective(t *testing.T) {
ctx := context.Background()
sourceBserv := dstest.Bserv()
sourceBs := sourceBserv.Blockstore()
dserv := merkledag.NewDAGService(sourceBserv)
a := merkledag.NewRawNode([]byte("aaaa"))
b := merkledag.NewRawNode([]byte("bbbb"))
c := merkledag.NewRawNode([]byte("cccc"))
nd1 := &merkledag.ProtoNode{}
nd1.AddNodeLink("cat", a)
nd2 := &merkledag.ProtoNode{}
nd2.AddNodeLink("first", nd1)
nd2.AddNodeLink("dog", b)
nd2.AddNodeLink("repeat", nd1)
nd3 := &merkledag.ProtoNode{}
nd3.AddNodeLink("second", nd2)
nd3.AddNodeLink("bear", c)
assertAddNodes(t, dserv, a, b, c, nd1, nd2, nd3)
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
// the graph assembled above looks as follows, in order:
// nd3 -> [c, nd2 -> [nd1 -> a, b, nd1 -> a]]
// this selector starts at n3, and traverses a link at index 1 (nd2, the second link, zero indexed)
// it then recursively traverses all of its children
// the only node skipped is 'c' -- link at index 0 immediately below nd3
// the purpose is simply to show we are not writing the entire merkledag underneath
// nd3
selector := ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert("Links",
ssb.ExploreIndex(1, ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreAll(ssb.ExploreRecursiveEdge()))))
}).Node()
sc := car.NewSelectiveCar(context.Background(), sourceBs, []car.Dag{{Root: nd3.Cid(), Selector: selector}})
// write car in one step
buf := new(bytes.Buffer)
blockCount := 0
var oneStepBlocks []car.Block
err := sc.Write(buf, func(block car.Block) error {
oneStepBlocks = append(oneStepBlocks, block)
blockCount++
return nil
})
require.Equal(t, blockCount, 5)
require.NoError(t, err)
// create a new builder for two-step write
sc2 := car.NewSelectiveCar(context.Background(), sourceBs, []car.Dag{{Root: nd3.Cid(), Selector: selector}})
// write car in two steps
var twoStepBlocks []car.Block
scp, err := sc2.Prepare(func(block car.Block) error {
twoStepBlocks = append(twoStepBlocks, block)
return nil
})
require.NoError(t, err)
buf2 := new(bytes.Buffer)
err = scp.Dump(ctx, buf2)
require.NoError(t, err)
// verify preparation step correctly assesed length and blocks
require.Equal(t, scp.Size(), uint64(buf.Len()))
require.Equal(t, len(scp.Cids()), blockCount)
// verify equal data written by both methods
require.Equal(t, buf.Bytes(), buf2.Bytes())
// verify equal blocks were passed to user block hook funcs
require.Equal(t, oneStepBlocks, twoStepBlocks)
// readout car and verify contents
bserv := dstest.Bserv()
ch, err := car.LoadCar(ctx, bserv.Blockstore(), buf)
require.NoError(t, err)
require.Equal(t, len(ch.Roots), 1)
require.True(t, ch.Roots[0].Equals(nd3.Cid()))
bs := bserv.Blockstore()
for _, nd := range []format.Node{a, b, nd1, nd2, nd3} {
has, err := bs.Has(ctx, nd.Cid())
require.NoError(t, err)
require.True(t, has)
}
for _, nd := range []format.Node{c} {
has, err := bs.Has(ctx, nd.Cid())
require.NoError(t, err)
require.False(t, has)
}
}
func TestNoLinkRepeatSelective(t *testing.T) {
sourceBserv := dstest.Bserv()
sourceBs := countingReadStore{bs: sourceBserv.Blockstore()}
dserv := merkledag.NewDAGService(sourceBserv)
a := merkledag.NewRawNode([]byte("aaaa"))
b := merkledag.NewRawNode([]byte("bbbb"))
c := merkledag.NewRawNode([]byte("cccc"))
nd1 := &merkledag.ProtoNode{}
nd1.AddNodeLink("cat", a)
nd2 := &merkledag.ProtoNode{}
nd2.AddNodeLink("first", nd1)
nd2.AddNodeLink("dog", b)
nd2.AddNodeLink("repeat", nd1)
nd3 := &merkledag.ProtoNode{}
nd3.AddNodeLink("second", nd2)
nd3.AddNodeLink("bear", c)
nd3.AddNodeLink("bearagain1", c)
nd3.AddNodeLink("bearagain2", c)
nd3.AddNodeLink("bearagain3", c)
assertAddNodes(t, dserv, a, b, c, nd1, nd2, nd3)
t.Run("TraverseLinksOnlyOnce off", func(t *testing.T) {
sourceBs.count = 0
sc := car.NewSelectiveCar(context.Background(),
&sourceBs,
[]car.Dag{{Root: nd3.Cid(), Selector: selectorparse.CommonSelector_ExploreAllRecursively}},
)
buf := new(bytes.Buffer)
blockCount := 0
err := sc.Write(buf, func(block car.Block) error {
blockCount++
return nil
})
require.Equal(t, blockCount, 6)
require.Equal(t, sourceBs.count, 11) // with TraverseLinksOnlyOnce off, we expect repeat block visits because our DAG has repeat links
require.NoError(t, err)
})
t.Run("TraverseLinksOnlyOnce on", func(t *testing.T) {
sourceBs.count = 0
sc := car.NewSelectiveCar(context.Background(),
&sourceBs,
[]car.Dag{{Root: nd3.Cid(), Selector: selectorparse.CommonSelector_ExploreAllRecursively}},
car.TraverseLinksOnlyOnce(),
)
buf := new(bytes.Buffer)
blockCount := 0
err := sc.Write(buf, func(block car.Block) error {
blockCount++
return nil
})
require.Equal(t, blockCount, 6)
require.Equal(t, sourceBs.count, 6) // only 6 blocks to load, no duplicate loading expected
require.NoError(t, err)
})
}
func TestLinkLimitSelective(t *testing.T) {
sourceBserv := dstest.Bserv()
sourceBs := sourceBserv.Blockstore()
dserv := merkledag.NewDAGService(sourceBserv)
a := merkledag.NewRawNode([]byte("aaaa"))
b := merkledag.NewRawNode([]byte("bbbb"))
c := merkledag.NewRawNode([]byte("cccc"))
nd1 := &merkledag.ProtoNode{}
nd1.AddNodeLink("cat", a)
nd2 := &merkledag.ProtoNode{}
nd2.AddNodeLink("first", nd1)
nd2.AddNodeLink("dog", b)
nd2.AddNodeLink("repeat", nd1)
nd3 := &merkledag.ProtoNode{}
nd3.AddNodeLink("second", nd2)
nd3.AddNodeLink("bear", c)
assertAddNodes(t, dserv, a, b, c, nd1, nd2, nd3)
sc := car.NewSelectiveCar(context.Background(),
sourceBs,
[]car.Dag{{Root: nd3.Cid(), Selector: selectorparse.CommonSelector_ExploreAllRecursively}},
car.MaxTraversalLinks(2))
buf := new(bytes.Buffer)
blockCount := 0
err := sc.Write(buf, func(block car.Block) error {
blockCount++
return nil
})
require.Equal(t, blockCount, 3) // root + 2
require.Error(t, err)
require.Regexp(t, "^traversal budget exceeded: budget for links reached zero while on path .*", err)
}
type countingReadStore struct {
bs car.ReadStore
count int
}
func (rs *countingReadStore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
rs.count++
return rs.bs.Get(ctx, c)
}