-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenRelTable.cpp
129 lines (115 loc) · 3.19 KB
/
OpenRelTable.cpp
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
#include <string>
#include <cstring>
#include "define/constants.h"
#include "define/errors.h"
#include "disk_structures.h"
#include "OpenRelTable.h"
OpenRelTableMetaInfo OpenRelTable::tableMetaInfo[MAX_OPEN];;
void OpenRelTable::initializeOpenRelationTable() {
for (int i = 0; i < MAX_OPEN; i++) {
if (i == RELCAT_RELID) {
tableMetaInfo[i].free = OCCUPIED;
strcpy(tableMetaInfo[i].relName, "RELATIONCAT");
}
else if (i == ATTRCAT_RELID) {
tableMetaInfo[i].free = OCCUPIED;
strcpy(tableMetaInfo[i].relName, "ATTRIBUTECAT");
}
else {
tableMetaInfo[i].free = FREE;
strcpy(tableMetaInfo[i].relName, "NULL");
}
}
}
int OpenRelTable::getRelationId(char relationName[ATTR_SIZE]) {
for (int i = 0; i < MAX_OPEN; i++)
if (tableMetaInfo[i].free == OCCUPIED && strcmp(tableMetaInfo[i].relName, relationName) == 0)
return i;
return E_RELNOTOPEN;
}
int OpenRelTable::getRelationName(int relationId, char relationName[ATTR_SIZE]) {
if (relationId < 0 || relationId >= MAX_OPEN) {
return E_OUTOFBOUND;
}
strcpy(relationName, tableMetaInfo[relationId].relName);
return SUCCESS;
}
int OpenRelTable::openRelation(char relationName[ATTR_SIZE]) {
Attribute relationCatalog[6];
/* check if relation exists
* for this check each entry in relation catalog
*/
int i;
for (i = 0; i < SLOTMAP_SIZE_RELCAT_ATTRCAT; i++) {
int retval = getRecord(relationCatalog, 4, i);
if (retval == SUCCESS &&
strcmp(relationCatalog[0].sval, relationName) == 0) {
break;
}
}
// if relation does not exist
if (i == SLOTMAP_SIZE_RELCAT_ATTRCAT) {
return E_RELNOTEXIST;
}
/* check if relation is already open
* if yes, return open relation id
* otherwise search for a free slot in open relation table
*/
for (i = 0; i < MAX_OPEN; i++) {
if (tableMetaInfo[i].free == OCCUPIED && strcmp(relationName, tableMetaInfo[i].relName) == 0) {
return i;
}
}
for (i = 0; i < MAX_OPEN; i++) {
if (tableMetaInfo[i].free == FREE) {
tableMetaInfo[i].free = OCCUPIED;
strcpy(tableMetaInfo[i].relName, relationName);
return i;
}
}
// if open relation table is already full
if (i == MAX_OPEN) {
return E_CACHEFULL;
}
}
int OpenRelTable::closeRelation(int relationId) {
if (relationId < 0 || relationId >= MAX_OPEN) {
return E_OUTOFBOUND;
}
if (relationId == RELCAT_RELID || relationId == ATTRCAT_RELID) {
return E_INVALID;
}
if (tableMetaInfo[relationId].free == FREE) {
return E_RELNOTOPEN;
}
tableMetaInfo[relationId].free = FREE;
strcpy(tableMetaInfo[relationId].relName, "NULL");
return SUCCESS;
}
int OpenRelTable::checkIfRelationOpen(char relationName[ATTR_SIZE]) {
for (auto relationIterator: tableMetaInfo) {
if (relationIterator.free == OCCUPIED && strcmp(relationIterator.relName, relationName) == 0) {
return SUCCESS;
}
}
return FAILURE;
}
int OpenRelTable::checkIfRelationOpen(int relationId) {
if (relationId < 0 || relationId >= MAX_OPEN) {
return E_OUTOFBOUND;
}
if (tableMetaInfo[relationId].free == FREE) {
return FAILURE;
}
else {
return SUCCESS;
}
}
int OpenRelTable::checkIfOpenRelTableHasFreeEntry() {
for (auto relationIterator: tableMetaInfo) {
if (relationIterator.free == FREE) {
return SUCCESS;
}
}
return FAILURE;
}