CS357: Database Systems Implementation
Solution to Homework Assignment 2


1.  Consider a single-platter disk containing 50,000 tracks and spinning at 7200rpm.  Each track holds 500 sectors, and each sector contains 512 bytes.
a)  What is the capacity of the platter?
500 sectors/track * 0.5 KB/sector = 250 KB/track.
250 KB/track * 50,000 tracks/platter = 12,500,000 KB/platter, which is approximately 10GB.
 
b)  What is the average rotational delay?
7200 revolutions/minute = 1 revolution every 8.33 msec.  The rotational delay is half that number, or 4.17 msec.
 
c)  What is the maximum transfer rate?
1 track every 8.33 msec * 250 KB/track = 30 KB/msec, which is approximately 30 MB/msec.
 
 
2.  The class Page implements a string by prepending its length to the string’s characters.  Another way to implement a string is to append a delimiter character after the string’s characters.  A reasonable delimiter character in Java is ‘\0’.  Modify the class accordingly.
    
The following three methods need to be modified:
 
   public static final int STR_SIZE(int n) {
     int bytesPerChar = 2;
     return (n+1) * bytesPerChar;
   }
 
   public synchronized String getString(int offset) {
     contents.position(offset);
     StringBuffer sb = new StringBuffer();
     boolean done = false;
     while (!done) {
        char c = contents.getChar();
        if (c == '\0')
          done = true;
        else
          sb.append(c);
     }
     return new String(sb);
   }
 
   public synchronized void setString(int offset, String val) {
     contents.position(offset);
     for (int i=0; i<val.length(); i++)
        contents.putChar(val.charAt(i));
     contents.putChar('\0');

   }
 

3.  Write a program to test your code from #2.  This code shouldn't use JDBC; instead, it should be similar to Figure 12-12 of the text.  For example, it could write some strings to a page and read them back.  (You also should make sure that SimpleDB still works when you replace its version of Page with yours.  But it is always good to write your own test code too.)

Here is a simple program.  It writes two strings to a page, and reads them back.  It uses STR_SIZE to determine where the second string should be placed in the page.


import simpledb.file.*;
import simpledb.server.SimpleDB;

public class PageTest {
    public static void main(String[] args) throws Exception {
        SimpleDB.init("pagetest");
        Page pg = new Page();
        Block blk = new Block("testfile", 0);
        pg.setString(20, "abcd");
        int len = Page.STR_SIZE(4);
        pg.setString(20+len, "xyz");

        System.out.println(pg.getString(20));
        System.out.println(pg.getString(20+len));
    }
}