| 19 | | |
| 20 | | // Simple struct-like class to represent a record of the file |
| 21 | | public static class Record { |
| 22 | | public String name, address, phone; |
| | 18 | public static class BTree { |
| | 19 | public static final int RECORDSIZE = 80; |
| | 20 | public static final int ORDER = 19; |
| | 21 | public static final int DATASIZE = (ORDER - 1) * RECORDSIZE; |
| | 22 | public static final int NODESIZE = DATASIZE + ORDER * 4; |
| | 23 | public static final String NULLRECORDTOKEN = "~~~~~~~"; |
| | 24 | public static final int NULLPOINTER = -1; |
| 24 | | // Read the next record in the input |
| 25 | | public Record(DataInput di) throws IOException { |
| 26 | | byte[] data = new byte[81]; |
| 27 | | di.readFully(data); |
| 28 | | String ds = new String(data); |
| 29 | | name = ds.substring(0, 40).trim(); |
| 30 | | address = ds.substring(40, 68).trim(); |
| 31 | | phone = ds.substring(68, 80).trim(); |
| | 26 | private RandomAccessFile file; |
| | 27 | private int baseNode; |
| | 28 | |
| | 29 | private class Node { |
| | 30 | private byte[] data = new byte[NODESIZE]; |
| | 31 | |
| | 32 | // Re-use this node for the given node/block number |
| | 33 | private void setNode(int blockno) throws IOException { |
| | 34 | file.seek(4 + blockno * NODESIZE); |
| | 35 | file.readFully(data); |
| | 36 | } |
| | 37 | |
| | 38 | public Node(int blockno) throws IOException { |
| | 39 | setNode(blockno); |
| | 40 | } |
| | 41 | |
| | 42 | public Record getRecord(int idx) { |
| | 43 | return new Record(data, idx * RECORDSIZE); |
| | 44 | } |
| | 45 | |
| | 46 | public int getPointer(int idx) { |
| | 47 | return bytechars.byte2int(data, DATASIZE + idx * 4); |
| | 48 | } |
| 34 | | public String toString() { |
| 35 | | return "name: '" + name + "', address: '" + address |
| 36 | | + "', phone: '" + phone + "'\n"; |
| 37 | | } |
| | 51 | public class Record { |
| | 52 | private byte[] data; |
| | 53 | private int offset; |
| | 54 | |
| | 55 | public Record(byte[] d, int off) { |
| | 56 | data = d; |
| | 57 | offset = off; |
| | 58 | } |
| | 59 | |
| | 60 | public String getName() { |
| | 61 | return bytechars.byte2string(data, offset, 40); |
| | 62 | } |
| | 63 | |
| | 64 | public String getAddress() { |
| | 65 | return bytechars.byte2string(data, offset + 40, 33); |
| | 66 | } |
| | 67 | |
| | 68 | public String getPhone() { |
| | 69 | return bytechars.byte2string(data, offset + 73, 7); |
| | 70 | } |
| | 71 | |
| | 72 | public boolean isNull() { |
| | 73 | return getPhone().equals(NULLRECORDTOKEN); |
| | 74 | } |
| | 75 | |
| | 76 | public String toString() { |
| | 77 | return "name: '" + getName() + "', address: '" + getAddress() |
| | 78 | + "', phone: '" + getPhone() + "'\n"; |
| | 79 | } |
| | 80 | } |
| | 81 | |
| | 82 | public BTree(String relPath) throws IOException { |
| | 83 | file = new RandomAccessFile(findFile(relPath), "r"); |
| | 84 | baseNode = file.readInt(); |
| | 85 | } |
| | 86 | |
| | 87 | public Node root() throws IOException { |
| | 88 | return new Node(baseNode); |
| | 89 | } |
| | 90 | |
| | 91 | |
| | 92 | // Test tree traversal |
| | 93 | public static class Functor { |
| | 94 | public void run(Record r) { } |
| | 95 | } |
| | 96 | |
| | 97 | public void traverse(Functor f, Node n) throws IOException { |
| | 98 | for (int i = 0; i < ORDER; ++i) { |
| | 99 | int ptr = n.getPointer(i); |
| | 100 | if (ptr != NULLPOINTER) |
| | 101 | traverse(f, new Node(ptr)); |
| | 102 | if (i != ORDER - 1) { |
| | 103 | Record r = n.getRecord(i); |
| | 104 | if (!r.isNull()) |
| | 105 | f.run(r); |
| | 106 | } |
| | 107 | } |
| | 108 | } |
| | 109 | |
| | 110 | public void traverse(Functor f) throws IOException { |
| | 111 | traverse(f, root()); |
| | 112 | } |
| 42 | | int[] offsets = { 5, 4, 6, 3, 7, 2, 8, 1, 9, 0 }; |
| 43 | | |
| 44 | | RandomAccessFile in = new RandomAccessFile( |
| 45 | | findFile("data/tbook"), "r"); |
| 46 | | for (int i = 0; i < offsets.length; ++i) { |
| 47 | | in.seek(offsets[i] * BLOCKSIZE); |
| 48 | | Record r = new Record(in); |
| 49 | | System.out.print(r); |
| 50 | | } |
| 51 | | |
| | 117 | BTree bt = new BTree("data/btree"); |
| | 118 | bt.traverse(new BTree.Functor() { |
| | 119 | public void run(BTree.Record r) { |
| | 120 | System.out.print(r); |
| | 121 | } |
| | 122 | }); |
| | 123 | System.out.println("done!"); |