Repository navigation
Expand file tree
/
Copy pathSQLAir.cpp
More file actions
301 lines (277 loc) · 11.2 KB
/
Copy pathSQLAir.cpp
File metadata and controls
301 lines (277 loc) · 11.2 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
/* copyright caohd 2023
* A very lightweight (light as air) implementation of a simple CSV-based
* database system that uses SQL-like syntax for querying and updating the
* CSV files.
*
*/
#include <string>
#include <fstream>
#include <tuple>
#include <algorithm>
#include <memory>
#include "SQLAir.h"
#include "HTTPFile.h"
#include <boost/format.hpp>
using namespace boost::asio::ip;
// A shortcut to refer to scoped_lock in code
using Guard = std::scoped_lock<std::mutex>;
/**
* A fixed HTTP response header that is used by the runServer method below.
* Note that this a constant (and not a global variable)
*/
// const std::string HTTPRespHeader = "HTTP/1.1 200 OK\r\n"
// "Server: localhost\r\n"
// "Connection: Close\r\n"
// "Content-Type: text/plain\r\n"
// "Content-Length: ";
const std::string HTTPRespHeader = "HTTP/1.1 200 OK\r\n"
"Server: SimpleServer\r\n"
"Content-Length: %1%\r\n"
"Connection: Close\r\n"
"Content-Type: text/html\r\n\r\n";
// Helper method to process each row in select queries
void SQLAir::selectRowProcess(CSV& csv, StrVec colNames,
const int whereColIdx, const std::string& cond,
const std::string& value,
std::string& rowText, int& rowCount) {
for (auto& row : csv) {
// lock the row
Guard g(row.rowMutex);
// Determine if this row matches "where" clause condition, if any
// see SQLAirBase::matches() helper method.
bool isMatch = (whereColIdx == -1) ? true :
matches(row.at(whereColIdx), cond, value);
if (isMatch) {
std::string delim = "";
for (auto& colName : colNames) {
std::string cell;
cell = row.at(csv.getColumnIndex(colName));
rowText += delim + cell;
delim = "\t";
}
// os << std::endl;
rowText += "\n";
rowCount++;
}
}
}
// Helper method to process each row in update queries
void SQLAir::updateRowProcess(CSV& csv, StrVec colNames, StrVec values,
const int whereColIdx, const std::string& cond,
const std::string& value, int& rowCount) {
for (auto& row : csv) {
// Determine if this row matches "where" clause condition, if any
// see SQLAirBase::matches() helper method.
Guard g(row.rowMutex);
bool isMatch;
if (whereColIdx != -1) {
isMatch = matches(row.at(whereColIdx), cond, value);
}
if (whereColIdx == -1 || isMatch) {
for (size_t i = 0; i < colNames.size(); i++) {
// update each cell
row.at(csv.getColumnIndex(colNames[i])) = values.at(i);
}
rowCount++;
}
}
}
// API method to perform operations associated with a "select" statement
// to print columns that match an optional condition.
void SQLAir::selectQuery(CSV& csv, bool mustWait, StrVec colNames,
const int whereColIdx, const std::string& cond,
const std::string& value, std::ostream& os) {
// Convert any "*" to suitable column names. See CSV::getColumnNames()
if (colNames[0] == "*") colNames = csv.getColumnNames();
// First print the column names.
// os << colNames << std::endl;
// row count
int rowCount = 0;
std::string rowText;
// Print each row that matches an optional condition.
selectRowProcess(csv, colNames, whereColIdx, cond, value, rowText,
rowCount);
while (rowCount == 0 && mustWait) {
// unique lock
std::unique_lock<std::mutex> lock(csv.csvMutex);
csv.csvCondVar.wait(lock);
selectRowProcess(csv, colNames, whereColIdx, cond, value, rowText,
rowCount); // critical section
if (rowCount != 0) {
mustWait = false;
}
}
if (rowCount != 0) os << colNames << std::endl;
os << rowText <<std::to_string(rowCount) + " row(s) selected." << std::endl;
}
void
SQLAir::updateQuery(CSV& csv, bool mustWait, StrVec colNames, StrVec values,
const int whereColIdx, const std::string& cond,
const std::string& value, std::ostream& os) {
// row count
int rowCount = 0;
// Print each row that matches an optional condition.
updateRowProcess(csv, colNames, values,
whereColIdx, cond, value, rowCount);
while (rowCount == 0 && mustWait) {
// unique lock
std::unique_lock<std::mutex> lock(csv.csvMutex);
csv.csvCondVar.wait(lock);
updateRowProcess(csv, colNames, values,
whereColIdx, cond, value , rowCount); // critical section
if (rowCount != 0) {
mustWait = false;
}
}
if (rowCount != 0) {
csv.csvCondVar.notify_all();
}
os << std::to_string(rowCount) + " row(s) updated." << std::endl;
}
void
SQLAir::insertQuery(CSV& csv, bool mustWait, StrVec colNames,
StrVec values, std::ostream& os) {
throw Exp("insert is not yet implemented.");
}
void
SQLAir::deleteQuery(CSV& csv, bool mustWait, const int whereColIdx,
const std::string& cond, const std::string& value, std::ostream& os) {
throw Exp("delete is not yet implemented.");
}
// Thread method for each thread
void SQLAir::clientThread(std::istream& is, std::ostream& os) {
std::string line, path;
is >> line >> path;
// Skip/ignore all the HTTP request & headers for now.
if (path.find("/sql-air?query=") != std::string::npos) {
// This is a command to be processed. So use a helper method
// to streamline the code.
for (std::string hdr; std::getline(is, hdr) && !hdr.empty()
&& hdr != "\r"; ) {}
std::ostringstream oss;
path = path.substr(15); // get rid of the syntax at the begin ?
path = Helper::url_decode(path);
try {
SQLAirBase::process(path, oss);
} catch (const std::exception &exp) {
oss << "Error: " << exp.what() << std::endl;
}
std::string html = oss.str();
auto httpRespHdr = boost::str(boost::format(HTTPRespHeader)
% html.length());
os << httpRespHdr << html;
} else if (!path.empty()) {
// In this case we assume the user is asking for a file. Have
// the helper http class do the processing.
// Remove the leading '/' sign.
path = path.substr(1);
// Use the http::file helper method to send the response back
// to the client.
path = Helper::url_decode(path);
os << http::file(path);
}
// decrease thread count
SQLAir::numThreads--;
SQLAir::thrCond.notify_one(); // end client thread by notify empty slot
}
// The method to have this class run as a web-server.
void
SQLAir::runServer(boost::asio::ip::tcp::acceptor& server, const int maxThr) {
// unique lock for the server
std::unique_lock<std::mutex> lock(SQLAir::serverMutex);
while (true) {
// we have 2 threads when starting the .exe
SQLAir::thrCond.wait(lock, [&]
{return SQLAir::numThreads <= maxThr; });
// Setup a server socket to accept connections on the socket
// Create garbage-collected, shared object on heap so we can
// send it to another thread and not worry about life-time of
// the socket connection.
TcpStreamPtr client = std::make_shared<tcp::iostream>();
// Wait for a client to connect
server.accept(*client->rdbuf());
// Process request from client on a separate background thread
std::thread thr([this, client]()
{ SQLAir::clientThread(*client, *client);});
thr.detach();
// increase thread count
SQLAir::numThreads++;
}
}
// Assume that an URL of the form http://data.com:8080/test.csv has been
// split into host = “data.com”, port = “8080” and path = “/test.csv”
void SQLAir::loadFromURL(CSV& csv, const std::string& hostName,
const std::string& port, const std::string& path) {
// Setup a boost tcp stream to send an HTTP request to the web-server
tcp::iostream client(hostName, port);
if (!client.good()) {
throw Exp("Unable to connect to " + hostName + " at port " + port);
}
// Send an HTTP get request to get the data from the server
client << "GET " << path << " HTTP/1.1\r\nHost: " << hostName << "\r\n"
<< "Connection: Close\r\n\r\n";
// Get response status from server to ensure we have a valid response.
std::string status;
// To check if response is 200 OK status code.
std::getline(client, status);
// throw exception if do not find 200 OK
if (status.find("200") == std::string::npos) {
throw Exp("Error (" + Helper::trim(status) + ") getting " + path +
" from " + hostName + " at port " + port);
}
// Skip headers
for (std::string hdr; std::getline(client, hdr) && !hdr.empty()
&& hdr != "\r"; ) {}
// Now have the CSV class do rest of the processing
csv.load(client);
}
CSV& SQLAir::loadAndGet(std::string fileOrURL) {
// Check if the specified fileOrURL is already loaded in a thread-safe
// manner to avoid race conditions on the unordered_map
{
std::scoped_lock<std::mutex> guard(recentCSVMutex);
// Use recent CSV if parameter was empty string.
fileOrURL = (fileOrURL.empty() ? recentCSV : fileOrURL);
// Update the most recently used CSV for the next round
recentCSV = fileOrURL;
if (inMemoryCSV.find(fileOrURL) != inMemoryCSV.end()) {
// Requested CSV is already in memory. Just return it.
return inMemoryCSV.at(fileOrURL);
}
}
// When control drops here, we need to load the CSV into memory.
// Loading or I/O is being done outside critical sections
CSV csv; // Load data into this csv
if (fileOrURL.find("http://") == 0) {
// This is an URL. We have to get the stream from a web-server
// Implement this feature.
std::string host, port, path;
std::tie(host, port, path) = Helper::breakDownURL(fileOrURL);
loadFromURL(csv, host, port, path);
} else {
// We assume it is a local file on the server. Load that file.
std::ifstream data(fileOrURL);
// This method may throw exceptions on errors.
csv.load(data);
}
// We get to this line of code only if the above if-else to load the
// CSV did not throw any exceptions. In this case we have a valid CSV
// to add to our inMemoryCSV list. We need to do that in a thread-safe
// manner.
std::scoped_lock<std::mutex> guard(recentCSVMutex);
// Move (instead of copy) the CSV data into our in-memory CSVs
inMemoryCSV[fileOrURL].move(csv);
// Return a reference to the in-memory CSV (not temporary one)
return inMemoryCSV.at(fileOrURL);
}
// Save the currently loaded CSV file to a local file.
void
SQLAir::saveQuery(std::ostream& os) {
if (recentCSV.empty() || recentCSV.find("http://") == 0) {
throw Exp("Saving CSV to an URL using POST is not implemented");
}
// Create a local file and have the CSV write itself.
std::ofstream csvData(recentCSV);
inMemoryCSV.at(recentCSV).save(csvData);
os << recentCSV << " saved.\n";
}