Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
string-buffer.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_RUNTIME_STRING_BUFFER_H
17 #define IMPALA_RUNTIME_STRING_BUFFER_H
18 
19 #include "runtime/mem-pool.h"
20 #include "runtime/string-value.h"
21 
22 namespace impala {
23 
30 class StringBuffer {
31  public:
35  : pool_(pool), buffer_size_(0) {
36  DCHECK(pool_ != NULL);
37  if (str != NULL) {
38  string_value_ = *str;
39  buffer_size_ = str->len;
40  }
41  }
42 
44  void Append(const char* str, int len) {
45  int new_len = len + string_value_.len;
46  if (new_len > buffer_size_) {
47  GrowBuffer(new_len);
48  }
49  memcpy(string_value_.ptr + string_value_.len, str, len);
50  string_value_.len = new_len;
51  }
52 
54  void Append(const uint8_t* str, int len) {
55  Append(reinterpret_cast<const char*>(str), len);
56  }
57 
59  void Assign(const char* str, int len) {
60  Clear();
61  Append(str, len);
62  }
63 
65  void Clear() {
66  string_value_.len = 0;
67  }
68 
70  void Reset() {
71  string_value_.len = 0;
72  buffer_size_ = 0;
73  }
74 
76  bool Empty() const {
77  return string_value_.len == 0;
78  }
79 
81  int Size() const {
82  return string_value_.len;
83  }
84 
86  const StringValue& str() const {
87  return string_value_;
88  }
89 
91  int buffer_size() const {
92  return buffer_size_;
93  }
94 
95  private:
98  void GrowBuffer(int new_len) {
99  // TODO: Release/reuse old buffers somehow
100  buffer_size_ = std::max(buffer_size_ * 2, new_len);
101  char* new_buffer = reinterpret_cast<char*>(pool_->Allocate(buffer_size_));
102  if (string_value_.len > 0) {
103  memcpy(new_buffer, string_value_.ptr, string_value_.len);
104  }
105  string_value_.ptr = new_buffer;
106  }
107 
111 };
112 
113 }
114 
115 #endif
const StringValue & str() const
Returns the underlying StringValue.
Definition: string-buffer.h:86
int Size() const
Returns the length of the current string.
Definition: string-buffer.h:81
void Append(const char *str, int len)
Append 'str' to the current string, allocating a new buffer as necessary.
Definition: string-buffer.h:44
void Assign(const char *str, int len)
Assigns contents to StringBuffer.
Definition: string-buffer.h:59
int buffer_size() const
Returns the buffer size.
Definition: string-buffer.h:91
void Clear()
Clear the underlying StringValue. The allocated buffer can be reused.
Definition: string-buffer.h:65
bool Empty() const
Returns whether the current string is empty.
Definition: string-buffer.h:76
StringBuffer(MemPool *pool, StringValue *str=NULL)
Definition: string-buffer.h:34
void GrowBuffer(int new_len)
Definition: string-buffer.h:98
StringValue string_value_
ObjectPool pool
void Append(const uint8_t *str, int len)
TODO: switch everything to uint8_t?
Definition: string-buffer.h:54
void Reset()
Clears the underlying buffer and StringValue.
Definition: string-buffer.h:70
uint8_t * Allocate(int size)
Definition: mem-pool.h:92