10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.5 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_DB_DB_IMPL_H_
  5. #define STORAGE_LEVELDB_DB_DB_IMPL_H_
  6. #include <set>
  7. #include "db/dbformat.h"
  8. #include "db/log_writer.h"
  9. #include "db/snapshot.h"
  10. #include "include/db.h"
  11. #include "include/env.h"
  12. #include "port/port.h"
  13. namespace leveldb {
  14. class MemTable;
  15. class TableCache;
  16. class Version;
  17. class VersionEdit;
  18. class VersionSet;
  19. class DBImpl : public DB {
  20. public:
  21. DBImpl(const Options& options, const std::string& dbname);
  22. virtual ~DBImpl();
  23. // Implementations of the DB interface
  24. virtual Status Put(const WriteOptions&, const Slice& key, const Slice& value);
  25. virtual Status Delete(const WriteOptions&, const Slice& key);
  26. virtual Status Write(const WriteOptions& options, WriteBatch* updates);
  27. virtual Status Get(const ReadOptions& options,
  28. const Slice& key,
  29. std::string* value);
  30. virtual Iterator* NewIterator(const ReadOptions&);
  31. virtual const Snapshot* GetSnapshot();
  32. virtual void ReleaseSnapshot(const Snapshot* snapshot);
  33. virtual bool GetProperty(const Slice& property, uint64_t* value);
  34. virtual void GetApproximateSizes(const Range* range, int n, uint64_t* sizes);
  35. // Extra methods (for testing) that are not in the public DB interface
  36. // Compact any files in the named level that overlap [begin,end]
  37. void TEST_CompactRange(
  38. int level,
  39. const std::string& begin,
  40. const std::string& end);
  41. // Force current memtable contents to be compacted.
  42. Status TEST_CompactMemTable();
  43. // Return an internal iterator over the current state of the database.
  44. // The keys of this iterator are internal keys (see format.h).
  45. // The returned iterator should be deleted when no longer needed.
  46. Iterator* TEST_NewInternalIterator();
  47. private:
  48. friend class DB;
  49. Iterator* NewInternalIterator(const ReadOptions&,
  50. SequenceNumber* latest_snapshot);
  51. Status NewDB();
  52. // Recover the descriptor from persistent storage. May do a significant
  53. // amount of work to recover recently logged updates. Any changes to
  54. // be made to the descriptor are added to *edit.
  55. Status Recover(VersionEdit* edit);
  56. // Apply the specified updates and save the resulting descriptor to
  57. // persistent storage. If cleanup_mem is non-NULL, arrange to
  58. // delete it when all existing snapshots have gone away iff Install()
  59. // returns OK.
  60. Status Install(VersionEdit* edit,
  61. uint64_t new_log_number,
  62. MemTable* cleanup_mem);
  63. void MaybeIgnoreError(Status* s) const;
  64. // Delete any unneeded files and stale in-memory entries.
  65. void DeleteObsoleteFiles();
  66. // Called when an iterator over a particular version of the
  67. // descriptor goes away.
  68. static void Unref(void* arg1, void* arg2);
  69. // Compact the in-memory write buffer to disk. Switches to a new
  70. // log-file/memtable and writes a new descriptor iff successful.
  71. Status CompactMemTable();
  72. Status RecoverLogFile(uint64_t log_number,
  73. VersionEdit* edit,
  74. SequenceNumber* max_sequence);
  75. Status WriteLevel0Table(MemTable* mem, VersionEdit* edit);
  76. bool HasLargeValues(const WriteBatch& batch) const;
  77. // Process data in "*updates" and return a status. "assigned_seq"
  78. // is the sequence number assigned to the first mod in "*updates".
  79. // If no large values are encountered, "*final" is set to "updates".
  80. // If large values were encountered, registers the references of the
  81. // large values with the VersionSet, writes the large values to
  82. // files (if appropriate), and allocates a new WriteBatch with the
  83. // large values replaced with indirect references and stores a
  84. // pointer to the new WriteBatch in *final. If *final != updates on
  85. // return, then the client should delete *final when no longer
  86. // needed. Returns OK on success, and an appropriate error
  87. // otherwise.
  88. Status HandleLargeValues(SequenceNumber assigned_seq,
  89. WriteBatch* updates,
  90. WriteBatch** final);
  91. // Helper routine for HandleLargeValues
  92. void MaybeCompressLargeValue(
  93. const Slice& raw_value,
  94. Slice* file_bytes,
  95. std::string* scratch,
  96. LargeValueRef* ref);
  97. struct CompactionState;
  98. void MaybeScheduleCompaction();
  99. static void BGWork(void* db);
  100. void BackgroundCall();
  101. void BackgroundCompaction();
  102. void CleanupCompaction(CompactionState* compact);
  103. Status DoCompactionWork(CompactionState* compact);
  104. Status OpenCompactionOutputFile(CompactionState* compact);
  105. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  106. Status InstallCompactionResults(CompactionState* compact);
  107. // Constant after construction
  108. Env* const env_;
  109. const InternalKeyComparator internal_comparator_;
  110. const Options options_; // options_.comparator == &internal_comparator_
  111. bool owns_info_log_;
  112. const std::string dbname_;
  113. // table_cache_ provides its own synchronization
  114. TableCache* table_cache_;
  115. // Lock over the persistent DB state. Non-NULL iff successfully acquired.
  116. FileLock* db_lock_;
  117. // State below is protected by mutex_
  118. port::Mutex mutex_;
  119. port::AtomicPointer shutting_down_;
  120. port::CondVar bg_cv_; // Signalled when !bg_compaction_scheduled_
  121. port::CondVar compacting_cv_; // Signalled when !compacting_
  122. SequenceNumber last_sequence_;
  123. MemTable* mem_;
  124. WritableFile* logfile_;
  125. log::Writer* log_;
  126. uint64_t log_number_;
  127. SnapshotList snapshots_;
  128. // Set of table files to protect from deletion because they are
  129. // part of ongoing compactions.
  130. std::set<uint64_t> pending_outputs_;
  131. // Has a background compaction been scheduled or is running?
  132. bool bg_compaction_scheduled_;
  133. // Is there a compaction running?
  134. bool compacting_;
  135. VersionSet* versions_;
  136. // Have we encountered a background error in paranoid mode?
  137. Status bg_error_;
  138. // No copying allowed
  139. DBImpl(const DBImpl&);
  140. void operator=(const DBImpl&);
  141. const Comparator* user_comparator() const {
  142. return internal_comparator_.user_comparator();
  143. }
  144. };
  145. // Sanitize db options. The caller should delete result.info_log if
  146. // it is not equal to src.info_log.
  147. extern Options SanitizeOptions(const std::string& db,
  148. const InternalKeyComparator* icmp,
  149. const Options& src);
  150. }
  151. #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_