-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmp_writer.cpp
More file actions
195 lines (166 loc) · 5.47 KB
/
mp_writer.cpp
File metadata and controls
195 lines (166 loc) · 5.47 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
#include "msgpuck/msgpuck.h"
#include "mp_writer.h"
#include "mp_reader.h"
using namespace std;
mp_writer::mp_writer(wtf_buffer &buf) : _buf(&buf) {}
void mp_writer::begin_array(uint32_t max_cardinality)
{
increment_container_counter();
_opened_containers.push({_buf->size(), max_cardinality});
_buf->end = mp_encode_array(_buf->end, max_cardinality);
assert(_buf->available() >= 0);
}
void mp_writer::begin_map(uint32_t max_cardinality)
{
increment_container_counter();
_opened_containers.push({_buf->size(), max_cardinality});
_buf->end = mp_encode_map(_buf->end, max_cardinality);
assert(_buf->available() >= 0);
}
void mp_writer::finalize()
{
if (_opened_containers.empty())
throw range_error("no container to finalize");
auto &c = _opened_containers.pop();
char *head = _buf->data() + c.head_offset;
// mp_encode_array() may reduce header's size if actual cardinality
// is smaller than initial value, so we update the header directly
uint32_t max_num_bytes = 0;
uint32_t actual_cardinality = c.items_count;
auto container_type = mp_typeof(*head);
if (container_type == MP_ARRAY)
{
if (actual_cardinality == c.max_cardinality)
return;
// get current header size
max_num_bytes = mp_sizeof_array(c.max_cardinality);
if (actual_cardinality > c.max_cardinality && mp_sizeof_array(actual_cardinality) > max_num_bytes)
throw overflow_error("array header size exceeded");
//throw overflow_error(std::format("array header size exceeded ({} of {})\n{}\n{}",
// actual_cardinality, c.max_cardinality,
// get_trace(), hex_dump(_buf->data(), _buf->end, head)));
if (max_num_bytes == 1)
{
// replace 1-byte header with the new size
mp_encode_array(head, actual_cardinality);
return;
}
}
else if (container_type == MP_MAP)
{
if (c.items_count & 0x1)
throw runtime_error("odd number of map items");
actual_cardinality = c.items_count / 2; // map cardinality
if (actual_cardinality == c.max_cardinality)
return;
// get current header size
max_num_bytes = mp_sizeof_map(c.max_cardinality);
if (actual_cardinality > c.max_cardinality && mp_sizeof_map(actual_cardinality) > max_num_bytes)
throw overflow_error("map header size exceeded");
if (max_num_bytes == 1)
{
// replace 1-byte header with new size
mp_encode_map(head, actual_cardinality);
return;
}
}
else
{
throw runtime_error("unexpected container header");
}
switch (max_num_bytes)
{
case 3:
mp_store_u16(++head, static_cast<uint16_t>(actual_cardinality));
break;
case 5:
mp_store_u32(++head, actual_cardinality);
break;
default:
throw runtime_error("previously not implemented container cardinality");
}
}
void mp_writer::finalize_all()
{
while (!_opened_containers.empty())
finalize();
}
void mp_writer::increment_container_counter(size_t items_added)
{
if (!_opened_containers.empty())
_opened_containers.top().items_count += items_added;
}
void mp_writer::write(const char *begin, const char *end, size_t cardinality)
{
// make sure the destination has free space
auto dst = _buf->end;
_buf->resize(_buf->size() + end - begin);
std::copy(begin, end, dst);
if (!_opened_containers.empty())
{
if (!cardinality)
{
mp_reader mp{begin, end};
while (mp.has_next())
{
mp.skip();
++cardinality;
}
}
_opened_containers.top().items_count += cardinality;
}
}
mp_writer &mp_writer::fill(mp_raw_view items_to_fill, uint32_t target_items_count)
{
if (_opened_containers.empty())
throw runtime_error("no opened containers");
auto &c = _opened_containers.top();
while (c.items_count < target_items_count)
{
if (c.items_count + items_to_fill.cardinality() <= target_items_count)
*this << items_to_fill;
else
break; // if target_items_count % items_to_fill.cardinality != 0
}
assert(_buf->available() >= 0);
return *this;
}
mp_writer &mp_writer::operator<<(nullptr_t)
{
_buf->end = mp_encode_nil(_buf->end);
increment_container_counter();
assert(_buf->available() >= 0);
return *this;
}
void mp_writer::set_state(const state &state)
{
if (_buf->capacity() >= state.content_len)
_buf->end = _buf->data() + state.content_len;
else
throw std::overflow_error("destination buffer was truncated");
_opened_containers = state.opened_containers;
}
mp_writer::state mp_writer::get_state()
{
return state{_buf->size(), _opened_containers};
}
mp_writer& mp_writer::operator<<(const string_view &val)
{
if (val.data() == nullptr)
_buf->end = mp_encode_nil(_buf->end);
else if (val.size() > std::numeric_limits<uint32_t>::max())
throw overflow_error("too long string");
else
_buf->end = mp_encode_str(_buf->end, val.data(), static_cast<uint32_t>(val.size()));
assert(_buf->available() >= 0);
increment_container_counter();
return *this;
}
mp_writer& mp_writer::operator<<(const mp_plain &src)
{
mp_reader r(src);
while (r.has_next(true))
*this << r;
assert(_buf->available() >= 0);
return *this;
}