-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferCorrigido.c
374 lines (346 loc) · 10.3 KB
/
bufferCorrigido.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BP 128
#define SIZE 128 //256 //1024
#define MAX_ATT 10
struct buffer {
unsigned char db;
unsigned char pc;
unsigned char nrec;
unsigned char nbyt;
char data[SIZE];
};
union c_float
{
float flt;
char cflt[sizeof(float)];
};
union c_double
{
double dbl;
char cdbl[sizeof(double)];
};
union c_int
{
int num;
char cnum[sizeof(int)];
};
struct t_buf_content {
char type;
unsigned char len;
};
void initbuffer(struct buffer *bp)
{
int i;
printf("Initializing buffer ...\n");
for (i=0;i<BP;i++)
{
//printf("%d",i+1);
bp->db=0;
bp->pc=0;
bp->nrec=0;
bp->nbyt=0;
bp++;
//printf("\b\b\b");
}
//printf("\n");
}
void copytup(struct buffer *bp,char *t, int pos)
{
int i=pos;
for (;i<pos+bp->nbyt;i++)
bp->data[i]=*(t++);
}
//
void fillbufpool(struct buffer *bp,char *t, unsigned char ttup)
{
unsigned int i=0, found=0;
//search for an available page in buffer
while (!found && i < BP)
{
int avail=bp[i].nrec==0?SIZE:SIZE-bp[i].nrec*ttup;
printf(">> Registers: %d Available bytes: %d\n",bp[i].nrec,avail);
if ((bp[i].nbyt==ttup || bp[i].nbyt==0) && ttup < avail) found=1;
i++;
}
if (found)
{
int rpos;
--i;
bp[i].nrec++;
bp[i].nbyt=ttup;
// find the next available position to copy the tuple
rpos=(bp[i].nrec-1)*ttup;
copytup(&bp[i],t,rpos);
printf("*** %d %d pos: %d\n",i,bp[i].nrec,rpos);
return;
}
// here we can implement buffer manager replacement policies
printf("(Fatal error) No availabe room in the buffer!\n");
return;
}
void cpystr(char *tg, char *sc, int st, int len)
{
int i=st,j=0;
for (;i<len+st;i++) {
tg[i]=sc[j++];
}
}
void filltuple(char *tp, const void *v, unsigned char offs, struct t_buf_content bufc)
{
char *str=(char *)v;
printf("Inserting data into the tuple: ");
switch (bufc.type)
{
case 'S':
{
cpystr(tp,str,offs,bufc.len);
printf("%s\n",tp+offs);
break;
}
case 'I':
{
int *i=(int *)v;
union c_int ci;
ci.num=*i;
cpystr(tp,ci.cnum,offs,bufc.len);
printf("%d\n",*i);
break;
}
case 'F':
{
float *f=(float *)v;
union c_float cf;
cf.flt=*f;
cpystr(tp,cf.cflt,offs,bufc.len);
printf("%f\n",*f);
break;
}
case 'D':
{
double *d=(double *)v;
union c_double cd;
cd.dbl=*d;
cpystr(tp,cd.cdbl,offs,bufc.len);
printf("%lf\n",cd.dbl);
break;
}
}
}
void printdebug(struct buffer *bp)
{
int i=0;
printf("\nBuffer Dump:\n");
for (;i<BP;i++)
{
printf("Page: %d\n",i+1);
if (bp[i].nrec > 0)
{
printf("---%d %d\n",bp[i].nrec,bp[i].nbyt);
} else printf("--- Empty\n");
}
}
void showBuffer(struct buffer *bp, unsigned char tl, struct t_buf_content *bc, int p)
{
int i=0, j;
if (p<0 || p>=BP)
{
printf("Invalid page number!\n");
return;
}
char found0=0;
j=0;
int sgmt=0;
for (;i<bp[p].nrec;i++) //For each record in a page
{
for (j=0;j<MAX_ATT && bc[j].type!='@';j++) //for each att in a record
{
printf("\n%d %c %d seg: %d ",j,bc[j].type,bc[j].len,sgmt);
switch (bc[j].type)
{
case 'S':
found0=0;
for (int k=0;k<bc[j].len;k++)
{
if (bp[p].data[sgmt]==0) found0=1;
if (!found0)
printf("%c",bp[p].data[sgmt]);
else
printf(" ");
sgmt++;
}
break;
case 'I':
{
int *v=(int *)&bp[p].data[sgmt];
printf("%d",*v);
sgmt+=bc[j].len;
break;
}
case 'F':
{
float *v=(float *)&bp[p].data[sgmt];
printf("%f ",*v);
sgmt+=bc[j].len;
break;
}
case 'D':
{
double *v=(double *)&bp[p].data[sgmt];
printf("%lf ",*v);
sgmt+=bc[j].len;
break;
}
}
}
printf("\n");
}
return;
}
int main()
{
char *tuple=(char *)malloc(sizeof(char)*38); //tuple's length
struct buffer *bufpool;
struct t_buf_content buf_cont[MAX_ATT];
unsigned char tup_len;
float sal;
int i;
printf("Starting buffer manager (%d pages)...\n",BP);
bufpool=(struct buffer *)malloc(sizeof(struct buffer)*BP);
if (bufpool==NULL)
{
printf("Out of memory for buffer pool!\n");
return 0;
}
initbuffer(bufpool);
// tuples in the buffer are on the form: string 30 and float 4
buf_cont[0].type='S';
buf_cont[0].len=30;
//buf_cont[1].type='S';
//buf_cont[1].len=10;
buf_cont[1].type='F';
buf_cont[1].len=sizeof(float);
buf_cont[2].type='I';
buf_cont[2].len=sizeof(int);
buf_cont[3].type='D';
buf_cont[3].len=sizeof(double);
buf_cont[4].type='@'; // fim de atributos
// tuple's size
tup_len=buf_cont[0].len+buf_cont[1].len+buf_cont[2].len+buf_cont[3].len;
printf("Tuple length: %d bytes\n",tup_len);
// tuple value type start struct
// | | | | |
filltuple(tuple,(void *)"João da Silva",0,buf_cont[0]);
sal=10.23;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
int inteiro = 10;
double doble = 10.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
///////
filltuple(tuple,(void *)"Carlos Cavalcanti",0,buf_cont[0]);
sal=15.67;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 11;
doble = 11.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)"23.45",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
/////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"Janete Constantina",0,buf_cont[0]);
sal=55.3;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 12;
doble = 12.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)&"23.34",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
//////////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"Silvia da Silva",0,buf_cont[0]);
sal=33.33;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 13;
doble = 13.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)"100.33",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
//////////////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"João New Page",0,buf_cont[0]);
sal=36.4;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 14;
doble = 14.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)"456.78",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
////////////////////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"Maria New Page",0,buf_cont[0]);
sal=51.1;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 15;
doble = 15.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)"111.11",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
////////////////////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"Carl Seagan",0,buf_cont[0]);
sal=1332.2;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 16;
doble = 16.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)&"222.22",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
/////////////////////////////
// fill tuple with a tuple
filltuple(tuple,(void *)"Edgar Codd",0,buf_cont[0]);
sal=1000.9;
filltuple(tuple, (void *)&sal,buf_cont[0].len,buf_cont[1]);
inteiro = 17;
doble = 17.22;
filltuple(tuple, (void *)&inteiro,(buf_cont[0].len+buf_cont[1].len),buf_cont[2]);
filltuple(tuple, (void *)&doble,(buf_cont[0].len + buf_cont[1].len + buf_cont[2].len),buf_cont[3]);
//filltuple(tuple, (void *)"333.33",buf_cont[0].len,buf_cont[1]);
// copy tuple to buffer pool
printf("Insert tuple into buffer pool...\n");
fillbufpool(bufpool,tuple,tup_len);
////////////////////////////////
//printdebug(bufpool);
////
i=0;
while (bufpool[i].nrec>0)
{
printf("Showing page: %d\n",i);
showBuffer(bufpool,tup_len,buf_cont,i);
i++;
}
free(bufpool);
return 1;
}