Skip to content

Commit 56a41e4

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 a03dd74 commit 56a41e4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

core/commands/add.go

+10
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
//
@@ -281,6 +287,10 @@ You can now check what blocks have been created by:
281287
fileAdder.NoCopy = nocopy
282288
fileAdder.Prefix = &prefix
283289

290+
if inline {
291+
fileAdder.Prefix = Inliner{fileAdder.Prefix, idHashLimit}
292+
}
293+
284294
if hash {
285295
md := dagtest.Mock()
286296
emptyDirNode := ft.EmptyDirNode()

core/commands/inliner.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package commands
2+
3+
import (
4+
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
5+
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
6+
)
7+
8+
type Inliner struct {
9+
base cid.Format
10+
limit int
11+
}
12+
13+
func (p Inliner) GetCodec() uint64 {
14+
return p.base.GetCodec()
15+
}
16+
17+
func (p Inliner) WithCodec(c uint64) cid.Format {
18+
return Inliner{p.base.WithCodec(c), p.limit}
19+
}
20+
21+
func (p Inliner) Sum(data []byte) (*cid.Cid, error) {
22+
if len(data) > p.limit {
23+
return p.base.Sum(data)
24+
}
25+
return cid.FormatV1{Codec: p.base.GetCodec(), HashFun: mh.ID}.Sum(data)
26+
}

test/sharness/t0046-id-hash.sh

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ 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 outputs the correct hash" '
52+
echo zrjkGSFoQBjsuadykdx7acCCDAmcLX4HmLUFCs = "$HASH" &&
53+
test zrjkGSFoQBjsuadykdx7acCCDAmcLX4HmLUFCs = "$HASH"
54+
'
55+
56+
test_expect_success "ipfs add --inline --raw-leaves works as expected" '
57+
echo $ID_HASH0_CONTENTS > afile &&
58+
HASH=$(ipfs add -q --inline --raw-leaves afile)
59+
'
60+
61+
test_expect_success "ipfs add --inline --raw-leaves outputs the correct hash" '
62+
echo "$ID_HASH0" = "$HASH" &&
63+
test "$ID_HASH0" = "$HASH"
64+
'
65+
4666
test_expect_success "enable filestore" '
4767
ipfs config --json Experimental.FilestoreEnabled true
4868
'

0 commit comments

Comments
 (0)