Skip to content

Commit d3cca0a

Browse files
Merge pull request ipfs#4888 from ipfs/fix/dedup-keys
dedup keys in GetMany
2 parents 823a071 + 7d5c56b commit d3cca0a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

merkledag.go

+13
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,20 @@ func (n *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld.
201201
return getNodesFromBG(ctx, n.Blocks, keys)
202202
}
203203

204+
func dedupKeys(keys []*cid.Cid) []*cid.Cid {
205+
set := cid.NewSet()
206+
for _, c := range keys {
207+
set.Add(c)
208+
}
209+
if set.Len() == len(keys) {
210+
return keys
211+
}
212+
return set.Keys()
213+
}
214+
204215
func getNodesFromBG(ctx context.Context, bs bserv.BlockGetter, keys []*cid.Cid) <-chan *ipld.NodeOption {
216+
keys = dedupKeys(keys)
217+
205218
out := make(chan *ipld.NodeOption, len(keys))
206219
blocks := bs.GetBlocks(ctx, keys)
207220
var count int

merkledag_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,36 @@ func TestCidRawDoesnNeedData(t *testing.T) {
550550
}
551551
}
552552

553+
func TestGetManyDuplicate(t *testing.T) {
554+
ctx := context.Background()
555+
556+
srv := NewDAGService(dstest.Bserv())
557+
558+
nd := NodeWithData([]byte("foo"))
559+
if err := srv.Add(ctx, nd); err != nil {
560+
t.Fatal(err)
561+
}
562+
nds := srv.GetMany(ctx, []*cid.Cid{nd.Cid(), nd.Cid(), nd.Cid()})
563+
out, ok := <-nds
564+
if !ok {
565+
t.Fatal("expecting node foo")
566+
}
567+
if out.Err != nil {
568+
t.Fatal(out.Err)
569+
}
570+
if !out.Node.Cid().Equals(nd.Cid()) {
571+
t.Fatal("got wrong node")
572+
}
573+
out, ok = <-nds
574+
if ok {
575+
if out.Err != nil {
576+
t.Fatal(out.Err)
577+
} else {
578+
t.Fatal("expecting no more nodes")
579+
}
580+
}
581+
}
582+
553583
func TestEnumerateAsyncFailsNotFound(t *testing.T) {
554584
ctx := context.Background()
555585

0 commit comments

Comments
 (0)