-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_skel.c
More file actions
executable file
·250 lines (223 loc) · 7.94 KB
/
table_skel.c
File metadata and controls
executable file
·250 lines (223 loc) · 7.94 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
/*
* File: table_skel.c
*
* Rotina de adaptação do servidor.
*
* Author: sd001 > Bruno Neves, n.º 31614
* > Vasco Orey, n.º 32550
*/
#include "table.h"
#include "table-private.h"
#include "remote_table.h" // Para os opcodes
#include "table_skel.h"
#include "utils.h"
#include "persistent_table.h"
/*
* MAX_LOG_SIZE: Tamanho máximo que o ficheiro log pode ter.
*/
#define MAX_LOG_SIZE 10000
static struct ptable_t *sharedPtable = NULL;
/*
* Inicia o skeleton da tabela.
* O main() do servidor deve chamar este método antes de usar a
* funço invoke(). O parâmetro n_lists define o número de listas a serem
* usadas pela tabela mantida no servidor. O parâmetro filename corresponde ao
* nome a ser usado nos ficheiros de log e checkpoint.
* Retorna 0 (OK) ou -1 (erro, por exemplo OUT OF MEMORY).
*/
int table_skel_init(int n_lists, char *filename) {
//verifica a validade dos parâmetros
if(n_lists <= 0 || filename == NULL) {
ERROR("table_skel: NULL filename or invalid nlists");
return -1;
}
if(!sharedPtable) {
//cria e verifica uma nova tabela
struct table_t *sharedTable;
if((sharedTable = table_create(n_lists)) == NULL) {
ERROR("table_skel: table_create");
return -1;
}
//cria e verifica um novo persistence_manager
struct pmanager_t *sharedPmanager;
if((sharedPmanager = pmanager_create(filename, MAX_LOG_SIZE, MODE_DSYNC)) == NULL) {
ERROR("table_skel: pmanager_create");
table_destroy(sharedTable);
return -1;
}
//cria e verifica um persistent_table
if((sharedPtable = ptable_open(sharedTable, sharedPmanager)) == NULL) {
ERROR("table_skel: ptable_open");
table_destroy(sharedTable);
pmanager_destroy(sharedPmanager);
return -1;
}
// Garbage collection
ptable_collect_garbage(sharedPtable);
}
//em caso de sucesso
return 0;
}
/*
* Serve para libertar toda a memória alocada pela função anterior.
*/
int table_skel_destroy() {
if(sharedPtable) {
ptable_close(sharedPtable);
}
//em caso de sucesso
return 0;
}
/*
* Executar uma função (indicada pelo opcode na msg) e retorna o resultado na
* própria struct msg.
* Retorna 0 (OK) ou -1 (erro, por exemplo, tabela nao inicializada).
*/
int invoke(struct message_t *msg) {
int retVal = 0;
char *key;
struct entry_t *entry;
if(sharedPtable && msg) {
switch (msg->opcode) {
case OP_RT_GET:
// table_get: (struct table_t* char*) -> (struct data_t*)
if(msg->content.key) {
key = msg->content.key;
if(!(msg->content.value = ptable_get(sharedPtable, key))) {
if(!(msg->content.value = data_create(0))) {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
}
if(msg->content.value) {
msg->opcode ++; // Incrementa para dizer que esta mensagem contem o resultado
msg->c_type = CT_VALUE;
free(key);
}
}
else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
break;
case OP_RT_PUT:
// table_put: (struct table_t* char* struct data_t*) -> (int)
if(msg->content.entry) {
entry = msg->content.entry;
if((retVal = ptable_put(sharedPtable, entry->key, entry->value)) != -1) {
msg->content.result = retVal;
msg->opcode ++;
msg->c_type = CT_RESULT;
}
else {
// Deu erro pq ficamos sem espaço de log.
//cria um ficheiro temporário com o estado da tabela
if(pmanager_store_table(sharedPtable->pmanager, sharedPtable->table) < 0) {
ERROR("persistent_table: pmanager_store_table");
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
//cria um ficheiro temporário com o estado da tabela
if(pmanager_rotate_log(sharedPtable->pmanager) != 0) {
ERROR("persistent_table: pmanager_rotate_log");
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
if((retVal = ptable_put(sharedPtable, entry->key, entry->value)) != -1) {
msg->content.result = retVal;
msg->opcode ++;
msg->c_type = CT_RESULT;
} else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
}
entry_destroy(entry);
}
else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
break;
case OP_RT_SIZE:
// table_size: (struct table_t*) -> (int)
msg->content.result = ptable_size(sharedPtable);
msg->opcode ++;
msg->c_type = CT_RESULT;
// Nao temos nada para libertar...
break;
case OP_RT_DEL:
// table_del: (struct table_t* char*) -> (int)
if(msg->content.key) {
key = msg->content.key;
msg->content.key = NULL;
retVal = ptable_del(sharedPtable, key);
msg->content.result = retVal;
msg->opcode ++;
msg->c_type = CT_RESULT;
free(key);
}
else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
break;
case OP_RT_GETKEYS:
// table_get_keys: (struct table_t*) -> (char**)
if((msg->content.keys = ptable_get_keys(sharedPtable))) {
msg->opcode ++;
msg->c_type = CT_KEYS;
}
else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
// Não temos nada para libertar...
break;
case OP_RT_GETTS:
// table_get: (struct table_t* char*) -> (int)
if(msg->content.key) {
key = strdup(msg->content.key);
free(msg->content.key);
// Verifica a validade da resposta
if((msg->content.timestamp = ptable_get_ts(sharedPtable, key)) < 0) {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
} else if(msg->content.timestamp >= 0) {
msg->opcode ++; // Incrementa para dizer que esta mensagem contem o resultado
msg->c_type = CT_TIMESTAMP;
}
}
else {
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
}
free(key);
break;
default:
ERROR("opcode");
msg->opcode = OP_RT_ERROR;
msg->c_type = CT_RESULT;
msg->content.result = -1;
break;
}
}
else {
ERROR("NULL sharedPtable");
retVal = -1;
}
return retVal;
}
void table_skel_collect() {
ptable_collect_garbage(sharedPtable);
}