/**
   * User should call this method if s/he needs to delete a page. this routine will call DB to
   * deallocate the page.
   *
   * @param globalPageId the page number in the data base.
   * @exception InvalidBufferException if buffer pool corrupted.
   * @exception ReplacerException if there is a replacer error.
   * @exception HashOperationException if there is a hash table error.
   * @exception InvalidFrameNumberException if there is an invalid frame number.
   * @exception PageNotReadException if a page cannot be read.
   * @exception BufferPoolExceededException if the buffer pool is already full.
   * @exception PagePinnedException if a page is left pinned.
   * @exception PageUnpinnedException if there is a page that is already unpinned.
   * @exception HashEntryNotFoundException if there is no entry of page in the hash table.
   * @exception IOException if there is other kinds of I/O error.
   * @exception BufMgrException other error occured in bufmgr layer
   * @exception DiskMgrException other error occured in diskmgr layer
   */
  public void freePage(PageId pageId)
      throws InvalidBufferException, ReplacerException, HashOperationException,
          InvalidFrameNumberException, PageNotReadException, BufferPoolExceededException,
          PagePinnedException, PageUnpinnedException, HashEntryNotFoundException, BufMgrException,
          DiskMgrException, IOException {

    BufMgrFrameDesc frame = pageTable.get(pageId);

    if (frame != null) {
      int pinCount = frame.getPinCount();

      if (pinCount > 1) {
        throw new PagePinnedException(null, "BufrMgr::freePage: page to be freed not loaded");
      } else {
        if (pinCount == 1) {
          frame.unpin();
          replacer.unpin(frame.getFrameNumber());
        }
        // remove from pagetable/frametable
        pageTable.remove(pageId);

        // add it to empty list
        frameTable[frame.getFrameNumber()] = null;
        replacer.free(frame.getFrameNumber());

        // free it on disk
        try {
          SystemDefs.JavabaseDB.deallocate_page(pageId);
        } catch (Exception e1) {
          throw new DiskMgrException(
              e1, "BUFMGR::freepage failed " + "after pinPage failed and deallocate page failed");
        }
      }

    } else {
      // free it on disk
      try {
        SystemDefs.JavabaseDB.deallocate_page(pageId);
      } catch (Exception e1) {
        throw new DiskMgrException(e1, "BUFMGR::freepage failed");
      }
    }
  }
  /**
   * To unpin a page specified by a pageId. If pincount>0, decrement it and if it becomes zero, put
   * it in a group of replacement candidates. if pincount=0 before this call, return error.
   *
   * @param globalPageId_in_a_DB page number in the minibase.
   * @param dirty the dirty bit of the frame
   * @exception ReplacerException if there is a replacer error.
   * @exception PageUnpinnedException if there is a page that is already unpinned.
   * @exception InvalidFrameNumberException if there is an invalid frame number .
   * @exception HashEntryNotFoundException if there is no entry of page in the hash table.
   */
  public void unpinPage(PageId pageId, boolean dirty)
      throws ReplacerException, PageUnpinnedException, HashEntryNotFoundException,
          InvalidFrameNumberException {

    BufMgrFrameDesc frame = pageTable.get(pageId);

    if (frame != null) {
      int pinCount = frame.getPinCount();
      if (pinCount == 0) {
        throw new PageUnpinnedException(
            null, "BufrMgr::unPinPage: page to be unpinned is already unpinned");
      } else {
        frame.unpin();
        if (frame.getPinCount() == 0) replacer.unpin(frame.getFrameNumber());
      }

      frame.setDirtybit(dirty);
    } else {
      throw new HashEntryNotFoundException(
          null, "BufrMgr::unPinPage: page to be unpinned not loaded");
    }
  }