Skip to content

Commit

Permalink
coreapi: Pin option for Object.Put
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Feb 4, 2018
1 parent a804b83 commit 5a9ac70
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ type ObjectAPI interface {
// * "base64"
WithDataType(t string) options.ObjectPutOption

// WithPin is an option for Put which specifies whether to pin the added
// objects, default is false
WithPin(bool) options.ObjectPutOption

// Get returns the node for the path
Get(context.Context, Path) (Node, error)

Expand Down
9 changes: 9 additions & 0 deletions core/coreapi/interface/options/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ObjectNewSettings struct {
type ObjectPutSettings struct {
InputEnc string
DataType string
Pin bool
}

type ObjectAddLinkSettings struct {
Expand Down Expand Up @@ -35,6 +36,7 @@ func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
options := &ObjectPutSettings{
InputEnc: "json",
DataType: "text",
Pin: false,
}

for _, opt := range opts {
Expand Down Expand Up @@ -83,6 +85,13 @@ func (api *ObjectOptions) WithDataType(t string) ObjectPutOption {
}
}

func (api *ObjectOptions) WithPin(pin bool) ObjectPutOption {
return func(settings *ObjectPutSettings) error {
settings.Pin = pin
return nil
}
}

func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
return func(settings *ObjectAddLinkSettings) error {
settings.Create = create
Expand Down
13 changes: 13 additions & 0 deletions core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
dag "github.com/ipfs/go-ipfs/merkledag"
dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
pin "github.com/ipfs/go-ipfs/pin"
ft "github.com/ipfs/go-ipfs/unixfs"

cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
Expand Down Expand Up @@ -119,11 +120,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 api.ParseCid(dagnode.Cid()), nil
}

Expand Down

0 comments on commit 5a9ac70

Please sign in to comment.