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 Jul 30, 2018
1 parent 6515128 commit a59e957
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 11 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 @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
pin "github.com/ipfs/go-ipfs/pin"
ft "github.com/ipfs/go-ipfs/unixfs"
dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag"

dag "gx/ipfs/QmRy4Qk9hbgFX9NGJRm8rBThrA8PZhNCitMgeRYyZ67s59/go-merkledag"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a59e957

Please sign in to comment.