Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251// Returns true iff some level needs a compaction.252bool NeedsCompaction() const {253Version* v = current_;254return (v->compaction_score_ >= 1) || (v->file_to_compact_ != nullptr);255}257// Add all files listed in any live version to *live.258// May also mutate some internal state.259void AddLiveFiles(std::set<uint64_t>* live);261// Return the approximate offset in the database of the data for262// "key" as of version "v".263uint64_t ApproximateOffsetOf(Version* v, const InternalKey& key);265// Return a human-readable short (single-line) summary of the number266// of files per level. Uses *scratch as backing store.267struct LevelSummaryStorage {268char buffer[100];269};270const char* LevelSummary(LevelSummaryStorage* scratch) const;272private:273class Builder;275friend class Compaction;276friend class Version;278bool ReuseManifest(const std::string& dscname, const std::string& dscbase);280void Finalize(Version* v);282void GetRange(const std::vector<FileMetaData*>& inputs, InternalKey* smallest,283InternalKey* largest);285void GetRange2(const std::vector<FileMetaData*>& inputs1,286const std::vector<FileMetaData*>& inputs2,287InternalKey* smallest, InternalKey* largest);289void SetupOtherInputs(Compaction* c);291// Save current contents to *log292Status WriteSnapshot(log::Writer* log);294void AppendVersion(Version* v);296Env* const env_;297const std::string dbname_;298const Options* const options_;299TableCache* const table_cache_;300const InternalKeyComparator icmp_;301uint64_t next_file_number_;302uint64_t manifest_file_number_;303uint64_t last_sequence_;304uint64_t log_number_;305uint64_t prev_log_number_; // 0 or backing store for memtable being compacted307// Opened lazily308WritableFile* descriptor_file_;309log::Writer* descriptor_log_;310Version dummy_versions_; // Head of circular doubly-linked list of versions.311Version* current_; // == dummy_versions_.prev_313// Per-level key at which the next compaction at that level should start.314// Either an empty string, or a valid InternalKey.315std::string compact_pointer_[config::kNumLevels];316};318// A Compaction encapsulates information about a compaction.319class Compaction {320public:321~Compaction();323// Return the level that is being compacted. Inputs from "level"324// and "level+1" will be merged to produce a set of "level+1" files.325int level() const { return level_; }327// Return the object that holds the edits to the descriptor done328// by this compaction.329VersionEdit* edit() { return &edit_; }331// "which" must be either 0 or 1332int num_input_files(int which) const { return inputs_[which].size(); }334// Return the ith input file at "level()+which" ("which" must be 0 or 1).335FileMetaData* input(int which, int i) const { return inputs_[which][i]; }337// Maximum size of files to build during this compaction.338uint64_t MaxOutputFileSize() const { return max_output_file_size_; }340// Is this a trivial compaction that can be implemented by just341// moving a single input file to the next level (no merging or splitting)342bool IsTrivialMove() const;344// Add all inputs to this compaction as delete operations to *edit.345void AddInputDeletions(VersionEdit* edit);347// Returns true if the information we have available guarantees that348// the compaction is producing data in "level+1" for which no data exists349// in levels greater than "level+1".350bool IsBaseLevelForKey(const Slice& user_key);352// Returns true iff we should stop building the current output353// before processing "internal_key".354bool ShouldStopBefore(const Slice& internal_key);356// Release the input version for the compaction, once the compaction357// is successful.358void ReleaseInputs();360private:361friend class Version;362friend class VersionSet;364Compaction(const Options* options, int level);366int level_;367uint64_t max_output_file_size_;368Version* input_version_;369VersionEdit edit_;371// Each compaction reads inputs from "level_" and "level_+1"372std::vector<FileMetaData*> inputs_[2]; // The two sets of inputs374// State used to check for number of overlapping grandparent files375// (parent == level_ + 1, grandparent == level_ + 2)376std::vector<FileMetaData*> grandparents_;377size_t grandparent_index_; // Index in grandparent_starts_378bool seen_key_; // Some output key has been seen379int64_t overlapped_bytes_; // Bytes of overlap between current output380// and grandparent files382// State for implementing IsBaseLevelForKey384// level_ptrs_ holds indices into input_version_->levels_: our state385// is that we are positioned at one of the file ranges for each386// higher level than the ones involved in this compaction (i.e. for387// all L >= level_ + 2).388size_t level_ptrs_[config::kNumLevels];389};391} // namespace leveldb393#endif // STORAGE_LEVELDB_DB_VERSION_SET_H_
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.