Skip to content

Commit 38fb74f

Browse files
authored
feat(pinning): add support for names
1 parent 79cace5 commit 38fb74f

File tree

5 files changed

+138
-69
lines changed

5 files changed

+138
-69
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ The following emojis are used to highlight certain changes:
1616

1717
### Added
1818

19+
* 🛠 `pinning/pinner`: you can now give a custom name when pinning a CID. To reflect this, the `Pinner` has been adjusted.
20+
1921
### Changed
2022

2123
### Removed

pinning/pinner/dspinner/pin.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,20 @@ func (p *pinner) SetAutosync(auto bool) bool {
174174
}
175175

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

183183
if recurse {
184-
return p.doPinRecursive(ctx, node.Cid(), true)
184+
return p.doPinRecursive(ctx, node.Cid(), true, name)
185185
} else {
186-
return p.doPinDirect(ctx, node.Cid())
186+
return p.doPinDirect(ctx, node.Cid(), name)
187187
}
188188
}
189189

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

193193
p.lock.Lock()
@@ -243,14 +243,14 @@ func (p *pinner) doPinRecursive(ctx context.Context, c cid.Cid, fetch bool) erro
243243
}
244244
}
245245

246-
_, err = p.addPin(ctx, c, ipfspinner.Recursive, "")
246+
_, err = p.addPin(ctx, c, ipfspinner.Recursive, name)
247247
if err != nil {
248248
return err
249249
}
250250
return p.flushPins(ctx, false)
251251
}
252252

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

256256
p.lock.Lock()
@@ -264,7 +264,7 @@ func (p *pinner) doPinDirect(ctx context.Context, c cid.Cid) error {
264264
return fmt.Errorf("%s already pinned recursively", c.String())
265265
}
266266

267-
_, err = p.addPin(ctx, c, ipfspinner.Direct, "")
267+
_, err = p.addPin(ctx, c, ipfspinner.Direct, name)
268268
if err != nil {
269269
return err
270270
}
@@ -665,17 +665,17 @@ func (p *pinner) loadPin(ctx context.Context, pid string) (*pin, error) {
665665
}
666666

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

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

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

680680
go func() {
681681
defer close(out)
@@ -688,21 +688,37 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer) <-chan
688688
err := index.ForEach(ctx, "", func(key, value string) bool {
689689
c, err := cid.Cast([]byte(key))
690690
if err != nil {
691-
out <- ipfspinner.StreamedCid{Err: err}
691+
out <- ipfspinner.StreamedPin{Err: err}
692692
return false
693693
}
694+
695+
var pin ipfspinner.Pinned
696+
if detailed {
697+
pp, err := p.loadPin(ctx, value)
698+
if err != nil {
699+
out <- ipfspinner.StreamedPin{Err: err}
700+
return false
701+
}
702+
703+
pin.Key = pp.Cid
704+
pin.Mode = pp.Mode
705+
pin.Name = pp.Name
706+
} else {
707+
pin.Key = c
708+
}
709+
694710
if !cidSet.Has(c) {
695711
select {
696712
case <-ctx.Done():
697713
return false
698-
case out <- ipfspinner.StreamedCid{C: c}:
714+
case out <- ipfspinner.StreamedPin{Pin: pin}:
699715
}
700716
cidSet.Add(c)
701717
}
702718
return true
703719
})
704720
if err != nil {
705-
out <- ipfspinner.StreamedCid{Err: err}
721+
out <- ipfspinner.StreamedPin{Err: err}
706722
}
707723
}()
708724

@@ -711,8 +727,8 @@ func (p *pinner) streamIndex(ctx context.Context, index dsindex.Indexer) <-chan
711727

712728
// InternalPins returns all cids kept pinned for the internal state of the
713729
// pinner
714-
func (p *pinner) InternalPins(ctx context.Context) <-chan ipfspinner.StreamedCid {
715-
c := make(chan ipfspinner.StreamedCid)
730+
func (p *pinner) InternalPins(ctx context.Context, detailed bool) <-chan ipfspinner.StreamedPin {
731+
c := make(chan ipfspinner.StreamedPin)
716732
close(c)
717733
return c
718734
}
@@ -725,11 +741,11 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
725741
p.lock.Lock()
726742
defer p.lock.Unlock()
727743

728-
found, err := p.cidRIndex.HasAny(ctx, from.KeyString())
744+
fromValues, err := p.cidRIndex.Search(ctx, from.KeyString())
729745
if err != nil {
730746
return err
731747
}
732-
if !found {
748+
if len(fromValues) != 1 {
733749
return errors.New("'from' cid was not recursively pinned already")
734750
}
735751

@@ -739,11 +755,11 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
739755
}
740756

741757
// Check if the `to` cid is already recursively pinned
742-
found, err = p.cidRIndex.HasAny(ctx, to.KeyString())
758+
toFound, err := p.cidRIndex.HasAny(ctx, to.KeyString())
743759
if err != nil {
744760
return err
745761
}
746-
if found {
762+
if toFound {
747763
return errors.New("'to' cid was already recursively pinned")
748764
}
749765

@@ -756,7 +772,13 @@ func (p *pinner) Update(ctx context.Context, from, to cid.Cid, unpin bool) error
756772
return err
757773
}
758774

759-
_, err = p.addPin(ctx, to, ipfspinner.Recursive, "")
775+
// Get pin information so that we can keep the name.
776+
pin, err := p.loadPin(ctx, fromValues[0])
777+
if err != nil {
778+
return err
779+
}
780+
781+
_, err = p.addPin(ctx, to, ipfspinner.Recursive, pin.Name)
760782
if err != nil {
761783
return err
762784
}
@@ -809,13 +831,13 @@ func (p *pinner) Flush(ctx context.Context) error {
809831

810832
// PinWithMode allows the user to have fine grained control over pin
811833
// counts
812-
func (p *pinner) PinWithMode(ctx context.Context, c cid.Cid, mode ipfspinner.Mode) error {
834+
func (p *pinner) PinWithMode(ctx context.Context, c cid.Cid, mode ipfspinner.Mode, name string) error {
813835
// TODO: remove his to support multiple pins per CID
814836
switch mode {
815837
case ipfspinner.Recursive:
816-
return p.doPinRecursive(ctx, c, false)
838+
return p.doPinRecursive(ctx, c, false, name)
817839
case ipfspinner.Direct:
818-
return p.doPinDirect(ctx, c)
840+
return p.doPinDirect(ctx, c, name)
819841
default:
820842
return errors.New("unrecognized pin mode")
821843
}

0 commit comments

Comments
 (0)