-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBFile.java
310 lines (273 loc) · 10.6 KB
/
DBFile.java
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
import java.io.*;
/**
* Low level database file. This abstraction allows the user to treat
* a database as a collection of pages.
* @author Dave Musicant, with considerable material reused from the
* UW-Madison Minibase project
*/
public class DBFile
{
public static class NonPositiveRunSizeException
extends RuntimeException {};
public static class FileFullException extends RuntimeException {};
public static class BadPageNumberException
extends RuntimeException {};
public static class EmptyFileException extends RuntimeException {};
public static class PageNotAllocatedException extends RuntimeException {};
private String dataFileName;
private String mapFileName;
private int numPages;
/**
* Creates a database with the specified number of pages. The
* number of pages in the database can never be increased.
* @param name name to be given to database.
* @param numPages maximum number of pages in database.
* @throws IOException passed through from underlying filesystem.
*/
public DBFile(String name, int numPages) throws IOException
{
// If numPages is too small, just create it with at least two pages
if (numPages < 2)
numPages = 2;
// Create the file
dataFileName = name;
RandomAccessFile dataFile = new RandomAccessFile(dataFileName,"rw");
// Make the file num_pages pages long.
dataFile.setLength(numPages * Page.PAGESIZE);
byte[] zzeros = new byte[numPages * Page.PAGESIZE];
dataFile.write(zzeros);
dataFile.close();
// Create a separate space map for each file.
mapFileName = name + ".map";
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"rw");
// Allocate one byte for each page in the data file.
mapFile.setLength(numPages);
// Fill in map pages with zeros.
byte[] zeros = new byte[numPages];
mapFile.write(zeros);
mapFile.close();
this.numPages = numPages;
}
/**
* Opens the database with the given name.
* @param name name of the database.
* @throws IOException passed through from underlying file system.
*/
public DBFile(String name) throws IOException
{
// Open the file
dataFileName = name;
if ((new File(dataFileName)).exists())
{
mapFileName = name + ".map";
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"rw");
numPages = (int)(mapFile.length());
mapFile.close();
}
else
numPages = 0;
}
/**
* Erases the database entirely from the filesystem. Dangerous to
* do if still have a DBFile object that refers to this file.
* @param name name of the database.
* @return true if operation succeeded.
*/
public static boolean erase(String name)
{
boolean success;
success = (new File(name)).delete();
if (success)
success = (new File(name + ".map")).delete();
return success;
}
/**
* Allocates a set of pages.
* @param runSize number of pages to be allocated in the run.
* @return page number of the first page of the allocated run.
* @throws NonPositiveRunSizeException if the run size is less
* than or equal to zero.
* @throws FileFullException if there are not enough free pages.
* @throws IOException passed through from underlying file system.
*/
public int allocatePages(int runSize) throws IOException
{
if (runSize <= 0)
throw new NonPositiveRunSizeException();
// Load the space map into memory to look for runs of the
// necessary size. Technically, should do this a page at a
// time (since might not have enough memory). Going with a
// simpler approach here for expediency.
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"rw");
mapFile.seek(0);
byte[] map = new byte[numPages];
mapFile.readFully(map);
// Loop over run starting positions
for (int i=0; i < mapFile.length() - (runSize-1); i++)
{
// Loop over entire possible run: give up if any spot in
// the possible run already has a 1 (page is taken).
int currentRunSize = 0;
for (int j=i; j < i + runSize; j++)
{
if (map[j] == 1)
break;
else
currentRunSize++;
}
// Found a run.
if (currentRunSize == runSize)
{
// Indicate pages are now used
byte[] mapUpdate = new byte[runSize];
for (int j=0; j < runSize; j++)
mapUpdate[j] = 1;
mapFile.seek(i);
mapFile.write(mapUpdate);
mapFile.close();
return i;
}
}
// If made it to here, then no run was found.
mapFile.close();
throw new FileFullException();
}
/**
* Deallocates a set of pages. Does not ensure that the pages
* being deallocated are in fact allocated to begin with. If the
* pages were already deallocated, they remain so.
* @param startPageNum page number at the beginning of the run to
* be deallocated.
* @param runSize number of pages to deallocate.
* @throws NonPositiveRunSizeException if the run size is less
* than or equal to zero.
* @throws BadPageNumberException if startPageNum is illegal.
* @throws IOException passed through from underlying file system.
*/
public void deallocatePages(int startPageNum, int runSize)
throws IOException
{
if (runSize <= 0)
throw new NonPositiveRunSizeException();
if (startPageNum < 0 || startPageNum > numPages-1 ||
startPageNum + runSize - 1 > numPages-1)
throw new BadPageNumberException();
byte[] mapUpdate = new byte[runSize];
for (int i=0; i < runSize; i++)
mapUpdate[i] = 0;
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"rw");
mapFile.seek(startPageNum);
mapFile.write(mapUpdate);
mapFile.close();
}
/**
* Reads the contents of the specified page from disk into the
* page object provided.
* @param pageNum the page number to be read.
* @param page a reference to an already allocated Page object.
* @throws BadPageNumberException if pageNum is not in the file.
* @throws IOException passed through from underlying file system.
* @throws PageNotAllocatedException if pageNum is not allocaated.
*/
public void readPage(int pageNum, Page page) throws IOException
{
if (pageNum < 0 || pageNum > numPages-1)
throw new BadPageNumberException();
// Make sure that page has actually been allocated
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"r");
mapFile.seek(pageNum);
byte[] map = new byte[1];
mapFile.readFully(map);
if (map[0] == 0)
throw new PageNotAllocatedException();
mapFile.close();
// Read the actual page from the file
RandomAccessFile dataFile = new RandomAccessFile(dataFileName,"r");
dataFile.seek(pageNum * Page.PAGESIZE);
dataFile.readFully(page.data);
dataFile.close();
}
/**
* Writes the contents of the specified page to disk.
* @param pageNum the page number to be written.
* @param page a Page object with data to be written.
* @throws EmptyFileException() if the file has no pages within it.
* @throws BadPageNumberException if pageNum is not in the file.
* @throws IOException passed through from underlying file system.
* @throws PageNotAllocatedException if pageNum is not allocaated.
*/
public void writePage(int pageNum, Page page) throws IOException
{
if (numPages == 0)
throw new EmptyFileException();
if (pageNum < 0 || pageNum > numPages-1)
throw new BadPageNumberException();
// Make sure that page has actually been allocated
RandomAccessFile mapFile = new RandomAccessFile(mapFileName,"r");
mapFile.seek(pageNum);
byte[] map = new byte[1];
mapFile.readFully(map);
if (map[0] == 0)
throw new PageNotAllocatedException();
mapFile.close();
RandomAccessFile dataFile = new RandomAccessFile(dataFileName,"rw");
dataFile.seek(pageNum * Page.PAGESIZE);
dataFile.write(page.data);
dataFile.close();
}
// Stub for testing.
public static void main(String[] args) throws IOException
{
DBFile file = new DBFile("testing",5);
file = new DBFile("testing");
System.out.println(file.allocatePages(4));
file.deallocatePages(1,2);
System.out.println(file.allocatePages(2));
System.out.println(file.allocatePages(1));
Page page = new Page();
page.data[1] = 100;
page.data[2] = 18;
file.writePage(2,page);
file.readPage(0,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
file.readPage(2,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
DBFile file2 = new DBFile("testagain",7);
System.out.println(file2.allocatePages(3));
file2.writePage(2,page);
file2.readPage(1,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
file2.readPage(2,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
file.readPage(0,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
file.readPage(2,page);
for (int i=0; i < 100; i++)
System.out.print(page.data[i] + " ");
System.out.println();
try {
file2.readPage(3,page);
}
catch (PageNotAllocatedException e) {
System.out.println("Correctly caught unallocated page read");
}
try {
file2.writePage(3,page);
}
catch (PageNotAllocatedException e) {
System.out.println("Correctly caught unallocated page write");
}
System.out.println(DBFile.erase("testing"));
System.out.println(DBFile.erase("testagain"));
}
}