Read a page from the file on disk. This should not be called directly but should be called from the BufferPool via getPage() @param pid - the id of the page to read from disk @return the page constructed from the contents on disk
(PageId pid)
| 87 | * @return the page constructed from the contents on disk |
| 88 | */ |
| 89 | public Page readPage(PageId pid) { |
| 90 | BTreePageId id = (BTreePageId) pid; |
| 91 | |
| 92 | try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { |
| 93 | if (id.pgcateg() == BTreePageId.ROOT_PTR) { |
| 94 | //指向根节点指针 |
| 95 | byte[] pageBuf = new byte[BTreeRootPtrPage.getPageSize()]; |
| 96 | int retval = bis.read(pageBuf, 0, BTreeRootPtrPage.getPageSize()); |
| 97 | if (retval == -1) { |
| 98 | throw new IllegalArgumentException("Read past end of table"); |
| 99 | } |
| 100 | if (retval < BTreeRootPtrPage.getPageSize()) { |
| 101 | throw new IllegalArgumentException("Unable to read " |
| 102 | + BTreeRootPtrPage.getPageSize() + " bytes from BTreeFile"); |
| 103 | } |
| 104 | Debug.log(1, "BTreeFile.readPage: read page %d", id.getPageNumber()); |
| 105 | return new BTreeRootPtrPage(id, pageBuf); |
| 106 | } else { |
| 107 | byte[] pageBuf = new byte[BufferPool.getPageSize()]; |
| 108 | if (bis.skip(BTreeRootPtrPage.getPageSize() + (long) (id.getPageNumber() - 1) * BufferPool.getPageSize()) != |
| 109 | BTreeRootPtrPage.getPageSize() + (long) (id.getPageNumber() - 1) * BufferPool.getPageSize()) { |
| 110 | throw new IllegalArgumentException( |
| 111 | "Unable to seek to correct place in BTreeFile"); |
| 112 | } |
| 113 | int retval = bis.read(pageBuf, 0, BufferPool.getPageSize()); |
| 114 | if (retval == -1) { |
| 115 | throw new IllegalArgumentException("Read past end of table"); |
| 116 | } |
| 117 | if (retval < BufferPool.getPageSize()) { |
| 118 | throw new IllegalArgumentException("Unable to read " |
| 119 | + BufferPool.getPageSize() + " bytes from BTreeFile"); |
| 120 | } |
| 121 | Debug.log(1, "BTreeFile.readPage: read page %d", id.getPageNumber()); |
| 122 | //分别是三种节点 |
| 123 | if (id.pgcateg() == BTreePageId.INTERNAL) { |
| 124 | return new BTreeInternalPage(id, pageBuf, keyField); |
| 125 | } else if (id.pgcateg() == BTreePageId.LEAF) { |
| 126 | return new BTreeLeafPage(id, pageBuf, keyField); |
| 127 | } else { // id.pgcateg() == BTreePageId.HEADER |
| 128 | return new BTreeHeaderPage(id, pageBuf); |
| 129 | } |
| 130 | } |
| 131 | } catch (IOException e) { |
| 132 | throw new RuntimeException(e); |
| 133 | } |
| 134 | // Close the file on success or error |
| 135 | // Ignore failures closing the file |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Write a page to disk. This should not be called directly but should |
nothing calls this directly
no test coverage detected