Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: remove builder return param from init #2165

Merged
merged 6 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/shares/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
// provided should be the namespace and shareVersion of the blob that precedes
// this padding in the data square.
func NamespacePaddingShare(ns appns.Namespace, shareVersion uint8) (Share, error) {
b, err := NewBuilder(ns, shareVersion, true).Init()
b := NewBuilder(ns, shareVersion, true)
err := b.Init()
if err != nil {
return Share{}, err
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/shares/share_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func NewEmptyBuilder() *Builder {
}
}

// Init() needs to be called right after this method
// NewBuilder returns a new share builder. Init() needs to be called right after
// this method.
func NewBuilder(ns appns.Namespace, shareVersion uint8, isFirstShare bool) *Builder {
return &Builder{
namespace: ns,
Expand All @@ -32,18 +33,12 @@ func NewBuilder(ns appns.Namespace, shareVersion uint8, isFirstShare bool) *Buil
}
}

func (b *Builder) Init() (*Builder, error) {
// Init initializes the share builder. It should be called right after NewBuilder.
func (b *Builder) Init() error {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
if b.isCompactShare {
if err := b.prepareCompactShare(); err != nil {
return nil, err
}
} else {
if err := b.prepareSparseShare(); err != nil {
return nil, err
}
return b.prepareCompactShare()
}

return b, nil
return b.prepareSparseShare()
}

func (b *Builder) AvailableBytes() int {
Expand Down
6 changes: 3 additions & 3 deletions pkg/shares/share_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestShareBuilderIsEmptyShare(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.builder.Init()
err := tc.builder.Init()
require.NoError(t, err)
tc.builder.AddData(tc.data)
assert.Equal(t, tc.want, tc.builder.IsEmptyShare())
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestShareBuilderWriteSequenceLen(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.builder.Init()
err := tc.builder.Init()
require.NoError(t, err)
if err := tc.builder.WriteSequenceLen(tc.wantLen); tc.wantErr {
assert.Error(t, err)
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestShareBuilderAddData(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.builder.Init()
err := tc.builder.Init()
require.NoError(t, err)

got := tc.builder.AddData(tc.data)
Expand Down
9 changes: 6 additions & 3 deletions pkg/shares/split_compact_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type CompactShareSplitter struct {
// NewCompactShareSplitter returns a CompactShareSplitter using the provided
// namespace and shareVersion.
func NewCompactShareSplitter(ns appns.Namespace, shareVersion uint8) *CompactShareSplitter {
sb, err := NewBuilder(ns, shareVersion, true).Init()
sb := NewBuilder(ns, shareVersion, true)
err := sb.Init()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -105,7 +106,8 @@ func (css *CompactShareSplitter) stackPending() error {
css.shares = append(css.shares, *pendingShare)

// Now we need to create a new builder
css.shareBuilder, err = NewBuilder(css.namespace, css.shareVersion, false).Init()
css.shareBuilder = NewBuilder(css.namespace, css.shareVersion, false)
err = css.shareBuilder.Init()
return err
}

Expand Down Expand Up @@ -163,7 +165,8 @@ func (css *CompactShareSplitter) writeSequenceLen(sequenceLen uint32) error {
}

// We may find a more efficient way to write seqLen
b, err := NewBuilder(css.namespace, css.shareVersion, true).Init()
b := NewBuilder(css.namespace, css.shareVersion, true)
err := b.Init()
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/shares/split_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (sss *SparseShareSplitter) Write(blob coretypes.Blob) error {
}

// First share
b, err := NewBuilder(blobNamespace, blob.ShareVersion, true).Init()
b := NewBuilder(blobNamespace, blob.ShareVersion, true)
err = b.Init()
if err != nil {
return err
}
Expand All @@ -57,7 +58,8 @@ func (sss *SparseShareSplitter) Write(blob coretypes.Blob) error {
}
sss.shares = append(sss.shares, *share)

b, err = NewBuilder(blobNamespace, blob.ShareVersion, false).Init()
b = NewBuilder(blobNamespace, blob.ShareVersion, false)
err = b.Init()
if err != nil {
return err
}
Expand Down
Loading