options_file_example.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 namespace ROCKSDB_NAMESPACE;
  20. std::string kDBPath = "/tmp/rocksdb_options_file_example";
  21. namespace {
  22. // A dummy compaction filter
  23. class DummyCompactionFilter : public CompactionFilter {
  24. public:
  25. virtual ~DummyCompactionFilter() {}
  26. virtual bool Filter(int level, const Slice& key, const Slice& existing_value,
  27. std::string* new_value, bool* value_changed) const {
  28. return false;
  29. }
  30. virtual const char* Name() const { return "DummyCompactionFilter"; }
  31. };
  32. } // namespace
  33. int main() {
  34. DBOptions db_opt;
  35. db_opt.create_if_missing = true;
  36. std::vector<ColumnFamilyDescriptor> cf_descs;
  37. cf_descs.push_back({kDefaultColumnFamilyName, ColumnFamilyOptions()});
  38. cf_descs.push_back({"new_cf", ColumnFamilyOptions()});
  39. // initialize BlockBasedTableOptions
  40. auto cache = NewLRUCache(1 * 1024 * 1024 * 1024);
  41. BlockBasedTableOptions bbt_opts;
  42. bbt_opts.block_size = 32 * 1024;
  43. bbt_opts.block_cache = cache;
  44. // initialize column families options
  45. std::unique_ptr<CompactionFilter> compaction_filter;
  46. compaction_filter.reset(new DummyCompactionFilter());
  47. cf_descs[0].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
  48. cf_descs[0].options.compaction_filter = compaction_filter.get();
  49. cf_descs[1].options.table_factory.reset(NewBlockBasedTableFactory(bbt_opts));
  50. // destroy and open DB
  51. DB* db;
  52. Status s = DestroyDB(kDBPath, Options(db_opt, cf_descs[0].options));
  53. assert(s.ok());
  54. s = DB::Open(Options(db_opt, cf_descs[0].options), kDBPath, &db);
  55. assert(s.ok());
  56. // Create column family, and rocksdb will persist the options.
  57. ColumnFamilyHandle* cf;
  58. s = db->CreateColumnFamily(ColumnFamilyOptions(), "new_cf", &cf);
  59. assert(s.ok());
  60. // close DB
  61. delete cf;
  62. delete db;
  63. // In the following code, we will reopen the rocksdb instance using
  64. // the options file stored in the db directory.
  65. // Load the options file.
  66. DBOptions loaded_db_opt;
  67. std::vector<ColumnFamilyDescriptor> loaded_cf_descs;
  68. s = LoadLatestOptions(kDBPath, Env::Default(), &loaded_db_opt,
  69. &loaded_cf_descs);
  70. assert(s.ok());
  71. assert(loaded_db_opt.create_if_missing == db_opt.create_if_missing);
  72. // Initialize pointer options for each column family
  73. for (size_t i = 0; i < loaded_cf_descs.size(); ++i) {
  74. auto* loaded_bbt_opt = reinterpret_cast<BlockBasedTableOptions*>(
  75. loaded_cf_descs[0].options.table_factory->GetOptions());
  76. // Expect the same as BlockBasedTableOptions will be loaded form file.
  77. assert(loaded_bbt_opt->block_size == bbt_opts.block_size);
  78. // However, block_cache needs to be manually initialized as documented
  79. // in rocksdb/utilities/options_util.h.
  80. loaded_bbt_opt->block_cache = cache;
  81. }
  82. // In addition, as pointer options are initialized with default value,
  83. // we need to properly initialized all the pointer options if non-defalut
  84. // values are used before calling DB::Open().
  85. assert(loaded_cf_descs[0].options.compaction_filter == nullptr);
  86. loaded_cf_descs[0].options.compaction_filter = compaction_filter.get();
  87. // reopen the db using the loaded options.
  88. std::vector<ColumnFamilyHandle*> handles;
  89. s = DB::Open(loaded_db_opt, kDBPath, loaded_cf_descs, &handles, &db);
  90. assert(s.ok());
  91. // close DB
  92. for (auto* handle : handles) {
  93. delete handle;
  94. }
  95. delete db;
  96. }