コード例 #1
0
ファイル: HeapFile.java プロジェクト: DragonZ/cs448
 /**
  * Reads a record from the file, given its id.
  *
  * @throws IllegalArgumentException if the rid is invalid
  */
 public byte[] selectRecord(RID rid) {
   PageId pid = rid.pageno;
   if (!pids.contains(pid.pid)) throw new IllegalArgumentException("the RID is invalid");
   Page page = new Page();
   global.Minibase.BufferManager.pinPage(pid, page, false);
   global.Minibase.BufferManager.unpinPage(pid, false);
   return page.getData();
 }
コード例 #2
0
ファイル: HeapFile.java プロジェクト: DragonZ/cs448
  /**
   * If the given name already denotes a file, this opens it; otherwise, this creates a new empty
   * file. A null name produces a temporary heap file which requires no DB entry.
   */
  public HeapFile(String name) throws ChainException {
    this.name = name;
    PageId firstPageId;
    Page page = new Page();
    pages = new ArrayList<PageId>();
    pids = new HashSet<Integer>();
    if (name == null) {
      // Temporary heap file
      firstPageId = global.Minibase.BufferManager.newPage(page, 1);
      current = new HFPage(page);
      current.setCurPage(firstPageId);
      pages.add(firstPageId);
      pids.add(firstPageId.pid);
      global.Minibase.BufferManager.unpinPage(firstPageId, true);
    } else {

      firstPageId = global.Minibase.DiskManager.get_file_entry(name);
      if (firstPageId == null) {
        firstPageId = global.Minibase.BufferManager.newPage(page, 1);
        global.Minibase.DiskManager.add_file_entry(name, firstPageId);
        global.Minibase.BufferManager.unpinPage(firstPageId, true);
        pages.add(firstPageId);
        pids.add(firstPageId.pid);
        global.Minibase.BufferManager.pinPage(firstPageId, page, false);

        recordNumber = 0;
        current = new HFPage(page);
        current.setCurPage(firstPageId);
        global.Minibase.BufferManager.unpinPage(firstPageId, true);
        return;
      }
      // add the new HFPages
      global.Minibase.BufferManager.pinPage(firstPageId, page, false);
      // System.err.println("hahahaha"+page.getData());
      current = new HFPage(page);
      current.setData(page.getData());
      pages.add(firstPageId);
      pids.add(firstPageId.pid);
      recordNumber += amount(current);
      global.Minibase.BufferManager.unpinPage(firstPageId, false);
      PageId currentPageId = current.getNextPage();
      while (currentPageId.pid != 0 & currentPageId.pid != -1) {
        HFPage temp = new HFPage();
        global.Minibase.BufferManager.pinPage(currentPageId, temp, false);
        pages.add(currentPageId);
        pids.add(currentPageId.pid);
        recordNumber += amount(temp);
        global.Minibase.BufferManager.unpinPage(currentPageId, false);
        currentPageId = temp.getNextPage();
      }
    }
  }
コード例 #3
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;
  }
コード例 #4
0
ファイル: BMTest.java プロジェクト: umairsajid/Projects-1
  /**
   * overrides the test4 function in TestDriver
   *
   * @return whether test4 has passed
   */
  protected boolean test4() {
    System.out.print("\n  Test 4 exercises some of the internals " + "of the buffer manager\n");

    int index;
    int numPages = NUMBUF + 10;
    Page pg = new Page();
    PageId pid = new PageId();
    PageId[] pids = new PageId[numPages];
    boolean status = OK;

    System.out.print("  - Allocate bunch of pages together \n");

    for (int i = 0; i < numPages / NUMBUF; i++) {
      for (index = i * NUMBUF; status == OK && index < (i + 1) * NUMBUF; ++index) {
        pg = new Page();
        try {
          pid = SystemDefs.JavabaseBM.newPage(pg, 1);
        } catch (Exception e) {
          status = FAIL;
          System.err.print("*** Could not allocate new page number " + index + 1 + "\n");
          e.printStackTrace();
        }

        if (status == OK) {
          pids[index] = pid;
        }

        if (status == OK) {

          // Copy the page number + 99999 onto each page.  It seems
          // unlikely that this bit pattern would show up there by
          // coincidence.
          int data = pid.pid + 99999;

          try {
            Convert.setIntValue(data, 0, pg.getpage());
          } catch (IOException e) {
            System.err.print("*** Convert value failed\n");
            status = FAIL;
            e.printStackTrace();
          }
        }
      }
      for (index = i * NUMBUF; status == OK && index < (i + 1) * NUMBUF; ++index) {
        try {
          SystemDefs.JavabaseBM.unpinPage(pids[index], true);
        } catch (Exception e) {
          status = FAIL;
          System.err.print("*** Could not unpin page " + pids[index].pid + "\n");
          e.printStackTrace();
        }
      }
    }

    if (status == OK) {
      System.out.print("  - Read the pages\n");

      for (int i = 0; i < numPages / NUMBUF; i++) {
        for (index = i * NUMBUF; status == OK && index < (i + 1) * NUMBUF; ++index) {
          pg = new Page();
          pid = pids[index];
          try {
            SystemDefs.JavabaseBM.pinPage(pid, pg, false);
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not pin page " + pid.pid + "\n");
            e.printStackTrace();
          }

          if (status == OK) {

            int data = 0;

            try {
              data = Convert.getIntValue(0, pg.getpage());
            } catch (IOException e) {
              System.err.print("*** Convert value failed \n");
              status = FAIL;
            }

            if (data != pid.pid + 99999) {
              status = FAIL;
              System.err.print("*** Read wrong data back from page " + pid.pid + "\n");
            }
          }
        }
        for (index = i * NUMBUF; status == OK && index < (i + 1) * NUMBUF; ++index) {
          try {
            SystemDefs.JavabaseBM.unpinPage(pids[index], true);
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not unpin page " + pids[index].pid + "\n");
            e.printStackTrace();
          }
        }
      }
    }

    if (status == OK) {
      System.out.print("  Test 4 completed successfully.\n");
    }

    return status;
  }
コード例 #5
0
ファイル: BMTest.java プロジェクト: umairsajid/Projects-1
  /**
   * overrides the test3 function in TestDriver. It exercises some of the internal of the buffer
   * manager
   *
   * @return whether test3 has passed
   */
  protected boolean test3() {

    System.out.print("\n  Test 3 exercises some of the internals " + "of the buffer manager\n");

    int index;
    int numPages = NUMBUF + 10;
    Page pg = new Page();
    PageId pid = new PageId();
    PageId[] pids = new PageId[numPages];
    boolean status = OK;

    System.out.print(
        "  - Allocate and dirty some new pages, one at " + "a time, and leave some pinned\n");

    for (index = 0; status == OK && index < numPages; ++index) {
      try {
        pid = SystemDefs.JavabaseBM.newPage(pg, 1);
      } catch (Exception e) {
        status = FAIL;
        System.err.print("*** Could not allocate new page number " + index + 1 + "\n");
        e.printStackTrace();
      }

      if (status == OK) pids[index] = pid;

      if (status == OK) {

        // Copy the page number + 99999 onto each page.  It seems
        // unlikely that this bit pattern would show up there by
        // coincidence.
        int data = pid.pid + 99999;

        try {
          Convert.setIntValue(data, 0, pg.getpage());
        } catch (IOException e) {
          System.err.print("*** Convert value failed\n");
          status = FAIL;
          e.printStackTrace();
        }

        // Leave the page pinned if it equals 12 mod 20.  This is a
        // random number based loosely on a bug report.
        if (status == OK) {
          if (pid.pid % 20 != 12) {
            try {
              SystemDefs.JavabaseBM.unpinPage(pid, /*dirty:*/ true);
            } catch (Exception e) {
              status = FAIL;
              System.err.print("*** Could not unpin dirty page " + pid.pid + "\n");
            }
          }
        }
      }
    }

    if (status == OK) {
      System.out.print("  - Read the pages\n");

      for (index = 0; status == OK && index < numPages; ++index) {
        pid = pids[index];
        try {
          SystemDefs.JavabaseBM.pinPage(pid, pg, false);
        } catch (Exception e) {
          status = FAIL;
          System.err.print("*** Could not pin page " + pid.pid + "\n");
          e.printStackTrace();
        }

        if (status == OK) {

          int data = 0;

          try {
            data = Convert.getIntValue(0, pg.getpage());
          } catch (IOException e) {
            System.err.print("*** Convert value failed \n");
            status = FAIL;
          }

          if (data != pid.pid + 99999) {
            status = FAIL;
            System.err.print("*** Read wrong data back from page " + pid.pid + "\n");
          }
        }

        if (status == OK) {
          try {
            SystemDefs.JavabaseBM.unpinPage(pid, true); // might not be dirty
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not unpin page " + pid.pid + "\n");
            e.printStackTrace();
          }
        }

        if (status == OK && (pid.pid % 20 == 12)) {
          try {
            SystemDefs.JavabaseBM.unpinPage(pid, /*dirty:*/ true);
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not unpin page " + pid.pid + "\n");
            e.printStackTrace();
          }
        }
      }
    }

    if (status == OK) System.out.print("  Test 3 completed successfully.\n");

    return status;
  }
コード例 #6
0
ファイル: BMTest.java プロジェクト: umairsajid/Projects-1
  /**
   * overrides the test1 function in TestDriver. It tests some simple normal buffer manager
   * operations.
   *
   * @return whether test1 has passed
   */
  protected boolean test1() {

    System.out.print("\n  Test 1 does a simple test of normal buffer ");
    System.out.print("manager operations:\n");

    // We choose this number to ensure that at least one page will have to be
    // written during this test.
    boolean status = OK;
    int numPages = SystemDefs.JavabaseBM.getNumUnpinnedBuffers() + 1;
    Page pg = new Page();
    PageId pid;
    PageId lastPid;
    PageId firstPid = new PageId();

    System.out.print("  - Allocate a bunch of new pages\n");

    try {
      firstPid = SystemDefs.JavabaseBM.newPage(pg, numPages);
    } catch (Exception e) {
      System.err.print("*** Could not allocate " + numPages);
      System.err.print(" new pages in the database.\n");
      e.printStackTrace();
      return false;
    }

    // Unpin that first page... to simplify our loop.
    try {
      SystemDefs.JavabaseBM.unpinPage(firstPid, false /*not dirty*/);
    } catch (Exception e) {
      System.err.print("*** Could not unpin the first new page.\n");
      e.printStackTrace();
      status = FAIL;
    }

    System.out.print("  - Write something on each one\n");

    pid = new PageId();
    lastPid = new PageId();

    for (pid.pid = firstPid.pid, lastPid.pid = pid.pid + numPages;
        status == OK && pid.pid < lastPid.pid;
        pid.pid = pid.pid + 1) {

      try {
        SystemDefs.JavabaseBM.pinPage(pid, pg, /*emptyPage:*/ true);
      } catch (Exception e) {
        status = FAIL;
        System.err.print("*** Could not pin new page " + pid.pid + "\n");
        e.printStackTrace();
      }

      if (status == OK) {

        // Copy the page number + 99999 onto each page.  It seems
        // unlikely that this bit pattern would show up there by
        // coincidence.
        int data = pid.pid + 99999;

        try {
          Convert.setIntValue(data, 0, pg.getpage());
        } catch (IOException e) {
          System.err.print("*** Convert value failed\n");
          status = FAIL;
        }

        if (status == OK) {
          try {
            SystemDefs.JavabaseBM.unpinPage(pid, /*dirty:*/ true);
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not unpin dirty page " + pid.pid + "\n");
            e.printStackTrace();
          }
        }
      }
    }

    if (status == OK)
      System.out.print(
          "  - Read that something back from each one\n"
              + "   (because we're buffering, this is where "
              + "most of the writes happen)\n");

    for (pid.pid = firstPid.pid; status == OK && pid.pid < lastPid.pid; pid.pid = pid.pid + 1) {

      try {
        SystemDefs.JavabaseBM.pinPage(pid, pg, /*emptyPage:*/ false);
      } catch (Exception e) {
        status = FAIL;
        System.err.print("*** Could not pin page " + pid.pid + "\n");
        e.printStackTrace();
      }

      if (status == OK) {

        int data = 0;

        try {
          data = Convert.getIntValue(0, pg.getpage());
        } catch (IOException e) {
          System.err.print("*** Convert value failed \n");
          status = FAIL;
        }

        if (status == OK) {
          if (data != (pid.pid) + 99999) {
            status = FAIL;
            System.err.print("*** Read wrong data back from page " + pid.pid + "\n");
          }
        }

        if (status == OK) {
          try {
            SystemDefs.JavabaseBM.unpinPage(pid, /*dirty:*/ true);
          } catch (Exception e) {
            status = FAIL;
            System.err.print("*** Could not unpin page " + pid.pid + "\n");
            e.printStackTrace();
          }
        }
      }
    }

    if (status == OK) System.out.print("  - Free the pages again\n");

    for (pid.pid = firstPid.pid; pid.pid < lastPid.pid; pid.pid = pid.pid + 1) {

      try {
        SystemDefs.JavabaseBM.freePage(pid);
      } catch (Exception e) {
        status = FAIL;
        System.err.print("*** Error freeing page " + pid.pid + "\n");
        e.printStackTrace();
      }
    }

    if (status == OK) System.out.print("  Test 1 completed successfully.\n");

    return status;
  }