-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.c
490 lines (417 loc) · 11.7 KB
/
storage.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* @file
* @brief This file contains the implementation of the storage server
* interface as specified in storage.h.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "storage.h"
#include "utils.h"
#include "file.h"
#include <sys/time.h>
#include <errno.h>
/**
* @brief for error checking.
*/
int errno;
/**
* @brief helper variable for checking authorisation.
*/
int auth;
/**
* @brief This is used to establish a connection with server.
*/
void* storage_connect(const char *hostname, const int port)
{
char buff[100];
if (hostname == NULL) {
errno = ERR_INVALID_PARAM;
return NULL;
}
// Assume port is given in the correct format.
if (my_strvalidate(hostname, 3)) {
errno=ERR_INVALID_PARAM;
return NULL;
}
//print statement here will tell us if it started connecting to the server or not
sprintf(buff, "Attempting to connect to server '%s'\n",
hostname);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Create a socket.
int sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0){
errno=ERR_INVALID_PARAM; //error :invalid
return NULL;
}
// Get info about the server.
struct addrinfo serveraddr, *res;
memset(&serveraddr, 0, sizeof serveraddr);
serveraddr.ai_family = AF_UNSPEC;
serveraddr.ai_socktype = SOCK_STREAM;
char portstr[MAX_PORT_LEN];
snprintf(portstr, sizeof portstr, "%d", port);
int status = getaddrinfo(hostname, portstr, &serveraddr, &res);
if (status != 0){
errno=ERR_INVALID_PARAM; //error :invalid
return NULL;
}
// Connect to the server.
status = connect(sock, res->ai_addr, res->ai_addrlen);
if (status != 0){
errno=ERR_CONNECTION_FAIL; //error :connection failed
return NULL;
}
return (void*) sock;
}
/**
* @brief This is used to authenticate the arguments from the client
*/
int storage_auth(const char *username, const char *passwd, void *conn)
{
if (!conn) {
errno = ERR_CONNECTION_FAIL;
return -1;
}
if (username == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (passwd == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
char buff[100];
//print statement here will tell us if it started logging in
sprintf(buff, "Attempting to Login with username '%s'\n",username);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Connection is really just a socket file descriptor.
int sock = (int)conn;
// Send some data.
char buf[MAX_CMD_LEN];
memset(buf, 0, sizeof buf);
char *encrypted_passwd = generate_encrypted_password(passwd, NULL);
snprintf(buf, sizeof buf, "AUTH %s %s\n", username, encrypted_passwd);
if (sendall(sock, buf, strlen(buf)) == 0 && recvline(sock, buf, sizeof buf) == 0) {
if (strcmp (buf, "-1") == 0) {
auth = 0;
errno = ERR_AUTHENTICATION_FAILED;
return -1;
}
auth=1;
return 0;
}
auth=0;
errno=ERR_AUTHENTICATION_FAILED; //error :authentication failed
return -1;
}
/**
* @brief This is used to get values back from the database
*/
int storage_get(const char *table, const char *key, struct storage_record *record, void *conn)
{
if (conn==NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (!conn) {
errno = ERR_CONNECTION_FAIL;
return -1;
}
if (record == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (table == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (key == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (strlen(table)>MAX_TABLE_LEN || strlen(key) > MAX_KEY_LEN){
errno=ERR_INVALID_PARAM;
return -1;
}
if (my_strvalidate(table, 1) ||my_strvalidate(key, 1)){
errno=ERR_INVALID_PARAM;
return -1;
}
struct timeval start_time, end_time;
// Remember when the experiment started.
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
char buff[100];
//print statement here will tell us if it started accessing the table
sprintf(buff, "Accessing table '%s' to fetch value from key '%s'\n",
table, key);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Connection is really just a socket file descriptor.
int sock = (int)conn;
// Send some data.
char buf[MAX_CMD_LEN];
memset(buf, 0, sizeof buf);
snprintf(buf, sizeof buf, "GET %s %s\n", table, key);
if(auth==1){
if (sendall(sock, buf, strlen(buf)) == 0 && recvline(sock, buf, sizeof buf) == 0) {
if (strcmp (buf, "-1") == 0) {
errno = ERR_TABLE_NOT_FOUND;
return -1;
}
if (strcmp (buf, "-2") == 0) {
errno = ERR_KEY_NOT_FOUND;
return -1;
}
int j = 0;
j = strcspn (buf, " ");
record->metadata[0] = atoi(strtok(buf, " "));
//memset (record->metadata, atoi(strtok(buf, " ")), sizeof(record->metadata));
buf[j] = ' ';;
strncpy(record->value, buf+j+1, sizeof record->value);
// Get the time at the end of the experiment.
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
// Calculate difference in time and output it.
double totaltime=t2-t1;
if (TESTING == 1) {
FILE* tFile = fopen ("test_times/get", "a");
fprintf (tFile, "%.6lf\n", totaltime);
fclose(tFile);
}
sprintf(buff,"The total time taken was %.6lf seconds.\n",totaltime);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
return 0;
}
}
else{
errno=ERR_NOT_AUTHENTICATED; //error :not authenticated
return -1;
}
// Get the time at the end of the experiment.
// Calculate difference in time and output it.
//errno=errno_test; //error :table or key not found
return -1;
}
/**
* @brief This is used to set data into database
*/
int storage_set(const char *table, const char *key, struct storage_record *record, void *conn)
{
char TEMP[100];
if (conn==NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (!conn) {
errno = ERR_CONNECTION_FAIL;
return -1;
}
if (record != NULL) {
if (record->value == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
}
if (table == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (key == NULL) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (strlen(table)>MAX_TABLE_LEN || strlen(key) > MAX_KEY_LEN){
errno=ERR_INVALID_PARAM;
return -1;
}
if (strlen(table) < 1 || strlen(key) < 1){
errno=ERR_INVALID_PARAM;
return -1;
}
if (my_strvalidate(table, 1) ||my_strvalidate(key, 1)){
errno=ERR_INVALID_PARAM;
return -1;
}
struct timeval start_time, end_time;
// Remember when the experiment started.
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
char buff[100];
strcpy (TEMP, key);
//print statement here will tell us if it started setting the value to table
sprintf(buff, "Accessing table '%s' to set value to key '%s'\n",
table, TEMP);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Connection is really just a socket file descriptor.
int sock = (int)conn;
// Send some data.
char buf[MAX_CMD_LEN];
memset(buf, 0, sizeof buf);
if (record == NULL)
snprintf(buf, sizeof buf, "SET %d %s %s %s\n", 0, table, TEMP, "NULL");
else if (strcmp (record->value, "FILESTORE") == 0)
snprintf(buf, sizeof buf, "FILE %s \n", table);
else
snprintf(buf, sizeof buf, "SET %d %s %s %s\n", record->metadata[0], table, TEMP, record->value);
if(auth==1){
// printf(" sending data \n");
if (buf != NULL && sendall(sock, buf, strlen(buf)) == 0 && recvline(sock, buf, sizeof buf) == 0) {
if (strcmp (buf, "-1") == 0) {
errno = ERR_TABLE_NOT_FOUND;
return -1;
}
if (strcmp (buf, "-2") == 0) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (strcmp (buf, "-3") == 0) {
errno = ERR_KEY_NOT_FOUND;
return -1;
}
if (strcmp (buf, "-4") == 0) {
errno = ERR_TRANSACTION_ABORT;
return -1;
}
// Get the time at the end of the experiment.
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
// Calculate difference in time and output it.
double totaltime=t2-t1;
//printf("The total time taken was %.6lf seconds.\n",totaltime);
// printf("The total time taken was %.6lf seconds.\n",totaltime);
if (TESTING == 1) {
FILE* tFile = fopen ("test_times/set", "a");
fprintf (tFile, "%.6lf\n", totaltime);
fclose(tFile);
}
sprintf(buff,"The total time taken was %.6lf seconds.\n",totaltime);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Calculate difference in time and output it.
return 0;
}
}
else{
printf(" authentication error \n");
errno=ERR_NOT_AUTHENTICATED; //error :not authenticated
return -1;
}
// Get the time at the end of the experiment.
// Calculate difference in time and output it.
//errno=errno_test; //key not found
return -1;
}
/**
* @brief This is used to query data into database
*/
int storage_query(const char *table, const char *predicates, char **keys, const int max_keys, void *conn) {
if (!conn) {
errno = ERR_CONNECTION_FAIL;
return -1;
}
struct timeval start_time, end_time;
// Remember when the experiment started.
gettimeofday(&start_time,NULL);
double t1=start_time.tv_sec+(start_time.tv_usec/1000000.0);
char buff[100];
//print statement here will tell us if it started accessing the table
sprintf(buff, "Accessing table '%s' to fetch keys from predicates: '%s'\n",
table, predicates);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
// Connection is really just a socket file descriptor.
int sock = (int)conn;
// Send some data.
char buf[MAX_CMD_LEN];
memset(buf, 0, sizeof buf);
snprintf(buf, sizeof buf, "QUERY %s %d %s\n", table, max_keys, predicates);
if(auth==1){
if (sendall(sock, buf, strlen(buf)) == 0 && recvline(sock, buf, sizeof buf) == 0) {
if (max_keys == 0)
keys == NULL;
if (strcmp (buf, "-1") == 0) {
errno = ERR_TABLE_NOT_FOUND;
return -1;
}
else if (strcmp (buf, "-2") == 0) {
errno = ERR_INVALID_PARAM;
return -1;
}
if (strcmp (buf, "0") == 0) {
keys[0] = "";
return 0;
}
// Take buf and parse it for the number of keys and the list of keys and separate them into their appropriate variables (return number and set list into keys).
int numKeys = 0;
int i = 0;
char* arg;
//char* delims;
//strcpy (delims, " ");
arg = strtok (buf, " ");
numKeys = atoi (arg);
arg = strtok (NULL, " ");
while (arg != NULL) {
// printf (arg);
strcpy (keys[i], arg);
i += 1;
arg = strtok (NULL, " ");
}
// Get the time at the end of the experiment.
gettimeofday(&end_time,NULL);
double t2=end_time.tv_sec+(end_time.tv_usec/1000000.0);
// Calculate difference in time and output it.
double totaltime=t2-t1;
// printf("The total time taken was %.6lf seconds.\n",totaltime);
if (TESTING == 1) {
FILE* tFile = fopen ("test_times/query", "a");
fprintf (tFile, "%.6lf\n", totaltime);
fclose(tFile);
}
sprintf(buff,"The total time taken was %.6lf seconds.\n",totaltime);
if (LOGGING == 1) logger(stdout, buff);
else if (LOGGING == 2) logger(file, buff);
return numKeys;
}
}
else{
errno=ERR_NOT_AUTHENTICATED; //error :not authenticated
return -1;
}
// Get the time at the end of the experiment.
// Calculate difference in time and output it.
//errno=errno_test; //error :table or key not found
return -1;
}
/**
* @brief This is used to disconnect the connection
*/
int storage_disconnect(void *conn)
{
if (!conn) {
errno = ERR_INVALID_PARAM;
return -1;
}
// Cleanup
int sock = (int)conn;
if (sock < 0){
errno=ERR_INVALID_PARAM; //error :invalid
return -1;
}
auth = 0;
int status = close(sock);
if (status == -1) {
errno = ERR_UNKNOWN;
return -1;
}
return 0;
}