Skip to content

Commit

Permalink
Move add logic to AddMany
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Oct 25, 2018
1 parent 5d25a60 commit cfe76e8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func (t *Batch) asyncCommit() {

// Add adds a node to the batch and commits the batch if necessary.
func (t *Batch) Add(ctx context.Context, nd Node) error {
return t.AddMany(ctx, []Node{nd})
}

// Add many calls Add for every given Node, thus batching and
// commiting them as needed.
func (t *Batch) AddMany(ctx context.Context, nodes []Node) error {
if t.err != nil {
return t.err
}
Expand All @@ -115,27 +121,17 @@ func (t *Batch) Add(ctx context.Context, nd Node) error {
return t.err
}

t.nodes = append(t.nodes, nd)
t.size += len(nd.RawData())
t.nodes = append(t.nodes, nodes...)
for _, nd := range nodes {
t.size += len(nd.RawData())
}

if t.size > t.opts.maxSize || len(t.nodes) > t.opts.maxNodes {
t.asyncCommit()
}
return t.err
}

// Add many calls Add for every given Node, thus batching and
// commiting them as needed.
func (t *Batch) AddMany(ctx context.Context, nodes []Node) error {
for _, n := range nodes {
err := t.Add(ctx, n)
if err != nil {
return err
}
}
return nil
}

// Commit commits batched nodes.
func (t *Batch) Commit() error {
if t.err != nil {
Expand Down

0 comments on commit cfe76e8

Please sign in to comment.