Skip to content

Commit

Permalink
Add support for inlining via the id-hash to the add command.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Aug 14, 2018
1 parent 56ba44e commit d1a2ddc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
12 changes: 11 additions & 1 deletion core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
fstoreCacheOptionName = "fscache"
cidVersionOptionName = "cid-version"
hashOptionName = "hash"
inlineOptionName = "inline"
idHashLimitOptionName = "id-hash-limit"
)

const adderOutChanSize = 8
Expand Down Expand Up @@ -120,6 +122,8 @@ You can now check what blocks have been created by:
cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
cmdkit.BoolOption(inlineOptionName, "Inline small objects using identity hash. (experimental)"),
cmdkit.IntOption(idHashLimitOptionName, "Identity hash maxium size. (experimental)").WithDefault(64),
},
PreRun: func(req *cmds.Request, env cmds.Environment) error {
quiet, _ := req.Options[quietOptionName].(bool)
Expand Down Expand Up @@ -173,6 +177,8 @@ You can now check what blocks have been created by:
fscache, _ := req.Options[fstoreCacheOptionName].(bool)
cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
hashFunStr, _ := req.Options[hashOptionName].(string)
inline, _ := req.Options[inlineOptionName].(bool)
idHashLimit, _ := req.Options[idHashLimitOptionName].(int)

// The arguments are subject to the following constraints.
//
Expand Down Expand Up @@ -279,7 +285,11 @@ You can now check what blocks have been created by:
fileAdder.Silent = silent
fileAdder.RawLeaves = rawblks
fileAdder.NoCopy = nocopy
fileAdder.CidBuilder = &prefix
fileAdder.CidBuilder = prefix

if inline {
fileAdder.CidBuilder = Inliner{fileAdder.CidBuilder, idHashLimit}
}

if hash {
md := dagtest.Mock()
Expand Down
31 changes: 31 additions & 0 deletions core/commands/inliner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package commands

import (
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
cid "gx/ipfs/Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y/go-cid"
)

// Inliner is a cid.Builder that will use the id multihash when the
// size of the content is no more than limit
type Inliner struct {
base cid.Builder
limit int
}

// GetCodec implements the cid.Builder interface
func (p Inliner) GetCodec() uint64 {
return p.base.GetCodec()
}

// WithCodec implements the cid.Builder interface
func (p Inliner) WithCodec(c uint64) cid.Builder {
return Inliner{p.base.WithCodec(c), p.limit}
}

// Sum implements the cid.Builder interface
func (p Inliner) Sum(data []byte) (*cid.Cid, error) {
if len(data) > p.limit {
return p.base.Sum(data)
}
return cid.V1Builder{Codec: p.base.GetCodec(), MhType: mh.ID}.Sum(data)
}
2 changes: 1 addition & 1 deletion test/sharness/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ test_add_cat_expensive "--cid-version=1" "zdj7WcatQrtuE4WMkS4XsfsMixuQN2po4irkYh
# encoded with the blake2b-256 hash funtion
test_add_cat_expensive '--hash=blake2b-256' "zDMZof1kwndounDzQCANUHjiE3zt1mPEgx7RE3JTHoZrRRa79xcv"

test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&pin=true&progress=true&recursive=true&stream-channels=true:"
test_add_named_pipe " Post http://$API_ADDR/api/v0/add?chunker=size-262144&encoding=json&hash=sha2-256&id-hash-limit=64&pin=true&progress=true&recursive=true&stream-channels=true:"

test_add_pwd_is_symlink

Expand Down
35 changes: 35 additions & 0 deletions test/sharness/t0046-id-hash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ test_expect_success "can still fetch it" '
test_cmp junk.txt actual
'

test_expect_success "ipfs add --inline works as expected" '
echo $ID_HASH0_CONTENTS > afile &&
HASH=$(ipfs add -q --inline afile)
'

test_expect_success "ipfs add --inline uses id multihash" '
MHTYPE=`cid-fmt %h $HASH`
echo "mhtype is $MHTYPE"
test "$MHTYPE" = id
'

test_expect_success "ipfs add --inline --raw-leaves works as expected" '
echo $ID_HASH0_CONTENTS > afile &&
HASH=$(ipfs add -q --inline --raw-leaves afile)
'

test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash" '
echo "$ID_HASH0" = "$HASH" &&
test "$ID_HASH0" = "$HASH"
'

test_expect_success "create 1000 bytes file and get its hash" '
random 1000 2 > 1000bytes &&
HASH0=$(ipfs add -q --raw-leaves --only-hash 1000bytes)
'

test_expect_success "ipfs add --inline --raw-leaves works as expected on large file" '
HASH=$(ipfs add -q --inline --raw-leaves 1000bytes)
'

test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash on large file" '
echo "$HASH0" = "$HASH" &&
test "$HASH0" = "$HASH"
'

test_expect_success "enable filestore" '
ipfs config --json Experimental.FilestoreEnabled true
'
Expand Down

0 comments on commit d1a2ddc

Please sign in to comment.