Commit c614d37c authored by Kevin Atkinson's avatar Kevin Atkinson

Add Inliner CID Builder.

parent b6f51d59
package cidutil
import (
cid "github.com/ipfs/go-cid"
mhash "github.com/multiformats/go-multihash"
)
// 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 {
cid.Builder
Limit int
}
// WithCodec implements the cid.Builder interface
func (p Inliner) WithCodec(c uint64) cid.Builder {
return Inliner{p.Builder.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.Builder.Sum(data)
}
return cid.V1Builder{Codec: p.GetCodec(), MhType: mhash.ID}.Sum(data)
}
package cidutil
import (
"math/rand"
"testing"
cid "github.com/ipfs/go-cid"
mhash "github.com/multiformats/go-multihash"
)
func TestInlinerSmallValue(t *testing.T) {
builder := Inliner{cid.V0Builder{}, 64}
c, err := builder.Sum([]byte("Hello World"))
if err != nil {
t.Fatal(err)
}
if c.Prefix().MhType != mhash.ID {
t.Fatal("Inliner builder failed to use ID Multihash on small values")
}
}
func TestInlinerLargeValue(t *testing.T) {
builder := Inliner{cid.V0Builder{}, 64}
data := make([]byte, 512)
rand.Read(data)
c, err := builder.Sum(data)
if err != nil {
t.Fatal(err)
}
if c.Prefix().MhType == mhash.ID {
t.Fatal("Inliner builder used ID Multihash on large values")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment