1 package simpledb.log; 2 3 import static simpledb.file.Page.*; 4 import simpledb.file.Page; 5 6 /** 7 * A class that provides the ability to read the values of 8 * a log record. 9 * The class has no idea what values are there. 10 * Instead, the methods {@link #nextInt() nextInt} 11 * and {@link #nextString() nextString} read the values 12 * sequentially. 13 * Thus the client is responsible for knowing how many values 14 * are in the log record, and what their types are. 15 * @author Edward Sciore 16 */ 17 public class BasicLogRecord { 18 private Page pg; 19 private int pos; 20 21 /** 22 * A log record located at the specified position of the specified page. 23 * This constructor is called exclusively by 24 * {@link LogIterator#next()}. 25 * @param pg the page containing the log record 26 * @param pos the position of the log record 27 */ 28 public BasicLogRecord(Page pg, int pos) { 29 this.pg = pg; 30 this.pos = pos; 31 } 32 33 /** 34 * Returns the next value of the current log record, 35 * assuming it is an integer. 36 * @return the next value of the current log record 37 */ 38 public int nextInt() { 39 int result = pg.getInt(pos); 40 pos += INT_SIZE; 41 return result; 42 } 43 44 /** 45 * Returns the next value of the current log record, 46 * assuming it is a string. 47 * @return the next value of the current log record 48 */ 49 public String nextString() { 50 String result = pg.getString(pos); 51 pos += STR_SIZE(result.length()); 52 return result; 53 } 54 }