-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_main.cc
127 lines (113 loc) · 3.95 KB
/
server_main.cc
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
#include "server/ServerSocket.h"
#include "server/SocketException.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <thread>
#include <map>
#include "App.h"
#include "Table.h"
#include "GarbageCollector.h"
using namespace std;
void restartWaitingTransaction(int interval, Transaction* transaction, Table* table, GarbageCollector* garbage)
{
std::thread([interval, transaction, table, garbage]()
{
while (true) {
std::this_thread::sleep_for(
std::chrono::milliseconds(interval));
// get transaction and execute updateCommand
vector<size_t> txWaitingList = transaction->getWaitingList();
for (size_t i = 0; i < txWaitingList.size(); i++) {
cout << "waiting transaction #" << i << endl;
size_t txIdx = txWaitingList.at(i);
ServerSocket* client = transaction->getClient(txIdx);
// execute command and return to client
string result = updateCommand(client, table, transaction, transaction->getCommand(txIdx), garbage, txIdx);
try {
if (result != "WAITING")
(*client) << result;
} catch(SocketException& e) {
std::cout<< "Exception caught: " << e.description() << std::endl;
}
}
}
}).detach();
}
int main(int argc, char* argv[]) {
puts("***** Simple Column-Store Database start ******");
// Create table orders
string createQuery = "create table orders (o_orderkey integer, o_orderstatus text, o_totalprice integer, o_comment text)";
Table* ordersTable = createTable(createQuery);
// Transaction
Transaction* transaction = new Transaction();
// start Garbage collection as thread
GarbageCollector* garbage = new GarbageCollector();
garbage->setTransaction(transaction);
garbage->setTable(ordersTable);
// run each 1000 ms
garbage->start(1000);
// restart waiting transaction as thread
restartWaitingTransaction(10000, transaction, ordersTable, garbage);
try {
// Create the Socket
int port = 30000;
ServerSocket server(port);
std::cout << ">>>> Server is listening at port: " << port << std::endl;
std::ofstream log ("server.log");
while(true) {
ServerSocket client;
server.accept(client);
try {
while(true) {
std::string data;
client >> data;
std::cout << "[Received]\t" << data << std::endl;
log << "\n[Received]\t" << data << std::endl;
/* CODE BEGIN */
// split data into command
vector<string> command;
string token;
string delim = "|";
size_t last = 0; size_t next = 0;
while ((next = data.find(delim, last)) != string::npos) {
token = data.substr(last, next - last);
last = next + delim.length();
command.push_back(token);
}
// get the last token
token = data.substr(last);
command.push_back(token);
// command type
string commandType = command.at(0);
string result = "";
if (commandType == "UPDATE") {
cout << ">> Update command" << endl;
result = updateCommand(&client, ordersTable, transaction, command, garbage);
}
else if (commandType == "INSERT") {
cout << ">> Insert command" << endl;
result = insertCommand(ordersTable, transaction, command);
}
else if (commandType == "SCAN") {
cout << ">> Scan command" << endl;
result = scanCommand(ordersTable, transaction, command);
}
else {
result = "NO VALID COMMAND FOUND !";
}
// send result to client
if (result != "WAITING")
client << result;
log << "Result:\n" << result << endl;
log.flush();
/* CODE END */
}
} catch(SocketException&) {}
}
} catch(SocketException& e) {
std::cout<< "Exception caught: " << e.description() << std::endl;
}
return 0;
}