提交 7ac229e2 作者: Łukasz Magiera

coreapi: Pin option for Object.Put

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 76a897cc
......@@ -7,6 +7,7 @@ type ObjectNewSettings struct {
type ObjectPutSettings struct {
InputEnc string
DataType string
Pin bool
}
type ObjectAddLinkSettings struct {
......@@ -35,6 +36,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
options := &ObjectPutSettings{
InputEnc: "json",
DataType: "text",
Pin: false,
}
for _, opt := range opts {
......@@ -103,6 +105,15 @@ func (objectOpts) DataType(t string) ObjectPutOption {
}
}
// WithPin is an option for Object.Put which specifies whether to pin the added
// objects, default is false
func (objectOpts) WithPin(pin bool) ObjectPutOption {
return func(settings *ObjectPutSettings) error {
settings.Pin = pin
return nil
}
}
// Create is an option for Object.AddLink which specifies whether create required
// directories for the child
func (objectOpts) Create(create bool) ObjectAddLinkOption {
......
......@@ -14,9 +14,10 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"github.com/ipfs/go-ipfs/dagutils"
"github.com/ipfs/go-ipfs/pin"
dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag"
ft "gx/ipfs/QmSaz8Qg77gGqvDvLKeSAY7ivDEnramSWF6T7TcRwFpHtP/go-unixfs"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
)
......@@ -116,11 +117,23 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
return nil, err
}
if options.Pin {
defer api.node.Blockstore.PinLock().Unlock()
}
err = api.node.DAG.Add(ctx, dagnode)
if err != nil {
return nil, err
}
if options.Pin {
api.node.Pinning.PinWithMode(dagnode.Cid(), pin.Recursive)
err = api.node.Pinning.Flush()
if err != nil {
return nil, err
}
}
return coreiface.IpfsPath(dagnode.Cid()), nil
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论