options_file_example.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. //
  6. // This file demonstrates how to use the utility functions defined in
  7. // rocksdb/utilities/options_util.h to open a rocksdb database without
  8. // remembering all the rocksdb options.
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include "rocksdb/cache.h"
  13. #include "rocksdb/compaction_filter.h"
  14. #include "rocksdb/db.h"
  15. #include "rocksdb/options.h"
  16. #include "rocksdb/slice.h"
  17. #include "rocksdb/table.h"
  18. #include "rocksdb/utilities/options_util.h"
  19. using ROCKSDB_NAMESPACE::BlockBasedTableOptions;
  20. using ROCKSDB_NAMESPACE::ColumnFamilyDescriptor;
  21. using ROCKSDB_NAMESPACE::ColumnFamilyHandle;
  22. using ROCKSDB_NAMESPACE::ColumnFamilyOptions;
  23. using ROCKSDB_NAMESPACE::CompactionFilter;
  24. using ROCKSDB_NAMESPACE::ConfigOptions;
  25. using ROCKSDB_NAMESPACE::DB;
  26. using ROCKSDB_NAMESPACE::DBOptions;
  27. using ROCKSDB_NAMESPACE::NewLRUCache;
  28. using ROCKSDB_NAMESPACE::Options;
  29. using ROCKSDB_NAMESPACE::Slice;
  30. using ROCKSDB_NAMESPACE::Status;
  31. #if defined(OS_WIN)
  32. std::string kDBPath = "C:\\Windows\\TEMP\\rocksdb_options_file_example";
  33. #else
  34. std::string kDBPath = "/tmp/rocksdb_options_file_example";
  35. #endif
  36. namespace {
  37. // A dummy compaction filter
  38. class DummyCompactionFilter : public CompactionFilter {
  39. public:
  40. virtual ~DummyCompactionFilter() {}
  41. virtual bool Filter(int level, const Slice& key, const Slice& existing_value,
  42. std::string* new_value, bool* value_changed) const {
  43. return false;
  44. }
  45. virtual const char* Name() const { return "DummyCompactionFilter"; }
  46. };
  47. } // namespace
  48. int main() {
  49. DBOptions db_opt;
  50. db_opt.create_if_missing = true;
  51. std::vector<ColumnFamilyDescriptor> cf_descs;
  52. cf_descs.push_back(
  53. {ROCKSDB_NAMESPACE::kDefaultColumnFamilyName, ColumnFamilyOptions()});
  54. cf_descs.push_back({"new_cf", ColumnFamilyOptions()});
  55. // initialize BlockBasedTableOptions
  56. auto cache = NewLRUCache(1 * 1024 * 1024 * 1024);
  57. BlockBasedTableOptions bbt_opts;
  58. bbt_opts.block_size = 32 * 1024;
  59. bbt_opts.block_cache = cache;
  60. // initialize column families options
  61. std::unique_ptr<CompactionFilter> compaction_filter;
  62. compaction_filter.reset(new DummyCompactionFilter());
  63. cf_descs[0].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
  64. cf_descs[0].options.compaction_filter = compaction_filter.get();
  65. cf_descs[1].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
  66. // destroy and open DB
  67. DB* db;
  68. Status s = ROCKSDB_NAMESPACE::DestroyDB(kDBPath,
  69. Options(db_opt, cf_descs[0].options));
  70. assert(s.ok());
  71. s = DB::Open(Options(db_opt, cf_descs[0].options), kDBPath, &db);
  72. assert(s.ok());
  73. // Create column family, and rocksdb will persist the options.
  74. ColumnFamilyHandle* cf;
  75. s = db->CreateColumnFamily(ColumnFamilyOptions(), "new_cf", &cf);
  76. assert(s.ok());
  77. // close DB
  78. delete cf;
  79. delete db;
  80. // In the following code, we will reopen the rocksdb instance using
  81. // the options file stored in the db directory.
  82. // Load the options file.
  83. DBOptions loaded_db_opt;
  84. std::vector<ColumnFamilyDescriptor> loaded_cf_descs;
  85. ConfigOptions config_options;
  86. s = LoadLatestOptions(config_options, kDBPath, &loaded_db_opt,
  87. &loaded_cf_descs);
  88. assert(s.ok());
  89. assert(loaded_db_opt.create_if_missing == db_opt.create_if_missing);
  90. // Initialize pointer options for each column family
  91. for (size_t i = 0; i < loaded_cf_descs.size(); ++i) {
  92. auto* loaded_bbt_opt =
  93. loaded_cf_descs[0]
  94. .options.table_factory->GetOptions<BlockBasedTableOptions>();
  95. // Expect the same as BlockBasedTableOptions will be loaded form file.
  96. assert(loaded_bbt_opt->block_size == bbt_opts.block_size);
  97. // However, block_cache needs to be manually initialized as documented
  98. // in rocksdb/utilities/options_util.h.
  99. loaded_bbt_opt->block_cache = cache;
  100. }
  101. // In addition, as pointer options are initialized with default value,
  102. // we need to properly initialized all the pointer options if non-defalut
  103. // values are used before calling DB::Open().
  104. assert(loaded_cf_descs[0].options.compaction_filter == nullptr);
  105. loaded_cf_descs[0].options.compaction_filter = compaction_filter.get();
  106. // reopen the db using the loaded options.
  107. std::vector<ColumnFamilyHandle*> handles;
  108. s = DB::Open(loaded_db_opt, kDBPath, loaded_cf_descs, &handles, &db);
  109. assert(s.ok());
  110. // close DB
  111. for (auto* handle : handles) {
  112. delete handle;
  113. }
  114. delete db;
  115. }