Skip to content

Commit

Permalink
Merge pull request #45 from ipfs/feat/batch-options
Browse files Browse the repository at this point in the history
batch: add functional opts to Batch
  • Loading branch information
Stebalien authored Oct 2, 2018
2 parents 6b969ca + 434489e commit 2f245e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
53 changes: 43 additions & 10 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ var ErrClosed = errors.New("error: batch closed")
// to add or remove a lot of nodes all at once.
//
// If the passed context is canceled, any in-progress commits are aborted.
func NewBatch(ctx context.Context, ds DAGService) *Batch {
func NewBatch(ctx context.Context, ds DAGService, opts ...BatchOption) *Batch {
ctx, cancel := context.WithCancel(ctx)
bopts := defaultBatchOptions
for _, o := range opts {
o(&bopts)
}
return &Batch{
ds: ds,
ctx: ctx,
cancel: cancel,
commitResults: make(chan error, ParallelBatchCommits),
MaxSize: 8 << 20,

// By default, only batch up to 128 nodes at a time.
// The current implementation of flatfs opens this many file
// descriptors at the same time for the optimized batch write.
MaxNodes: 128,
opts: bopts,
}
}

Expand All @@ -53,8 +52,7 @@ type Batch struct {
nodes []Node
size int

MaxSize int
MaxNodes int
opts batchOptions
}

func (t *Batch) processResults() {
Expand Down Expand Up @@ -120,7 +118,7 @@ func (t *Batch) Add(nd Node) error {
t.nodes = append(t.nodes, nd)
t.size += len(nd.RawData())

if t.size > t.MaxSize || len(t.nodes) > t.MaxNodes {
if t.size > t.opts.maxSize || len(t.nodes) > t.opts.maxNodes {
t.asyncCommit()
}
return t.err
Expand Down Expand Up @@ -175,3 +173,38 @@ loop:
t.size = 0
t.activeCommits = 0
}

// BatchOption provides a way of setting internal options of
// a Batch.
//
// See this post about the "functional options" pattern:
// http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
type BatchOption func(o *batchOptions)

type batchOptions struct {
maxSize int
maxNodes int
}

var defaultBatchOptions = batchOptions{
maxSize: 8 << 20,

// By default, only batch up to 128 nodes at a time.
// The current implementation of flatfs opens this many file
// descriptors at the same time for the optimized batch write.
maxNodes: 128,
}

// MaxSizeBatchOption sets the maximum size of a Batch.
func MaxSizeBatchOption(size int) BatchOption {
return func(o *batchOptions) {
o.maxSize = size
}
}

// MaxNodesBatchOption sets the maximum number of nodes in a Batch.
func MaxNodesBatchOption(num int) BatchOption {
return func(o *batchOptions) {
o.maxNodes = num
}
}
16 changes: 16 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,19 @@ func TestBatch(t *testing.T) {
t.Fatal("should have one node")
}
}

func TestBatchOptions(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

wantMaxSize := 8 << 10
wantMaxNodes := 500
d := newTestDag()
b := NewBatch(ctx, d, MaxSizeBatchOption(wantMaxSize), MaxNodesBatchOption(wantMaxNodes))
if b.opts.maxSize != wantMaxSize {
t.Fatalf("maxSize incorrect, want: %d, got: %d", wantMaxSize, b.opts.maxSize)
}
if b.opts.maxNodes != wantMaxNodes {
t.Fatalf("maxNodes incorrect, want: %d, got: %d", wantMaxNodes, b.opts.maxNodes)
}
}

0 comments on commit 2f245e4

Please sign in to comment.