blob_db.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <functional>
  7. #include <limits>
  8. #include <string>
  9. #include <vector>
  10. #include "rocksdb/db.h"
  11. #include "rocksdb/status.h"
  12. #include "rocksdb/utilities/stackable_db.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. namespace blob_db {
  15. // A wrapped database which puts values of KV pairs in a separate log
  16. // and store location to the log in the underlying DB.
  17. //
  18. // The factory needs to be moved to include/rocksdb/utilities to allow
  19. // users to use blob DB.
  20. constexpr uint64_t kNoExpiration = std::numeric_limits<uint64_t>::max();
  21. struct BlobDBOptions {
  22. // Name of the directory under the base DB where blobs will be stored. Using
  23. // a directory where the base DB stores its SST files is not supported.
  24. // Default is "blob_dir"
  25. std::string blob_dir = "blob_dir";
  26. // whether the blob_dir path is relative or absolute.
  27. bool path_relative = true;
  28. // When max_db_size is reached, evict blob files to free up space
  29. // instead of returnning NoSpace error on write. Blob files will be
  30. // evicted from oldest to newest, based on file creation time.
  31. bool is_fifo = false;
  32. // Maximum size of the database (including SST files and blob files).
  33. //
  34. // Default: 0 (no limits)
  35. uint64_t max_db_size = 0;
  36. // a new bucket is opened, for ttl_range. So if ttl_range is 600seconds
  37. // (10 minutes), and the first bucket starts at 1471542000
  38. // then the blob buckets will be
  39. // first bucket is 1471542000 - 1471542600
  40. // second bucket is 1471542600 - 1471543200
  41. // and so on
  42. uint64_t ttl_range_secs = 3600;
  43. // The smallest value to store in blob log. Values smaller than this threshold
  44. // will be inlined in base DB together with the key.
  45. uint64_t min_blob_size = 0;
  46. // Allows OS to incrementally sync blob files to disk for every
  47. // bytes_per_sync bytes written. Users shouldn't rely on it for
  48. // persistency guarantee.
  49. uint64_t bytes_per_sync = 512 * 1024;
  50. // the target size of each blob file. File will become immutable
  51. // after it exceeds that size
  52. uint64_t blob_file_size = 256 * 1024 * 1024;
  53. // what compression to use for Blob's
  54. CompressionType compression = kNoCompression;
  55. // If enabled, BlobDB cleans up stale blobs in non-TTL files during compaction
  56. // by rewriting the remaining live blobs to new files.
  57. bool enable_garbage_collection = false;
  58. // The cutoff in terms of blob file age for garbage collection. Blobs in
  59. // the oldest N non-TTL blob files will be rewritten when encountered during
  60. // compaction, where N = garbage_collection_cutoff * number_of_non_TTL_files.
  61. double garbage_collection_cutoff = 0.25;
  62. // Disable all background job. Used for test only.
  63. bool disable_background_tasks = false;
  64. void Dump(Logger* log) const;
  65. };
  66. class BlobDB : public StackableDB {
  67. public:
  68. using ROCKSDB_NAMESPACE::StackableDB::Put;
  69. Status Put(const WriteOptions& options, const Slice& key,
  70. const Slice& value) override = 0;
  71. Status Put(const WriteOptions& options, ColumnFamilyHandle* column_family,
  72. const Slice& key, const Slice& value) override {
  73. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  74. return Status::NotSupported(
  75. "Blob DB doesn't support non-default column family.");
  76. }
  77. return Put(options, key, value);
  78. }
  79. using ROCKSDB_NAMESPACE::StackableDB::Delete;
  80. Status Delete(const WriteOptions& options, ColumnFamilyHandle* column_family,
  81. const Slice& key) override {
  82. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  83. return Status::NotSupported(
  84. "Blob DB doesn't support non-default column family.");
  85. }
  86. assert(db_ != nullptr);
  87. return db_->Delete(options, column_family, key);
  88. }
  89. virtual Status PutWithTTL(const WriteOptions& options, const Slice& key,
  90. const Slice& value, uint64_t ttl) = 0;
  91. virtual Status PutWithTTL(const WriteOptions& options,
  92. ColumnFamilyHandle* column_family, const Slice& key,
  93. const Slice& value, uint64_t ttl) {
  94. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  95. return Status::NotSupported(
  96. "Blob DB doesn't support non-default column family.");
  97. }
  98. return PutWithTTL(options, key, value, ttl);
  99. }
  100. // Put with expiration. Key with expiration time equal to
  101. // std::numeric_limits<uint64_t>::max() means the key don't expire.
  102. virtual Status PutUntil(const WriteOptions& options, const Slice& key,
  103. const Slice& value, uint64_t expiration) = 0;
  104. virtual Status PutUntil(const WriteOptions& options,
  105. ColumnFamilyHandle* column_family, const Slice& key,
  106. const Slice& value, uint64_t expiration) {
  107. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  108. return Status::NotSupported(
  109. "Blob DB doesn't support non-default column family.");
  110. }
  111. return PutUntil(options, key, value, expiration);
  112. }
  113. using ROCKSDB_NAMESPACE::StackableDB::Get;
  114. Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family,
  115. const Slice& key, PinnableSlice* value,
  116. std::string* timestamp) override = 0;
  117. // Get value and expiration.
  118. virtual Status Get(const ReadOptions& options,
  119. ColumnFamilyHandle* column_family, const Slice& key,
  120. PinnableSlice* value, uint64_t* expiration) = 0;
  121. virtual Status Get(const ReadOptions& options, const Slice& key,
  122. PinnableSlice* value, uint64_t* expiration) {
  123. return Get(options, DefaultColumnFamily(), key, value, expiration);
  124. }
  125. using ROCKSDB_NAMESPACE::StackableDB::SingleDelete;
  126. Status SingleDelete(const WriteOptions& /*wopts*/,
  127. ColumnFamilyHandle* /*column_family*/,
  128. const Slice& /*key*/) override {
  129. return Status::NotSupported("Not supported operation in blob db.");
  130. }
  131. using ROCKSDB_NAMESPACE::StackableDB::Merge;
  132. Status Merge(const WriteOptions& /*options*/,
  133. ColumnFamilyHandle* /*column_family*/, const Slice& /*key*/,
  134. const Slice& /*value*/) override {
  135. return Status::NotSupported("Not supported operation in blob db.");
  136. }
  137. Status Write(const WriteOptions& opts, WriteBatch* updates) override = 0;
  138. using ROCKSDB_NAMESPACE::StackableDB::NewIterator;
  139. Iterator* NewIterator(const ReadOptions& options) override = 0;
  140. Iterator* NewIterator(const ReadOptions& options,
  141. ColumnFamilyHandle* column_family) override {
  142. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  143. // Blob DB doesn't support non-default column family.
  144. return nullptr;
  145. }
  146. return NewIterator(options);
  147. }
  148. Status CompactFiles(
  149. const CompactionOptions& compact_options,
  150. const std::vector<std::string>& input_file_names, const int output_level,
  151. const int output_path_id = -1,
  152. std::vector<std::string>* const output_file_names = nullptr,
  153. CompactionJobInfo* compaction_job_info = nullptr) override = 0;
  154. Status CompactFiles(
  155. const CompactionOptions& compact_options,
  156. ColumnFamilyHandle* column_family,
  157. const std::vector<std::string>& input_file_names, const int output_level,
  158. const int output_path_id = -1,
  159. std::vector<std::string>* const output_file_names = nullptr,
  160. CompactionJobInfo* compaction_job_info = nullptr) override {
  161. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  162. return Status::NotSupported(
  163. "Blob DB doesn't support non-default column family.");
  164. }
  165. return CompactFiles(compact_options, input_file_names, output_level,
  166. output_path_id, output_file_names, compaction_job_info);
  167. }
  168. using ROCKSDB_NAMESPACE::StackableDB::Close;
  169. Status Close() override = 0;
  170. // Opening blob db.
  171. static Status Open(const Options& options, const BlobDBOptions& bdb_options,
  172. const std::string& dbname, BlobDB** blob_db);
  173. static Status Open(const DBOptions& db_options,
  174. const BlobDBOptions& bdb_options,
  175. const std::string& dbname,
  176. const std::vector<ColumnFamilyDescriptor>& column_families,
  177. std::vector<ColumnFamilyHandle*>* handles,
  178. BlobDB** blob_db);
  179. virtual BlobDBOptions GetBlobDBOptions() const = 0;
  180. virtual Status SyncBlobFiles(const WriteOptions& write_options) = 0;
  181. ~BlobDB() override {}
  182. protected:
  183. explicit BlobDB();
  184. };
  185. // Destroy the content of the database.
  186. Status DestroyBlobDB(const std::string& dbname, const Options& options,
  187. const BlobDBOptions& bdb_options);
  188. } // namespace blob_db
  189. } // namespace ROCKSDB_NAMESPACE