-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlottedPageFile.java
154 lines (137 loc) · 3.78 KB
/
SlottedPageFile.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
package storage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* A {@code SlottedPageFile} represents a file consisting of {@code SlottedPage}s.
*
* @author Jeong-Hyon Hwang ([email protected])
*/
public class SlottedPageFile {
/**
* The name of this {@code SlottedPageFile}.
*/
String name;
/**
* A {@code RandomAccessFile}
*/
RandomAccessFile file;
/**
* The size (in bytes) of {@code SlottedPage}s.
*/
int slottedPageSize;
/**
* The number of seeks performed so far.
*/
int seeks = 0;
/**
* The number of {@code SlottedPage}s read.
*/
int reads = 0;
/**
* The number of {@code SlottedPage}s written.
*/
int writes = 0;
/**
* Constructs a {@code SlottedPageFile}.
*
* @param name
* the system-dependent filename
* @param slottedPageSize
* the size (in bytes) of {@code SlottedPage}s
* @throws FileNotFoundException
* if the specified file cannot be found/created
*/
public SlottedPageFile(String name, int slottedPageSize) throws FileNotFoundException {
this.name = name;
this.slottedPageSize = slottedPageSize;
file = new java.io.RandomAccessFile(name, "rw");
}
@Override
public String toString() {
return "{name:" + name + ", reads:" + reads + ", writes:" + writes + "}";
}
/**
* Returns the number of {@code SlottedPage}s in this {@code SlottedPageFile}.
*
* @return the number of {@code SlottedPage}s in this {@code SlottedPageFile}
* @throws IOException
* if an I/O error occurs
*/
public int size() throws IOException {
return (int) (file.length() / slottedPageSize);
}
/**
* Closes this {@code SlottedPageFile} and releases any system resources associated with this
* {@code SlottedPageFile}.
*
* @throws IOException
* if an I/O error occurs
*/
public void close() throws IOException {
file.close();
}
/**
* Removes all data from this {@code SlottedPageFile}.
*
* @throws IOException
* if an I/O error occurs
*/
public void clear() throws IOException {
file.close();
new File(name).delete();
file = new java.io.RandomAccessFile(name, "rw");
}
/**
* Creates a {@code SlottedPage} from this {@code SlottedPageFile} according to the specified page ID ({@code null}
* if no corresponding data is stored in this {@code SlottedPageFile}).
*
* @param pageID
* the ID of the {@code SlottedPage} to create
* @return a {@code SlottedPage} from this {@code SlottedPageFile}; {@code null} if no corresponding data is stored
* in this {@code SlottedPageFile}
* @throws IOException
* if an I/O error occurs
*/
public SlottedPage get(int pageID) throws IOException {
if (pageID < 0)
return null;
long pos = ((long) pageID) * slottedPageSize;
if (pos + slottedPageSize > file.length())
return null;
seek(pos);
SlottedPage p = new SlottedPage(pageID, slottedPageSize);
file.read(p.data());
reads++;
return p;
}
/**
* Saves the specified {@code SlottedPage} to this {@code SlottedPageFile}.
*
* @param p
* {@code SlottedPage}
* @throws IOException
* if an I/O error occurs
*/
public void save(SlottedPage p) throws IOException {
seek(p.pageID() * slottedPageSize);
file.write(p.data());
writes++;
}
/**
* Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
*
* @param pos
* the offset position, measured in bytes from the beginning of the file, at which to set the file
* pointer.
* @throws IOException
* if an I/O error occurs.
*/
void seek(long pos) throws IOException {
if (pos != file.getFilePointer()) {
file.seek(pos);
seeks++;
}
}
}