From 21527fa6cb6a12b08c67aa09e0aa8b638394be0f Mon Sep 17 00:00:00 2001 From: Iaroslav Gridin Date: Tue, 4 Jun 2019 16:18:30 +0300 Subject: [PATCH] Support named pins [https://github.com/ipfs/go-ipfs/pull/4757] --- options/block.go | 10 +++++++++ options/object.go | 10 +++++++++ options/pin.go | 55 ++++++++++++++++++++++++++++------------------- options/unixfs.go | 11 ++++++++++ pin.go | 16 +++++++++----- tests/block.go | 4 ++-- tests/pin.go | 24 +++++++++++---------- tests/unixfs.go | 2 +- 8 files changed, 91 insertions(+), 41 deletions(-) diff --git a/options/block.go b/options/block.go index 043dfde..b566cc3 100644 --- a/options/block.go +++ b/options/block.go @@ -11,6 +11,7 @@ type BlockPutSettings struct { MhType uint64 MhLength int Pin bool + PinPath string } type BlockRmSettings struct { @@ -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 { @@ -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 { diff --git a/options/object.go b/options/object.go index e484a9f..3847e3a 100644 --- a/options/object.go +++ b/options/object.go @@ -8,6 +8,7 @@ type ObjectPutSettings struct { InputEnc string DataType string Pin bool + PinPath string } type ObjectAddLinkSettings struct { @@ -37,6 +38,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) { InputEnc: "json", DataType: "text", Pin: false, + PinPath: "added/", } for _, opt := range opts { @@ -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 { diff --git a/options/pin.go b/options/pin.go index 6b211bb..d16d798 100644 --- a/options/pin.go +++ b/options/pin.go @@ -2,10 +2,12 @@ package options type PinAddSettings struct { Recursive bool + PinPath string } type PinLsSettings struct { - Type string + Type string + Recursive bool } // PinRmSettings represents the settings of pin rm command @@ -14,7 +16,6 @@ type PinRmSettings struct { } type PinUpdateSettings struct { - Unpin bool } type PinAddOption func(*PinAddSettings) error @@ -29,6 +30,7 @@ type PinUpdateOption func(*PinUpdateSettings) error func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) { options := &PinAddSettings{ Recursive: true, + PinPath: "default/", } for _, opt := range opts { @@ -58,7 +60,8 @@ func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) { func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) { options := &PinLsSettings{ - Type: "all", + Type: "all", + Recursive: false, } for _, opt := range opts { @@ -72,9 +75,7 @@ func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) { } func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) { - options := &PinUpdateSettings{ - Unpin: true, - } + options := &PinUpdateSettings{} for _, opt := range opts { err := opt(options) @@ -89,7 +90,8 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) { type pinType struct{} type pinOpts struct { - Type pinType + Type pinType + Recursively bool } var Pin pinOpts @@ -100,24 +102,42 @@ func (pinType) All() PinLsOption { return Pin.pinType("all") } -// Recursive is an option for Pin.Ls which will make it only return recursive -// pins -func (pinType) Recursive() PinLsOption { - return Pin.pinType("recursive") -} - // Direct is an option for Pin.Ls which will make it only return direct (non // recursive) pins func (pinType) Direct() PinLsOption { return Pin.pinType("direct") } +// Recursive is an option for Pin.Ls which will make it only return recursive (non +// direct) pins +func (pinType) Recursive() PinLsOption { + return Pin.pinType("recursive") +} + // Indirect is an option for Pin.Ls which will make it only return indirect pins // (objects referenced by other recursively pinned objects) func (pinType) Indirect() PinLsOption { return Pin.pinType("indirect") } +// Recursive is an option for Pin.Ls which allows to specify whether +// argument should be recursively searched for pins +func (pinOpts) RecursiveList(recursive bool) PinLsOption { + return func(settings *PinLsSettings) error { + settings.Recursive = recursive + return nil + } +} + +// PinPath is an option for Pin.Add which specifies pin path +// default: "default/" +func (pinOpts) PinPath(pinPath string) PinAddOption { + return func(settings *PinAddSettings) error { + settings.PinPath = pinPath + return nil + } +} + // Recursive is an option for Pin.Add which specifies whether to pin an entire // object tree or just one object. Default: true func (pinOpts) Recursive(recursive bool) PinAddOption { @@ -152,12 +172,3 @@ func (pinOpts) pinType(t string) PinLsOption { 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 - } -} diff --git a/options/unixfs.go b/options/unixfs.go index 3fd96f7..b78a52e 100644 --- a/options/unixfs.go +++ b/options/unixfs.go @@ -29,6 +29,7 @@ type UnixfsAddSettings struct { Layout Layout Pin bool + PinPath string OnlyHash bool FsCache bool NoCopy bool @@ -59,6 +60,7 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, Layout: BalancedLayout, Pin: false, + PinPath: "added", OnlyHash: false, FsCache: false, NoCopy: false, @@ -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 { diff --git a/pin.go b/pin.go index 7df2956..67baf93 100644 --- a/pin.go +++ b/pin.go @@ -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 } @@ -20,6 +23,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 @@ -38,17 +44,17 @@ 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) ([]Pin, error) + Ls(context.Context, string, ...options.PinLsOption) ([]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) diff --git a/tests/block.go b/tests/block.go index 6b648f3..18113c1 100644 --- a/tests/block.go +++ b/tests/block.go @@ -216,7 +216,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") } @@ -225,7 +225,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) { t.Fatal(err) } - pins, err := api.Pin().Ls(ctx) + pins, err := api.Pin().Ls(ctx, "", opt.Pin.RecursiveList(true)) if err != nil { return } diff --git a/tests/pin.go b/tests/pin.go index 9b28a68..e0fbca1 100644 --- a/tests/pin.go +++ b/tests/pin.go @@ -14,6 +14,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 { @@ -40,7 +42,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) } @@ -59,12 +61,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 := api.Pin().Ls(ctx) + list, err := api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } @@ -81,12 +83,12 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) { t.Error("unexpected pin type") } - err = api.Pin().Rm(ctx, p) + err = api.Pin().Rm(ctx, testPrefix+p.Cid().String()) if err != nil { t.Fatal(err) } - list, err = api.Pin().Ls(ctx) + list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } @@ -128,17 +130,17 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) { t.Fatal(err) } - err = api.Pin().Add(ctx, path.IpldPath(nd2.Cid())) + err = api.Pin().Add(ctx, testPrefix, path.IpldPath(nd2.Cid())) if err != nil { t.Fatal(err) } - err = api.Pin().Add(ctx, path.IpldPath(nd3.Cid()), opt.Pin.Recursive(false)) + err = api.Pin().Add(ctx, testPrefix, path.IpldPath(nd3.Cid()), opt.Pin.Recursive(false)) if err != nil { t.Fatal(err) } - list, err := api.Pin().Ls(ctx) + list, err := api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } @@ -147,7 +149,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) { t.Errorf("unexpected pin list len: %d", len(list)) } - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct()) + list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.Type.Direct(), opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } @@ -160,7 +162,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) { t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd2.Cid()).String()) } - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive()) + list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.Type.Recursive(), opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } @@ -173,7 +175,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) { t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String()) } - list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect()) + list, err = api.Pin().Ls(ctx, "", opt.Pin.Type.Indirect(), opt.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) } diff --git a/tests/unixfs.go b/tests/unixfs.go index 47ce505..b934ae8 100644 --- a/tests/unixfs.go +++ b/tests/unixfs.go @@ -541,7 +541,7 @@ func (tp *TestSuite) TestAddPinned(t *testing.T) { t.Fatal(err) } - pins, err := api.Pin().Ls(ctx) + pins, err := api.Pin().Ls(ctx, "", options.Pin.RecursiveList(true)) if err != nil { t.Fatal(err) }