-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSQLiteCommand.cpp
99 lines (91 loc) · 3.36 KB
/
SQLiteCommand.cpp
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
#include "SQLiteCommand.h"
#include <libstuff/libstuff.h>
#include <libstuff/SRandom.h>
SData SQLiteCommand::preprocessRequest(SData&& request) {
// If the request doesn't specify an execution time, default to right now.
if (request.isSet("commandExecuteTime")) {
// We are deprecating `commandExecuteTime` so need to figure out where it's used.
auto now = STimeNow();
auto executeTime = request.calcU64("commandExecuteTime");
if (executeTime > now + 5'000'000) {
auto difference = executeTime - now;
SINFO("Command '" << request.methodLine << "' requested execution time " << difference << "us in the future.");
}
}
// Add a request ID if one was missing.
if (!request.isSet("requestID")) {
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string requestID;
for (int i = 0; i < 6; i++) {
requestID += chars[SRandom::rand64() % chars.size()];
}
request["requestID"] = requestID;
}
return move(request);
}
SQLiteCommand::SQLiteCommand(SData&& _request) :
privateRequest(preprocessRequest(move(_request))),
request(privateRequest),
initiatingClientID(0),
writeConsistency(SQLiteNode::ASYNC),
complete(false),
escalationTimeUS(0),
creationTime(STimeNow()),
escalated(false)
{
// Initialize the consistency, if supplied.
if (request.isSet("writeConsistency")) {
int tempConsistency = request.calc("writeConsistency");
switch (tempConsistency) {
// For any valid case, we just set the value directly.
case SQLiteNode::ASYNC:
case SQLiteNode::ONE:
case SQLiteNode::QUORUM:
writeConsistency = static_cast<SQLiteNode::ConsistencyLevel>(tempConsistency);
break;
default:
// But an invalid case gets set to ASYNC, and a warning is thrown.
SWARN("'" << request.methodLine << "' requested invalid consistency: " << writeConsistency);
writeConsistency = SQLiteNode::ASYNC;
break;
}
}
}
SQLiteCommand::SQLiteCommand(SQLiteCommand&& from) :
privateRequest(move(from.privateRequest)),
request(privateRequest),
initiatingClientID(from.initiatingClientID),
id(move(from.id)),
jsonContent(move(from.jsonContent)),
response(move(from.response)),
writeConsistency(from.writeConsistency),
complete(from.complete),
escalationTimeUS(from.escalationTimeUS),
creationTime(from.creationTime),
escalated(from.escalated)
{
}
SQLiteCommand& SQLiteCommand::operator=(SQLiteCommand&& from) noexcept {
privateRequest = move(from.privateRequest);
const_cast<SData&>(request) = privateRequest;
initiatingClientID = from.initiatingClientID;
id = move(from.id);
jsonContent = move(from.jsonContent);
response = move(from.response);
writeConsistency = from.writeConsistency;
complete = from.complete;
escalationTimeUS = from.escalationTimeUS;
creationTime = from.creationTime;
escalated = from.escalated;
return *this;
}
SQLiteCommand::SQLiteCommand() :
privateRequest(),
request(privateRequest),
initiatingClientID(0),
writeConsistency(SQLiteNode::ASYNC),
complete(false),
escalationTimeUS(0),
creationTime(STimeNow()),
escalated(false)
{ }