forked from rmeloca/APS1
/
bloco.c
393 lines (330 loc) · 12.4 KB
/
bloco.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
/*
* File: dados.c
* Author: romulo
*
* Created on 2 de Outubro de 2015, 18:03
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Headers/bloco.h"
void gerarBloco(char* nomeArquivo) {
FILE* file = fopen(nomeArquivo, "w");
int i, n = 0;
for (i = 0; i < 512; i++) {
fwrite(&n, sizeof (int), 1, file);
}
short int deslocamento = 2048;
fseek(file, 4, SEEK_SET);
fwrite(&deslocamento, sizeof (short int), 1, file);
fclose(file);
}
//Devolve o numero do bloco para insercao
int obterBloco(Tabela* tabela, int tamanhoRegistro) {
short int qtdRegistros, iniEspacoLivre, fimEspacoLivre;
FILE *file;
file = fopen(tabela->nomesArquivosBlocos[tabela->numeroBlocos - 1], "r+b");
fread(&qtdRegistros, sizeof (short int), 1, file);
fseek(file, 4, SEEK_SET);
fread(&iniEspacoLivre, sizeof (short int), 1, file);
fimEspacoLivre = (qtdRegistros * 2) + 6;
if ((iniEspacoLivre - fimEspacoLivre) >= tamanhoRegistro) {
return tabela->numeroBlocos - 1;
}
printf("Criar novo bloco\n");
char* temp = (char*) calloc(100, sizeof (char));
char* nomeArquivo = (char*) calloc(100, sizeof (char));
//criar bloco para cada tabela
sprintf(temp, "_%d", tabela->numeroBlocos);
strcpy(nomeArquivo, "Arquivos/");
strcat(nomeArquivo, tabela->nome);
strcat(nomeArquivo, temp);
strcat(nomeArquivo, ".dat");
gerarBloco(nomeArquivo);
adicionarBloco(tabela, nomeArquivo);
return tabela->numeroBlocos - 1;
}
int getTamanhoRegistro(Tupla *tupla, int qtdAssociacoes) {
Associacao *associacao;
int i, tamanho;
tamanho = 0;
//percorre todas associacoes
for (i = 0; i < qtdAssociacoes; i++) {
associacao = tupla->associacoes[i];
if (associacao->campo->tipo == VARCHAR) {
//ponteiros, ainda que campo nulo
tamanho += 4;
if (associacao->valor != NULL) {
tamanho += strlen(associacao->valor);
}
} else {
tamanho += associacao->campo->bytes;
}
}
//soma o tamanho do mapa de bits
if (qtdAssociacoes % 8 != 0) {
tamanho++;
}
tamanho += qtdAssociacoes / 8;
return tamanho;
}
//getMapaBits
void setMapaBits(int* mapaBits, int posicao) {
int i, bit = 2;
if ((posicao % 8) == 0) {
mapaBits[(posicao / 8)] += 1;
} else {
for (i = 1; i < (posicao % 8); i++) {
bit *= bit;
}
mapaBits[(posicao / 8)] += bit;
}
}
//preciso de tabela->numeroBlocos, portanto só se eu recebesse como parâmetro
int inserirRegistros(Tabela* tabela) {
FILE* file;
Tupla* tupla;
Associacao* associacao;
Campo* campo;
int* mapaBits;
int tamanhoRegistro;
int numeroDoBloco;
int quantidadeInsercoes;
int i, j;
short int qtdRegistros, comecoRegistro, espacoLivre, qtdVarchar, tamanhoVar, comecoVar;
if (!tabela) {
return -1;
}
quantidadeInsercoes = 0;
//Percorre todas as tuplas
for (i = 0; i < i + tabela->numeroTuplas; i++) {
tupla = tabela->tuplas[i];
tamanhoRegistro = getTamanhoRegistro(tupla, tabela->numeroCampos);
printf("Tamanho registro: %d\n", tamanhoRegistro);
//Verifica se é possivel inserir o registro, devido ao tamanho dos blocos
if (tamanhoRegistro >= 2040) {
return;
}
//Descobre qual bloco deve inserir o registro
numeroDoBloco = obterBloco(tabela, tamanhoRegistro);
printf("numeroBloco: %d\n", numeroDoBloco);
file = fopen(tabela->nomesArquivosBlocos[numeroDoBloco], "r+");
//Atualiza o cabeçalho do arquivo
//Le no arq a qtd de registros no bloco
fread(&qtdRegistros, sizeof (qtdRegistros), 1, file);
//Le a posicao do comeco do espaco livre
fseek(file, 4, SEEK_SET);
fread(&espacoLivre, sizeof (espacoLivre), 1, file);
//Escreve o novo espaco livre
fseek(file, 4, SEEK_SET);
printf("\nEspa livre: %d\n", espacoLivre);
espacoLivre -= tamanhoRegistro;
espacoLivre--;
fwrite(&espacoLivre, sizeof (espacoLivre), 1, file);
//Escreve ponteiro para o comeco do novo registro
printf("\nEspa livre: %d\n", espacoLivre);
comecoRegistro = espacoLivre + 1;
fseek(file, 6 + (qtdRegistros * 2), SEEK_SET);
fwrite(&comecoRegistro, sizeof (comecoRegistro), 1, file);
//Acrescenta um registro armazanado
fseek(file, 0, SEEK_SET);
qtdRegistros++;
fwrite(&qtdRegistros, sizeof (qtdRegistros), 1, file);
/*
* Inserir apontadores varchar
* Calcular deslocamento (tamanho fixo+mapa de bits)
* Inserir valores varchar
* Inserir o restante
* Inserir mapa de bits
*/
//mapa de bits
mapaBits = (int*) calloc((tabela->numeroCampos / 8) + 1, sizeof (int));
qtdVarchar = -1;
comecoVar = 0;
//Insere todos VARCHARs
for (j = 0; j < tabela->numeroCampos; j++) {
associacao = tupla->associacoes[j];
campo = associacao->campo;
if (campo->tipo == VARCHAR) {
qtdVarchar++;
tamanhoVar = 0;
if (associacao->valor) {
tamanhoVar = strlen(associacao->valor);
}
if (comecoVar == 0) {
comecoVar = (tamanhoRegistro - tamanhoVar);
} else {
comecoVar -= tamanhoVar;
}
printf("Comeco do Var: %d\n", comecoVar);
printf("Tamanho do Reg: %d\n", tamanhoRegistro);
printf("tamanho var: %d\n", tamanhoVar);
printf("comecoRegistros: %d\n", comecoRegistro);
//insere ponteiro e tamanho do varchar
fseek(file, comecoRegistro, SEEK_SET);
fwrite(&comecoVar, sizeof (short int), 1, file);
fwrite(&tamanhoVar, sizeof (short int), 1, file);
//inseri o varchar na posicao correta
fseek(file, comecoVar, SEEK_SET);
fwrite(associacao->valor, tamanhoVar, 1, file);
// printf("debug\n");
setMapaBits(mapaBits, j);
comecoRegistro += 4;
}
}
//Insere o restante dos registros
for (j = 0; j < tabela->numeroCampos; j++) {
printf("1comecoRegistros: %d\n", comecoRegistro);
associacao = tupla->associacoes[j];
campo = associacao->campo;
if (associacao->valor && (campo->tipo != VARCHAR)) {
fseek(file, comecoRegistro, SEEK_SET);
if (campo->tipo == INTEGER) {
fwrite(associacao->valor, sizeof (int), 1, file);
comecoRegistro += 4;
} else if (campo->tipo == CHAR) {
fwrite(associacao->valor, sizeof (char), campo->bytes, file);
comecoRegistro += campo->bytes;
} else if (campo->tipo == BOOLEAN) {
fwrite(associacao->valor, sizeof (char), 1, file);
comecoRegistro += campo->bytes;
}
setMapaBits(mapaBits, j);
}
printf("2comecoRegistros: %d\n", comecoRegistro);
}
// inserir mapabits
// int la = 0;
// int ka = tabela->numeroCampos / 8;
// if ((tabela->numeroCampos % 8) != 0) {
// ka++;
// }
// for (; la < ka; la++) {
// fwrite(mapaBits[la], sizeof (int), 1, file);
// }
quantidadeInsercoes++;
free(mapaBits);
freeTupla(tabela, tupla);
fclose(file);
}
return quantidadeInsercoes;
}
void carregarRegistros(Tabela* tabela) {
FILE* file;
Tupla* tupla;
Campo* campo;
Associacao* associacao;
void* valor;
char* nomeArquivoBloco;
int i, j;
short int numeroRegistros, deslocamentoRegistro, deslocamentoVarchar, tamanho;
//liberar as tuplas
freeTuplas(tabela);
//mapaBits=tabela->numeroCampos/8+1
//olhar em todos os blocos
for (i = 0; i < tabela->numeroBlocos; i++) {
nomeArquivoBloco = tabela->nomesArquivosBlocos[i];
file = fopen(nomeArquivoBloco, "rb");
fread(&numeroRegistros, sizeof (numeroRegistros), 1, file);
fseek(file, 6, SEEK_SET);
//ler todos os registros
while (tabela->numeroTuplas < numeroRegistros) {
tupla = criarTupla(tabela);
//pula os primeiros bytes e aponta para o próximo registro
fseek(file, 6 + (tabela->numeroTuplas * 2), SEEK_SET);
fread(&deslocamentoRegistro, sizeof (deslocamentoRegistro), 1, file);
fseek(file, deslocamentoRegistro, SEEK_SET);
//para cada registro
//encontrar todos campos varchar
for (j = 0; j < tabela->numeroCampos; j++) {
associacao = tupla->associacoes[j];
campo = associacao->campo;
if (campo->tipo == VARCHAR) {
fread(&deslocamentoVarchar, sizeof (deslocamentoVarchar), 1, file);
fread(&tamanho, sizeof (tamanho), 1, file);
fseek(file, deslocamentoVarchar, SEEK_CUR);
valor = calloc(tamanho, sizeof (char));
fread(valor, tamanho, 1, file);
associarValor(associacao, valor);
//aponta para próximo campo
deslocamentoRegistro += 4;
fseek(file, deslocamentoRegistro, SEEK_SET);
free(valor);
}
}
//restante dos arquivos
for (j = 0; j < tabela->numeroCampos; j++) {
associacao = tupla->associacoes[j];
campo = associacao->campo;
if (campo->tipo != VARCHAR) {
valor = malloc(campo->bytes);
fread(valor, campo->bytes, 1, file);
associarValor(associacao, valor);
//aponta para próximo campo
deslocamentoRegistro += campo->bytes;
fseek(file, deslocamentoRegistro, SEEK_SET);
free(valor);
}
}
//ler mapa de bits e anular os dados na estrutura
//populá-los nas tuplas
adicionarTupla(tabela, tupla);
}
}
fclose(file);
}
int remover(Tabela* tabela, Campo* campo, char operador, void* valor) {
FILE* file;
Associacao* associacao;
char* nomeArquivoBloco;
int tuplasDeletadas;
int i, j, k;
short int numeroRegistros, deslocamento;
//para todos os blocos
tuplasDeletadas = 0;
for (i = 0; i < tabela->numeroBlocos; i++) {
nomeArquivoBloco = tabela->nomesArquivosBlocos[i];
file = fopen(nomeArquivoBloco, "rb");
fread(&numeroRegistros, sizeof (numeroRegistros), 1, file);
fseek(file, 6, SEEK_SET);
//para cada registro
for (j = 0; j < numeroRegistros; j++) {
fseek(file, 6 + (j * 2), SEEK_SET);
fread(&deslocamento, sizeof (deslocamento), 1, file);
fseek(file, deslocamento, SEEK_SET);
//para cada campo
//todos varchar
for (k = 0; k < tabela->numeroCampos; k++) {
//se varchar:
//le deslocamento
//le tamanho
//pede memória pra void*
//compara
//int comparaValor(void* valor1, char operador, void* valor2, Tipo* tipo);
if (operador == '=') {
if (associacao->valor == valor) {
}
}
//remove item do cabeçalho e incrementa removidos
tuplasDeletadas++;
//libera void*
}
for (k = 0; k < tabela->numeroCampos; k++) {
//se não varchar
//le valor
//compara
//int comparaValor(void* valor1, char operador, void* valor2, Tipo* tipo);
if (operador == '=') {
if (associacao->valor == valor) {
}
}
//remove item do cabeçalho e incrementa removidos
tuplasDeletadas++;
//libera void*
}
}
fclose(file);
}
return tuplasDeletadas;
}