-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHeapFileIterator.java
More file actions
86 lines (77 loc) · 2.58 KB
/
Copy pathHeapFileIterator.java
File metadata and controls
86 lines (77 loc) · 2.58 KB
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
package simpledb;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Helper class that implements the Java Iterator for tuples on a HeapFile
*/
public class HeapFileIterator implements DbFileIterator {
private final HeapFile heapFile;
private final TransactionId transactionId;
private int currentPageNo;
private Iterator<Tuple> pageIterator;
public HeapFileIterator(HeapFile hf, TransactionId tid) {
heapFile = hf;
transactionId = tid;
}
/**
* Opens the iterator
* @throws DbException when there are problems opening/accessing the database.
*/
public void open()
throws DbException, TransactionAbortedException {
currentPageNo = 0;
pageIterator = getTupleIteratorByPageNo(currentPageNo);
}
/** @return true if there are more tuples available. */
public boolean hasNext()
throws DbException, TransactionAbortedException {
if (pageIterator == null) {
return false;
}
while (!pageIterator.hasNext()) {
++currentPageNo;
if (currentPageNo >= heapFile.numPages()) {
return false;
}
pageIterator = getTupleIteratorByPageNo(currentPageNo);
}
return true;
}
/**
* Gets the next tuple from the operator (typically implementing by reading
* from a child operator or an access method).
*
* @return The next tuple in the iterator.
* @throws NoSuchElementException if there are no more tuples
*/
public Tuple next()
throws DbException, TransactionAbortedException, NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
}
return pageIterator.next();
}
/**
* Resets the iterator to the start.
* @throws DbException When rewind is unsupported.
*/
public void rewind() throws DbException, TransactionAbortedException {
currentPageNo = 0;
pageIterator = getTupleIteratorByPageNo(currentPageNo);
}
/**
* Closes the iterator.
*/
public void close() {
pageIterator = null;
}
private Iterator<Tuple> getTupleIteratorByPageNo(int pageNo)
throws NoSuchElementException, TransactionAbortedException, DbException {
HeapPage page;
page = (HeapPage) Database.getBufferPool().getPage(
transactionId, new HeapPageId(heapFile.getId(), pageNo), Permissions.READ_ONLY);
return page.iterator();
}
}