From cfe76e8fb58636697a9743313bb9afe758c3c3ed Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Thu, 25 Oct 2018 19:24:07 +0200 Subject: [PATCH] Move add logic to AddMany --- batch.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/batch.go b/batch.go index 58715fc..a9c8c17 100644 --- a/batch.go +++ b/batch.go @@ -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 } @@ -115,8 +121,10 @@ 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() @@ -124,18 +132,6 @@ func (t *Batch) Add(ctx context.Context, nd Node) error { 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 {