-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_mgr.c
559 lines (469 loc) · 13.4 KB
/
record_mgr.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "record_mgr.h"
#include "buffer_mgr.h"
#include "storage_mgr.h"
char unit=sizeof(char);
int INT=sizeof(int);
BM_PageHandle *ph;
BM_BufferPool *bm;
RID *rid;
char *Temp;
//DataStructure to keep track of table
typedef struct tableData
{
int numOfTuples;
int freeSlot;
char *name;
BM_PageHandle handlePages;
BM_BufferPool poolHandle;
}tableData;
tableData *ts;
//DataStructure to Scan tuples in table
typedef struct scanTuple
{
Expr *Condition;
RID key;
BM_PageHandle handlePages;
} scanTuple;
SM_FileHandle handleFile;
SM_PageHandle handlePage;
#define INCREMENT_PH() handlePage += 4;
/************** TABLE AND MANAGER ************/
/* function Name: initRecordManager
* functionality: Initializes and record manager.
*/
RC initRecordManager (void *mgmtData)
{
return RC_OK; //we don't have to initialize the record manager
}
/* function Name: shutdownRecordManager
* functionality: Shut down the present record manager
*/
RC shutdownRecordManager ()
{
return RC_OK;//we don't have to shout down the record manager
}
/* function Name: CReateTable
* functionality: creates new page file for given table and stores the schema in first page.
*/
RC createTable (char *name, Schema *schema)
{
char pageSize[PAGE_SIZE];
int i=0;
ts=(tableData*)malloc(sizeof(tableData));
bm = &ts->poolHandle;
initBufferPool(bm, name, 50, RS_FIFO, NULL);
char *handlePages = pageSize;
for(i=0; i<PAGE_SIZE; i++) handlePages[i]=0;
for(i=0;i<=unit;i++)
{
*(int*)handlePages = i;
handlePages += 4;
}
*(int*)handlePages = 3;
handlePages += 4;
*(int*)handlePages = (int)schema->dataTypes[i];
handlePages += unit;
*(int*)handlePages = (int) schema->typeLength[i];
handlePages += unit;
if(name != NULL)
{
createPageFile(name);
openPageFile(name, &handleFile);
writeBlock(0, &handleFile, pageSize);
}
else
{
return RC_FILE_NOT_FOUND;
}
return RC_OK;
}
/* function Name: OpenTable
* functionality: Access the table by accessing the page file using buffer manager .
*/
RC openTable (RM_TableData *rel, char *name)
{
int i=0;
rel->mgmtData = ts;
ph = &ts->handlePages;
bm = &ts->poolHandle;
pinPage(bm, ph, -1);
handlePage = (char*) ts->handlePages.data;
ts->numOfTuples= *(int*)handlePage;
INCREMENT_PH();
ts->freeSlot= *(int*)handlePage;
INCREMENT_PH();
i = *(int*)handlePage;
INCREMENT_PH();
Schema *sh;
sh= (Schema*) malloc(sizeof(Schema));
sh->numAttr= i;
sh->attrNames= (char**) malloc(sizeof(char*)*3);
sh->dataTypes= (DataType*) malloc(INT*3);
sh->typeLength= (int*) malloc(INT*3);
for(i=0; i<= unit; i++)
sh->attrNames[i]=(char*)malloc(unit+INT);
for(i=i; i<= (unit+INT); i++)
sh->dataTypes[i]= *(int*)handlePage;
rel->schema = sh;
unpinPage(bm, ph);
return RC_OK;
}
/* function Name: closeTable
* functionality: closes the table buy shut downing buffer pool .
*/
RC closeTable (RM_TableData *rel)
{
BM_BufferPool *poolHandle=(BM_BufferPool *)rel->mgmtData;
ts = rel->mgmtData;
bm = &ts->poolHandle;
shutdownBufferPool(bm);
return RC_OK;
}
/* function Name: deleteTable
* functionality: delets the table by destroying the associated pages of file.
*/
RC deleteTable (char *name)
{
destroyPageFile(name);
return RC_OK;
}
/* function Name: getNumTuples
* functionality: returns the number of tuples of a table.
*/
int getNumTuples (RM_TableData *rel)
{
ts = (tableData*)rel->mgmtData;
return ts->numOfTuples;
}
/*************** HANDLING RECORDS IN A TABLE ********************/
/* function Name: getFreeSlot
* functionality: returns the available free slot.
*/
int getFreeSlot(char *v, int size)
{
int i, totalSlots;
totalSlots = ceil(PAGE_SIZE/size);
for(i=0; i<totalSlots; i++)
{
if (v[i*size] != '$')
return i;
}
return -1;
}
/* function Name: insertRecord
* functionality: will insert new record in available page and slot and will assign that to record parameter.
*/
RC insertRecord (RM_TableData *rel, Record *record)
{
ts = rel->mgmtData;
rid = &record->id;
rid->page = ts->freeSlot;
bm = &ts->poolHandle;
ph = &ts->handlePages;
int recordSize = getRecordSize(rel->schema);
int i = rid->page, j;
pinPage(bm, ph, i);
char *data = ts->handlePages.data;
rid->slot = getFreeSlot(data, recordSize);
j = rid->slot;
while(j<0)
{
i++;
pinPage(bm, ph, i);
data = ts->handlePages.data;
j = getFreeSlot(data, recordSize);
}
markDirty(bm,ph);
data += (j * recordSize);
*data = '$';
data++;
rid->slot = j;
rid->page = i;
memmove(data, record->data+1, recordSize);
unpinPage(bm, ph);
pinPage(bm, ph, 1);
return RC_OK;
}
/* function Name: deleteRecord
* functionality: will delete the record based on the RID parameter that is passed.
*/
RC deleteRecord (RM_TableData *rel, RID id)
{
bm = &ts->poolHandle;
ph = &ts->handlePages;
int tuples, recordSize;
openPageFile(ts->name, &handleFile);
int rc = pinPage(bm, ph,id.page);
if(rc == RC_OK){
Temp = ts->handlePages.data;
}else{
return RC_WRITE_FAILED;
}
tuples = ts->numOfTuples;
recordSize = getRecordSize(rel->schema);
tuples += (id.slot * recordSize);
markDirty(bm, ph);
unpinPage(bm, ph);
return RC_OK;
}
/* function Name: updateRecord
* functionality: will update an existing record with a new value on the slot and page that has been passed.
*/
RC updateRecord (RM_TableData *rel, Record *record)
{
bm = &ts->poolHandle;
ph = &ts->handlePages;
rid = &record->id;
int page, recordSize;
page = record->id.page;
pinPage(bm, ph, page);
recordSize = getRecordSize(rel->schema);
Temp = (ts->handlePages.data) + (rid->slot * recordSize);
Temp += 1;
memmove(Temp,record->data + 1, recordSize);
markDirty(bm, ph);
unpinPage(bm, ph);
return RC_OK;
}
/* function Name: getRecord
* functionality: will get an existing record value based on RID parameter(id) and assing the value to record plus assign that RID to record->id.
*/
RC getRecord (RM_TableData *rel, RID id, Record *record)
{
bm = &ts->poolHandle;
ph = &ts->handlePages;
int page, recordSize;
page = id.page;
char *ptr;
pinPage(bm, ph, page);
recordSize = getRecordSize(rel->schema);
ptr = (ts->handlePages.data) + ((id.slot)*recordSize);
ptr++;
Temp = record->data;
Temp += 1;
memmove(Temp,ptr,recordSize - 1);
record->id = id;
unpinPage(bm, ph);
return RC_OK;
}
/* function Name: startScan
* functionality: will scan 30 records at a time.
*/
RC startScan (RM_TableData *rel, RM_ScanHandle *scan, Expr *cond)
{
openTable(rel, Temp);
scanTuple *scanPtr; \
scanPtr = (scanTuple*) malloc(sizeof(scanTuple));
scanPtr->key.page= 1;
scanPtr->key.slot= 0;
scanPtr->Condition = cond;
scan->mgmtData = scanPtr;
ts = rel->mgmtData;
ts->numOfTuples = 30;
scan->rel= rel;
return RC_OK;
}
/* function Name: next
* functionality: will scan the next records that match the condition.
*/
RC next (RM_ScanHandle *scan, Record *record)
{
scanTuple *scanPtr;
scanPtr = (scanTuple*)scan->mgmtData;
Schema *sptr = scan->rel->schema;
ts = (tableData*)scan->rel->mgmtData;
Value *valPtr;
valPtr = (Value *) malloc(sizeof(Value));
int recordSize;
char *ptr;
bm = &ts->poolHandle;
ph = &scanPtr->handlePages;
recordSize = getRecordSize(sptr);
int i;
for(i=0; i <= ts->numOfTuples; i++)
{
scanPtr->key.slot += 1;
pinPage(bm, ph, scanPtr->key.page);
ptr = scanPtr->handlePages.data;
ptr += (scanPtr->key.slot * recordSize);
record->id.page = scanPtr->key.page;
record->id.slot = scanPtr->key.slot;
Temp = record->data;
Temp += 1;
memmove(Temp,ptr+1,recordSize-1);
evalExpr(record, sptr, scanPtr->Condition, &valPtr);
if((valPtr->v.floatV == TRUE)||(valPtr->v.boolV == TRUE))
{
unpinPage(bm, ph);
return RC_OK;
}
}
unpinPage(bm, ph);
return RC_RM_NO_MORE_TUPLES;
}
/* function Name: closeScan
* functionality: will close a current scan.
*/
RC closeScan (RM_ScanHandle *scan)
{
scanTuple *scanPtr;
scanPtr = (scanTuple*)scan->mgmtData;
bm = &ts->poolHandle;
ph = &scanPtr->handlePages;
unpinPage(bm, ph);
return RC_OK;
}
/* function Name: getRecordSize
* functionality: will return the size of a record.
*/
int getRecordSize (Schema *schema)
{
int i, Offset=0;
for(i=0; i<schema->numAttr; i++)
{
if (schema->dataTypes[i] == DT_INT)
Offset += 4;
else if(schema->dataTypes[i] == DT_STRING)
Offset += schema->typeLength[i];
}
Offset++;
return Offset;
}
/* function Name: createSchema
* functionality: will create and initialise the new schema and returns the pointer to it.
*/
Schema *createSchema (int numAttr, char **attrNames, DataType *dataTypes, int *typeLength, int keySize, int *keys)
{
Schema *sh=(Schema*)malloc(sizeof(Schema));
sh->numAttr = numAttr;
sh->attrNames = attrNames;
sh->dataTypes =dataTypes;
sh->typeLength = typeLength;
sh->keySize = keySize;
sh->keyAttrs = keys;
return sh;
}
/* function Name: freeSchema
* functionality: deallocate the memory allocated to a schema.
*/
RC freeSchema (Schema *schema)
{
free(schema);
return RC_OK;
}
/* function Name: createRecord
* functionality: creates a new record
*/
RC createRecord(Record **record, Schema *schema)
{
int i, recordSize;
for(i=0; i < INT; i++)
{
switch(*(schema->dataTypes + i))
{
case DT_INT:
case DT_FLOAT: recordSize += INT;
break;
case DT_BOOL: recordSize += 1;
break;
case DT_STRING: recordSize += (*(schema->typeLength + i));
break;
}
}
for(i=0; i < INT + unit; i++)
{
Temp = (char *)malloc(INT + unit);
Temp[i]='\0';
*record = (Record *)malloc(INT);
record[0]->data=Temp;
}
return RC_OK;
}
/* function Name: freeRecord
* functionality: deallocate the memory allocated to record
*/
RC freeRecord (Record *record)
{
free(record);
return RC_OK;
}
RC getAttr (Record *record, Schema *schema, int attrNum, Value **value)
{
int Offset = 1, i;
DataType *type = schema->dataTypes;
Value *valPtr = (Value *) malloc(sizeof(Value));
Temp = record->data;
for(i = 0; i < attrNum; i++)
switch (type[i])
{
case DT_STRING:
Offset += schema->typeLength[i];
break;
case DT_INT:
Offset += INT;
break;
case DT_FLOAT:
Offset += sizeof(float);
break;
case DT_BOOL:
Offset += sizeof(bool);
break;
}
Temp += Offset;
if(attrNum == 1) type[attrNum] = 1;
switch (type[attrNum]){
case DT_INT:
memcpy(&valPtr->v.intV,Temp, 4);
break;
case DT_FLOAT:
memcpy(&valPtr->v.floatV,Temp,4);
break;
case DT_BOOL:
memcpy(&valPtr->v.boolV,Temp,1);
break;
case DT_STRING:
i = schema->typeLength[attrNum];
valPtr->v.stringV = (char *) malloc(unit);
strncpy(valPtr->v.stringV, Temp, i);
break;
default :
return RC_RM_UNKOWN_DATATYPE;
}
valPtr->dt = type[attrNum];
*value = valPtr;
return RC_OK;
}
RC setAttr (Record *record, Schema *schema, int attrNum, Value *value)
{
int Offset = 1, i;
DataType *type = schema->dataTypes;
for(i = 0; i < attrNum; i++)
switch (type[i])
{
case DT_STRING:
Offset += schema->typeLength[i];
break;
case DT_INT:
Offset += INT;
break;
case DT_FLOAT:
Offset += sizeof(float);
break;
case DT_BOOL:
Offset += sizeof(bool);
break;
}
Temp = record->data;
Temp += Offset;
if(type[attrNum] == DT_INT)
*(int *)Temp = value->v.intV;
else if(type[attrNum] == DT_STRING)
strncpy(Temp, value->v.stringV, unit+INT);
else
return RC_RM_UNKOWN_DATATYPE;
return RC_OK;
}