utsaslab / pebblesdb

The PebblesDB write-optimized key-value store (SOSP 17)
BSD 3-Clause "New" or "Revised" License
506 stars 99 forks source link

Some question about VersionSet::PickCompactionForGuards #26

Open baiqiubai opened 2 years ago

baiqiubai commented 2 years ago

Why not add all files at the level level directly to guards_compaction_and_all_files instead of taking the intersection of Complete_guards and Guards? I'm a little confused about this code, so I'd appreciate explaining it 。 `

              int guard_index_iter = 0;
      for (size_t i = 0; i < complete_guards.size(); i++) {
          GuardMetaData* cg = complete_guards[i];
          int guard_index = -1;
          Slice guard_key = cg->guard_key.user_key(), next_guard_key;
          if (i + 1 < complete_guards.size()) {
              next_guard_key = complete_guards[i+1]->guard_key.user_key();
          }

          for (; guard_index_iter < guards.size(); guard_index_iter++) {
              int compare = icmp_.user_comparator()->Compare(guards[guard_index_iter]->guard_key.user_key(), guard_key);
              if (compare == 0) {
                  guard_index = guard_index_iter;
                  guard_index_iter++;
                  break;
              } else if (compare > 0) {
                  break;
              } else {
                  // Ideally it should never reach here since there are no duplicates in complete_guards and complete_guards is a superset of guards
              }
          }

          if (guard_index == -1) { // If guard is not found for this complete guard
              continue;
          }
          GuardMetaData* g = guards[guard_index];
          bool guard_added = false;
          for (unsigned j = 0; j < g->files.size(); j++) {
              FileMetaData* file = g->file_metas[j];
              Slice file_smallest = file->smallest.user_key();
              Slice file_largest = file->largest.user_key();
              if ((i < complete_guards.size()-1                             // If it is not the last guard, checking for smallest and largest to fit in the range
                              && (icmp_.user_comparator()->Compare(file_smallest, guard_key) < 0
                                      || icmp_.user_comparator()->Compare(file_largest, next_guard_key) >= 0))
                      || (i == complete_guards.size()-1                         // If it is the last guard, checking for the smallest to fit in the guard
                              && icmp_.user_comparator()->Compare(file_smallest, guard_key) < 0)) {
                  guards_to_add_to_compaction.push_back(g);
                  guards_compaction_add_all_files.push_back(true);  
                  guard_added = true;
                  break; // No need to check other files
              }
          }
          if (!guard_added && which == 0 && (force_compact || v->guard_compaction_scores_[current_level][guard_index] >= 1.0)) {
              guards_to_add_to_compaction.push_back(g);
              guards_compaction_add_all_files.push_back(false);
              continue;
          }
      }

`