Skip to content

Commit

Permalink
"add": add "--allow-dup" option.
Browse files Browse the repository at this point in the history
This option adds a files the the primary blockstore even if the block
is in another blockstore such as the filestore.

License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed Sep 25, 2016
1 parent 70dc15a commit 67da189
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 9 additions & 0 deletions blocks/blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ type GCBlockstore interface {
GCLocker
}

func NewGCBlockstore (bs Blockstore, gcl GCLocker) GCBlockstore {
return gcBlockstore {bs,gcl}
}

type gcBlockstore struct {
Blockstore
GCLocker
}

func NewBlockstore(d ds.Batching) *blockstore {
return NewBlockstoreWPrefix(d, "")
}
Expand Down
28 changes: 22 additions & 6 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/ipfs/go-ipfs/core/coreunix"
"gx/ipfs/QmeWjRodbcZFKe5tMN7poEx3izym6osrLSnTLf9UjJZBbs/pb"

blockservice "github.com/ipfs/go-ipfs/blockservice"
bs "github.com/ipfs/go-ipfs/blocks/blockstore"
bserv "github.com/ipfs/go-ipfs/blockservice"
cmds "github.com/ipfs/go-ipfs/commands"
files "github.com/ipfs/go-ipfs/commands/files"
core "github.com/ipfs/go-ipfs/core"
Expand All @@ -32,6 +33,7 @@ const (
onlyHashOptionName = "only-hash"
chunkerOptionName = "chunker"
pinOptionName = "pin"
allowDupName = "allow-dup"
)

var AddCmd = &cmds.Command{
Expand Down Expand Up @@ -78,6 +80,7 @@ You can now refer to the added file in a gateway, like so:
cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add.").Default(false),
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm to use."),
cmds.BoolOption(pinOptionName, "Pin this object when adding.").Default(true),
cmds.BoolOption(allowDupName, "Add even if blocks are in non-cache blockstore.").Default(false),
},
PreRun: func(req cmds.Request) error {
if quiet, _, _ := req.Option(quietOptionName).Bool(); quiet {
Expand Down Expand Up @@ -135,6 +138,7 @@ You can now refer to the added file in a gateway, like so:
silent, _, _ := req.Option(silentOptionName).Bool()
chunker, _, _ := req.Option(chunkerOptionName).String()
dopin, _, _ := req.Option(pinOptionName).Bool()
allowDup, _, _ := req.Option(allowDupName).Bool()

if hash {
nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
Expand All @@ -149,18 +153,30 @@ You can now refer to the added file in a gateway, like so:
n = nilnode
}

dserv := n.DAG
exchange := n.Exchange
local, _, _ := req.Option("local").Bool()
if local {
offlineexch := offline.Exchange(n.Blockstore)
bserv := blockservice.New(n.Blockstore, offlineexch)
dserv = dag.NewDAGService(bserv)
exchange = offline.Exchange(n.Blockstore)
}

outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))

fileAdder, err := coreunix.NewAdder(req.Context(), n.Pinning, n.Blockstore, dserv)
var fileAdder *coreunix.Adder
if allowDup {
// add directly to the first mount bypassing
// the Has() check of the multi-blockstore
blockstore := bs.NewGCBlockstore(n.Blockstore.FirstMount(), n.Blockstore)
blockService := bserv.New(blockstore, exchange)
dagService := dag.NewDAGService(blockService)
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, blockstore, dagService)
} else if exchange != n.Exchange {
blockService := bserv.New(n.Blockstore, exchange)
dagService := dag.NewDAGService(blockService)
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, n.Blockstore, dagService)
} else {
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, n.Blockstore, n.DAG)
}
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down

0 comments on commit 67da189

Please sign in to comment.