Skip to content

Commit

Permalink
make Resize and Init virtual on builder
Browse files Browse the repository at this point in the history
  • Loading branch information
emkornfield committed Apr 17, 2016
1 parent 8ab5315 commit e71810b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
21 changes: 13 additions & 8 deletions cpp/src/arrow/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ class ArrayBuilder {
// Set the next length bits to not null (i.e. valid).
Status SetNotNull(int32_t length);

// Allocates requires memory at this level, but children need to be
// initialized independently
Status Init(int32_t capacity);

// Resizes the null_bitmap array
Status Resize(int32_t new_bits);

Status Reserve(int32_t extra_bits);
// Allocates initial capacity requirements for the builder. In most
// cases subclasses should override and call there parent classes
// method as well.
virtual Status Init(int32_t capacity);

// Resizes the null_bitmap array. In most
// cases subclasses should override and call there parent classes
// method as well.
virtual Status Resize(int32_t new_bits);

// Ensures there is enough space for adding the number of elements by checking
// capacity and calling Resize if necessary.
Status Reserve(int32_t elements);

// For cases where raw data was memcpy'd into the internal buffers, allows us
// to advance the length of the builder. It is your responsibility to use
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/types/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Status ListArray::Validate() const {
}
if (offset_buf_->size() / sizeof(int32_t) < length_) {
std::stringstream ss;
ss << "offset buffer size: " << offset_buf_->size()
ss << "offset buffer size (bytes): " << offset_buf_->size()
<< " isn't large enough for length: " << length_;
return Status::Invalid(ss.str());
}
Expand Down
16 changes: 4 additions & 12 deletions cpp/src/arrow/types/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,18 @@ class ListBuilder : public ArrayBuilder {
offset_builder_(pool),
values_(values) {}

Status Init(int32_t elements) {
Status Init(int32_t elements) override {
RETURN_NOT_OK(ArrayBuilder::Init(elements));
// one more then requested for offsets
return offset_builder_.Resize(elements + 1);
return offset_builder_.Resize((elements + 1) * sizeof(int32_t));
}

Status Resize(int32_t capacity) {
// +1 because we Need space for the end offset
Status Resize(int32_t capacity) override {
// one more then requested for offsets
RETURN_NOT_OK(offset_builder_.Resize((capacity + 1) * sizeof(int32_t)));
return ArrayBuilder::Resize(capacity);
}

Status Reserve(int32_t elements) {
if (length_ + elements > capacity_) {
int32_t new_capacity = util::next_power2(length_ + elements);
return Resize(new_capacity);
}
return Status::OK();
}

// Vector append
//
// If passed, valid_bytes is of equal length to values, and any zero byte
Expand Down
11 changes: 1 addition & 10 deletions cpp/src/arrow/types/primitive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,10 @@ Status PrimitiveBuilder<T>::Resize(int32_t capacity) {
return Status::OK();
}

template <typename T>
Status PrimitiveBuilder<T>::Reserve(int32_t elements) {
if (length_ + elements > capacity_) {
int32_t new_capacity = util::next_power2(length_ + elements);
return Resize(new_capacity);
}
return Status::OK();
}

template <typename T>
Status PrimitiveBuilder<T>::Append(
const value_type* values, int32_t length, const uint8_t* valid_bytes) {
RETURN_NOT_OK(PrimitiveBuilder<T>::Reserve(length));
RETURN_NOT_OK(Reserve(length));

if (length > 0) {
memcpy(raw_data_ + length_, values, type_traits<T>::bytes_required(length));
Expand Down
10 changes: 3 additions & 7 deletions cpp/src/arrow/types/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,13 @@ class PrimitiveBuilder : public ArrayBuilder {
Status Append(
const value_type* values, int32_t length, const uint8_t* valid_bytes = nullptr);

// Ensure that builder can accommodate an additional number of
// elements. Resizes if the current capacity is not sufficient
Status Reserve(int32_t elements);

std::shared_ptr<Array> Finish() override;

Status Init(int32_t capacity);
Status Init(int32_t capacity) override;

// Increase the capacity of the builder to accommodate at least the indicated
// number of elements
Status Resize(int32_t capacity);
Status Resize(int32_t capacity) override;

protected:
std::shared_ptr<PoolBuffer> data_;
Expand All @@ -143,7 +139,7 @@ class NumericBuilder : public PrimitiveBuilder<T> {

// Scalar append.
void Append(value_type val) {
PrimitiveBuilder<T>::Reserve(1);
ArrayBuilder::Reserve(1);
UnsafeAppend(val);
}

Expand Down

0 comments on commit e71810b

Please sign in to comment.