Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
table_builder.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_TABLE_BUILDER_H
19 #define ARROW_TABLE_BUILDER_H
20 
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include "arrow/status.h"
27 #include "arrow/type.h"
28 #include "arrow/util/visibility.h"
29 
30 namespace arrow {
31 
32 class ArrayBuilder;
33 class MemoryPool;
34 class RecordBatch;
35 class Schema;
36 
41  public:
46  static Status Make(const std::shared_ptr<Schema>& schema, MemoryPool* pool,
47  std::unique_ptr<RecordBatchBuilder>* builder);
48 
54  static Status Make(const std::shared_ptr<Schema>& schema, MemoryPool* pool,
55  int64_t initial_capacity,
56  std::unique_ptr<RecordBatchBuilder>* builder);
57 
61  ArrayBuilder* GetField(int i) { return raw_field_builders_[i]; }
62 
66  template <typename T>
67  T* GetFieldAs(int i) {
68  return static_cast<T*>(raw_field_builders_[i]);
69  }
70 
75  Status Flush(bool reset_builders, std::shared_ptr<RecordBatch>* batch);
76 
80  Status Flush(std::shared_ptr<RecordBatch>* batch);
81 
83  void SetInitialCapacity(int64_t capacity);
84 
86  int64_t initial_capacity() const { return initial_capacity_; }
87 
89  int num_fields() const { return schema_->num_fields(); }
90 
92  std::shared_ptr<Schema> schema() const { return schema_; }
93 
94  private:
95  RecordBatchBuilder(const std::shared_ptr<Schema>& schema, MemoryPool* pool,
96  int64_t initial_capacity);
97 
98  Status CreateBuilders();
99  Status InitBuilders();
100 
101  std::shared_ptr<Schema> schema_;
102  int64_t initial_capacity_;
103  MemoryPool* pool_;
104 
105  std::vector<std::unique_ptr<ArrayBuilder>> field_builders_;
106  std::vector<ArrayBuilder*> raw_field_builders_;
107 };
108 
109 } // namespace arrow
110 
111 #endif // ARROW_TABLE_BUILDER_H
T * GetFieldAs(int i)
Return field builder casted to indicated specific builder type.
Definition: table_builder.h:67
int64_t initial_capacity() const
The initial capacity for builders.
Definition: table_builder.h:86
Base class for all data array builders.
Definition: builder.h:57
Definition: status.h:106
ArrayBuilder * GetField(int i)
Get base pointer to field builder.
Definition: table_builder.h:61
Status Flush(bool reset_builders, std::shared_ptr< RecordBatch > *batch)
Finish current batch and optionally reset.
Helper class for creating record batches iteratively given a known schema.
Definition: table_builder.h:40
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
static Status Make(const std::shared_ptr< Schema > &schema, MemoryPool *pool, std::unique_ptr< RecordBatchBuilder > *builder)
Create an initialize a RecordBatchBuilder.
std::shared_ptr< Schema > schema() const
The number of fields in the schema.
Definition: table_builder.h:92
Base class for memory allocation.
Definition: memory_pool.h:34
int num_fields() const
The number of fields in the schema.
Definition: table_builder.h:89
void SetInitialCapacity(int64_t capacity)
Set the initial capacity for new builders.