Skip to content

Commit ada15d0

Browse files
committed
Named pins, storing pins in Datastore
* Change pin structure to a tree stored in KV store * Adjust pin-related commands accordingly * Make pinning routines use meaningful default prefixes License: MIT Signed-off-by: Iaroslav Gridin <[email protected]>
1 parent c2e7fe6 commit ada15d0

40 files changed

+661
-1230
lines changed

assets/assets.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,9 @@ func addAssetList(nd *core.IpfsNode, l []string) (cid.Cid, error) {
8080
return cid.Cid{}, err
8181
}
8282

83-
if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
83+
if err := nd.Pinning.Pin(nd.Context(), "assets", dir, true); err != nil {
8484
return cid.Cid{}, fmt.Errorf("assets: Pinning on init-docu failed: %s", err)
8585
}
8686

87-
if err := nd.Pinning.Flush(); err != nil {
88-
return cid.Cid{}, fmt.Errorf("assets: Pinning flush failed: %s", err)
89-
}
90-
9187
return dir.Cid(), nil
9288
}

core/builder.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
268268
n.Blocks = bserv.New(n.Blockstore, n.Exchange)
269269
n.DAG = dag.NewDAGService(n.Blocks)
270270

271-
internalDag := dag.NewDAGService(bserv.New(n.Blockstore, offline.Exchange(n.Blockstore)))
272-
n.Pinning, err = pin.LoadPinner(n.Repo.Datastore(), n.DAG, internalDag)
273-
if err != nil {
274-
// TODO: we should move towards only running 'NewPinner' explicitly on
275-
// node init instead of implicitly here as a result of the pinner keys
276-
// not being found in the datastore.
277-
// this is kinda sketchy and could cause data loss
278-
n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG, internalDag)
279-
}
271+
n.Pinning = pin.NewPinner(n.DAG, n.Repo.Datastore())
280272
n.Resolver = resolver.NewBasicResolver(n.DAG)
281273

282274
if cfg.Online {

core/commands/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
onlyHashOptionName = "only-hash"
4040
chunkerOptionName = "chunker"
4141
pinOptionName = "pin"
42+
pinPathOptionName = "pinpath"
4243
rawLeavesOptionName = "raw-leaves"
4344
noCopyOptionName = "nocopy"
4445
fstoreCacheOptionName = "fscache"
@@ -119,6 +120,7 @@ You can now check what blocks have been created by:
119120
cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
120121
cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"),
121122
cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
123+
cmdkit.StringOption(pinPathOptionName, "P", "Pin object under this path.").WithDefault("added/"),
122124
cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. (experimental)"),
123125
cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
124126
cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
@@ -160,6 +162,7 @@ You can now check what blocks have been created by:
160162
silent, _ := req.Options[silentOptionName].(bool)
161163
chunker, _ := req.Options[chunkerOptionName].(string)
162164
dopin, _ := req.Options[pinOptionName].(bool)
165+
pinPath, _ := req.Options[pinPathOptionName].(string)
163166
rawblks, rbset := req.Options[rawLeavesOptionName].(bool)
164167
nocopy, _ := req.Options[noCopyOptionName].(bool)
165168
fscache, _ := req.Options[fstoreCacheOptionName].(bool)
@@ -185,6 +188,7 @@ You can now check what blocks have been created by:
185188
options.Unixfs.Chunker(chunker),
186189

187190
options.Unixfs.Pin(dopin),
191+
options.Unixfs.PinPath(pinPath),
188192
options.Unixfs.HashOnly(hash),
189193
options.Unixfs.FsCache(fscache),
190194
options.Unixfs.Nocopy(nocopy),

core/commands/dag/dag.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ into an object of the specified format.
6060
Options: []cmdkit.Option{
6161
cmdkit.StringOption("format", "f", "Format that the object will be added as.").WithDefault("cbor"),
6262
cmdkit.StringOption("input-enc", "Format that the input object will be.").WithDefault("json"),
63-
cmdkit.BoolOption("pin", "Pin this object when adding."),
63+
cmdkit.StringOption("pin", "Pin this object when adding.").WithDefault(""),
6464
cmdkit.StringOption("hash", "Hash function to use").WithDefault(""),
6565
},
6666
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
@@ -72,7 +72,7 @@ into an object of the specified format.
7272
ienc, _ := req.Options["input-enc"].(string)
7373
format, _ := req.Options["format"].(string)
7474
hash, _ := req.Options["hash"].(string)
75-
dopin, _ := req.Options["pin"].(bool)
75+
pinpath, _, err := req.Options["pin"].(string)
7676

7777
// mhType tells inputParser which hash should be used. MaxUint64 means 'use
7878
// default hash' (sha256 for cbor, sha1 for git..)
@@ -128,13 +128,11 @@ into an object of the specified format.
128128
return err
129129
}
130130

131-
if dopin {
132-
cids.ForEach(func(c cid.Cid) error {
133-
nd.Pinning.PinWithMode(c, pin.Recursive)
134-
return nil
131+
if pinpath != "" {
132+
err := cids.ForEach(func(c cid.Cid) error {
133+
return n.Pinning.AddPin(pinpath, c, true)
135134
})
136135

137-
err := nd.Pinning.Flush()
138136
if err != nil {
139137
return err
140138
}

core/commands/object/object.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ And then run:
383383
cmdkit.StringOption("inputenc", "Encoding type of input data. One of: {\"protobuf\", \"json\"}.").WithDefault("json"),
384384
cmdkit.StringOption("datafieldenc", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
385385
cmdkit.BoolOption("pin", "Pin this object when adding."),
386+
cmdkit.StringOption("pinpath", "Pin under this path").WithDefault("added/"),
386387
cmdkit.BoolOption("quiet", "q", "Write minimal output."),
387388
},
388389
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
@@ -406,15 +407,22 @@ And then run:
406407
return err
407408
}
408409

409-
dopin, _ := req.Options["pin"].(bool)
410+
pinpath, _, err := req.Options["pinpath"].(string)
411+
if err != nil {
412+
res.SetError(err, cmdkit.ErrNormal)
413+
return
414+
}
415+
416+
pin, _, err := req.Option("pin").Bool()
410417
if err != nil {
411418
return err
412419
}
413420

414421
p, err := api.Object().Put(req.Context, file,
415422
options.Object.DataType(datafieldenc),
416423
options.Object.InputEnc(inputenc),
417-
options.Object.Pin(dopin))
424+
options.Object.Pin(pin),
425+
options.Object.PinPath(pinpath))
418426
if err != nil {
419427
return err
420428
}

0 commit comments

Comments
 (0)