-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcommand.cpp
More file actions
319 lines (283 loc) · 9.84 KB
/
Copy pathcommand.cpp
File metadata and controls
319 lines (283 loc) · 9.84 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* BSD 2-Clause License
*
* Copyright (c) 2021-2024, Hewlett Packard Enterprise
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "command.h"
#include "srexception.h"
using namespace SmartRedis;
// Command copy constructor
Command::Command(const Command& cmd)
{
*this = cmd;
}
// Command copy assignment operator
Command& Command::operator=(const Command& cmd)
{
// Check for self-assignment
if (this == &cmd)
return *this; // we have met the enemy and he is us
make_empty();
_fields.resize(cmd._fields.size());
// copy pointer fields and put into _fields
_ptr_fields = cmd._ptr_fields;
std::vector<std::pair<char*, size_t> >::const_iterator ptr_it =
_ptr_fields.begin();
for (; ptr_it != _ptr_fields.end(); ptr_it++)
_fields[ptr_it->second] = ptr_it->first;
// copy _local_fields and _cmd_keys, and put the fields into _fields
std::vector<std::pair<char*, size_t> >::const_iterator local_it =
cmd._local_fields.begin();
for (; local_it != cmd._local_fields.end(); local_it++) {
// allocate memory and copy a local field
size_t field_size = cmd._fields[local_it->second].size();
char* f = NULL;
try {
f = new char[field_size];
}
catch (std::bad_alloc& e) {
throw SRBadAllocException("field data");
}
std::memcpy(f, local_it->first, field_size);
_local_fields.push_back(
std::pair<char*, size_t>(f, local_it->second));
_fields[local_it->second] = std::string_view(f, field_size);
if (cmd._cmd_keys.count(cmd._fields[local_it->second]) != 0) {
// copy a command key
_cmd_keys[std::string_view(f, field_size)] =
local_it->second;
}
}
return *this;
}
// Command destructor
Command::~Command()
{
make_empty();
}
// Add a field to the Command from a string.
void Command::add_field(std::string field, bool is_key)
{
/* Copy the field string into a char* that is stored
locally in the Command object. The new string is not
null terminated because the fields vector is of type
string_view which stores the length of the string.
If is_key is true, the key will be added to the command
keys.
*/
size_t field_size = field.size();
char* f = NULL;
try {
f = (char*)new unsigned char[field_size + 1];
}
catch (std::bad_alloc& e) {
throw SRBadAllocException("field");
}
field.copy(f, field_size, 0);
f[field_size] = '\0';
_local_fields.push_back({f, _fields.size()});
_fields.push_back(std::string_view(f, field_size));
if (is_key) {
_cmd_keys[std::string_view(f, field_size)] = _fields.size() - 1;
}
}
// Replace a field in a command
void Command::set_field_at(std::string new_field, size_t pos, bool is_key)
{
size_t field_size = new_field.size();
char* f = NULL;
try {
f = (char*)new unsigned char[field_size + 1];
}
catch (std::bad_alloc& e) {
throw SRBadAllocException("field");
}
new_field.copy(f, field_size, 0);
f[field_size] = '\0';
// Free memory from the previous occupant of this
// field if locally allocated. Replace if we find it,
// otherwise, add it fresh (happens if we're not
// replacing a field that was locally allcoated)
bool replaced = false;
std::vector<std::pair<char*, size_t> >::iterator it =
_local_fields.begin();
for ( ; it != _local_fields.end(); it++) {
if (it->second == pos) {
delete(it->first);
it->first = f;
replaced = true;
}
}
if (!replaced)
_local_fields.push_back({f, pos});
_fields.at(pos) = std::string_view(f, field_size);
if (is_key) {
_cmd_keys[std::string_view(f, field_size)] = pos;
}
}
// Add a field to the Command from a c-string.
void Command::add_field(const char* field, bool is_key)
{
/* Copy the field char* into a char* that is stored
locally in the Command object. The new string is not
null terminated because the fields vector is of type
string_view which stores the length of the string.
If is_key is true, the key will be added to the command
keys.
*/
size_t field_size = std::strlen(field);
char* f = NULL;
try {
f = new char[field_size];
}
catch (std::bad_alloc& e) {
throw SRBadAllocException("field");
}
std::memcpy(f, field, field_size);
_local_fields.push_back({f, _fields.size()});
_fields.push_back(std::string_view(f, field_size));
if (is_key) {
_cmd_keys[std::string_view(f, field_size)] = _fields.size() - 1;
}
}
// Add a field to the Command from a c-string without copying the data.
void Command::add_field_ptr(char* field, size_t field_size)
{
/* This function adds a field to the fields data
structure without copying the data. This means
that the memory needs to be valid when it is later
accessed. This function should be used for very large
fields. Field pointers cannot act as Command keys.
*/
_ptr_fields.push_back({field, _fields.size()});
_fields.push_back(std::string_view(field, field_size));
}
// Add a field to the Command from a std::string_view without copying the data
void Command::add_field_ptr(std::string_view field)
{
/* This function adds a field to the fields data
structure without copying the data. This means
that the memory needs to be valid when it is later
accessed. This function should be used for very large
fields. If is_key is true, the key will be added to the
command keys. Field pointers cannot act as Command keys.
*/
_ptr_fields.push_back({const_cast<char*>(field.data()), _fields.size()});
_fields.push_back(field);
}
// Add fields to the Command from a vector of strings.
void Command::add_fields(const std::vector<std::string>& fields, bool is_key)
{
/* Copy field strings into a char* that is stored
locally in the Command object. The new string is not
null terminated because the fields vector is of type
string_view which stores the length of the string
*/
for (size_t i = 0; i < fields.size(); i++) {
add_field(fields[i], is_key);
}
}
// Add fields to the Command from a vector of strings.
void Command::add_keys(const std::vector<std::string>& keyfields)
{
/* Copy field strings into a char* that is stored
locally in the Command object. The new string is not
null terminated because the fields vector is of type
string_view which stores the length of the string
*/
for (size_t i = 0; i < keyfields.size(); i++) {
add_field(keyfields[i], true);
}
}
// Get the value of the field field
std::string Command::first_field() const
{
if (cbegin() == cend())
throw SRRuntimeException("No fields exist in the Command.");
return std::string(cbegin()->data(), cbegin()->size());
}
// Get a string of the entire Command
std::string Command::to_string()
{
std::string output;
for (Command::iterator it = begin(); it != end(); it++) {
output += " " + std::string(it->data(), it->size());
}
return output;
}
// Returns an iterator pointing to the first field in the Command
Command::iterator Command::begin()
{
return _fields.begin();
}
Command::const_iterator Command::cbegin() const
{
return _fields.cbegin();
}
// Returns an iterator pointing to the past-the-end field in the Command
Command::iterator Command::end()
{
return _fields.end();
}
Command::const_iterator Command::cend() const
{
return _fields.cend();
}
// Return true if the Command has keys
bool Command::has_keys()
{
return (_cmd_keys.size()>0);
}
// Return a copy of all Command keys
std::vector<std::string> Command::get_keys() {
/* This function returns a vector of key copies
to the user. Original keys are not returned
to the user because memory management
becomes complicated for std::string_view that
may need to grow or decrease in size.
*/
std::vector<std::string> keys;
std::unordered_map<std::string_view,size_t>::iterator it =
_cmd_keys.begin();
for ( ; it != _cmd_keys.end(); it++) {
keys.push_back(std::string(it->first.data(), it->first.length()));
}
return keys;
}
// Helper function for emptying the Command
void Command::make_empty()
{
std::vector<std::pair<char*, size_t> >::iterator it =
_local_fields.begin();
for ( ; it != _local_fields.end(); it++) {
if (it->first != NULL)
delete(it->first);
*it = {NULL, 0};
}
_local_fields.clear();
_ptr_fields.clear();
_cmd_keys.clear();
_fields.clear();
}