Skip to content

Commit

Permalink
Support named pins [ipfs/kubo#4757]
Browse files Browse the repository at this point in the history
  • Loading branch information
Voker57 committed Sep 19, 2020
1 parent b935dfe commit 89fc7f2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 38 deletions.
10 changes: 10 additions & 0 deletions options/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type BlockPutSettings struct {
MhType uint64
MhLength int
Pin bool
PinPath string
}

type BlockRmSettings struct {
Expand All @@ -26,6 +27,7 @@ func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, err
MhType: mh.SHA2_256,
MhLength: -1,
Pin: false,
PinPath: "default/",
}

for _, opt := range opts {
Expand Down Expand Up @@ -116,6 +118,14 @@ func (blockOpts) Pin(pin bool) BlockPutOption {
}
}

// PinPath is an option for Block.Put which specifies under which path to pin the block, default is "added/"
func (blockOpts) PinPath(pinPath string) BlockPutOption {
return func(settings *BlockPutSettings) error {
settings.PinPath = pinPath
return nil
}
}

// Force is an option for Block.Rm which, when set to true, will ignore
// non-existing blocks
func (blockOpts) Force(force bool) BlockRmOption {
Expand Down
10 changes: 10 additions & 0 deletions options/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ObjectPutSettings struct {
InputEnc string
DataType string
Pin bool
PinPath string
}

type ObjectAddLinkSettings struct {
Expand Down Expand Up @@ -37,6 +38,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
InputEnc: "json",
DataType: "text",
Pin: false,
PinPath: "added/",
}

for _, opt := range opts {
Expand Down Expand Up @@ -114,6 +116,14 @@ func (objectOpts) Pin(pin bool) ObjectPutOption {
}
}

// PinPath is an option for Object.Put which specifies under which path to pin the object, default is "added/"
func (objectOpts) PinPath(pinPath string) ObjectPutOption {
return func(settings *ObjectPutSettings) error {
settings.PinPath = pinPath
return nil
}
}

// Create is an option for Object.AddLink which specifies whether create required
// directories for the child
func (objectOpts) Create(create bool) ObjectAddLinkOption {
Expand Down
39 changes: 22 additions & 17 deletions options/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import "fmt"
// PinAddSettings represent the settings for PinAPI.Add
type PinAddSettings struct {
Recursive bool
PinPath string
}

// PinLsSettings represent the settings for PinAPI.Ls
type PinLsSettings struct {
Type string
Type string
Recursive bool
PinPath string
}

// PinIsPinnedSettings represent the settings for PinAPI.IsPinned
Expand All @@ -24,7 +27,7 @@ type PinRmSettings struct {

// PinUpdateSettings represent the settings for PinAPI.Update
type PinUpdateSettings struct {
Unpin bool

}

// PinAddOption is the signature of an option for PinAPI.Add
Expand All @@ -47,6 +50,7 @@ type PinUpdateOption func(*PinUpdateSettings) error
func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
options := &PinAddSettings{
Recursive: true,
PinPath: "default/",
}

for _, opt := range opts {
Expand All @@ -59,11 +63,13 @@ func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
return options, nil
}


// PinLsOptions compile a series of PinLsOption into a ready to use
// PinLsSettings and set the default values.
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
options := &PinLsSettings{
Type: "all",
Type: "all",
Recursive: false,
}

for _, opt := range opts {
Expand Down Expand Up @@ -112,9 +118,7 @@ func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
// PinUpdateOptions compile a series of PinUpdateOption into a ready to use
// PinUpdateSettings and set the default values.
func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
options := &PinUpdateSettings{
Unpin: true,
}
options := &PinUpdateSettings{}

for _, opt := range opts {
err := opt(options)
Expand All @@ -129,6 +133,7 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
type pinOpts struct {
Ls pinLsOpts
IsPinned pinIsPinnedOpts
Recursively bool
}

// Pin provide an access to all the options for the Pin API.
Expand All @@ -142,8 +147,8 @@ func (pinLsOpts) All() PinLsOption {
return Pin.Ls.pinType("all")
}

// Recursive is an option for Pin.Ls which will make it only return recursive
// pins
// Recursive is an option for Pin.Ls which will make it only return recursive (non
// direct) pins
func (pinLsOpts) Recursive() PinLsOption {
return Pin.Ls.pinType("recursive")
}
Expand All @@ -160,6 +165,15 @@ func (pinLsOpts) Indirect() PinLsOption {
return Pin.Ls.pinType("indirect")
}

// Recursive is an option for Pin.Ls which allows to specify whether
// argument should be recursively searched for pins
func (pinLsOpts) RecursiveList(recursive bool) PinLsOption {
return func(settings *PinLsSettings) error {
settings.Recursive = recursive
return nil
}
}

// Type is an option for Pin.Ls which will make it only return pins of the given
// type.
//
Expand Down Expand Up @@ -272,12 +286,3 @@ func (pinOpts) RmRecursive(recursive bool) PinRmOption {
return nil
}
}

// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
// Default is true.
func (pinOpts) Unpin(unpin bool) PinUpdateOption {
return func(settings *PinUpdateSettings) error {
settings.Unpin = unpin
return nil
}
}
11 changes: 11 additions & 0 deletions options/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UnixfsAddSettings struct {
Layout Layout

Pin bool
PinPath string
OnlyHash bool
FsCache bool
NoCopy bool
Expand Down Expand Up @@ -59,6 +60,7 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
Layout: BalancedLayout,

Pin: false,
PinPath: "added",
OnlyHash: false,
FsCache: false,
NoCopy: false,
Expand Down Expand Up @@ -221,6 +223,15 @@ func (unixfsOpts) Pin(pin bool) UnixfsAddOption {
}
}

// PinPath tells the adder the pin path to use
func (unixfsOpts) PinPath(pinPath string) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.PinPath = pinPath
return nil
}
}


// HashOnly will make the adder calculate data hash without storing it in the
// blockstore or announcing it to the network
func (unixfsOpts) HashOnly(hashOnly bool) UnixfsAddOption {
Expand Down
24 changes: 15 additions & 9 deletions pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
type Pin interface {
// Path to the pinned object
Path() path.Resolved


// Pinpath of pinned object
PinPath() string

// Type of the pin
Type() string

Expand All @@ -23,6 +26,9 @@ type Pin interface {
type PinStatus interface {
// Ok indicates whether the pin has been verified to be correct
Ok() bool

// Pinpath of pinned object
PinPath() string

// BadNodes returns any bad (usually missing) nodes from the pin
BadNodes() []BadPinNode
Expand All @@ -41,22 +47,22 @@ type BadPinNode interface {
type PinAPI interface {
// Add creates new pin, be default recursive - pinning the whole referenced
// tree
Add(context.Context, path.Path, ...options.PinAddOption) error
Add(context.Context, string, path.Path, ...options.PinAddOption) error

// Ls returns list of pinned objects on this node
Ls(context.Context, ...options.PinLsOption) (<-chan Pin, error)

// IsPinned returns whether or not the given cid is pinned
// and an explanation of why its pinned
IsPinned(context.Context, path.Path, ...options.PinIsPinnedOption) (string, bool, error)
Ls(context.Context, string, ...options.PinLsOption) (<-chan Pin, error)

// Rm removes pin for object specified by the path
Rm(context.Context, path.Path, ...options.PinRmOption) error
Rm(context.Context, string, ...options.PinRmOption) error

// Update changes one pin to another, skipping checks for matching paths in
// the old tree
Update(ctx context.Context, from path.Path, to path.Path, opts ...options.PinUpdateOption) error
Update(ctx context.Context, from string, to path.Path, opts ...options.PinUpdateOption) error

// Verify verifies the integrity of pinned objects
Verify(context.Context) (<-chan PinStatus, error)

// IsPinned returns whether or not the given cid is pinned
// and an explanation of why its pinned
IsPinned(context.Context, path.Path, ...options.PinIsPinnedOption) (string, bool, error)
}
4 changes: 2 additions & 2 deletions tests/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
t.Fatal(err)
}

if pins, err := api.Pin().Ls(ctx); err != nil || len(pins) != 0 {
if pins, err := api.Pin().Ls(ctx, ""); err != nil || len(pins) != 0 {
t.Fatal("expected 0 pins")
}

Expand All @@ -251,7 +251,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
t.Fatal(err)
}

pins, err := accPins(api.Pin().Ls(ctx))
pins, err := accPins(api.Pin().Ls(ctx, "", opt.Pin.RecursiveList(true)))
if err != nil {
t.Fatal(err)
}
Expand Down
20 changes: 11 additions & 9 deletions tests/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
ipld "github.com/ipfs/go-ipld-format"
)

var testPrefix = "test/"

func (tp *TestSuite) TestPin(t *testing.T) {
tp.hasApi(t, func(api iface.CoreAPI) error {
if api.Pin() == nil {
Expand Down Expand Up @@ -44,7 +46,7 @@ func (tp *TestSuite) TestPinAdd(t *testing.T) {
t.Fatal(err)
}

err = api.Pin().Add(ctx, p)
err = api.Pin().Add(ctx, testPrefix, p)
if err != nil {
t.Fatal(err)
}
Expand All @@ -63,12 +65,12 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
t.Fatal(err)
}

err = api.Pin().Add(ctx, p)
err = api.Pin().Add(ctx, testPrefix, p)
if err != nil {
t.Fatal(err)
}

list, err := accPins(api.Pin().Ls(ctx))
list, err := accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.Ls.Recursive()))
if err != nil {
t.Fatal(err)
}
Expand All @@ -87,12 +89,12 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {

assertIsPinned(t, ctx, api, p, "recursive")

err = api.Pin().Rm(ctx, p)
err = api.Pin().Rm(ctx, testPrefix+p.Cid().String())
if err != nil {
t.Fatal(err)
}

list, err = accPins(api.Pin().Ls(ctx))
list, err = accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.Ls.Recursive()))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -144,7 +146,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
t.Fatal(err)
}

list, err := accPins(api.Pin().Ls(ctx))
list, err := accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true)))
if err != nil {
t.Fatal(err)
}
Expand All @@ -153,7 +155,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
t.Errorf("unexpected pin list len: %d", len(list))
}

list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Direct()))
list, err = accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true), opt.Pin.Ls.Direct()))
if err != nil {
t.Fatal(err)
}
Expand All @@ -166,7 +168,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd3.Cid()).String())
}

list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Recursive()))
list, err = accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true), opt.Pin.Ls.Recursive()))
if err != nil {
t.Fatal(err)
}
Expand All @@ -179,7 +181,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd2.Cid()).String())
}

list, err = accPins(api.Pin().Ls(ctx, opt.Pin.Ls.Indirect()))
list, err = accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true), opt.Pin.Ls.Indirect()))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (tp *TestSuite) TestAddPinned(t *testing.T) {
t.Fatal(err)
}

pins, err := accPins(api.Pin().Ls(ctx))
pins, err := accPins(api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true)))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 89fc7f2

Please sign in to comment.