Skip to content

Commit 21527fa

Browse files
iaroslav-gridinVoker57
authored andcommitted
Support named pins [ipfs/kubo#4757]
1 parent f77aa7e commit 21527fa

File tree

8 files changed

+91
-41
lines changed

8 files changed

+91
-41
lines changed

options/block.go

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type BlockPutSettings struct {
1111
MhType uint64
1212
MhLength int
1313
Pin bool
14+
PinPath string
1415
}
1516

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

3133
for _, opt := range opts {
@@ -116,6 +118,14 @@ func (blockOpts) Pin(pin bool) BlockPutOption {
116118
}
117119
}
118120

121+
// PinPath is an option for Block.Put which specifies under which path to pin the block, default is "added/"
122+
func (blockOpts) PinPath(pinPath string) BlockPutOption {
123+
return func(settings *BlockPutSettings) error {
124+
settings.PinPath = pinPath
125+
return nil
126+
}
127+
}
128+
119129
// Force is an option for Block.Rm which, when set to true, will ignore
120130
// non-existing blocks
121131
func (blockOpts) Force(force bool) BlockRmOption {

options/object.go

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type ObjectPutSettings struct {
88
InputEnc string
99
DataType string
1010
Pin bool
11+
PinPath string
1112
}
1213

1314
type ObjectAddLinkSettings struct {
@@ -37,6 +38,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
3738
InputEnc: "json",
3839
DataType: "text",
3940
Pin: false,
41+
PinPath: "added/",
4042
}
4143

4244
for _, opt := range opts {
@@ -114,6 +116,14 @@ func (objectOpts) Pin(pin bool) ObjectPutOption {
114116
}
115117
}
116118

119+
// PinPath is an option for Object.Put which specifies under which path to pin the object, default is "added/"
120+
func (objectOpts) PinPath(pinPath string) ObjectPutOption {
121+
return func(settings *ObjectPutSettings) error {
122+
settings.PinPath = pinPath
123+
return nil
124+
}
125+
}
126+
117127
// Create is an option for Object.AddLink which specifies whether create required
118128
// directories for the child
119129
func (objectOpts) Create(create bool) ObjectAddLinkOption {

options/pin.go

+33-22
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package options
22

33
type PinAddSettings struct {
44
Recursive bool
5+
PinPath string
56
}
67

78
type PinLsSettings struct {
8-
Type string
9+
Type string
10+
Recursive bool
911
}
1012

1113
// PinRmSettings represents the settings of pin rm command
@@ -14,7 +16,6 @@ type PinRmSettings struct {
1416
}
1517

1618
type PinUpdateSettings struct {
17-
Unpin bool
1819
}
1920

2021
type PinAddOption func(*PinAddSettings) error
@@ -29,6 +30,7 @@ type PinUpdateOption func(*PinUpdateSettings) error
2930
func PinAddOptions(opts ...PinAddOption) (*PinAddSettings, error) {
3031
options := &PinAddSettings{
3132
Recursive: true,
33+
PinPath: "default/",
3234
}
3335

3436
for _, opt := range opts {
@@ -58,7 +60,8 @@ func PinRmOptions(opts ...PinRmOption) (*PinRmSettings, error) {
5860

5961
func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
6062
options := &PinLsSettings{
61-
Type: "all",
63+
Type: "all",
64+
Recursive: false,
6265
}
6366

6467
for _, opt := range opts {
@@ -72,9 +75,7 @@ func PinLsOptions(opts ...PinLsOption) (*PinLsSettings, error) {
7275
}
7376

7477
func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
75-
options := &PinUpdateSettings{
76-
Unpin: true,
77-
}
78+
options := &PinUpdateSettings{}
7879

7980
for _, opt := range opts {
8081
err := opt(options)
@@ -89,7 +90,8 @@ func PinUpdateOptions(opts ...PinUpdateOption) (*PinUpdateSettings, error) {
8990
type pinType struct{}
9091

9192
type pinOpts struct {
92-
Type pinType
93+
Type pinType
94+
Recursively bool
9395
}
9496

9597
var Pin pinOpts
@@ -100,24 +102,42 @@ func (pinType) All() PinLsOption {
100102
return Pin.pinType("all")
101103
}
102104

103-
// Recursive is an option for Pin.Ls which will make it only return recursive
104-
// pins
105-
func (pinType) Recursive() PinLsOption {
106-
return Pin.pinType("recursive")
107-
}
108-
109105
// Direct is an option for Pin.Ls which will make it only return direct (non
110106
// recursive) pins
111107
func (pinType) Direct() PinLsOption {
112108
return Pin.pinType("direct")
113109
}
114110

111+
// Recursive is an option for Pin.Ls which will make it only return recursive (non
112+
// direct) pins
113+
func (pinType) Recursive() PinLsOption {
114+
return Pin.pinType("recursive")
115+
}
116+
115117
// Indirect is an option for Pin.Ls which will make it only return indirect pins
116118
// (objects referenced by other recursively pinned objects)
117119
func (pinType) Indirect() PinLsOption {
118120
return Pin.pinType("indirect")
119121
}
120122

123+
// Recursive is an option for Pin.Ls which allows to specify whether
124+
// argument should be recursively searched for pins
125+
func (pinOpts) RecursiveList(recursive bool) PinLsOption {
126+
return func(settings *PinLsSettings) error {
127+
settings.Recursive = recursive
128+
return nil
129+
}
130+
}
131+
132+
// PinPath is an option for Pin.Add which specifies pin path
133+
// default: "default/"
134+
func (pinOpts) PinPath(pinPath string) PinAddOption {
135+
return func(settings *PinAddSettings) error {
136+
settings.PinPath = pinPath
137+
return nil
138+
}
139+
}
140+
121141
// Recursive is an option for Pin.Add which specifies whether to pin an entire
122142
// object tree or just one object. Default: true
123143
func (pinOpts) Recursive(recursive bool) PinAddOption {
@@ -152,12 +172,3 @@ func (pinOpts) pinType(t string) PinLsOption {
152172
return nil
153173
}
154174
}
155-
156-
// Unpin is an option for Pin.Update which specifies whether to remove the old pin.
157-
// Default is true.
158-
func (pinOpts) Unpin(unpin bool) PinUpdateOption {
159-
return func(settings *PinUpdateSettings) error {
160-
settings.Unpin = unpin
161-
return nil
162-
}
163-
}

options/unixfs.go

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type UnixfsAddSettings struct {
2929
Layout Layout
3030

3131
Pin bool
32+
PinPath string
3233
OnlyHash bool
3334
FsCache bool
3435
NoCopy bool
@@ -59,6 +60,7 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
5960
Layout: BalancedLayout,
6061

6162
Pin: false,
63+
PinPath: "added",
6264
OnlyHash: false,
6365
FsCache: false,
6466
NoCopy: false,
@@ -221,6 +223,15 @@ func (unixfsOpts) Pin(pin bool) UnixfsAddOption {
221223
}
222224
}
223225

226+
// PinPath tells the adder the pin path to use
227+
func (unixfsOpts) PinPath(pinPath string) UnixfsAddOption {
228+
return func(settings *UnixfsAddSettings) error {
229+
settings.PinPath = pinPath
230+
return nil
231+
}
232+
}
233+
234+
224235
// HashOnly will make the adder calculate data hash without storing it in the
225236
// blockstore or announcing it to the network
226237
func (unixfsOpts) HashOnly(hashOnly bool) UnixfsAddOption {

pin.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
type Pin interface {
1212
// Path to the pinned object
1313
Path() path.Resolved
14-
14+
15+
// Pinpath of pinned object
16+
PinPath() string
17+
1518
// Type of the pin
1619
Type() string
1720
}
@@ -20,6 +23,9 @@ type Pin interface {
2023
type PinStatus interface {
2124
// Ok indicates whether the pin has been verified to be correct
2225
Ok() bool
26+
27+
// Pinpath of pinned object
28+
PinPath() string
2329

2430
// BadNodes returns any bad (usually missing) nodes from the pin
2531
BadNodes() []BadPinNode
@@ -38,17 +44,17 @@ type BadPinNode interface {
3844
type PinAPI interface {
3945
// Add creates new pin, be default recursive - pinning the whole referenced
4046
// tree
41-
Add(context.Context, path.Path, ...options.PinAddOption) error
47+
Add(context.Context, string, path.Path, ...options.PinAddOption) error
4248

4349
// Ls returns list of pinned objects on this node
44-
Ls(context.Context, ...options.PinLsOption) ([]Pin, error)
50+
Ls(context.Context, string, ...options.PinLsOption) ([]Pin, error)
4551

4652
// Rm removes pin for object specified by the path
47-
Rm(context.Context, path.Path, ...options.PinRmOption) error
53+
Rm(context.Context, string, ...options.PinRmOption) error
4854

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

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

tests/block.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
216216
t.Fatal(err)
217217
}
218218

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

@@ -225,7 +225,7 @@ func (tp *TestSuite) TestBlockPin(t *testing.T) {
225225
t.Fatal(err)
226226
}
227227

228-
pins, err := api.Pin().Ls(ctx)
228+
pins, err := api.Pin().Ls(ctx, "", opt.Pin.RecursiveList(true))
229229
if err != nil {
230230
return
231231
}

tests/pin.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
ipld "github.com/ipfs/go-ipld-format"
1515
)
1616

17+
var testPrefix = "test/"
18+
1719
func (tp *TestSuite) TestPin(t *testing.T) {
1820
tp.hasApi(t, func(api iface.CoreAPI) error {
1921
if api.Pin() == nil {
@@ -40,7 +42,7 @@ func (tp *TestSuite) TestPinAdd(t *testing.T) {
4042
t.Fatal(err)
4143
}
4244

43-
err = api.Pin().Add(ctx, p)
45+
err = api.Pin().Add(ctx, testPrefix, p)
4446
if err != nil {
4547
t.Fatal(err)
4648
}
@@ -59,12 +61,12 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
5961
t.Fatal(err)
6062
}
6163

62-
err = api.Pin().Add(ctx, p)
64+
err = api.Pin().Add(ctx, testPrefix, p)
6365
if err != nil {
6466
t.Fatal(err)
6567
}
6668

67-
list, err := api.Pin().Ls(ctx)
69+
list, err := api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true))
6870
if err != nil {
6971
t.Fatal(err)
7072
}
@@ -81,12 +83,12 @@ func (tp *TestSuite) TestPinSimple(t *testing.T) {
8183
t.Error("unexpected pin type")
8284
}
8385

84-
err = api.Pin().Rm(ctx, p)
86+
err = api.Pin().Rm(ctx, testPrefix+p.Cid().String())
8587
if err != nil {
8688
t.Fatal(err)
8789
}
8890

89-
list, err = api.Pin().Ls(ctx)
91+
list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true))
9092
if err != nil {
9193
t.Fatal(err)
9294
}
@@ -128,17 +130,17 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
128130
t.Fatal(err)
129131
}
130132

131-
err = api.Pin().Add(ctx, path.IpldPath(nd2.Cid()))
133+
err = api.Pin().Add(ctx, testPrefix, path.IpldPath(nd2.Cid()))
132134
if err != nil {
133135
t.Fatal(err)
134136
}
135137

136-
err = api.Pin().Add(ctx, path.IpldPath(nd3.Cid()), opt.Pin.Recursive(false))
138+
err = api.Pin().Add(ctx, testPrefix, path.IpldPath(nd3.Cid()), opt.Pin.Recursive(false))
137139
if err != nil {
138140
t.Fatal(err)
139141
}
140142

141-
list, err := api.Pin().Ls(ctx)
143+
list, err := api.Pin().Ls(ctx, testPrefix, opt.Pin.RecursiveList(true))
142144
if err != nil {
143145
t.Fatal(err)
144146
}
@@ -147,7 +149,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
147149
t.Errorf("unexpected pin list len: %d", len(list))
148150
}
149151

150-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Direct())
152+
list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.Type.Direct(), opt.Pin.RecursiveList(true))
151153
if err != nil {
152154
t.Fatal(err)
153155
}
@@ -160,7 +162,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
160162
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpfsPath(nd2.Cid()).String())
161163
}
162164

163-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Recursive())
165+
list, err = api.Pin().Ls(ctx, testPrefix, opt.Pin.Type.Recursive(), opt.Pin.RecursiveList(true))
164166
if err != nil {
165167
t.Fatal(err)
166168
}
@@ -173,7 +175,7 @@ func (tp *TestSuite) TestPinRecursive(t *testing.T) {
173175
t.Errorf("unexpected path, %s != %s", list[0].Path().String(), path.IpldPath(nd3.Cid()).String())
174176
}
175177

176-
list, err = api.Pin().Ls(ctx, opt.Pin.Type.Indirect())
178+
list, err = api.Pin().Ls(ctx, "", opt.Pin.Type.Indirect(), opt.Pin.RecursiveList(true))
177179
if err != nil {
178180
t.Fatal(err)
179181
}

tests/unixfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func (tp *TestSuite) TestAddPinned(t *testing.T) {
541541
t.Fatal(err)
542542
}
543543

544-
pins, err := api.Pin().Ls(ctx)
544+
pins, err := api.Pin().Ls(ctx, "", options.Pin.RecursiveList(true))
545545
if err != nil {
546546
t.Fatal(err)
547547
}

0 commit comments

Comments
 (0)