-
Notifications
You must be signed in to change notification settings - Fork 15
/
HeapFile.java
212 lines (186 loc) · 5.71 KB
/
HeapFile.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
package simpledb;
import java.io.*;
import java.util.*;
/**
* HeapFile is an implementation of a DbFile that stores a collection of tuples
* in no particular order. Tuples are stored on pages, each of which is a fixed
* size, and the file is simply a collection of those pages. HeapFile works
* closely with HeapPage. The format of HeapPages is described in the HeapPage
* constructor.
*
* @see simpledb.HeapPage#HeapPage
* @author Sam Madden
*/
public class HeapFile implements DbFile {
private final File file;
private final TupleDesc td;
/**
* Constructs a heap file backed by the specified file.
*
* @param file
* the file that stores the on-disk backing store for this heap
* file.
*/
public HeapFile(File file, TupleDesc td) {
// Done
this.file = file;
this.td = td;
}
/**
* Returns the File backing this HeapFile on disk.
*
* @return the File backing this HeapFile on disk.
*/
public File getFile() {
// Done
return file;
}
/**
* Returns an ID uniquely identifying this HeapFile. Implementation note:
* you will need to generate this tableid somewhere ensure that each
* HeapFile has a "unique id," and that you always return the same value for
* a particular HeapFile. We suggest hashing the absolute file name of the
* file underlying the heapfile, i.e. f.getAbsoluteFile().hashCode().
*
* @return an ID uniquely identifying this HeapFile.
*/
public int getId() {
// Done
return file.getAbsoluteFile().hashCode();
}
/**
* Returns the TupleDesc of the table stored in this DbFile.
*
* @return TupleDesc of this DbFile.
*/
public TupleDesc getTupleDesc() {
// Done
return td;
}
// see DbFile.java for javadocs
public Page readPage(PageId pid) {
// Done
if (getId() == pid.getTableId()) {
int pgNo = pid.pageNumber();
if (pgNo>=0 && pgNo<numPages()) {
byte[] bytes = HeapPage.createEmptyPageData();
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
try {
raf.seek(1L*BufferPool.getPageSize()*pid.pageNumber());
raf.read(bytes, 0, BufferPool.getPageSize());
return new HeapPage(HeapPageId.valueOf(pid), bytes);
} finally {
raf.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
throw new IllegalArgumentException("page not in the file");
}
// see DbFile.java for javadocs
public void writePage(Page page) throws IOException {
// Done
int pgNo = page.getId().pageNumber();
if (pgNo>=0 && pgNo<=numPages()) {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
try {
raf.seek(1L*BufferPool.getPageSize()*pgNo);
raf.write(page.getPageData(), 0, BufferPool.getPageSize());
return;
} finally {
raf.close();
}
}
throw new IllegalArgumentException("pageId out of range");
}
/**
* Returns the number of pages in this HeapFile.
*/
public int numPages() {
// Done
return (int)(file.length()/BufferPool.getPageSize());
}
// see DbFile.java for javadocs
public ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
throws DbException, IOException, TransactionAbortedException {
// Done
ArrayList<Page> list = new ArrayList<Page>();
BufferPool pool = Database.getBufferPool();
int tableId = getId(), pgNo = 0;
for (; pgNo<numPages(); pgNo++) {
HeapPage page = (HeapPage)pool.getPage(tid, new HeapPageId(tableId,pgNo), Permissions.READ_WRITE);
if (page.getNumEmptySlots() > 0) {
page.insertTuple(t);
list.add(page);
break;
}
}
if (pgNo == numPages()) {
HeapPage page = new HeapPage(new HeapPageId(tableId, pgNo), HeapPage.createEmptyPageData());
page.insertTuple(t);
list.add(page);
writePage(page);
}
return list;
}
// see DbFile.java for javadocs
public Page deleteTuple(TransactionId tid, Tuple t) throws DbException,
TransactionAbortedException {
// Done
BufferPool pool = Database.getBufferPool();
HeapPage page = (HeapPage)pool.getPage(tid, t.getRecordId().getPageId(), Permissions.READ_WRITE);
page.deleteTuple(t);
return page;
}
// see DbFile.java for javadocs
public DbFileIterator iterator(final TransactionId tid) {
// Done
return new DbFileIterator() {
private final BufferPool pool = Database.getBufferPool();
private final int tableId = getId();
private int pid = -1;
private Iterator<Tuple> child;
@Override
public void open() throws DbException, TransactionAbortedException {
pid = 0;
child = null;
}
@Override
public boolean hasNext() throws DbException,
TransactionAbortedException {
if (null != child && child.hasNext()) {
return true;
} else if (pid < 0 || pid >= numPages()) {
return false;
} else {
child = ((HeapPage)pool.getPage(tid, new HeapPageId(tableId,pid++),
Permissions.READ_ONLY)).iterator();
return hasNext();
}
}
@Override
public Tuple next() throws DbException,
TransactionAbortedException, NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
} else {
return child.next();
}
}
@Override
public void rewind() throws DbException,
TransactionAbortedException {
pid = 0;
child = null;
}
@Override
public void close() {
pid = -1;
child = null;
}
};
}
}