예제 #1
0
파일: HeapFile.java 프로젝트: DragonZ/cs448
  /**
   * Searches the directory for a data page with enough free space to store a record of the given
   * size. If no suitable page is found, this creates a new data page.
   */
  protected PageId getAvailPage(int reclen) {
    for (int i = 0; i < pages.size(); i++) {
      PageId pid = pages.get(i);
      Page page = new Page();
      global.Minibase.BufferManager.pinPage(pid, page, false);
      HFPage hfpage = new HFPage();
      hfpage.copyPage(page);
      if (hfpage.getFreeSpace() >= reclen) {
        global.Minibase.BufferManager.unpinPage(pid, true);
        return pid;
      }
      global.Minibase.BufferManager.unpinPage(pid, false);
    }

    Page page = new Page();
    PageId pid = global.Minibase.BufferManager.newPage(page, 1);
    HFPage hfpage = new HFPage(page);
    // initialize HFPage
    hfpage.setCurPage(pid);
    pages.add(pid);
    pids.add(pid.pid);
    current.setNextPage(pid);
    hfpage.setPrevPage(current.getCurPage());
    current = hfpage;
    global.Minibase.BufferManager.unpinPage(pid, true);
    return pid;
  }
예제 #2
0
파일: HeapFile.java 프로젝트: DragonZ/cs448
  /**
   * Inserts a new record into the file and returns its RID.
   *
   * @throws IllegalArgumentException if the record is too large
   */
  public RID insertRecord(byte[] record) throws IllegalArgumentException, ChainException {
    if (record.length > GlobalConst.MAX_TUPSIZE)
      throw new IllegalArgumentException("the record's size is too large");

    for (int i = 0; i < pages.size(); i++) {
      PageId pid = pages.get(i);
      Page page = new Page();
      global.Minibase.BufferManager.pinPage(pid, page, false);
      HFPage hfpage = new HFPage(page);
      hfpage.setCurPage(pid);
      hfpage.setData(page.getData());
      if (hfpage.getFreeSpace() > record.length) {

        RID rid = hfpage.insertRecord(record);
        recordNumber++;
        global.Minibase.BufferManager.unpinPage(pid, true);
        return rid;
      }
      global.Minibase.BufferManager.unpinPage(pid, false);
    }

    Page page = new Page();
    PageId pid = global.Minibase.BufferManager.newPage(page, 1);
    HFPage hfpage = new HFPage(page);
    // initialize HFPage
    hfpage.initDefaults();
    hfpage.setCurPage(pid);
    // hfpage.print();
    RID rid = hfpage.insertRecord(record);
    pages.add(pid);
    pids.add(pid.pid);
    hfpage.setNextPage(pid);
    hfpage.setPrevPage(current.getCurPage());
    current = hfpage;
    global.Minibase.BufferManager.unpinPage(pid, true);
    // hfpage.print();
    // System.out.println ("The PID is "+pid);
    recordNumber++;
    return rid;
  }