这个迭代器的作用是用来遍历所有的tuple,但是不要将所有tuple一次性放入内存,而是一页一页的读和遍历
| 183 | * 这个迭代器的作用是用来遍历所有的tuple,但是不要将所有tuple一次性放入内存,而是一页一页的读和遍历 |
| 184 | */ |
| 185 | public class HeapFileIterator implements DbFileIterator{ |
| 186 | TransactionId tid; |
| 187 | Permissions permissions; |
| 188 | BufferPool bufferPool =Database.getBufferPool(); |
| 189 | Iterator<Tuple> iterator; //这个iterator是每一页的迭代器 |
| 190 | int num = 0; |
| 191 | |
| 192 | public HeapFileIterator(TransactionId tid,Permissions permissions){ |
| 193 | this.tid = tid; |
| 194 | this.permissions = permissions; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * 开始进行遍历,默认从第一页开始 |
| 199 | * @throws DbException |
| 200 | * @throws TransactionAbortedException |
| 201 | */ |
| 202 | @Override |
| 203 | public void open() throws DbException, TransactionAbortedException { |
| 204 | num = 0; |
| 205 | HeapPageId heapPageId = new HeapPageId(getId(), num); |
| 206 | HeapPage page = (HeapPage)this.bufferPool.getPage(tid, heapPageId, permissions); |
| 207 | if(page==null){ |
| 208 | throw new DbException("page null"); |
| 209 | }else{ |
| 210 | iterator = page.iterator(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * 获取下一有数据的页 |
| 216 | * @return |
| 217 | * @throws DbException |
| 218 | * @throws TransactionAbortedException |
| 219 | */ |
| 220 | public boolean nextPage() throws DbException, TransactionAbortedException { |
| 221 | while(true){ |
| 222 | num++; |
| 223 | if(num>=numPages()){ |
| 224 | return false; |
| 225 | } |
| 226 | HeapPageId heapPageId = new HeapPageId(getId(), num); |
| 227 | HeapPage page = (HeapPage)bufferPool.getPage(tid,heapPageId,permissions); |
| 228 | if(page==null){ |
| 229 | continue; |
| 230 | } |
| 231 | iterator = page.iterator(); |
| 232 | if(iterator.hasNext()){ |
| 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | @Override |
| 241 | public boolean hasNext() throws DbException, TransactionAbortedException { |
| 242 | if(iterator==null){ |
nothing calls this directly
no test coverage detected