You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by sb...@apache.org on 2019/04/26 17:06:37 UTC

[arrow] branch master updated: ARROW-5212: [Go] Support reserve for the data buffer in the BinaryBuilder

This is an automated email from the ASF dual-hosted git repository.

sbinet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 338c62a  ARROW-5212: [Go] Support reserve for the data buffer in the BinaryBuilder
338c62a is described below

commit 338c62a2a20574072fde5c478fcd42bf27a2d4b6
Author: Jonathan A. Sternberg <jo...@influxdata.com>
AuthorDate: Fri Apr 26 19:06:22 2019 +0200

    ARROW-5212: [Go] Support reserve for the data buffer in the BinaryBuilder
    
    Author: Jonathan A. Sternberg <jo...@influxdata.com>
    
    Closes #4204 from jsternberg/master and squashes the following commits:
    
    47db7f61 <Jonathan A. Sternberg> ARROW-5212:  Support reserve for the data buffer in the BinaryBuilder
---
 go/arrow/array/binarybuilder.go      | 17 ++++++++++++++++-
 go/arrow/array/binarybuilder_test.go | 26 ++++++++++++++++++++++++++
 go/arrow/go.sum                      |  2 ++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/go/arrow/array/binarybuilder.go b/go/arrow/array/binarybuilder.go
index 95945e1..17562fc 100644
--- a/go/arrow/array/binarybuilder.go
+++ b/go/arrow/array/binarybuilder.go
@@ -146,14 +146,29 @@ func (b *BinaryBuilder) init(capacity int) {
 	b.offsets.resize((capacity + 1) * arrow.Int32SizeBytes)
 }
 
+// DataLen returns the number of bytes in the data array.
+func (b *BinaryBuilder) DataLen() int { return b.values.length }
+
+// DataCap returns the total number of bytes that can be stored
+// without allocating additional memory.
+func (b *BinaryBuilder) DataCap() int { return b.values.capacity }
+
 // Reserve ensures there is enough space for appending n elements
 // by checking the capacity and calling Resize if necessary.
 func (b *BinaryBuilder) Reserve(n int) {
 	b.builder.reserve(n, b.Resize)
 }
 
+// ReserveData ensures there is enough space for appending n bytes
+// by checking the capacity and resizing the data buffer if necessary.
+func (b *BinaryBuilder) ReserveData(n int) {
+	if b.values.capacity < b.values.length+n {
+		b.values.resize(b.values.Len() + n)
+	}
+}
+
 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(),
-// additional memory will be allocated. If n is smaller, the allocated memory may reduced.
+// additional memory will be allocated. If n is smaller, the allocated memory may be reduced.
 func (b *BinaryBuilder) Resize(n int) {
 	b.offsets.resize((n + 1) * arrow.Int32SizeBytes)
 	b.builder.resize(n, b.init)
diff --git a/go/arrow/array/binarybuilder_test.go b/go/arrow/array/binarybuilder_test.go
index 4de5124..145435b 100644
--- a/go/arrow/array/binarybuilder_test.go
+++ b/go/arrow/array/binarybuilder_test.go
@@ -17,6 +17,7 @@
 package array_test
 
 import (
+	"bytes"
 	"testing"
 
 	"github.com/apache/arrow/go/arrow"
@@ -59,3 +60,28 @@ func TestBinaryBuilder(t *testing.T) {
 	assert.Zero(t, ab.Cap(), "unexpected ArrayBuilder.Cap(), NewBinaryArray did not reset state")
 	assert.Zero(t, ab.NullN(), "unexpected ArrayBuilder.NullN(), NewBinaryArray did not reset state")
 }
+
+func TestBinaryBuilder_ReserveData(t *testing.T) {
+	mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+	defer mem.AssertSize(t, 0)
+
+	ab := array.NewBinaryBuilder(mem, arrow.BinaryTypes.Binary)
+
+	// call ReserveData and ensure the capacity doesn't change
+	// when appending entries until that count.
+	ab.ReserveData(256)
+	expCap := ab.DataCap()
+	for i := 0; i < 256 / 8; i++ {
+		ab.Append(bytes.Repeat([]byte("a"), 8))
+	}
+	assert.Equal(t, expCap, ab.DataCap(), "unexpected BinaryArrayBuilder.DataCap()")
+
+	ar := ab.NewBinaryArray()
+	ab.Release()
+	ar.Release()
+
+	// check state of builder after NewBinaryArray
+	assert.Zero(t, ab.Len(), "unexpected ArrayBuilder.Len(), NewBinaryArray did not reset state")
+	assert.Zero(t, ab.Cap(), "unexpected ArrayBuilder.Cap(), NewBinaryArray did not reset state")
+	assert.Zero(t, ab.NullN(), "unexpected ArrayBuilder.NullN(), NewBinaryArray did not reset state")
+}
diff --git a/go/arrow/go.sum b/go/arrow/go.sum
index 2639967..990cb77 100644
--- a/go/arrow/go.sum
+++ b/go/arrow/go.sum
@@ -1,5 +1,7 @@
 github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/google/flatbuffers v1.10.0 h1:wHCM5N1xsJ3VwePcIpVqnmjAqRXlR44gv4hpGi+/LIw=
+github.com/google/flatbuffers v1.10.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
 github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=