Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathInvocationResponse.hpp
More file actions
300 lines (265 loc) · 10.5 KB
/
Copy pathInvocationResponse.hpp
File metadata and controls
300 lines (265 loc) · 10.5 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
/* This file is part of VoltDB.
* Copyright (C) 2008-2018 VoltDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef VOLTDB_INVOCATIONRESPONSE_HPP_
#define VOLTDB_INVOCATIONRESPONSE_HPP_
#include <boost/shared_array.hpp>
#include <vector>
#include <sstream>
#include <boost/shared_ptr.hpp>
#include "ByteBuffer.hpp"
#include "Table.h"
#include <iostream>
namespace voltdb {
enum StatusCode {
/*
* Returned when a procedure executes without aborting.
*/
STATUS_CODE_SUCCESS = 1,
/*
* Returned when a procedure throws a VoltAbortException and is rolled back
*/
STATUS_CODE_USER_ABORT = -1,
/*
* Returned when a procedure fails due to something like a constraint violation
*/
STATUS_CODE_GRACEFUL_FAILURE = -2,
/*
* Returned when a procedure invocation fails. This can be because the procedure does not exist
* or it could be due to a runtime error within VoltDB
*/
STATUS_CODE_UNEXPECTED_FAILURE = -3,
/*
* Returned by the API when the connection to the server that a request was sent to is lost
*/
STATUS_CODE_CONNECTION_LOST = -4,
/**
* Returned by the API when the request didn't receive a response before the per-client
* timeout.
*/
STATUS_CODE_CONNECTION_TIMEOUT = -6,
/*
* Default value for the user specified app status code field
*/
STATUS_CODE_UNINITIALIZED_APP_STATUS_CODE = INT8_MIN,
};
/*
* Response to a stored procedure invocation. Generated by the API when a response is received from the server
* or the connection to the server the request was sent to is lost.
*/
class InvocationResponse {
public:
#ifdef SWIG
%ignore InvocationResponse();
#endif
/*
* Default constructor generates an error response indicating the connection
* to the database was lost
*/
InvocationResponse() :
m_clientData(0),
m_statusCode(voltdb::STATUS_CODE_CONNECTION_LOST),
m_statusString(std::string("Connection to the database was lost")),
m_appStatusCode(STATUS_CODE_UNINITIALIZED_APP_STATUS_CODE),
m_appStatusString(std::string("")),
m_clusterRoundTripTime(0),
m_results() {
}
#ifdef SWIG
%ignore InvocationResponse(boost::shared_array<char> data, int32_t length);
#endif
/*
* Constructor for taking shared ownership of a message buffer
* containing a response to a stored procedure invocation
*/
InvocationResponse(boost::shared_array<char>& data, int32_t length) : m_results(0) {
SharedByteBuffer buffer(data, length);
int8_t version = buffer.getInt8();
assert(version == 0);
m_clientData = buffer.getInt64();
int8_t presentFields = buffer.getInt8();
m_statusCode = buffer.getInt8();
bool wasNull = false;
if ((presentFields & (1 << 5)) != 0) {
m_statusString = buffer.getString(wasNull);
}
m_appStatusCode = buffer.getInt8();
if ((presentFields & (1 << 7)) != 0) {
m_appStatusString = buffer.getString(wasNull);
}
assert(!wasNull);
m_clusterRoundTripTime = buffer.getInt32();
if ((presentFields & (1 << 6)) != 0) {
int32_t position = buffer.position() + 4;
buffer.position(position + buffer.getInt32());
}
size_t resultCount = static_cast<size_t>(buffer.getInt16());
m_results.resize(resultCount);
int32_t startLimit = buffer.limit();
for (size_t ii = 0; ii < resultCount; ii++) {
int32_t tableLength = buffer.getInt32();
assert(tableLength >= 4);
buffer.limit(buffer.position() + tableLength);
m_results[ii] = voltdb::Table(buffer.slice());
buffer.limit(startLimit);
}
}
InvocationResponse(int64_t requestHandle, int8_t status,
const std::string& statusString,
int8_t appStatus, const std::string& appStatusString,
const std::vector<voltdb::Table>& results,
int32_t clusterRoudTripTime = 0): m_clientData(requestHandle),
m_statusCode(status),
m_statusString(statusString),
m_appStatusCode(appStatus),
m_appStatusString(appStatusString),
m_clusterRoundTripTime(clusterRoudTripTime),
m_results(results) { }
/*
* Returns the client data generated by the API on behalf of user. Can be ignored.
*/
int64_t clientData() const { return m_clientData; }
/*
* Status code returned by VoltDB
*/
int8_t statusCode() const { return m_statusCode; }
/*
* Returns true if the status code was success, false otherwise
*/
bool success() const { return m_statusCode == STATUS_CODE_SUCCESS; }
/*
* Returns true if the status code was not success, false otherwise
*/
bool failure() const { return m_statusCode != STATUS_CODE_SUCCESS; }
/*
* Returns a human readable string describing what occured. Will be the empty string if the
* status code is success
*/
std::string statusString() const { return m_statusString; }
/*
* Return status code set by the application (not Volt) while executing the stored procedure.
* Default value is -128 if the application does not set the code.
*/
int8_t appStatusCode() const { return m_appStatusCode; }
/*
* Return the status string set by the application (not Volt) while executing the stored procedure.
* Default value is the empty string.
*/
std::string appStatusString() const { return m_appStatusString; }
// Setters
void setClientData(int64_t handle) {
m_clientData = handle;
}
void setStatusCode(int8_t status) {
m_statusCode = status;
}
void setStatusString(const std::string& msg) {
m_statusString = msg;
}
void setAppStatusCode(int8_t status) {
m_appStatusCode = status;
}
void setAppStatusString(const std::string& appMsg) {
m_appStatusString = appMsg;
}
void setClusterRoundTripTime(int32_t roundTripTime) {
m_clusterRoundTripTime = roundTripTime;
}
/*
* Returns the round trip execution time of the stored procedure as measured by the VoltDB node
* that initiated the stored procedure invocation.
*/
int32_t clusterRoundTripTime() const { return m_clusterRoundTripTime; }
/*
* Returns a vector of tables containing result data returned by the stored procedure
*/
std::vector<voltdb::Table> results() const { return m_results; }
/*
* Generate a string representation of the contents of the message
*/
std::string toString() const {
std::ostringstream ostream;
ostream << "Status: " << static_cast<int32_t>(statusCode()) << ", " << statusString() << std::endl;
ostream << "App Status: " << static_cast<int32_t>(appStatusCode()) << ", " << appStatusString() << std::endl;
ostream << "Client Data: " << clientData() << std::endl;
ostream << "Cluster Round Trip Time: " << clusterRoundTripTime() << std::endl;
for (size_t ii = 0; ii < m_results.size(); ii++) {
ostream << "Result Table " << ii << std::endl;
m_results[ii].toString(ostream, std::string(" "));
}
return ostream.str();
}
void operator >> (std::ostream &ostream) const {
ostream.write((const char*)&m_statusCode, sizeof(m_statusCode));
writeString(ostream, m_statusString);
ostream.write((const char*)&m_appStatusCode, sizeof(m_appStatusCode));
writeString(ostream, m_appStatusString);
ostream.write((const char*)&m_clientData, sizeof(m_clientData));
ostream.write((const char*)&m_clusterRoundTripTime, sizeof(m_clusterRoundTripTime));
size_t size = m_results.size();
ostream.write((const char *)&size, sizeof(size));
for (size_t ii = 0; ii < m_results.size(); ii++) {
m_results[ii] >> ostream;
}
}
InvocationResponse(std::istream &istream) {
istream.read((char *)&m_statusCode, sizeof(m_statusCode));
m_statusString = readString(istream);
istream.read((char *)&m_appStatusCode, sizeof(m_appStatusCode));
m_appStatusString = readString(istream);
istream.read((char *)&m_clientData, sizeof(m_clientData));
istream.read((char *)&m_clusterRoundTripTime, sizeof(m_clusterRoundTripTime));
size_t size;
istream.read((char *)&size, sizeof(size));
m_results.resize(size);
for (size_t ii = 0; ii < size; ii++) {
m_results[ii] = voltdb::Table(istream);
}
}
private:
static std::ostream &writeString(std::ostream &ostream, const std::string &str) {
const int32_t size = str.size();
ostream.write((const char*)&size, sizeof(size));
if (size != 0) {
ostream.write(str.data(), size);
} return ostream;
}
std::string readString(std::istream &istream) {
std::string str;
int32_t size;
istream.read((char*)&size, sizeof(size));
if (size != 0) {
str.resize(size);
istream.read((char *)str.data(), size);
} return str;
}
private:
int64_t m_clientData;
int8_t m_statusCode;
std::string m_statusString;
int8_t m_appStatusCode;
std::string m_appStatusString;
int32_t m_clusterRoundTripTime;
std::vector<voltdb::Table> m_results;
};
}
#endif /* VOLTDB_INVOCATIONRESPONSE_HPP_ */
You can’t perform that action at this time.
