blob_db.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #ifndef ROCKSDB_LITE
  7. #include <functional>
  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. struct BlobDBOptions {
  21. // name of the directory under main db, where blobs will be stored.
  22. // default is "blob_dir"
  23. std::string blob_dir = "blob_dir";
  24. // whether the blob_dir path is relative or absolute.
  25. bool path_relative = true;
  26. // When max_db_size is reached, evict blob files to free up space
  27. // instead of returnning NoSpace error on write. Blob files will be
  28. // evicted from oldest to newest, based on file creation time.
  29. bool is_fifo = false;
  30. // Maximum size of the database (including SST files and blob files).
  31. //
  32. // Default: 0 (no limits)
  33. uint64_t max_db_size = 0;
  34. // a new bucket is opened, for ttl_range. So if ttl_range is 600seconds
  35. // (10 minutes), and the first bucket starts at 1471542000
  36. // then the blob buckets will be
  37. // first bucket is 1471542000 - 1471542600
  38. // second bucket is 1471542600 - 1471543200
  39. // and so on
  40. uint64_t ttl_range_secs = 3600;
  41. // The smallest value to store in blob log. Values smaller than this threshold
  42. // will be inlined in base DB together with the key.
  43. uint64_t min_blob_size = 0;
  44. // Allows OS to incrementally sync blob files to disk for every
  45. // bytes_per_sync bytes written. Users shouldn't rely on it for
  46. // persistency guarantee.
  47. uint64_t bytes_per_sync = 512 * 1024;
  48. // the target size of each blob file. File will become immutable
  49. // after it exceeds that size
  50. uint64_t blob_file_size = 256 * 1024 * 1024;
  51. // what compression to use for Blob's
  52. CompressionType compression = kNoCompression;
  53. // If enabled, BlobDB cleans up stale blobs in non-TTL files during compaction
  54. // by rewriting the remaining live blobs to new files.
  55. bool enable_garbage_collection = false;
  56. // The cutoff in terms of blob file age for garbage collection. Blobs in
  57. // the oldest N non-TTL blob files will be rewritten when encountered during
  58. // compaction, where N = garbage_collection_cutoff * number_of_non_TTL_files.
  59. double garbage_collection_cutoff = 0.25;
  60. // Disable all background job. Used for test only.
  61. bool disable_background_tasks = false;
  62. void Dump(Logger* log) const;
  63. };
  64. class BlobDB : public StackableDB {
  65. public:
  66. using ROCKSDB_NAMESPACE::StackableDB::Put;
  67. virtual Status Put(const WriteOptions& options, const Slice& key,
  68. const Slice& value) override = 0;
  69. virtual Status Put(const WriteOptions& options,
  70. ColumnFamilyHandle* column_family, const Slice& key,
  71. const Slice& value) override {
  72. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  73. return Status::NotSupported(
  74. "Blob DB doesn't support non-default column family.");
  75. }
  76. return Put(options, key, value);
  77. }
  78. using ROCKSDB_NAMESPACE::StackableDB::Delete;
  79. virtual Status Delete(const WriteOptions& options,
  80. 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. virtual Status Get(const ReadOptions& options,
  115. ColumnFamilyHandle* column_family, const Slice& key,
  116. PinnableSlice* value) 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::MultiGet;
  126. virtual std::vector<Status> MultiGet(
  127. const ReadOptions& options,
  128. const std::vector<Slice>& keys,
  129. std::vector<std::string>* values) override = 0;
  130. virtual std::vector<Status> MultiGet(
  131. const ReadOptions& options,
  132. const std::vector<ColumnFamilyHandle*>& column_families,
  133. const std::vector<Slice>& keys,
  134. std::vector<std::string>* values) override {
  135. for (auto column_family : column_families) {
  136. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  137. return std::vector<Status>(
  138. column_families.size(),
  139. Status::NotSupported(
  140. "Blob DB doesn't support non-default column family."));
  141. }
  142. }
  143. return MultiGet(options, keys, values);
  144. }
  145. virtual void MultiGet(const ReadOptions& /*options*/,
  146. ColumnFamilyHandle* /*column_family*/,
  147. const size_t num_keys, const Slice* /*keys*/,
  148. PinnableSlice* /*values*/, Status* statuses,
  149. const bool /*sorted_input*/ = false) override {
  150. for (size_t i = 0; i < num_keys; ++i) {
  151. statuses[i] = Status::NotSupported(
  152. "Blob DB doesn't support batched MultiGet");
  153. }
  154. }
  155. using ROCKSDB_NAMESPACE::StackableDB::SingleDelete;
  156. virtual Status SingleDelete(const WriteOptions& /*wopts*/,
  157. ColumnFamilyHandle* /*column_family*/,
  158. const Slice& /*key*/) override {
  159. return Status::NotSupported("Not supported operation in blob db.");
  160. }
  161. using ROCKSDB_NAMESPACE::StackableDB::Merge;
  162. virtual Status Merge(const WriteOptions& /*options*/,
  163. ColumnFamilyHandle* /*column_family*/,
  164. const Slice& /*key*/, const Slice& /*value*/) override {
  165. return Status::NotSupported("Not supported operation in blob db.");
  166. }
  167. virtual Status Write(const WriteOptions& opts,
  168. WriteBatch* updates) override = 0;
  169. using ROCKSDB_NAMESPACE::StackableDB::NewIterator;
  170. virtual Iterator* NewIterator(const ReadOptions& options) override = 0;
  171. virtual Iterator* NewIterator(const ReadOptions& options,
  172. ColumnFamilyHandle* column_family) override {
  173. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  174. // Blob DB doesn't support non-default column family.
  175. return nullptr;
  176. }
  177. return NewIterator(options);
  178. }
  179. Status CompactFiles(
  180. const CompactionOptions& compact_options,
  181. const std::vector<std::string>& input_file_names, const int output_level,
  182. const int output_path_id = -1,
  183. std::vector<std::string>* const output_file_names = nullptr,
  184. CompactionJobInfo* compaction_job_info = nullptr) override = 0;
  185. Status CompactFiles(
  186. const CompactionOptions& compact_options,
  187. ColumnFamilyHandle* column_family,
  188. const std::vector<std::string>& input_file_names, const int output_level,
  189. const int output_path_id = -1,
  190. std::vector<std::string>* const output_file_names = nullptr,
  191. CompactionJobInfo* compaction_job_info = nullptr) override {
  192. if (column_family->GetID() != DefaultColumnFamily()->GetID()) {
  193. return Status::NotSupported(
  194. "Blob DB doesn't support non-default column family.");
  195. }
  196. return CompactFiles(compact_options, input_file_names, output_level,
  197. output_path_id, output_file_names, compaction_job_info);
  198. }
  199. using ROCKSDB_NAMESPACE::StackableDB::Close;
  200. virtual Status Close() override = 0;
  201. // Opening blob db.
  202. static Status Open(const Options& options, const BlobDBOptions& bdb_options,
  203. const std::string& dbname, BlobDB** blob_db);
  204. static Status Open(const DBOptions& db_options,
  205. const BlobDBOptions& bdb_options,
  206. const std::string& dbname,
  207. const std::vector<ColumnFamilyDescriptor>& column_families,
  208. std::vector<ColumnFamilyHandle*>* handles,
  209. BlobDB** blob_db);
  210. virtual BlobDBOptions GetBlobDBOptions() const = 0;
  211. virtual Status SyncBlobFiles() = 0;
  212. virtual ~BlobDB() {}
  213. protected:
  214. explicit BlobDB();
  215. };
  216. // Destroy the content of the database.
  217. Status DestroyBlobDB(const std::string& dbname, const Options& options,
  218. const BlobDBOptions& bdb_options);
  219. } // namespace blob_db
  220. } // namespace ROCKSDB_NAMESPACE
  221. #endif // ROCKSDB_LITE