Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cid streaming with reference count #11

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 87 additions & 39 deletions pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,67 +429,115 @@ func (p *RcPinner) CheckIfPinned(
// DirectKeys returns a slice containing the directly pinned keys
func (p *RcPinner) DirectKeys(ctx context.Context) <-chan pin.StreamedCid {
out := make(chan pin.StreamedCid)
re := make(chan *StreamedCidWithCount)
go func() {
p.mu.RLock()
defer p.mu.RUnlock()
defer close(out)
defer close(re)

cidSet := cid.NewSet()
if err := p.cidDIdx.forEach(
ctx,
func(c cid.Cid, cnt uint16) (bool, error) {
if cidSet.Has(c) || cnt == 0 {
return true, nil
}

select {
case <-ctx.Done():
return false, nil
case out <- pin.StreamedCid{C: c}:
}
cidSet.Add(c)
return true, nil
},
); err != nil {
out <- pin.StreamedCid{Err: err}
keysWithCount(ctx, p.cidDIdx, re)
}()

go func() {
defer close(out)
for v := range re {
out <- v.Cid
}
}()

return out
}

// RecursiveKeys returns a slice containing the recursively pinned keys
// DirectKeysWithCount streams out directly pinned keys with reference count.
func (p *RcPinner) DirectKeysWithCount(
ctx context.Context,
) <-chan *StreamedCidWithCount {
out := make(chan *StreamedCidWithCount)
go func() {
p.mu.RLock()
defer p.mu.RUnlock()
defer close(out)
keysWithCount(ctx, p.cidDIdx, out)
}()

return out
}

// RecursiveKeys streams out recursively pinned keys
func (p *RcPinner) RecursiveKeys(ctx context.Context) <-chan pin.StreamedCid {
out := make(chan pin.StreamedCid)
re := make(chan *StreamedCidWithCount)
go func() {
p.mu.RLock()
defer p.mu.RUnlock()
defer close(out)
defer close(re)
keysWithCount(ctx, p.cidRIdx, re)
}()

cidSet := cid.NewSet()
if err := p.cidRIdx.forEach(
ctx,
func(c cid.Cid, cnt uint16) (bool, error) {
if cidSet.Has(c) || cnt == 0 {
return true, nil
}

select {
case <-ctx.Done():
return false, nil
case out <- pin.StreamedCid{C: c}:
}
cidSet.Add(c)
return true, nil
},
); err != nil {
out <- pin.StreamedCid{Err: err}
go func() {
defer close(out)
for v := range re {
out <- v.Cid
}
}()

return out
}

// RecursiveKeysWithCount streams out recursively pinned keys with
// reference count.
func (p *RcPinner) RecursiveKeysWithCount(
ctx context.Context,
) <-chan *StreamedCidWithCount {
out := make(chan *StreamedCidWithCount)
go func() {
p.mu.RLock()
defer p.mu.RUnlock()
defer close(out)
keysWithCount(ctx, p.cidRIdx, out)
}()

return out
}

type StreamedCidWithCount struct {
Cid pin.StreamedCid
Count uint16
}

// keysWithCount streams out pinned keys with corresponding reference count.
func keysWithCount(
ctx context.Context,
idx *index,
out chan *StreamedCidWithCount,
) {
cidSet := cid.NewSet()
if err := idx.forEach(
ctx,
func(c cid.Cid, cnt uint16) (bool, error) {
if cidSet.Has(c) || cnt == 0 {
return true, nil
}

select {
case <-ctx.Done():
return false, nil
case out <- &StreamedCidWithCount{
Cid: pin.StreamedCid{C: c},
Count: cnt,
}:
}

cidSet.Add(c)
return true, nil
},
); err != nil {
out <- &StreamedCidWithCount{
Cid: pin.StreamedCid{Err: err},
}
}
}

// InternalPins returns all cids kept pinned for the internal state of the
// pinner
func (p *RcPinner) InternalPins(ctx context.Context) <-chan pin.StreamedCid {
Expand Down
106 changes: 88 additions & 18 deletions pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,9 @@ func TestPinnerBasics(t *testing.T) {

cids := readCh(t, p.RecursiveKeys(ctx))
require.Equal(t, 3, len(cids))
m := map[cid.Cid]bool{}
for _, c := range cids {
m[c] = true
}
require.True(t, m[a.Cid()])
require.True(t, m[b.Cid()])
require.True(t, m[d.Cid()])
require.True(t, cids[a.Cid()])
require.True(t, cids[b.Cid()])
require.True(t, cids[d.Cid()])

// DAG: D{A,C,E}, B{A,C}
// Recursive: A,B,D
Expand All @@ -217,13 +213,9 @@ func TestPinnerBasics(t *testing.T) {
cids = readCh(t, p.DirectKeys(ctx))
require.NoError(t, err)
require.Equal(t, 3, len(cids))
m = map[cid.Cid]bool{}
for _, c := range cids {
m[c] = true
}
require.True(t, m[a.Cid()])
require.True(t, m[d.Cid()])
require.True(t, m[f.Cid()])
require.True(t, cids[a.Cid()])
require.True(t, cids[d.Cid()])
require.True(t, cids[f.Cid()])

cids = readCh(t, p.InternalPins(ctx))
require.NoError(t, err)
Expand Down Expand Up @@ -493,6 +485,72 @@ func TestPinRecursiveFail(t *testing.T) {
require.NoError(t, p.Pin(mctx, a, true))
}

func TestStreamKeys(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

dstore := dssync.MutexWrap(ds.NewMapDatastore())
bstore := blockstore.NewBlockstore(dstore)
bserv := bs.New(bstore, offline.Exchange(bstore))
dserv := mdag.NewDAGService(bserv)
p, err := New(ctx, dstore, dserv)
require.NoError(t, err)

// A{B,C{D,E},F}
a := rndNode(t)
b := rndNode(t)
c := rndNode(t)
d := rndNode(t)
e := rndNode(t)
f := rndNode(t)
require.NoError(t, c.AddNodeLink("c0", d))
require.NoError(t, c.AddNodeLink("c1", e))
require.NoError(t, a.AddNodeLink("c0", b))
require.NoError(t, a.AddNodeLink("c0", c))
require.NoError(t, a.AddNodeLink("c0", f))

require.NoError(t, dserv.Add(ctx, a))
require.NoError(t, dserv.Add(ctx, b))
require.NoError(t, dserv.Add(ctx, c))
require.NoError(t, dserv.Add(ctx, d))
require.NoError(t, dserv.Add(ctx, e))
require.NoError(t, dserv.Add(ctx, f))

require.NoError(t, p.Pin(ctx, a, true))
require.NoError(t, p.Pin(ctx, a, true))
require.NoError(t, p.Pin(ctx, b, true))

require.NoError(t, p.Pin(ctx, a, false))
require.NoError(t, p.Pin(ctx, c, false))
require.NoError(t, p.Pin(ctx, d, false))
require.NoError(t, p.Pin(ctx, d, false))
require.NoError(t, p.Pin(ctx, e, false))

cids := readCh(t, p.RecursiveKeys(ctx))
require.Equal(t, 2, len(cids))
require.True(t, cids[a.Cid()])
require.True(t, cids[b.Cid()])

cidCnts := readCountCh(t, p.RecursiveKeysWithCount(ctx))
require.Equal(t, 2, len(cidCnts))
require.Equal(t, 2, cidCnts[a.Cid()])
require.Equal(t, 1, cidCnts[b.Cid()])

cids = readCh(t, p.DirectKeys(ctx))
require.Equal(t, 4, len(cids))
require.True(t, cids[a.Cid()])
require.True(t, cids[c.Cid()])
require.True(t, cids[d.Cid()])
require.True(t, cids[e.Cid()])

cidCnts = readCountCh(t, p.DirectKeysWithCount(ctx))
require.Equal(t, 4, len(cidCnts))
require.Equal(t, 1, cidCnts[a.Cid()])
require.Equal(t, 1, cidCnts[c.Cid()])
require.Equal(t, 2, cidCnts[d.Cid()])
require.Equal(t, 1, cidCnts[e.Cid()])
}

func TestCidIndex(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -772,11 +830,23 @@ func benchmarkPinAll(
}
}

func readCh(t *testing.T, ch <-chan pin.StreamedCid) []cid.Cid {
var arr []cid.Cid
func readCh(t *testing.T, ch <-chan pin.StreamedCid) map[cid.Cid]bool {
m := map[cid.Cid]bool{}
for re := range ch {
require.NoError(t, re.Err)
arr = append(arr, re.C)
m[re.C] = true
}
return m
}

func readCountCh(
t *testing.T,
ch <-chan *StreamedCidWithCount,
) map[cid.Cid]int {
m := map[cid.Cid]int{}
for re := range ch {
require.NoError(t, re.Cid.Err)
m[re.Cid.C] = int(re.Count)
}
return arr
return m
}
Loading