@cryptotaxi247 / kubo / commits / 7ac229e28

coreapi: Pin option for Object.Put

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Feb 2, 2018 at 16:28 UTC 7ac229e28620738112953ce89fae0e5a404e248b
2 files changed +25 -1
core/coreapi/interface/options/object.go
+11
@@ -7,6 +7,7 @@ type ObjectNewSettings struct {
7 type ObjectPutSettings struct {
8 InputEnc string
9 DataType string
10 + Pin bool
11 }
12
13 type ObjectAddLinkSettings struct {
@@ -35,6 +36,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
36 options := &ObjectPutSettings{
37 InputEnc: "json",
38 DataType: "text",
39 + Pin: false,
40 }
41
42 for _, opt := range opts {
@@ -103,6 +105,15 @@ func (objectOpts) DataType(t string) ObjectPutOption {
105 }
106 }
107
108 +// WithPin is an option for Object.Put which specifies whether to pin the added
109 +// objects, default is false
110 +func (objectOpts) WithPin(pin bool) ObjectPutOption {
111 + return func(settings *ObjectPutSettings) error {
112 + settings.Pin = pin
113 + return nil
114 + }
115 +}
116 +
117 // Create is an option for Object.AddLink which specifies whether create required
118 // directories for the child
119 func (objectOpts) Create(create bool) ObjectAddLinkOption {
core/coreapi/object.go
+14 -1
@@ -14,9 +14,10 @@ import (
14 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15 caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16 "github.com/ipfs/go-ipfs/dagutils"
17 + "github.com/ipfs/go-ipfs/pin"
18 +
19 dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag"
20 ft "gx/ipfs/QmSaz8Qg77gGqvDvLKeSAY7ivDEnramSWF6T7TcRwFpHtP/go-unixfs"
19 -
21 cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
22 ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
23 )
@@ -116,11 +117,23 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
117 return nil, err
118 }
119
120 + if options.Pin {
121 + defer api.node.Blockstore.PinLock().Unlock()
122 + }
123 +
124 err = api.node.DAG.Add(ctx, dagnode)
125 if err != nil {
126 return nil, err
127 }
128
129 + if options.Pin {
130 + api.node.Pinning.PinWithMode(dagnode.Cid(), pin.Recursive)
131 + err = api.node.Pinning.Flush()
132 + if err != nil {
133 + return nil, err
134 + }
135 + }
136 +
137 return coreiface.IpfsPath(dagnode.Cid()), nil
138 }
139