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
iaroslav-gridin authored and Voker57 committed Jun 6, 2019
1 parent f77aa7e commit 21527fa
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 41 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
55 changes: 33 additions & 22 deletions options/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +16,6 @@ type PinRmSettings struct {
}

type PinUpdateSettings struct {
Unpin bool
}

type PinAddOption func(*PinAddSettings) error
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
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
16 changes: 11 additions & 5 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 @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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
}
Expand Down
24 changes: 13 additions & 11 deletions tests/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 21527fa

Please sign in to comment.