Skip to content

SSTIndex::SearchInSST() handling malformed lines in SST file #43

@kkli08

Description

@kkli08

Implement Logic for properly handling the malformed lines and continue searching to the end of the file.

/*
   * Implement binary search in the SST file
   * This is how the kv data stored in sst:
   * ofstream sst_file(filepath);
   * for (const auto& pair : kv_pairs) {
   *  sst_file << pair.first << ", " << pair.second << "\n";
   * }
   * sst_file.close();
   */
  long long left = 0;
  long long right = file_size;
  long long key;
  long long value;

  while (left <= right) {
    // Find the middle position
    long long mid = left + (right - left) / 2;
    infile.seekg(mid);

    // Move to the start of the line to read the full key-value pair
    // This is necessary because seekg may land in the middle of a line
    std::string line;
    std::getline(infile, line);

    if (infile.eof()) {
      infile.clear();  // Clear the eof flag to continue reading
    }

    // Skip to the next line if not at the beginning of a new line
    std::getline(infile, line);

    // Parse the key-value pair
    std::istringstream iss(line);
    std::string key_str, value_str;
    if (std::getline(iss, key_str, ',') && std::getline(iss, value_str)) {
      key = std::stoll(key_str);
      value = std::stoll(value_str);

      if (key == _key) {
        infile.close();
        return value;  // Key found, return the associated value
      } else if (key < _key) {
        left = infile.tellg();  // Move right
      } else {
        right = mid - 1;  // Move left
      }
    } else {
      // If the line is not formatted correctly, continue searching
      /*
       *  Update NEEDED HERE.
       */
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions