Skip to content

Commit 4683ce3

Browse files
committed
Change signature
Signed-off-by: Jakub Sztandera <[email protected]>
1 parent 6c756c5 commit 4683ce3

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

mapreduce.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"sync"
1010

1111
cid "github.com/ipfs/go-cid"
12+
cbor "github.com/ipfs/go-ipld-cbor"
1213
cbg "github.com/whyrusleeping/cbor-gen"
14+
xerrors "golang.org/x/xerrors"
1315
)
1416

1517
type cacheEntry[T any] struct {
@@ -107,11 +109,13 @@ func NewCachedMapReduce[T any, PT interface {
107109
}
108110

109111
// MapReduce applies the map reduce function to the given root node.
110-
func (cmr *CachedMapReduce[T, PT, U]) MapReduce(ctx context.Context, root *Node) (U, error) {
112+
func (cmr *CachedMapReduce[T, PT, U]) MapReduce(ctx context.Context, cs cbor.IpldStore, c cid.Cid, options ...Option) (U, error) {
111113
var res U
112-
if root == nil {
113-
return res, errors.New("root is nil")
114+
root, err := LoadNode(ctx, cs, c, options...)
115+
if err != nil {
116+
return res, xerrors.Errorf("failed to load root node: %w", err)
114117
}
118+
115119
ce, err := cmr.mapReduceInternal(ctx, root)
116120
if err != nil {
117121
return res, err

mapreduce_test.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,24 @@ func TestMapReduceSimple(t *testing.T) {
2525
ctx := context.Background()
2626
opts := []Option{UseTreeBitWidth(5)}
2727
cs := &readCounterStore{cbor.NewCborStore(newMockBlocks()), 0}
28-
begn, err := NewNode(cs, opts...)
29-
require.NoError(t, err)
3028

31-
golden := make(map[string]string)
3229
N := 50000
33-
for range N {
34-
k := randKey()
35-
v := randValue()
36-
golden[k] = string([]byte(*v))
37-
begn.Set(ctx, k, v)
38-
}
39-
40-
reLoadNode := func(node *Node) *Node {
41-
c, err := node.Write(ctx)
30+
var rootCid cid.Cid
31+
golden := make(map[string]string)
32+
{
33+
begn, err := NewNode(cs, opts...)
4234
require.NoError(t, err)
43-
res, err := LoadNode(ctx, cs, c, opts...)
35+
36+
for range N {
37+
k := randKey()
38+
v := randValue()
39+
golden[k] = string([]byte(*v))
40+
begn.Set(ctx, k, v)
41+
}
42+
43+
rootCid, err = begn.Write(ctx)
4444
require.NoError(t, err)
45-
return res
4645
}
47-
begn = reLoadNode(begn)
4846

4947
type kv struct {
5048
k string
@@ -70,14 +68,13 @@ func TestMapReduceSimple(t *testing.T) {
7068
require.NoError(t, err)
7169

7270
cs.readCount = 0
73-
res, err := cmr.MapReduce(ctx, begn)
71+
res, err := cmr.MapReduce(ctx, cs, rootCid, opts...)
7472
require.NoError(t, err)
7573
require.Equal(t, len(golden), len(res))
7674
t.Logf("fresh readCount: %d", cs.readCount)
7775

78-
begn = reLoadNode(begn)
7976
cs.readCount = 0
80-
res, err = cmr.MapReduce(ctx, begn)
77+
res, err = cmr.MapReduce(ctx, cs, rootCid, opts...)
8178
require.NoError(t, err)
8279
t.Logf("fresh re-readCount: %d", cs.readCount)
8380
require.Less(t, cs.readCount, 200)
@@ -94,39 +91,42 @@ func TestMapReduceSimple(t *testing.T) {
9491
verifyConsistency(res)
9592

9693
{
94+
begn, err := LoadNode(ctx, cs, rootCid, opts...)
95+
require.NoError(t, err)
9796
// add new key
9897
k := randKey()
9998
v := randValue()
10099
golden[k] = string([]byte(*v))
101100
begn.Set(ctx, k, v)
102101

103-
begn = reLoadNode(begn)
102+
rootCid, err = begn.Write(ctx)
103+
require.NoError(t, err)
104104
}
105105

106106
cs.readCount = 0
107-
res, err = cmr.MapReduce(ctx, begn)
107+
res, err = cmr.MapReduce(ctx, cs, rootCid, opts...)
108108
require.NoError(t, err)
109109
verifyConsistency(res)
110110
t.Logf("new key readCount: %d", cs.readCount)
111111
require.Less(t, cs.readCount, 200)
112112

113-
begn = reLoadNode(begn)
114113
cs.readCount = 0
115-
res, err = cmr.MapReduce(ctx, begn)
114+
res, err = cmr.MapReduce(ctx, cs, rootCid, opts...)
116115
require.NoError(t, err)
117116
verifyConsistency(res)
118117
t.Logf("repeat readCount: %d", cs.readCount)
119118
require.Less(t, cs.readCount, 200)
120119

121-
begn = reLoadNode(begn)
122120
cs.readCount = 0
123-
res, err = cmr.MapReduce(ctx, begn)
121+
res, err = cmr.MapReduce(ctx, cs, rootCid, opts...)
124122
require.NoError(t, err)
125123
verifyConsistency(res)
126124
t.Logf("repeat readCount: %d", cs.readCount)
127125
require.Less(t, cs.readCount, 200)
128126

129127
{
128+
begn, err := LoadNode(ctx, cs, rootCid, opts...)
129+
require.NoError(t, err)
130130
// add two new keys
131131
k := randKey()
132132
v := randValue()
@@ -137,11 +137,12 @@ func TestMapReduceSimple(t *testing.T) {
137137
golden[k] = string([]byte(*v))
138138
begn.Set(ctx, k, v)
139139

140-
begn = reLoadNode(begn)
140+
rootCid, err = begn.Write(ctx)
141+
require.NoError(t, err)
141142
}
142143

143144
cs.readCount = 0
144-
res, err = cmr.MapReduce(ctx, begn)
145+
res, err = cmr.MapReduce(ctx, cs, rootCid, opts...)
145146
require.NoError(t, err)
146147
verifyConsistency(res)
147148
t.Logf("new two keys readCount: %d", cs.readCount)

0 commit comments

Comments
 (0)