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

feat(pinning): add support for names #525

Merged
merged 2 commits into from
Jan 4, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The following emojis are used to highlight certain changes:

### Added

* 🛠 `pinning/pinner`: you can now give a custom name when pinning a CID. To reflect this, the `Pinner` has been adjusted.

### Changed

### Removed
Expand Down
74 changes: 48 additions & 26 deletions pinning/pinner/dspinner/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@
}

// Pin the given node, optionally recursive
func (p *pinner) Pin(ctx context.Context, node ipld.Node, recurse bool) error {
func (p *pinner) Pin(ctx context.Context, node ipld.Node, recurse bool, name string) error {
err := p.dserv.Add(ctx, node)
if err != nil {
return err
}

if recurse {
return p.doPinRecursive(ctx, node.Cid(), true)
return p.doPinRecursive(ctx, node.Cid(), true, name)
} else {
return p.doPinDirect(ctx, node.Cid())
return p.doPinDirect(ctx, node.Cid(), name)
}
}

func (p *pinner) doPinRecursive(ctx context.Context, c cid.Cid, fetch bool) error {
func (p *pinner) doPinRecursive(ctx context.Context, c cid.Cid, fetch bool, name string) error {
cidKey := c.KeyString()

p.lock.Lock()
Expand Down Expand Up @@ -243,14 +243,14 @@
}
}

_, err = p.addPin(ctx, c, ipfspinner.Recursive, "")
_, err = p.addPin(ctx, c, ipfspinner.Recursive, name)
if err != nil {
return err
}
return p.flushPins(ctx, false)
}

func (p *pinner) doPinDirect(ctx context.Context, c cid.Cid) error {
func (p *pinner) doPinDirect(ctx context.Context, c cid.Cid, name string) error {
cidKey := c.KeyString()

p.lock.Lock()
Expand All @@ -264,7 +264,7 @@
return fmt.Errorf("%s already pinned recursively", c.String())
}

_, err = p.addPin(ctx, c, ipfspinner.Direct, "")
_, err = p.addPin(ctx, c, ipfspinner.Direct, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -665,17 +665,17 @@
}

// DirectKeys returns a slice containing the directly pinned keys
func (p *pinner) DirectKeys(ctx context.Context) <-chan ipfspinner.StreamedCid {
return p.streamIndex(ctx, p.cidDIndex)
func (p *pinner) DirectKeys(ctx context.Context, detailed bool) <-chan ipfspinner.StreamedPin {
return p.streamIndex(ctx, p.cidDIndex, detailed)
}

// RecursiveKeys returns a slice containing the recursively pinned keys
func (p *pinner) RecursiveKeys(ctx context.Context) <-chan ipfspinner.StreamedCid {
return p.streamIndex(ctx, p.cidRIndex)
func (p *pinner) RecursiveKeys(ctx context.Context, detailed bool) <-chan ipfspinner.StreamedPin {
return p.streamIndex(ctx, p.cidRIndex, detailed)
}

func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer) <-chan ipfspinner.StreamedCid {
out := make(chan ipfspinner.StreamedCid)
func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer, detailed bool) <-chan ipfspinner.StreamedPin {
out := make(chan ipfspinner.StreamedPin)

go func() {
defer close(out)
Expand All @@ -688,21 +688,37 @@
err := index.ForEach(ctx, "", func(key, value string) bool {
c, err := cid.Cast([]byte(key))
if err != nil {
out <- ipfspinner.StreamedCid{Err: err}
out <- ipfspinner.StreamedPin{Err: err}

Check warning on line 691 in pinning/pinner/dspinner/pin.go

View check run for this annotation

Codecov / codecov/patch

pinning/pinner/dspinner/pin.go#L691

Added line #L691 was not covered by tests
return false
}

var pin ipfspinner.Pinned
if detailed {
pp, err := p.loadPin(ctx, value)
if err != nil {
out <- ipfspinner.StreamedPin{Err: err}
return false
}

Check warning on line 701 in pinning/pinner/dspinner/pin.go

View check run for this annotation

Codecov / codecov/patch

pinning/pinner/dspinner/pin.go#L699-L701

Added lines #L699 - L701 were not covered by tests

pin.Key = pp.Cid
pin.Mode = pp.Mode
pin.Name = pp.Name
} else {
pin.Key = c
}

if !cidSet.Has(c) {
select {
case <-ctx.Done():
return false
case out <- ipfspinner.StreamedCid{C: c}:
case out <- ipfspinner.StreamedPin{Pin: pin}:
}
cidSet.Add(c)
}
return true
})
if err != nil {
out <- ipfspinner.StreamedCid{Err: err}
out <- ipfspinner.StreamedPin{Err: err}

Check warning on line 721 in pinning/pinner/dspinner/pin.go

View check run for this annotation

Codecov / codecov/patch

pinning/pinner/dspinner/pin.go#L721

Added line #L721 was not covered by tests
}
}()

Expand All @@ -711,8 +727,8 @@

// InternalPins returns all cids kept pinned for the internal state of the
// pinner
func (p *pinner) InternalPins(ctx context.Context) <-chan ipfspinner.StreamedCid {
c := make(chan ipfspinner.StreamedCid)
func (p *pinner) InternalPins(ctx context.Context, detailed bool) <-chan ipfspinner.StreamedPin {
c := make(chan ipfspinner.StreamedPin)
close(c)
return c
}
Expand All @@ -725,11 +741,11 @@
p.lock.Lock()
defer p.lock.Unlock()

found, err := p.cidRIndex.HasAny(ctx, from.KeyString())
fromValues, err := p.cidRIndex.Search(ctx, from.KeyString())
if err != nil {
return err
}
if !found {
if len(fromValues) != 1 {
return errors.New("'from' cid was not recursively pinned already")
}

Expand All @@ -739,11 +755,11 @@
}

// Check if the `to` cid is already recursively pinned
found, err = p.cidRIndex.HasAny(ctx, to.KeyString())
toFound, err := p.cidRIndex.HasAny(ctx, to.KeyString())
if err != nil {
return err
}
if found {
if toFound {
return errors.New("'to' cid was already recursively pinned")
}

Expand All @@ -756,7 +772,13 @@
return err
}

_, err = p.addPin(ctx, to, ipfspinner.Recursive, "")
// Get pin information so that we can keep the name.
pin, err := p.loadPin(ctx, fromValues[0])
if err != nil {
return err
}

Check warning on line 779 in pinning/pinner/dspinner/pin.go

View check run for this annotation

Codecov / codecov/patch

pinning/pinner/dspinner/pin.go#L778-L779

Added lines #L778 - L779 were not covered by tests

_, err = p.addPin(ctx, to, ipfspinner.Recursive, pin.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -809,13 +831,13 @@

// PinWithMode allows the user to have fine grained control over pin
// counts
func (p *pinner) PinWithMode(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) error {
func (p *pinner) PinWithMode(ctx context.Context, c cid.Cid, mode ipfspinner.Mode, name string) error {
// TODO: remove his to support multiple pins per CID
switch mode {
case ipfspinner.Recursive:
return p.doPinRecursive(ctx, c, false)
return p.doPinRecursive(ctx, c, false, name)
case ipfspinner.Direct:
return p.doPinDirect(ctx, c)
return p.doPinDirect(ctx, c, name)

Check warning on line 840 in pinning/pinner/dspinner/pin.go

View check run for this annotation

Codecov / codecov/patch

pinning/pinner/dspinner/pin.go#L840

Added line #L840 was not covered by tests
default:
return errors.New("unrecognized pin mode")
}
Expand Down
Loading