Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

196 linhas
6.6 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. // Return the maximum overlapping data (in bytes) at next level for any
  48. // file at a level >= 1.
  49. int64_t TEST_MaxNextLevelOverlappingBytes();
  50. private:
  51. friend class DB;
  52. Iterator* NewInternalIterator(const ReadOptions&,
  53. SequenceNumber* latest_snapshot);
  54. Status NewDB();
  55. // Recover the descriptor from persistent storage. May do a significant
  56. // amount of work to recover recently logged updates. Any changes to
  57. // be made to the descriptor are added to *edit.
  58. Status Recover(VersionEdit* edit);
  59. // Apply the specified updates and save the resulting descriptor to
  60. // persistent storage. If cleanup_mem is non-NULL, arrange to
  61. // delete it when all existing snapshots have gone away iff Install()
  62. // returns OK.
  63. Status Install(VersionEdit* edit,
  64. uint64_t new_log_number,
  65. MemTable* cleanup_mem);
  66. void MaybeIgnoreError(Status* s) const;
  67. // Delete any unneeded files and stale in-memory entries.
  68. void DeleteObsoleteFiles();
  69. // Called when an iterator over a particular version of the
  70. // descriptor goes away.
  71. static void Unref(void* arg1, void* arg2);
  72. // Compact the in-memory write buffer to disk. Switches to a new
  73. // log-file/memtable and writes a new descriptor iff successful.
  74. Status CompactMemTable();
  75. Status RecoverLogFile(uint64_t log_number,
  76. VersionEdit* edit,
  77. SequenceNumber* max_sequence);
  78. Status WriteLevel0Table(MemTable* mem, VersionEdit* edit);
  79. bool HasLargeValues(const WriteBatch& batch) const;
  80. // Process data in "*updates" and return a status. "assigned_seq"
  81. // is the sequence number assigned to the first mod in "*updates".
  82. // If no large values are encountered, "*final" is set to "updates".
  83. // If large values were encountered, registers the references of the
  84. // large values with the VersionSet, writes the large values to
  85. // files (if appropriate), and allocates a new WriteBatch with the
  86. // large values replaced with indirect references and stores a
  87. // pointer to the new WriteBatch in *final. If *final != updates on
  88. // return, then the client should delete *final when no longer
  89. // needed. Returns OK on success, and an appropriate error
  90. // otherwise.
  91. Status HandleLargeValues(SequenceNumber assigned_seq,
  92. WriteBatch* updates,
  93. WriteBatch** final);
  94. // Helper routine for HandleLargeValues
  95. void MaybeCompressLargeValue(
  96. const Slice& raw_value,
  97. Slice* file_bytes,
  98. std::string* scratch,
  99. LargeValueRef* ref);
  100. struct CompactionState;
  101. void MaybeScheduleCompaction();
  102. static void BGWork(void* db);
  103. void BackgroundCall();
  104. void BackgroundCompaction();
  105. void CleanupCompaction(CompactionState* compact);
  106. Status DoCompactionWork(CompactionState* compact);
  107. Status OpenCompactionOutputFile(CompactionState* compact);
  108. Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
  109. Status InstallCompactionResults(CompactionState* compact);
  110. // Constant after construction
  111. Env* const env_;
  112. const InternalKeyComparator internal_comparator_;
  113. const Options options_; // options_.comparator == &internal_comparator_
  114. bool owns_info_log_;
  115. const std::string dbname_;
  116. // table_cache_ provides its own synchronization
  117. TableCache* table_cache_;
  118. // Lock over the persistent DB state. Non-NULL iff successfully acquired.
  119. FileLock* db_lock_;
  120. // State below is protected by mutex_
  121. port::Mutex mutex_;
  122. port::AtomicPointer shutting_down_;
  123. port::CondVar bg_cv_; // Signalled when !bg_compaction_scheduled_
  124. port::CondVar compacting_cv_; // Signalled when !compacting_
  125. SequenceNumber last_sequence_;
  126. MemTable* mem_;
  127. WritableFile* logfile_;
  128. log::Writer* log_;
  129. uint64_t log_number_;
  130. SnapshotList snapshots_;
  131. // Set of table files to protect from deletion because they are
  132. // part of ongoing compactions.
  133. std::set<uint64_t> pending_outputs_;
  134. // Has a background compaction been scheduled or is running?
  135. bool bg_compaction_scheduled_;
  136. // Is there a compaction running?
  137. bool compacting_;
  138. VersionSet* versions_;
  139. // Have we encountered a background error in paranoid mode?
  140. Status bg_error_;
  141. // No copying allowed
  142. DBImpl(const DBImpl&);
  143. void operator=(const DBImpl&);
  144. const Comparator* user_comparator() const {
  145. return internal_comparator_.user_comparator();
  146. }
  147. };
  148. // Sanitize db options. The caller should delete result.info_log if
  149. // it is not equal to src.info_log.
  150. extern Options SanitizeOptions(const std::string& db,
  151. const InternalKeyComparator* icmp,
  152. const Options& src);
  153. }
  154. #endif // STORAGE_LEVELDB_DB_DB_IMPL_H_