A utility method to create a new HeapFile with a single empty page, assuming the path does not already exist. If the path exists, the file will be overwritten. The new table will be added to the Catalog with the specified number of columns as IntFields.
(String path, int cols)
| 103 | * the specified number of columns as IntFields. |
| 104 | */ |
| 105 | public static HeapFile createEmptyHeapFile(String path, int cols) |
| 106 | throws IOException { |
| 107 | File f = new File(path); |
| 108 | // touch the file |
| 109 | FileOutputStream fos = new FileOutputStream(f); |
| 110 | fos.write(new byte[0]); |
| 111 | fos.close(); |
| 112 | |
| 113 | HeapFile hf = openHeapFile(cols, f); |
| 114 | HeapPageId pid = new HeapPageId(hf.getId(), 0); |
| 115 | |
| 116 | HeapPage page = null; |
| 117 | try { |
| 118 | page = new HeapPage(pid, HeapPage.createEmptyPageData()); |
| 119 | } catch (IOException e) { |
| 120 | // this should never happen for an empty page; bail; |
| 121 | throw new RuntimeException("failed to create empty page in HeapFile"); |
| 122 | } |
| 123 | |
| 124 | hf.writePage(page); |
| 125 | return hf; |
| 126 | } |
| 127 | |
| 128 | /** Opens a HeapFile and adds it to the catalog. |
| 129 | * |