forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.cc
More file actions
304 lines (275 loc) · 6.35 KB
/
buffer.cc
File metadata and controls
304 lines (275 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2014 The Trident Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <trident/buffer.h>
#include <trident/tran_buf_pool.h>
namespace trident {
ReadBuffer::ReadBuffer()
: _total_bytes(0),
_cur_it(_buf_list.begin()),
_cur_pos(0),
_last_bytes(0),
_read_bytes(0)
{}
ReadBuffer::~ReadBuffer()
{
for (BufHandleListIterator it = _buf_list.begin();
it != _buf_list.end(); ++it)
{
TranBufPool::free(it->data);
}
_buf_list.clear();
}
void ReadBuffer::Append(const BufHandle& buf_handle)
{
SCHECK_GT(buf_handle.size, 0);
_buf_list.push_back(buf_handle);
TranBufPool::add_ref(buf_handle.data);
_total_bytes += buf_handle.size;
_cur_it = _buf_list.begin();
}
int64 ReadBuffer::TotalCount() const
{
return _total_bytes;
}
int ReadBuffer::BlockCount() const
{
return _buf_list.size();
}
int ReadBuffer::LastBytes() const
{
return _last_bytes;
}
std::string ReadBuffer::ToString() const
{
std::string str;
str.reserve(_total_bytes);
for (std::deque<BufHandle>::const_iterator it = _buf_list.begin();
it != _buf_list.end(); ++it)
{
str.append(it->data + it->offset, it->size);
}
return str;
}
bool ReadBuffer::Next(const void** data, int* size)
{
if (_cur_it != _buf_list.end())
{
SCHECK_LT(_cur_pos, _cur_it->size);
*data = _cur_it->data + _cur_it->offset + _cur_pos;
*size = _cur_it->size - _cur_pos;
++_cur_it;
_cur_pos = 0;
_last_bytes = *size;
_read_bytes += _last_bytes;
return true;
}
else
{
_last_bytes = 0;
return false;
}
}
// BackUp() can only be called after a successful Next().
// "count" should be greater than or equal to 0.
void ReadBuffer::BackUp(int count)
{
SCHECK_GT(_last_bytes, 0);
SCHECK_GE(count, 0);
SCHECK_LE(count, _last_bytes);
if (count > 0)
{
--_cur_it;
_cur_pos = _cur_it->size - count;
}
_last_bytes = 0;
_read_bytes -= count;
}
// "count" should be greater than or equal to 0.
bool ReadBuffer::Skip(int count)
{
SCHECK_GE(count, 0);
const void* data;
int size;
while (count > 0 && Next(&data, &size))
{
if (size > count)
{
BackUp(size - count);
size = count;
}
count -= size;
}
_last_bytes = 0;
return count == 0;
}
int64 ReadBuffer::ByteCount() const
{
return _read_bytes;
}
WriteBuffer::WriteBuffer()
: _total_capacity(0)
, _last_bytes(0)
, _write_bytes(0)
{}
WriteBuffer::~WriteBuffer()
{
for (BufHandleListIterator it = _buf_list.begin();
it != _buf_list.end(); ++it)
{
TranBufPool::free(it->data);
}
_buf_list.clear();
}
int64 WriteBuffer::TotalCapacity() const
{
return _total_capacity;
}
int WriteBuffer::BlockCount() const
{
return _buf_list.size();
}
int WriteBuffer::LastBytes() const
{
return _last_bytes;
}
void WriteBuffer::SwapOut(ReadBuffer* is)
{
while (!_buf_list.empty())
{
BufHandle& buf_handle = _buf_list.front();
if (buf_handle.size > 0)
{
buf_handle.offset = 0; // capacity -> offset
is->Append(buf_handle);
}
TranBufPool::free(buf_handle.data);
_buf_list.pop_front();
}
_total_capacity = 0;
_last_bytes = 0;
_write_bytes = 0;
}
int64 WriteBuffer::Reserve(int count)
{
SCHECK_GT(count, 0);
int64 head = ByteCount();
void* data;
int size;
while (count > 0)
{
if (!Next(&data, &size))
return -1;
if (size > count)
{
BackUp(size - count);
count = 0;
}
else
{
count -= size;
}
}
return head;
}
void WriteBuffer::SetData(int64 pos, const char* data, int size)
{
SCHECK_GE(pos, 0);
SCHECK_GT(size, 0);
SCHECK_LE(pos + size, ByteCount());
BufHandleListIterator cur_it = _buf_list.begin();
int cur_offset = 0;
while (pos > 0)
{
SCHECK(cur_it != _buf_list.end());
int cur_size = cur_it->size - cur_offset;
if (cur_size > pos)
{
cur_offset += pos;
pos = 0;
}
else
{
pos -= cur_size;
++cur_it;
cur_offset = 0;
}
}
while (size > 0)
{
SCHECK(cur_it != _buf_list.end());
int cur_size = cur_it->size - cur_offset;
if (cur_size > size)
{
memcpy(cur_it->data + cur_offset, data, size);
size = 0;
}
else
{
memcpy(cur_it->data + cur_offset, data, cur_size);
size -= cur_size;
data += cur_size;
++cur_it;
cur_offset = 0;
}
}
}
bool WriteBuffer::Next(void** data, int* size)
{
BufHandleListReverseIterator last = _buf_list.rbegin();
if (last == _buf_list.rend() || last->size == last->capacity)
{
if (!Extend())
{
_last_bytes = 0;
return false;
}
last = _buf_list.rbegin();
}
*data = last->data + last->size;
*size = last->capacity - last->size;
last->size = last->capacity;
_last_bytes = *size;
_write_bytes += _last_bytes;
return true;
}
// BackUp() can only be called after a successful Next().
// "count" should be greater than or equal to 0.
void WriteBuffer::BackUp(int count)
{
SCHECK_GT(_last_bytes, 0);
SCHECK_GE(count, 0);
SCHECK_LE(count, _last_bytes);
_buf_list.back().size -= count;
_last_bytes = 0;
_write_bytes -= count;
}
int64 WriteBuffer::ByteCount() const
{
return _write_bytes;
}
bool WriteBuffer::Extend()
{
char* block = static_cast<char*>(TranBufPool::malloc());
if (block == NULL) return false;
_buf_list.push_back(BufHandle(block, TranBufPool::block_size()));
_total_capacity += TranBufPool::block_size();
return true;
}
bool WriteBuffer::Append(const std::string& data)
{
return Append(data.c_str(), data.size());
}
bool WriteBuffer::Append(const char* data, int size)
{
SCHECK_GE(size, 0);
if (size == 0) return true;
int64 head = Reserve(size);
if (head < 0)
{
return false;
}
SetData(head, data, size);
return true;
}
} // namespace trident