コード例 #1
0
  public boolean insertRecord(char[] record) {
    char[] key = getKey(record, 0); // Get key array
    int sectorNum = getSector(convertToString(key)); // Get sector number
    disk.readSector(sectorNum, buffer); // Read the sector to the buffer
    boolean notDuplicate = checkNotDuplicate(buffer, key); // Check duplicate key

    if (notDuplicate) // If its not a duplicate key
    {
      boolean availableSpace =
          findAvailableAndStore(buffer, record); // find an available space in buffer
      if (availableSpace) // Found available space
      {
        disk.writeSector(sectorNum, buffer); // Write the buffer to disk sector
        return true;
      } else // Need to go overflow sectors
      {
        if (overflowSectors == 0) // Oops i don't have a overflow sector
        {
          overflowStart =
              this.indexStart + this.indexSectors; // Allocated the first overflow sector
          disk.readSector(
              overflowStart, buffer); // Read in the first empty overflow sector into buffer
          findAvailableAndStore(buffer, record); // write the record into buffer
          disk.writeSector(overflowStart, buffer); // Write to the disk overflow sector
          overflowSectors++; // Increment number of overflow sectors in use
          return true;
        } else // If not the first time
        {
          int currOverflowSector = overflowStart;
          while ((currOverflowSector < overflowStart + overflowSectors) && notDuplicate) {
            disk.readSector(currOverflowSector, buffer); // Read in overflow sector
            notDuplicate = checkNotDuplicate(buffer, key); // Check duplicate key
            currOverflowSector++; // Move to next overflow sector
          }
          if (notDuplicate) // No duplicate key in overflow sector
          {
            currOverflowSector--; // Back to current overflow sector
            availableSpace =
                findAvailableAndStore(buffer, record); // Find an available space in buffer
            if (!availableSpace) // If not more space in this overflow sector
            {
              currOverflowSector++; // Move to next overflow sector
              overflowSectors++; // Increment number of overflow sectors in use
              disk.readSector(
                  currOverflowSector, buffer); // Read next overflow sector(empty) into buffer
              findAvailableAndStore(buffer, record); // write the record into empty buffer
            }
            disk.writeSector(currOverflowSector, buffer); // Write to the disk overflow sector
            return true;
          } else // Found duplicate key in overflow sector
          return false;
        }
      }
    } else // Same key is found no further action, return false
    return false;
  }
コード例 #2
0
  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<DELETE THIS FUNCTION WHEN
  // DONE>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  public int getSectorPublic(String key) // Returns sector number indicated by key
      {
    boolean notFound = true; // Not found the target sector
    boolean notDoneCurrSector = true; // Not done with comparing the current sector
    char[] keyChar = new char[keySize];
    key.getChars(0, key.length(), keyChar, 0); // Convert the key to characters
    char[] currSector = new char[disk.getSectorSize()];
    int address = indexRoot; // Start from root sector
    int currLevel = 0; // Count current tree level

    while (notFound) {
      disk.readSector(address, currSector); // Read sector to buffer
      int currKeyPlace = 0; // First index of the KeyOne
      notDoneCurrSector = true; // Reset notDone

      while (notDoneCurrSector) {
        boolean lastKeyInSector = false; // is the last key in sector
        int compVal = 0;
        int nextKeyPlace = currKeyPlace + indexRecordSize; // Get next Key place
        if (nextKeyPlace >= disk.getSectorSize()
            || currSector[nextKeyPlace] == 0) // if current key is the last key in sector
        lastKeyInSector = true;
        else
          compVal = compareKeys(keyChar, getKey(currSector, nextKeyPlace)); // Compare with next Key

        if (compVal == -1
            || lastKeyInSector) // If argument key is less than next key, or last key in sector
        {
          address = getSectorNumber(currSector, currKeyPlace); // Get the address of the current key
          notDoneCurrSector = false;
          currLevel++; // Increment leve;
        } else // Else if equals or greater than next key
        currKeyPlace = nextKeyPlace; // Set current key equals to next key
      }
      if (currLevel == indexLevels) // When we get to bottom tree level
      notFound = false; // We done, we found the sector number
    }

    return address;
  }
コード例 #3
0
  public boolean findRecord(char[] record) {
    char[] key = getKey(record, 0); // Get key array
    int sectorNum = getSector(convertToString(key)); // Get sector number
    disk.readSector(sectorNum, buffer); // Read the sector to the buffer

    int numKeysChecked = 0; // Count number of key we checked
    int nextKeyPlace = 0; // Index of next key
    int compVal = 1;

    while ((numKeysChecked < recordsPerSector)
        && (buffer[nextKeyPlace] != 0)
        && compVal
            != 0) { // while not the last record in buffer && has next record && not the same Keys
      char[] tempKey = getKey(buffer, nextKeyPlace); // Get keys from buffer
      compVal = compareKeys(key, tempKey); // Compare key
      numKeysChecked++; // Increment number of keys checked
      nextKeyPlace = nextKeyPlace + recordSize; // Move to next key place
    }
    if (compVal == 0) // If found
    {
      char[] tmpRecord =
          getRecord(
              buffer,
              nextKeyPlace - recordSize); // Back to previous key place where we found the key
      copyRecord(tmpRecord, record); // Copy the entire found record into parameter record
      return true;
    } else if ((compVal != 0)
        && (numKeysChecked
            < recordsPerSector)) // If not found and there is still space left, we know its not in
                                 // file
    return false;
    else // If not found and sector is full, we need to check overflow sectors
    {
      int currOverflowSector = overflowStart; // First overflow sector
      numKeysChecked = 0; // Count number of key we checked
      nextKeyPlace = 0; // Index of next key
      compVal = 1;

      while ((currOverflowSector < overflowStart + overflowSectors)
          && compVal != 0) { // While has more overflow sectors and not found
        disk.readSector(currOverflowSector, buffer); // Read in overflow sector to buffer
        while ((numKeysChecked < recordsPerSector) && (buffer[nextKeyPlace] != 0) && compVal != 0) {
          char[] tempKey = getKey(buffer, nextKeyPlace); // Get keys from buffer
          compVal = compareKeys(key, tempKey); // Compare key
          numKeysChecked++; // Increment number of keys checked
          nextKeyPlace = nextKeyPlace + recordSize; // Move to next key place
        }
        currOverflowSector++; // Move to next overflow sector
      }
      if (compVal != 0) // Not found in overflow sector
      return false;
      else {
        char[] tmpRecord =
            getRecord(
                buffer,
                nextKeyPlace - recordSize); // Back to previous key place where we found the key
        copyRecord(tmpRecord, record); // Copy the entire found record into parameter record
        return true;
      }
    }
  }