From c614d37c78d072bb92df4ae65c1e9d8492f60120 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 17 Aug 2018 18:08:38 -0400 Subject: [PATCH 1/2] Add Inliner CID Builder. --- inliner.go | 26 ++++++++++++++++++++++++++ inliner_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 inliner.go create mode 100644 inliner_test.go diff --git a/inliner.go b/inliner.go new file mode 100644 index 0000000..39dd405 --- /dev/null +++ b/inliner.go @@ -0,0 +1,26 @@ +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) +} diff --git a/inliner_test.go b/inliner_test.go new file mode 100644 index 0000000..2de9687 --- /dev/null +++ b/inliner_test.go @@ -0,0 +1,33 @@ +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") + } +} From 4709f60d494d1879e18eb1d187eec9b2b47f1eda Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Tue, 21 Aug 2018 14:38:56 -0400 Subject: [PATCH 2/2] Rename Inliner to InlineBuilder. --- inliner.go => inline.go | 14 +++++++------- inliner_test.go => inline_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) rename inliner.go => inline.go (52%) rename inliner_test.go => inline_test.go (73%) diff --git a/inliner.go b/inline.go similarity index 52% rename from inliner.go rename to inline.go index 39dd405..f623099 100644 --- a/inliner.go +++ b/inline.go @@ -5,20 +5,20 @@ import ( mhash "github.com/multiformats/go-multihash" ) -// Inliner is a cid.Builder that will use the id multihash when the +// InlineBuilder 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 +type InlineBuilder struct { + cid.Builder // Parent Builder + Limit int // Limit (inclusive) } // WithCodec implements the cid.Builder interface -func (p Inliner) WithCodec(c uint64) cid.Builder { - return Inliner{p.Builder.WithCodec(c), p.Limit} +func (p InlineBuilder) WithCodec(c uint64) cid.Builder { + return InlineBuilder{p.Builder.WithCodec(c), p.Limit} } // Sum implements the cid.Builder interface -func (p Inliner) Sum(data []byte) (*cid.Cid, error) { +func (p InlineBuilder) Sum(data []byte) (*cid.Cid, error) { if len(data) > p.Limit { return p.Builder.Sum(data) } diff --git a/inliner_test.go b/inline_test.go similarity index 73% rename from inliner_test.go rename to inline_test.go index 2de9687..99bad7d 100644 --- a/inliner_test.go +++ b/inline_test.go @@ -8,8 +8,8 @@ import ( mhash "github.com/multiformats/go-multihash" ) -func TestInlinerSmallValue(t *testing.T) { - builder := Inliner{cid.V0Builder{}, 64} +func TestInlineBuilderSmallValue(t *testing.T) { + builder := InlineBuilder{cid.V0Builder{}, 64} c, err := builder.Sum([]byte("Hello World")) if err != nil { t.Fatal(err) @@ -19,8 +19,8 @@ func TestInlinerSmallValue(t *testing.T) { } } -func TestInlinerLargeValue(t *testing.T) { - builder := Inliner{cid.V0Builder{}, 64} +func TestInlinerBuilderLargeValue(t *testing.T) { + builder := InlineBuilder{cid.V0Builder{}, 64} data := make([]byte, 512) rand.Read(data) c, err := builder.Sum(data)