-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathix_manager.cc
243 lines (189 loc) · 6.1 KB
/
ix_manager.cc
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
#include "ix_internal.h"
#include "ix_btree.h"
#include "ix_hash.h"
#include <sstream>
#include <iostream>
using namespace std;
IX_Manager::IX_Manager(PF_Manager &pfm) : pPfm(&pfm)
{
}
IX_Manager::~IX_Manager()
{
}
// Create a new Index
RC IX_Manager::CreateIndex(const char *fileName, int indexNo,
AttrType attrType, int attrLength)
{
RC rc;
PF_FileHandle pfFileHandle;
PF_PageHandle pageHandle;
char* pData;
IX_FileHdr *fileHdr;
const char *realFileName = GenerateFileName(fileName, indexNo);
// Verify that the index should be a positive number
if(indexNo < 0)
return IX_NON_POSITIVE_INDEX_NUMBER;
// Sanity Check: recordSize should not be too large (or small)
// Note that PF_Manager::CreateFile() will take care of fileName
if (sizeof(IX_FileHdr) >= PF_PAGE_SIZE)
// Test: invalid creation
return (IX_INSUFFISANT_PAGE_SIZE);
// Call PF_Manager::CreateFile()
if (rc = pPfm->CreateFile(realFileName))
// Test: existing realFileName, wrong permission
goto err_return;
// Call PF_Manager::OpenFile()
if (rc = pPfm->OpenFile(realFileName, pfFileHandle))
// Should not happen
goto err_destroy;
// Allocate the header page (pageNum must be 0)
if (rc = pfFileHandle.AllocatePage(pageHandle))
// Should not happen
goto err_close;
// Get a pointer where header information will be written
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Write the file header (to the buffer pool)
fileHdr = (IX_FileHdr *) pData;
fileHdr->rootNum = IX_EMPTY;
fileHdr->attrLength=attrLength;
fileHdr->attrType=attrType;
fileHdr->height = 0;
fileHdr->firstLeafNum = IX_EMPTY;
#ifdef IX_USE_HASH
fileHdr->directoryNum = IX_EMPTY;
#endif
// Mark the header page as dirty
if (rc = pfFileHandle.MarkDirty(IX_HEADER_PAGE_NUM))
// Should not happen
goto err_unpin;
// Unpin the header page
if (rc = pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM))
// Should not happen
goto err_close;
// Call PF_Manager::CloseFile()
if (rc = pPfm->CloseFile(pfFileHandle))
// Should not happen
goto err_destroy;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM);
err_close:
pPfm->CloseFile(pfFileHandle);
err_destroy:
pPfm->DestroyFile(realFileName);
err_return:
// Return error
return (rc);
}
// Destroy and Index
RC IX_Manager::DestroyIndex(const char *fileName, int indexNo)
{
RC rc;
const char *realFileName = GenerateFileName(fileName, indexNo);
// Call PF_Manager::DestroyFile()
if (rc = pPfm->DestroyFile(realFileName))
// Test: non-existing fileName, wrong permission
goto err_return;
// Return ok
return (0);
err_return:
// Return error
return (rc);
}
// Open an Index
RC IX_Manager::OpenIndex(const char *fileName, int indexNo,
IX_IndexHandle &indexHandle)
{
RC rc;
PF_PageHandle pageHandle;
char* pData;
const char *realFileName = GenerateFileName(fileName, indexNo);
// Call PF_Manager::OpenFile()
if (rc = pPfm->OpenFile(realFileName, indexHandle.pfFileHandle))
// Test: non-existing realFileName, opened fileHandle
goto err_return;
// update the file handle in the index structures
indexHandle.pBTree->setFileHandle(indexHandle.pfFileHandle);
#ifdef IX_USE_HASH
indexHandle.pHash->setFileHandle(indexHandle.pfFileHandle);
#endif
// Get the header page
if (rc = indexHandle.pfFileHandle.GetFirstPage(pageHandle))
// Test: invalid file
goto err_close;
// Get a pointer where header information resides
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Read the file header (from the buffer pool to RM_FileHandle)
memcpy(&indexHandle.fileHdr, pData, sizeof(IX_FileHdr));
// update the file header in the index structures
indexHandle.pBTree->setFileHdr(indexHandle.fileHdr);
#ifdef IX_USE_HASH
indexHandle.pHash->setFileHdr(indexHandle.fileHdr);
#endif
// Unpin the header page
if(rc = indexHandle.pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM))
// Should not happen
goto err_close;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
indexHandle.pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM);
err_close:
pPfm->CloseFile(indexHandle.pfFileHandle);
err_return:
// Return error
return (rc);
}
// Close an Index
RC IX_Manager::CloseIndex(IX_IndexHandle &indexHandle)
{
RC rc;
PF_PageHandle pageHandle;
char* pData;
// Get the header page
if (rc = indexHandle.pfFileHandle.GetFirstPage(pageHandle))
// Test: unopened(closed) indexHandle, invalid file
goto err_return;
// Get a pointer where header information will be written
if (rc = pageHandle.GetData(pData))
// Should not happen
goto err_unpin;
// Write the file header (to the buffer pool)
memcpy(pData, &indexHandle.fileHdr, sizeof(IX_FileHdr));
// Mark the header page as dirty
if (rc = indexHandle.pfFileHandle.MarkDirty(IX_HEADER_PAGE_NUM))
// Should not happen
goto err_unpin;
// Unpin the header page
if (rc = indexHandle.pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM))
// Should not happen
goto err_return;
indexHandle.ForcePages();
// Call PF_Manager::CloseFile()
if (rc = pPfm->CloseFile(indexHandle.pfFileHandle))
goto err_return;
// Return ok
return (0);
// Recover from inconsistent state due to unexpected error
err_unpin:
indexHandle.pfFileHandle.UnpinPage(IX_HEADER_PAGE_NUM);
err_return:
// Return error
return (rc);
}
const char* IX_Manager::GenerateFileName(const char *fileName, int indexNo)
{
std::string outputFileName(fileName);
outputFileName.append(".");
std::ostringstream oss;
oss << indexNo;
outputFileName.append(oss.str());
return outputFileName.c_str();
}