Skip to content

Commit d1a2ddc

Browse files
committed
Add support for inlining via the id-hash to the add command.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 56ba44e commit d1a2ddc

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

core/commands/add.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const (
4444
fstoreCacheOptionName = "fscache"
4545
cidVersionOptionName = "cid-version"
4646
hashOptionName = "hash"
47+
inlineOptionName = "inline"
48+
idHashLimitOptionName = "id-hash-limit"
4749
)
4850

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

177183
// The arguments are subject to the following constraints.
178184
//
@@ -279,7 +285,11 @@ You can now check what blocks have been created by:
279285
fileAdder.Silent = silent
280286
fileAdder.RawLeaves = rawblks
281287
fileAdder.NoCopy = nocopy
282-
fileAdder.CidBuilder = &prefix
288+
fileAdder.CidBuilder = prefix
289+
290+
if inline {
291+
fileAdder.CidBuilder = Inliner{fileAdder.CidBuilder, idHashLimit}
292+
}
283293

284294
if hash {
285295
md := dagtest.Mock()

core/commands/inliner.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package commands
2+
3+
import (
4+
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
5+
cid "gx/ipfs/Qmdu2AYUV7yMoVBQPxXNfe7FJcdx16kYtsx6jAPKWQYF1y/go-cid"
6+
)
7+
8+
// Inliner is a cid.Builder that will use the id multihash when the
9+
// size of the content is no more than limit
10+
type Inliner struct {
11+
base cid.Builder
12+
limit int
13+
}
14+
15+
// GetCodec implements the cid.Builder interface
16+
func (p Inliner) GetCodec() uint64 {
17+
return p.base.GetCodec()
18+
}
19+
20+
// WithCodec implements the cid.Builder interface
21+
func (p Inliner) WithCodec(c uint64) cid.Builder {
22+
return Inliner{p.base.WithCodec(c), p.limit}
23+
}
24+
25+
// Sum implements the cid.Builder interface
26+
func (p Inliner) Sum(data []byte) (*cid.Cid, error) {
27+
if len(data) > p.limit {
28+
return p.base.Sum(data)
29+
}
30+
return cid.V1Builder{Codec: p.base.GetCodec(), MhType: mh.ID}.Sum(data)
31+
}

test/sharness/t0040-add-and-cat.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ test_add_cat_expensive "--cid-version=1" "zdj7WcatQrtuE4WMkS4XsfsMixuQN2po4irkYh
590590
# encoded with the blake2b-256 hash funtion
591591
test_add_cat_expensive '--hash=blake2b-256' "zDMZof1kwndounDzQCANUHjiE3zt1mPEgx7RE3JTHoZrRRa79xcv"
592592

593-
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:"
593+
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:"
594594

595595
test_add_pwd_is_symlink
596596

test/sharness/t0046-id-hash.sh

+35
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ test_expect_success "can still fetch it" '
4343
test_cmp junk.txt actual
4444
'
4545

46+
test_expect_success "ipfs add --inline works as expected" '
47+
echo $ID_HASH0_CONTENTS > afile &&
48+
HASH=$(ipfs add -q --inline afile)
49+
'
50+
51+
test_expect_success "ipfs add --inline uses id multihash" '
52+
MHTYPE=`cid-fmt %h $HASH`
53+
echo "mhtype is $MHTYPE"
54+
test "$MHTYPE" = id
55+
'
56+
57+
test_expect_success "ipfs add --inline --raw-leaves works as expected" '
58+
echo $ID_HASH0_CONTENTS > afile &&
59+
HASH=$(ipfs add -q --inline --raw-leaves afile)
60+
'
61+
62+
test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash" '
63+
echo "$ID_HASH0" = "$HASH" &&
64+
test "$ID_HASH0" = "$HASH"
65+
'
66+
67+
test_expect_success "create 1000 bytes file and get its hash" '
68+
random 1000 2 > 1000bytes &&
69+
HASH0=$(ipfs add -q --raw-leaves --only-hash 1000bytes)
70+
'
71+
72+
test_expect_success "ipfs add --inline --raw-leaves works as expected on large file" '
73+
HASH=$(ipfs add -q --inline --raw-leaves 1000bytes)
74+
'
75+
76+
test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash on large file" '
77+
echo "$HASH0" = "$HASH" &&
78+
test "$HASH0" = "$HASH"
79+
'
80+
4681
test_expect_success "enable filestore" '
4782
ipfs config --json Experimental.FilestoreEnabled true
4883
'

0 commit comments

Comments
 (0)