4
4
#include " ../base/wire_format.h"
5
5
6
6
namespace {
7
- const size_t DEFAULT_BLOCK_SIZE = 4096 ;
7
+
8
+ constexpr size_t DEFAULT_BLOCK_SIZE = 4096 ;
8
9
9
10
template <typename Container>
10
- size_t ComputeTotalSize (const Container & strings, size_t begin = 0 , size_t len = -1 )
11
- {
11
+ size_t ComputeTotalSize (const Container & strings, size_t begin = 0 , size_t len = -1 ) {
12
12
size_t result = 0 ;
13
13
if (begin < strings.size ()) {
14
14
len = std::min (len, strings.size () - begin);
@@ -64,8 +64,7 @@ std::string_view ColumnFixedString::operator [](size_t n) const {
64
64
return std::string_view (&data_[pos], string_size_);
65
65
}
66
66
67
- size_t ColumnFixedString::FixedSize () const
68
- {
67
+ size_t ColumnFixedString::FixedSize () const {
69
68
return string_size_;
70
69
}
71
70
@@ -126,8 +125,8 @@ struct ColumnString::Block
126
125
127
126
explicit Block (size_t starting_capacity)
128
127
: size(0 ),
129
- capacity(starting_capacity),
130
- data_(new CharT[capacity])
128
+ capacity(starting_capacity),
129
+ data_(new CharT[capacity])
131
130
{}
132
131
133
132
inline auto GetAvailable () const
@@ -167,8 +166,8 @@ ColumnString::ColumnString()
167
166
{
168
167
}
169
168
170
- ColumnString::ColumnString (const std::vector<std::string> & data)
171
- : Column(Type::CreateString() )
169
+ ColumnString::ColumnString (const std::vector<std::string>& data)
170
+ : ColumnString( )
172
171
{
173
172
items_.reserve (data.size ());
174
173
blocks_.emplace_back (ComputeTotalSize (data));
@@ -177,6 +176,18 @@ ColumnString::ColumnString(const std::vector<std::string> & data)
177
176
{
178
177
AppendUnsafe (s);
179
178
}
179
+ };
180
+
181
+ ColumnString::ColumnString (std::vector<std::string>&& data)
182
+ : ColumnString()
183
+ {
184
+ items_.reserve (data.size ());
185
+
186
+ for (auto && d : data) {
187
+ append_data_.emplace_back (std::move (d));
188
+ auto & last_data = append_data_.back ();
189
+ items_.emplace_back (std::string_view{ last_data.data (),last_data.length () });
190
+ }
180
191
}
181
192
182
193
ColumnString::~ColumnString ()
@@ -191,14 +202,34 @@ void ColumnString::Append(std::string_view str) {
191
202
items_.emplace_back (blocks_.back ().AppendUnsafe (str));
192
203
}
193
204
194
- void ColumnString::AppendUnsafe (std::string_view str)
195
- {
205
+ void ColumnString::Append (const char * str) {
206
+ auto len = strlen (str);
207
+ if (blocks_.size () == 0 || blocks_.back ().GetAvailable () < len) {
208
+ blocks_.emplace_back (std::max (DEFAULT_BLOCK_SIZE, len));
209
+ }
210
+
211
+ items_.emplace_back (blocks_.back ().AppendUnsafe (str));
212
+ }
213
+
214
+ void ColumnString::Append (std::string&& steal_value) {
215
+ append_data_.emplace_back (std::move (steal_value));
216
+ auto & last_data = append_data_.back ();
217
+ items_.emplace_back (std::string_view{ last_data.data (),last_data.length () });
218
+ }
219
+
220
+ void ColumnString::AppendNoManagedLifetime (std::string_view str) {
221
+ items_.emplace_back (str);
222
+ }
223
+
224
+ void ColumnString::AppendUnsafe (std::string_view str) {
196
225
items_.emplace_back (blocks_.back ().AppendUnsafe (str));
197
226
}
198
227
199
228
void ColumnString::Clear () {
200
229
items_.clear ();
201
230
blocks_.clear ();
231
+ append_data_.clear ();
232
+ append_data_.shrink_to_fit ();
202
233
}
203
234
204
235
std::string_view ColumnString::At (size_t n) const {
@@ -283,6 +314,7 @@ void ColumnString::Swap(Column& other) {
283
314
auto & col = dynamic_cast <ColumnString &>(other);
284
315
items_.swap (col.items_ );
285
316
blocks_.swap (col.blocks_ );
317
+ append_data_.swap (col.append_data_ );
286
318
}
287
319
288
320
ItemView ColumnString::GetItem (size_t index) const {
0 commit comments