-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
144 lines (126 loc) · 4.08 KB
/
server.c
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
#include "mapipbtest.h"
static void create_result_from_csv(Mapipbtest__QueryResult *result, char* file,
size_t ncols, size_t nrows, char* coltypes, bool print_summary) {
mapipbtest__query_result__init(result);
result->rows = nrows;
result->n_columns = ncols;
FILE *fr = fopen(DATA_FILE, "rt");
char line[MAX_LINE_LEN];
char * pch;
size_t row = 0;
size_t col = 0;
Mapipbtest__QueryResult__Column **columns;
columns = malloc(sizeof(Mapipbtest__QueryResult__Column*) * (DATA_COLS));
for (col = 0; col < DATA_COLS; col++) {
columns[col] = malloc(sizeof(Mapipbtest__QueryResult__Column));
mapipbtest__query_result__column__init(columns[col]);
if (DATA_TYPES[col] == 'i') {
columns[col]->intvalues = malloc(sizeof(int64_t) * nrows);
columns[col]->n_intvalues = nrows;
columns[col]->type =
MAPIPBTEST__QUERY_RESULT__COLUMN__TYPE__INTEGER;
}
if (DATA_TYPES[col] == 'f') {
columns[col]->floatvalues = malloc(sizeof(double) * nrows);
columns[col]->n_floatvalues = nrows;
columns[col]->type = MAPIPBTEST__QUERY_RESULT__COLUMN__TYPE__FLOAT;
}
if (DATA_TYPES[col] == 's') {
columns[col]->stringvalues = malloc(sizeof(char*) * nrows);
columns[col]->n_stringvalues = nrows;
columns[col]->type = MAPIPBTEST__QUERY_RESULT__COLUMN__TYPE__STRING;
}
}
result->columns = columns;
size_t datalen = 0;
row = 0;
while (fgets(line, MAX_LINE_LEN, fr) != NULL && row <= DATA_ROWS ) {
assert(row <= nrows);
col = 0;
datalen += strlen(line);
pch = strtok(line, "|");
while (pch != NULL && col < ncols) {
if (DATA_TYPES[col] == 'i') {
result->columns[col]->intvalues[row] = atol(pch);
}
if (DATA_TYPES[col] == 'f') {
result->columns[col]->floatvalues[row] = atof(pch);
}
if (DATA_TYPES[col] == 's') {
result->columns[col]->stringvalues[row] = strdup(pch);
}
col++;
pch = strtok(NULL, "|");
}
row++;
}
if (print_summary)
printf("READ %s, %i cols, %i rows, %zu bytes.\n", DATA_FILE, DATA_COLS,
DATA_ROWS, datalen);
fclose(fr);
}
int main(void) {
// read data (this is outside time measurements, since the data comes from anywhere and is assumed in (virtual) memory)
Mapipbtest__QueryResult query_result;
create_result_from_csv(&query_result, DATA_FILE, DATA_COLS, DATA_ROWS,
DATA_TYPES, 1);
struct snappy_env se;
snappy_init_env(&se);
// react on queries
void *context = zmq_ctx_new();
void *socket = zmq_socket(context, ZMQ_REP);
int rc;
rc = zmq_bind(socket, SERVER_SOCKET);
if (rc != 0) {
fprintf(stderr, "Failed to bind to socket %s\n", SERVER_SOCKET);
perror("Socket error ");
return -1;
}
long s_time, c_time, req_time;
struct timeval start, req_start;
while (1) {
gettimeofday(&req_start, NULL );
// receive request
message query_msg;
message_receive(socket, &query_msg);
size_t query_msg_size = message_size(&query_msg);
void * query_msg_data = message_data(&query_msg);
if (COMPRESS) {
query_msg_data = message_uncompress(query_msg_data,
&query_msg_size);
}
Mapipbtest__ExecuteQuery * request = mapipbtest__execute_query__unpack(
NULL, query_msg_size, query_msg_data);
message_close(&query_msg);
if (COMPRESS) {
free(query_msg_data);
}
//printf("%s\n", request->sqlquery);
//mapipbtest__execute_query__free_unpacked(request, NULL );
// send response
gettimeofday(&start, NULL );
size_t query_response_msg_size =
mapipbtest__query_result__get_packed_size(&query_result);
void * query_response_msg_data = malloc(query_response_msg_size);
mapipbtest__query_result__pack(&query_result, query_response_msg_data);
s_time = end_timer_ms(&start);
gettimeofday(&start, NULL );
char * free_ptr;
if (COMPRESS) {
free_ptr = query_response_msg_data;
query_response_msg_data = message_compress(&se,
query_response_msg_data, &query_response_msg_size);
}
c_time = end_timer_ms(&start);
message_send(socket, query_response_msg_data, query_response_msg_size);
//free(query_response_msg_data);
req_time = end_timer_ms(&req_start);
//free(query_response_msg_data);
if (COMPRESS) {
free(free_ptr);
}
}
zmq_close(socket);
zmq_ctx_destroy(context);
return 0;
}