Changeset 900

Show
Ignore:
Timestamp:
11/07/07 11:50:09 (2 years ago)
Author:
vasi
Message:

worky....

Location:
cs420
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • cs420/.classpath

    r881 r900  
    22<classpath> 
    33        <classpathentry kind="src" path="asst1"/> 
     4        <classpathentry kind="src" path="asst5"/> 
    45        <classpathentry kind="src" path="asst2"/> 
    56        <classpathentry kind="src" path="asst3"/> 
  • cs420/asst5/asst5420.java

    r899 r900  
    1 /* CS 420 assignment 4: seek in a file 
     1/* CS 420 assignment 5: search in a B-tree 
    22 * David Vasilevsky, ID 110126642 
    33 */ 
     
    55import java.io.*; 
    66 
    7 public class asst4420 { 
    8         private static final String COURSE = "/home/course/cs420"; 
    9         private static final int BLOCKSIZE = 81; 
    10          
     7public class asst5420 { 
    118        // Find a file given a relative path 
    129        private static File findFile(String rel) { 
    1310                File f = new File(rel); // prefer using the relative path 
    14                 if (!f.exists()) 
     11                if (!f.exists()) { 
     12                        final String COURSE = "/home/course/cs420"; 
    1513                        f = new File(COURSE, rel); // fall back to the course dir 
     14                } 
    1615                return f; 
    1716        } 
    1817         
    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; 
    2325                 
    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                        } 
    3249                } 
    3350                 
    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                } 
    38113        } 
    39114         
    40115        public static void main(String[] args) { 
    41116                try { 
    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!"); 
    52124                } catch (IOException e) { 
    53125                        e.printStackTrace();